setnonzeros_param.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_SETNONZEROS_PARAM_HPP
27 #define CASADI_SETNONZEROS_PARAM_HPP
28 
29 #include "mx_node.hpp"
30 #include <map>
31 #include <stack>
32 
34 
35 namespace casadi {
36 
43  template<bool Add>
44  class CASADI_EXPORT SetNonzerosParam : public MXNode {
45  public:
47 
53  static MX create(const MX& y, const MX& x, const MX& nz);
54  static MX create(const MX& y, const MX& x, const MX& inner, const Slice& outer);
55  static MX create(const MX& y, const MX& x, const Slice& inner, const MX& outer);
56  static MX create(const MX& y, const MX& x, const MX& inner, const MX& outer);
58 
60  SetNonzerosParam(const MX& y, const MX& x, const MX& nz);
61  SetNonzerosParam(const MX& y, const MX& x, const MX& nz, const MX& nz2);
62 
64  ~SetNonzerosParam() override = 0;
65 
69  int sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
70 
74  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
75 
79  casadi_int op() const override { return Add ? OP_ADDNONZEROS_PARAM : OP_SETNONZEROS_PARAM;}
80 
82  casadi_int n_inplace() const override { return 1;}
83 
87  void generate(CodeGenerator& g,
88  const std::vector<casadi_int>& arg,
89  const std::vector<casadi_int>& res,
90  const std::vector<bool>& arg_is_ref,
91  std::vector<bool>& res_is_ref) const override;
92 
96  static MXNode* deserialize(DeserializingStream& s);
97 
98  protected:
102  explicit SetNonzerosParam(DeserializingStream& s) : MXNode(s) {}
103  };
104 
105 
112  template<bool Add>
113  class CASADI_EXPORT SetNonzerosParamVector : public SetNonzerosParam<Add>{
114  public:
115 
117  SetNonzerosParamVector(const MX& y, const MX& x, const MX& nz);
118 
120  ~SetNonzerosParamVector() override {}
121 
125  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
126  const std::vector<bool>& unique={}) const override;
127 
129  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
130 
134  void ad_forward(const std::vector<std::vector<MX> >& fseed,
135  std::vector<std::vector<MX> >& fsens) const override;
136 
140  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
141  std::vector<std::vector<MX> >& asens) const override;
142 
146  std::string disp(const std::vector<std::string>& arg) const override;
147 
151  void generate(CodeGenerator& g,
152  const std::vector<casadi_int>& arg,
153  const std::vector<casadi_int>& res,
154  const std::vector<bool>& arg_is_ref,
155  std::vector<bool>& res_is_ref) const override;
156 
160  void serialize_body(SerializingStream& s) const override;
164  void serialize_type(SerializingStream& s) const override;
165 
169  explicit SetNonzerosParamVector(DeserializingStream& s);
170  };
171 
172  // Specialization of the above when nz_ is a Slice
173  template<bool Add>
174  class CASADI_EXPORT SetNonzerosParamSlice : public SetNonzerosParam<Add>{
175  public:
176 
180  size_t sz_iw() const override;
181 
183  SetNonzerosParamSlice(const MX& y, const MX& x, const MX& inner, const Slice& outer) :
184  SetNonzerosParam<Add>(y, x, inner), outer_(outer) {}
185 
187  ~SetNonzerosParamSlice() override {}
188 
192  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
193  const std::vector<bool>& unique={}) const override;
194 
198  void ad_forward(const std::vector<std::vector<MX> >& fseed,
199  std::vector<std::vector<MX> >& fsens) const override;
200 
204  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
205  std::vector<std::vector<MX> >& asens) const override;
206 
208  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
209 
213  std::string disp(const std::vector<std::string>& arg) const override;
214 
218  void generate(CodeGenerator& g,
219  const std::vector<casadi_int>& arg,
220  const std::vector<casadi_int>& res,
221  const std::vector<bool>& arg_is_ref,
222  std::vector<bool>& res_is_ref) const override;
223 
224  // Data member
225  Slice outer_;
226 
230  void serialize_body(SerializingStream& s) const override;
234  void serialize_type(SerializingStream& s) const override;
235 
239  explicit SetNonzerosParamSlice(DeserializingStream& s);
240  };
241 
242 
243  // Specialization of the above when nz_ is a Slice
244  template<bool Add>
245  class CASADI_EXPORT SetNonzerosSliceParam : public SetNonzerosParam<Add>{
246  public:
247 
249  SetNonzerosSliceParam(const MX& y, const MX& x, const Slice& inner, const MX& outer) :
250  SetNonzerosParam<Add>(y, x, outer), inner_(inner) {}
251 
253  ~SetNonzerosSliceParam() override {}
254 
258  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
259  const std::vector<bool>& unique={}) const override;
260 
262  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
263 
267  void ad_forward(const std::vector<std::vector<MX> >& fseed,
268  std::vector<std::vector<MX> >& fsens) const override;
269 
273  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
274  std::vector<std::vector<MX> >& asens) const override;
275 
279  std::string disp(const std::vector<std::string>& arg) const override;
280 
284  void generate(CodeGenerator& g,
285  const std::vector<casadi_int>& arg,
286  const std::vector<casadi_int>& res,
287  const std::vector<bool>& arg_is_ref,
288  std::vector<bool>& res_is_ref) const override;
289 
290  // Data member
291  Slice inner_;
292 
296  void serialize_body(SerializingStream& s) const override;
300  void serialize_type(SerializingStream& s) const override;
301 
305  explicit SetNonzerosSliceParam(DeserializingStream& s);
306  };
307 
308  // Specialization of the above when nz_ is a Slice
309  template<bool Add>
310  class CASADI_EXPORT SetNonzerosParamParam : public SetNonzerosParam<Add>{
311  public:
312 
316  size_t sz_iw() const override;
317 
319  SetNonzerosParamParam(const MX& y, const MX& x, const MX& inner, const MX& outer) :
320  SetNonzerosParam<Add>(y, x, inner, outer) {}
321 
323  ~SetNonzerosParamParam() override {}
324 
328  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
329  const std::vector<bool>& unique={}) const override;
330 
332  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
333 
337  void ad_forward(const std::vector<std::vector<MX> >& fseed,
338  std::vector<std::vector<MX> >& fsens) const override;
339 
343  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
344  std::vector<std::vector<MX> >& asens) const override;
345 
349  std::string disp(const std::vector<std::string>& arg) const override;
350 
354  void generate(CodeGenerator& g,
355  const std::vector<casadi_int>& arg,
356  const std::vector<casadi_int>& res,
357  const std::vector<bool>& arg_is_ref,
358  std::vector<bool>& res_is_ref) const override;
359 
363  void serialize_type(SerializingStream& s) const override;
364 
368  explicit SetNonzerosParamParam(DeserializingStream& s);
369  };
370 
371 } // namespace casadi
373 
374 #endif // CASADI_SETNONZEROS_PARAM_HPP
The casadi namespace.
Definition: archiver.hpp:32