bspline.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_BSPLINE_HPP
27 #define CASADI_BSPLINE_HPP
28 
29 #include "mx_node.hpp"
30 #include <map>
31 #include <stack>
32 
34 
35 namespace casadi {
42  class CASADI_EXPORT BSplineCommon : public MXNode {
43  public:
44 
46  BSplineCommon(const std::vector<double>& knots,
47  const std::vector<casadi_int>& offset,
48  const std::vector<casadi_int>& degree,
49  casadi_int m,
50  const std::vector<casadi_int>& lookup_mode);
51 
53  ~BSplineCommon() override {}
54 
55  static void prepare(casadi_int m, const std::vector<casadi_int>& offset,
56  const std::vector<casadi_int>& degree, casadi_int &coeffs_size,
57  std::vector<casadi_int>& coeffs_dims, std::vector<casadi_int>& strides);
58 
59  static casadi_int get_coeff_size(casadi_int m, const std::vector<casadi_int>& offset,
60  const std::vector<casadi_int>& degree);
61 
62  template<class M>
63  static M derivative_coeff(casadi_int i,
64  const std::vector< std::vector<double> >& knots,
65  const std::vector<casadi_int>& degree,
66  const std::vector<casadi_int>& coeffs_dims,
67  const M& coeffs,
68  std::vector< std::vector<double> >& new_knots,
69  std::vector<casadi_int>& new_degree);
70 
71  std::vector<double> knots_;
72  std::vector<casadi_int> offset_;
73  std::vector<casadi_int> degree_;
74  casadi_int m_;
75  std::vector<casadi_int> lookup_mode_;
76 
77  // Derived fields
78  std::vector<casadi_int> strides_;
79  std::vector<casadi_int> coeffs_dims_;
80  casadi_int coeffs_size_;
81 
88  mutable MX jac_cache_;
89 
90 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
92  mutable std::mutex jac_cache_mtx_;
93 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
94 
95  virtual MX jac_cached() const = 0;
96 
100  static size_t n_iw(const std::vector<casadi_int> &degree);
101 
105  static size_t n_w(const std::vector<casadi_int> &degree);
106 
110  size_t sz_iw() const override;
111 
115  size_t sz_w() const override;
116 
120  casadi_int op() const override { return OP_BSPLINE;}
121 
125  void ad_forward(const std::vector<std::vector<MX> >& fseed,
126  std::vector<std::vector<MX> >& fsens) const override;
127 
131  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
132  std::vector<std::vector<MX> >& asens) const override;
133 
137  void generate(CodeGenerator& g,
138  const std::vector<casadi_int>& arg,
139  const std::vector<casadi_int>& res,
140  const std::vector<bool>& arg_is_ref,
141  std::vector<bool>& res_is_ref) const override;
142 
146  virtual std::string generate(CodeGenerator& g,
147  const std::vector<casadi_int>& arg,
148  const std::vector<bool>& arg_is_ref) const = 0;
149 
153  static MXNode* deserialize(DeserializingStream& s);
154 
155  template<class T>
156  MX jac(const MX& x, const T& coeffs) const;
157 
161  void serialize_body(SerializingStream& s) const override;
162 
163  protected:
164 
168  explicit BSplineCommon(DeserializingStream& s);
169 
170  };
171 
180  class CASADI_EXPORT BSpline : public BSplineCommon {
181  public:
182 
183  static MX create(const MX& x, const std::vector< std::vector<double> >& knots,
184  const std::vector<double>& coeffs,
185  const std::vector<casadi_int>& degree,
186  casadi_int m,
187  const Dict& opts);
188 
190  BSpline(const MX& x, const std::vector<double>& knots,
191  const std::vector<casadi_int>& offset,
192  const std::vector<double>& coeffs,
193  const std::vector<casadi_int>& degree,
194  casadi_int m,
195  const std::vector<casadi_int>& lookup_mode);
196 
198  ~BSpline() override {}
199 
201  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
202 
206  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
207  const std::vector<bool>& unique={}) const override;
208 
212  std::string generate(CodeGenerator& g,
213  const std::vector<casadi_int>& arg,
214  const std::vector<bool>& arg_is_ref) const override;
215 
219  std::string disp(const std::vector<std::string>& arg) const override;
220 
221  // Numeric coefficients
222  std::vector<double> coeffs_;
223 
224  MX jac_cached() const override;
225 
236  static DM dual(const std::vector<double>& x,
237  const std::vector< std::vector<double> >& knots,
238  const std::vector<casadi_int>& degree,
239  const Dict& opts);
243  void serialize_body(SerializingStream& s) const override;
247  void serialize_type(SerializingStream& s) const override;
248 
252  explicit BSpline(DeserializingStream& s);
253  };
254 
255  // Symbolic coefficients
256  class CASADI_EXPORT BSplineParametric : public BSplineCommon {
257  public:
258  static MX create(const MX& x, const MX& coeffs,
259  const std::vector< std::vector<double> >& knots,
260  const std::vector<casadi_int>& degree,
261  casadi_int m,
262  const Dict& opts);
263 
265  static MX create(const MX& x, const MX& coeffs,
266  const std::vector<MX>& knots,
267  const std::vector<casadi_int>& degree,
268  casadi_int m,
269  const Dict& opts);
270 
272  BSplineParametric(const MX& x, const MX& coeffs,
273  const std::vector<double>& knots,
274  const std::vector<casadi_int>& offset,
275  const std::vector<casadi_int>& degree,
276  casadi_int m,
277  const std::vector<casadi_int>& lookup_mode);
278 
280  ~BSplineParametric() override {}
281 
283  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
284 
288  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
289  const std::vector<bool>& unique={}) const override;
290 
291  MX jac_cached() const override;
292 
296  std::string generate(CodeGenerator& g,
297  const std::vector<casadi_int>& arg,
298  const std::vector<bool>& arg_is_ref) const override;
299 
303  std::string disp(const std::vector<std::string>& arg) const override;
304 
308  void serialize_type(SerializingStream& s) const override;
309 
313  explicit BSplineParametric(DeserializingStream& s) : BSplineCommon(s) {}
314  };
315 
316 } // namespace casadi
318 
319 #endif // CASADI_BSPLINE_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Matrix< double > DM
Definition: dm_fwd.hpp:33