26 #ifndef CASADI_BSPLINE_IMPL_HPP
27 #define CASADI_BSPLINE_IMPL_HPP
29 #include "bspline.hpp"
30 #include "interpolant_impl.hpp"
31 #include "casadi_low.hpp"
36 M BSplineCommon::derivative_coeff(casadi_int i,
37 const std::vector< std::vector<double> >& knots,
38 const std::vector<casadi_int>& degree,
39 const std::vector<casadi_int>& coeffs_dims,
const M& coeffs,
40 std::vector< std::vector<double> >& new_knots,
41 std::vector<casadi_int>& new_degree) {
42 casadi_int n_knots = knots[i].size();
43 casadi_int n = n_knots-degree[i]-1;
45 DM delta_knots = K(range(1+degree[i], n_knots-1))
46 - K(range(1, n_knots-degree[i]-1));
47 DM d = degree[i]/delta_knots;
49 std::vector<casadi_int> coeffs_dims_new = coeffs_dims;
50 coeffs_dims_new[i+1] = n-1;
55 casadi_int L = 1, R = 1;
56 for (casadi_int k=0; k<=i; ++k) L *= coeffs_dims[k];
57 for (casadi_int k=i+2; k<(casadi_int)coeffs_dims.size(); ++k) R *= coeffs_dims[k];
58 casadi_int K_sz = coeffs_dims[i+1];
61 M M_coeffs = reshape(coeffs, L*K_sz, R);
62 M top = M_coeffs(Slice(L, L*K_sz), Slice());
63 M bot = M_coeffs(Slice(0, L*(K_sz-1)), Slice());
66 std::vector<casadi_int> dims{L, Kp, R};
67 std::vector<casadi_int> a{-1, -2, -3};
68 std::vector<casadi_int> b{-2};
69 std::vector<casadi_int> c{-1, -2, -3};
70 M coeff_matrix = einstein(vec(diffed), M(d),
71 dims, std::vector<casadi_int>{Kp}, dims,
76 for (casadi_int k=0;k<degree.size();++k) {
79 std::vector<double>(knots[k].begin()+1, knots[k].end()-1));
80 new_degree.push_back(degree[k]-1);
82 new_knots.push_back(knots[k]);
83 new_degree.push_back(degree[k]);
92 MX BSplineCommon::jac(
const MX& x,
const T& coeffs)
const {
93 casadi_int n_dims = degree_.size();
94 std::vector<MX> parts;
97 std::vector<std::string> lookup_mode;
98 for (
auto e : lookup_mode_) lookup_mode.push_back(Low::lookup_mode_from_enum(e));
99 opts[
"lookup_mode"] = lookup_mode;
102 std::vector< std::vector<double> > knots_unflat(n_dims);
103 for (casadi_int k=0;k<n_dims;++k) {
104 knots_unflat[k] = std::vector<double>(
105 get_ptr(knots_)+offset_[k], get_ptr(knots_)+offset_[k+1]);
109 for (casadi_int k=0;k<n_dims;++k) {
110 std::vector< std::vector<double> > knots;
111 std::vector< casadi_int> degree;
112 T dC = derivative_coeff(k, knots_unflat, degree_, coeffs_dims_, coeffs, knots, degree);
113 MX d =
MX::bspline(x, dC, knots, degree, m_, opts);
117 return horzcat(parts);
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.