Skip to content

Commit

Permalink
Add some const correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
vss2sn committed Apr 1, 2022
1 parent eb87216 commit e1afab8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 60 deletions.
22 changes: 0 additions & 22 deletions binomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ std::array<int, 1> pascals_triangle<0>() {
return find_all_binomial_coefficients<0>();
}

template<int start, int end>
void compile_time_for() {
if constexpr (start < end) {
const auto ans = find_all_binomial_coefficients<start>();
for (const auto& ele : ans) {
std::cout << ele << ' ';
}
std::cout << '\n';
compile_time_for<start+1, end>();
}
}

template<size_t N>
struct BinomialParamterValues {
public:
Expand All @@ -88,13 +76,3 @@ struct BinomialParamterValues {
};

#endif // BINOMAIAL_HPP

// int main() {
// constexpr int N = 5;
// // compile_time_for<0, N+1>();
// for (const auto& ele : pascals_triangle<N>()) {
// std::cout << ele << ' ';
// }
// std::cout << '\n';
// return 0;
// }
23 changes: 9 additions & 14 deletions hermite_splines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class HermiteSplines {
}
}

void print() {
void print() const {
for (const auto& p : points) {
for (int j = 0; j < dimensions; j++) {
std::cout << p[j] << ", ";
Expand All @@ -51,21 +51,20 @@ class HermiteSplines {
void calculate_coefficients() {
std::array<double, degree+1> coeffs_for_ploy;
std::fill(coeffs_for_ploy.begin(), coeffs_for_ploy.end(), 1);
Polynomial<degree> poly(coeffs_for_ploy);
const Polynomial<degree> poly(coeffs_for_ploy);
std::array<Polynomial<degree>, degree+1> polys;
auto coeffecient_matrix_of_p_to_dnp = get_coefficients_of_poly_and_all_derivatives(poly); // dnp = (d)^n p
const auto coeffecient_matrix_of_p_to_dnp = get_coefficients_of_poly_and_all_derivatives(poly); // dnp = (d)^n p
std::array<std::array<double, degree+1>, degree+1> coeffs;
compile_for<0>(coeffecient_matrix_of_p_to_dnp, coeffs);
// print(coeffs);
coefficients_of_basis_curves = inverse_using_LU_decomp(coeffs);
}

template<size_t I>
void compile_for(
std::array<std::array<double, degree+1>, degree+1>& coeffecient_matrix_of_p_to_dnp,
const std::array<std::array<double, degree+1>, degree+1>& coeffecient_matrix_of_p_to_dnp,
std::array<std::array<double, degree+1>, degree+1>& coeffs
) {
if constexpr (degree < I) {
if constexpr (degree < I) {
return;
} else {
std::array<double, degree-I/2 + 1> temp;
Expand All @@ -74,14 +73,10 @@ class HermiteSplines {
std::end(coeffecient_matrix_of_p_to_dnp[I/2]),
std::begin(temp)
);
auto poly_at_i = Polynomial<degree - I/2>(temp);
auto temp2 = poly_at_i.get_component_value(double(I%2));
for (int i = 0; i < temp2.size(); i++) {
coeffs[I][i] = temp2[i];
}
for (int i = temp2.size(); i < coeffs[I].size(); i++) {
coeffs[I][i] = 0;
}
const auto poly_at_i = Polynomial<degree - I/2>(temp);
const auto temp2 = poly_at_i.get_component_value(double(I%2));
std::copy(std::begin(temp2), std::end(temp2), std::begin(coeffs[I]));
std::fill(std::begin(coeffs[I]) + temp2.size(), std::end(coeffs[I]), 0.);
compile_for<I+1>(coeffecient_matrix_of_p_to_dnp, coeffs);
}
}
Expand Down
12 changes: 6 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <array>
#include <iostream>

// #include "bezier_curve.hpp"
#include "bezier_curve.hpp"
#include "cubic_hermite_spline.hpp"
// #include "catmull_rom_spline.hpp"
// #include "curve_conversion.hpp"
#include "catmull_rom_spline.hpp"
#include "curve_conversion.hpp"
#include "hermite_splines.hpp"
#include "polynomials.hpp"
#include "utils.hpp"
Expand Down Expand Up @@ -92,7 +93,7 @@ int main () {
constexpr size_t degree = 5;
constexpr size_t dimensions = 2;
constexpr size_t n_points = 10;
std::array<Point<dimensions>, degree+1> p = {
constexpr std::array<Point<dimensions>, degree+1> p = {
Point<dimensions>{0., 0.},
Point<dimensions>{4., 1.},
Point<dimensions>{0., 1.},
Expand All @@ -101,13 +102,12 @@ int main () {
Point<dimensions>{0., 1.},
};


// auto chs = CubicHermiteSpline<n_points, dimensions>(p);
// std::cout << "Cubic Hermite Spline" << '\n';
// chs.print();
// std::cout << '\n';

auto hs = HermiteSplines<degree, n_points, dimensions>(p);
const auto hs = HermiteSplines<degree, n_points, dimensions>(p);
std::cout << "Hermite Spline" << '\n';
hs.print();
std::cout << '\n';
Expand Down
33 changes: 16 additions & 17 deletions polynomials.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Polynomial {
public:
explicit Polynomial(const std::array<double, degree+1>& coefficients = {}) : coefficients(coefficients) {}

double get_value(const double x) {
double get_value(const double x) const {
double ans = 0;
double variable = 1;
for (auto it= coefficients.rbegin(); it != coefficients.rend(); it++) {
Expand All @@ -18,33 +18,33 @@ class Polynomial {
return ans;
}

std::array<double, degree + 1> get_component_value(const double x) {
std::array<double, degree + 1> get_component_value(const double x) const {
std::array<double, degree + 1> ans;
double variable = 1;
for (int i = degree; i >=0; i--) {
for (int i = degree; i >= 0; i--) {
ans[i] = coefficients[i] * variable;
variable *= x;
}
return ans;
}

Polynomial<degree - 1> get_derivative () {
Polynomial<degree - 1> get_derivative () const {
std::array<double, degree> new_coefficients;
for (int i = 0; i < degree; i++) {
new_coefficients[i] = coefficients[i] * (degree - i);
}
return Polynomial<degree - 1>(new_coefficients);
}

std::array<double, degree + 1> get_coefficients () {
std::array<double, degree + 1> get_coefficients () const {
return coefficients;
}

size_t get_degree() {
size_t get_degree() const {
return degree;
}

void print() {
void print() const {
for (int i = 0; i < degree; i++) {
std::cout << coefficients[i] << "x" << degree - i << " + ";
}
Expand All @@ -60,27 +60,27 @@ class Polynomial<0> {
public:
explicit Polynomial(const std::array<double, 1>& coefficients = {0}) : coefficients(coefficients) {}

double get_value(const double x) {
double get_value(const double x) const {
return coefficients[0];
}

std::array<double, 1> get_component_value(const double x) {
std::array<double, 1> get_component_value(const double x) const {
return coefficients;
}

Polynomial<0> get_derivative () {
Polynomial<0> get_derivative () const {
return Polynomial<0>({0});
}

std::array<double, 1> get_coefficients () {
std::array<double, 1> get_coefficients () const {
return coefficients;
}

size_t get_degree() {
size_t get_degree() const {
return 0;
}

void print() {
void print() const {
std::cout << coefficients[0] << '\n';
}

Expand All @@ -89,7 +89,7 @@ class Polynomial<0> {
};

template<size_t degree>
std::array<std::array<double, degree+1>, degree+1> get_coefficients_of_poly_and_all_derivatives(Polynomial<degree>& poly) {
std::array<std::array<double, degree+1>, degree+1> get_coefficients_of_poly_and_all_derivatives(const Polynomial<degree>& poly) {
std::array<std::array<double, degree+1>, degree+1> ans;
ans[0] = poly.get_coefficients();
auto poly_d = poly.get_derivative();
Expand All @@ -106,9 +106,8 @@ std::array<std::array<double, degree+1>, degree+1> get_coefficients_of_poly_and_
}

template<>
std::array<std::array<double, 1>, 1> get_coefficients_of_poly_and_all_derivatives<0>(Polynomial<0>& poly) {
std::array<std::array<double, 1>, 1> ans;
return ans;
std::array<std::array<double, 1>, 1> get_coefficients_of_poly_and_all_derivatives<0>(const Polynomial<0>& poly) {
return {0};
}

#endif // POLYNOMIALS_HPP
1 change: 0 additions & 1 deletion utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ std::array<std::array<double, N>, N> inverse_using_LU_decomp(std::array<std::arr
}
}

std::array<std::array<double, N>, N> inverse;
for (int i_main = 0; i_main < N; i_main++) {
std::array<double, N> b;
for (int j = 0; j < N; j++) {
Expand Down

0 comments on commit e1afab8

Please sign in to comment.