kron.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_KRON_HPP
27 #define CASADI_KRON_HPP
28 
29 #include "mx_node.hpp"
30 
32 
33 namespace casadi {
34 
49  class CASADI_EXPORT Kron : public MXNode {
50  public:
51 
53  static MX create(const MX& a, const MX& b);
54 
56  Kron(const MX& a, const MX& b);
57 
59  ~Kron() override {}
60 
62  virtual void eval_kernel(const double** arg, double** res) const;
63  virtual void eval_kernel(const SXElem** arg, SXElem** res) const;
64 
66  template<typename T>
67  int eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
68  eval_kernel(arg, res);
69  return 0;
70  }
71 
75  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override {
76  return eval_gen<double>(arg, res, iw, w);
77  }
78 
82  int eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const override {
83  return eval_gen<SXElem>(arg, res, iw, w);
84  }
85 
89  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
90  const std::vector<bool>& unique={}) const override;
91 
95  int sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
96 
100  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
101 
105  void ad_forward(const std::vector<std::vector<MX> >& fseed,
106  std::vector<std::vector<MX> >& fsens) const override;
107 
111  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
112  std::vector<std::vector<MX> >& asens) const override;
113 
117  void generate(CodeGenerator& g,
118  const std::vector<casadi_int>& arg,
119  const std::vector<casadi_int>& res,
120  const std::vector<bool>& arg_is_ref,
121  std::vector<bool>& res_is_ref) const override;
122 
126  std::string disp(const std::vector<std::string>& arg) const override;
127 
131  casadi_int op() const override { return OP_KRON;}
132 
136  void serialize_type(SerializingStream& s) const override;
137 
141  static MXNode* deserialize(DeserializingStream& s);
142 
143  protected:
144 
148  explicit Kron(DeserializingStream& s) : MXNode(s) {}
149  };
150 
151 
155  class CASADI_EXPORT DenseKron : public Kron {
156  public:
157  static MXNode* try_create(const MX& a, const MX& b);
158 
159  DenseKron(const MX& a, const MX& b) : Kron(a, b) {}
160  ~DenseKron() override {}
161 
162  void eval_kernel(const double** arg, double** res) const override;
163  void eval_kernel(const SXElem** arg, SXElem** res) const override;
164 
165  void generate(CodeGenerator& g,
166  const std::vector<casadi_int>& arg,
167  const std::vector<casadi_int>& res,
168  const std::vector<bool>& arg_is_ref,
169  std::vector<bool>& res_is_ref) const override;
170 
171  void serialize_type(SerializingStream& s) const override;
172  explicit DenseKron(DeserializingStream& s) : Kron(s) {}
173  };
174 
175 
179  class CASADI_EXPORT DenseSparseKron : public Kron {
180  public:
181  static MXNode* try_create(const MX& a, const MX& b);
182 
183  DenseSparseKron(const MX& a, const MX& b) : Kron(a, b) {}
184  ~DenseSparseKron() override {}
185 
186  void eval_kernel(const double** arg, double** res) const override;
187  void eval_kernel(const SXElem** arg, SXElem** res) const override;
188 
189  void generate(CodeGenerator& g,
190  const std::vector<casadi_int>& arg,
191  const std::vector<casadi_int>& res,
192  const std::vector<bool>& arg_is_ref,
193  std::vector<bool>& res_is_ref) const override;
194 
195  void serialize_type(SerializingStream& s) const override;
196  explicit DenseSparseKron(DeserializingStream& s) : Kron(s) {}
197  };
198 
199 
203  class CASADI_EXPORT SparseDenseKron : public Kron {
204  public:
205  static MXNode* try_create(const MX& a, const MX& b);
206 
207  SparseDenseKron(const MX& a, const MX& b) : Kron(a, b) {}
208  ~SparseDenseKron() override {}
209 
210  void eval_kernel(const double** arg, double** res) const override;
211  void eval_kernel(const SXElem** arg, SXElem** res) const override;
212 
213  void generate(CodeGenerator& g,
214  const std::vector<casadi_int>& arg,
215  const std::vector<casadi_int>& res,
216  const std::vector<bool>& arg_is_ref,
217  std::vector<bool>& res_is_ref) const override;
218 
219  void serialize_type(SerializingStream& s) const override;
220  explicit SparseDenseKron(DeserializingStream& s) : Kron(s) {}
221  };
222 
223 
241  class CASADI_EXPORT KronContract : public MXNode {
242  public:
243 
245  static MX create(const MX& m, const MX& x, bool inner);
246 
248  KronContract(const MX& m, const MX& x, bool inner);
249 
251  ~KronContract() override {}
252 
255  virtual void eval_kernel(const double** arg, double** res, double* w) const;
256  virtual void eval_kernel(const SXElem** arg, SXElem** res, SXElem* w) const;
257 
259  template<typename T>
260  int eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
261  eval_kernel(arg, res, w);
262  return 0;
263  }
264 
268  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override {
269  return eval_gen<double>(arg, res, iw, w);
270  }
271 
275  int eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const override {
276  return eval_gen<SXElem>(arg, res, iw, w);
277  }
278 
282  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
283  const std::vector<bool>& unique={}) const override;
284 
288  int sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
289 
293  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
294 
298  void ad_forward(const std::vector<std::vector<MX> >& fseed,
299  std::vector<std::vector<MX> >& fsens) const override;
300 
304  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
305  std::vector<std::vector<MX> >& asens) const override;
306 
310  size_t sz_w() const override;
311 
315  void generate(CodeGenerator& g,
316  const std::vector<casadi_int>& arg,
317  const std::vector<casadi_int>& res,
318  const std::vector<bool>& arg_is_ref,
319  std::vector<bool>& res_is_ref) const override;
320 
324  std::string disp(const std::vector<std::string>& arg) const override;
325 
329  casadi_int op() const override { return OP_KRON_CONTRACT;}
330 
334  void serialize_body(SerializingStream& s) const override;
335 
339  void serialize_type(SerializingStream& s) const override;
340 
344  static MXNode* deserialize(DeserializingStream& s);
345 
347  bool inner_;
348 
349  protected:
350 
354  explicit KronContract(DeserializingStream& s);
355  };
356 
357 
361  class CASADI_EXPORT DenseKronContract : public KronContract {
362  public:
363  static MXNode* try_create(const MX& m, const MX& x, bool inner);
364 
365  DenseKronContract(const MX& m, const MX& x, bool inner) : KronContract(m, x, inner) {}
366  ~DenseKronContract() override {}
367 
368  void eval_kernel(const double** arg, double** res, double* w) const override;
369  void eval_kernel(const SXElem** arg, SXElem** res, SXElem* w) const override;
370 
371  // sz_w stays at base size (dep(1).numel()) because sp_forward uses it
372  // even though our specialized eval_kernel doesn't.
373 
374  void generate(CodeGenerator& g,
375  const std::vector<casadi_int>& arg,
376  const std::vector<casadi_int>& res,
377  const std::vector<bool>& arg_is_ref,
378  std::vector<bool>& res_is_ref) const override;
379 
380  void serialize_type(SerializingStream& s) const override;
381  explicit DenseKronContract(DeserializingStream& s) : KronContract(s) {}
382  };
383 
384 
388  class CASADI_EXPORT DenseSparseKronContract : public KronContract {
389  public:
390  static MXNode* try_create(const MX& m, const MX& x, bool inner);
391 
392  DenseSparseKronContract(const MX& m, const MX& x, bool inner) : KronContract(m, x, inner) {}
393  ~DenseSparseKronContract() override {}
394 
395  void eval_kernel(const double** arg, double** res, double* w) const override;
396  void eval_kernel(const SXElem** arg, SXElem** res, SXElem* w) const override;
397 
398  // sz_w stays at base size for sp_forward's densify step.
399 
400  void generate(CodeGenerator& g,
401  const std::vector<casadi_int>& arg,
402  const std::vector<casadi_int>& res,
403  const std::vector<bool>& arg_is_ref,
404  std::vector<bool>& res_is_ref) const override;
405 
406  void serialize_type(SerializingStream& s) const override;
407  explicit DenseSparseKronContract(DeserializingStream& s) : KronContract(s) {}
408  };
409 
410 
414  class CASADI_EXPORT SparseDenseKronContract : public KronContract {
415  public:
416  static MXNode* try_create(const MX& m, const MX& x, bool inner);
417 
418  SparseDenseKronContract(const MX& m, const MX& x, bool inner) : KronContract(m, x, inner) {}
419  ~SparseDenseKronContract() override {}
420 
421  void eval_kernel(const double** arg, double** res, double* w) const override;
422  void eval_kernel(const SXElem** arg, SXElem** res, SXElem* w) const override;
423 
424  // sz_w stays at base size for sp_forward's densify step.
425 
426  void generate(CodeGenerator& g,
427  const std::vector<casadi_int>& arg,
428  const std::vector<casadi_int>& res,
429  const std::vector<bool>& arg_is_ref,
430  std::vector<bool>& res_is_ref) const override;
431 
432  void serialize_type(SerializingStream& s) const override;
433  explicit SparseDenseKronContract(DeserializingStream& s) : KronContract(s) {}
434  };
435 
436 } // namespace casadi
438 
439 #endif // CASADI_KRON_HPP
The casadi namespace.
Definition: archiver.hpp:32