getnonzeros_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_GETNONZEROS_PARAM_HPP
27 #define CASADI_GETNONZEROS_PARAM_HPP
28 
29 #include "mx_node.hpp"
30 #include <map>
31 #include <stack>
32 
34 
35 namespace casadi {
42  class CASADI_EXPORT GetNonzerosParam : public MXNode {
43  public:
44 
47  static MX create(const MX& x, const MX& nz);
48  static MX create(const MX& x, const MX& inner, const Slice& outer);
49  static MX create(const MX& x, const Slice& inner, const MX& outer);
50  static MX create(const MX& x, const MX& inner, const MX& outer);
52 
54  GetNonzerosParam(const Sparsity& sp, const MX& y, const MX& nz);
55  GetNonzerosParam(const Sparsity& sp, const MX& y, const MX& nz, const MX& nz_extra);
56 
58  ~GetNonzerosParam() override {}
59 
63  int sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
64 
68  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
69 
73  casadi_int op() const override { return OP_GETNONZEROS_PARAM;}
74 
78  static MXNode* deserialize(DeserializingStream& s);
79 
80  protected:
84  explicit GetNonzerosParam(DeserializingStream& s) : MXNode(s) {}
85  };
86 
87 
88  class CASADI_EXPORT GetNonzerosParamVector : public GetNonzerosParam {
89  public:
91  GetNonzerosParamVector(const MX& x,
92  const MX& nz) : GetNonzerosParam(nz.sparsity(), x, nz) {}
93 
95  ~GetNonzerosParamVector() override {}
96 
100  void ad_forward(const std::vector<std::vector<MX> >& fseed,
101  std::vector<std::vector<MX> >& fsens) const override;
102 
106  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
107  std::vector<std::vector<MX> >& asens) const override;
108 
110  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
111 
115  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
116  const std::vector<bool>& unique={}) const override;
117 
121  std::string disp(const std::vector<std::string>& arg) const override;
122 
126  void generate(CodeGenerator& g,
127  const std::vector<casadi_int>& arg,
128  const std::vector<casadi_int>& res,
129  const std::vector<bool>& arg_is_ref,
130  std::vector<bool>& res_is_ref) const override;
131 
135  void serialize_body(SerializingStream& s) const override;
139  void serialize_type(SerializingStream& s) const override;
140 
144  explicit GetNonzerosParamVector(DeserializingStream& s);
145  };
146 
147  // Specialization of the above when nz_ is a nested Slice
148  class CASADI_EXPORT GetNonzerosSliceParam : public GetNonzerosParam {
149  public:
150 
152  GetNonzerosSliceParam(const Sparsity& sp, const MX& x, const Slice& inner,
153  const MX& outer) :
154  GetNonzerosParam(sp, x, outer), inner_(inner) {}
155 
157  ~GetNonzerosSliceParam() override {}
158 
160  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
161 
165  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
166  const std::vector<bool>& unique={}) const override;
167 
171  void ad_forward(const std::vector<std::vector<MX> >& fseed,
172  std::vector<std::vector<MX> >& fsens) const override;
173 
177  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
178  std::vector<std::vector<MX> >& asens) const override;
179 
183  std::string disp(const std::vector<std::string>& arg) const override;
184 
188  void generate(CodeGenerator& g,
189  const std::vector<casadi_int>& arg,
190  const std::vector<casadi_int>& res,
191  const std::vector<bool>& arg_is_ref,
192  std::vector<bool>& res_is_ref) const override;
193 
195  Dict info() const override { return {{"inner", inner_.info()}}; }
196 
197  // Data members
198  Slice inner_;
199 
203  void serialize_body(SerializingStream& s) const override;
207  void serialize_type(SerializingStream& s) const override;
208 
212  explicit GetNonzerosSliceParam(DeserializingStream& s);
213  };
214 
215  // Specialization of the above when nz_ is a nested Slice
216  class CASADI_EXPORT GetNonzerosParamSlice : public GetNonzerosParam {
217  public:
218 
220  GetNonzerosParamSlice(const Sparsity& sp, const MX& x, const MX& inner,
221  const Slice& outer) :
222  GetNonzerosParam(sp, x, inner), outer_(outer) {}
223 
225  ~GetNonzerosParamSlice() override {}
226 
230  size_t sz_iw() const override;
231 
233  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
234 
238  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
239  const std::vector<bool>& unique={}) const override;
240 
244  void ad_forward(const std::vector<std::vector<MX> >& fseed,
245  std::vector<std::vector<MX> >& fsens) const override;
246 
250  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
251  std::vector<std::vector<MX> >& asens) const override;
252 
256  std::string disp(const std::vector<std::string>& arg) const override;
257 
261  void generate(CodeGenerator& g,
262  const std::vector<casadi_int>& arg,
263  const std::vector<casadi_int>& res,
264  const std::vector<bool>& arg_is_ref,
265  std::vector<bool>& res_is_ref) const override;
266 
268  Dict info() const override { return {{"outer", outer_.info()}}; }
269 
270  // Data members
271  Slice outer_;
272 
276  void serialize_body(SerializingStream& s) const override;
280  void serialize_type(SerializingStream& s) const override;
281 
285  explicit GetNonzerosParamSlice(DeserializingStream& s);
286  };
287 
288 
289  // Specialization of the above when nz_ is a nested Slice
290  class CASADI_EXPORT GetNonzerosParamParam : public GetNonzerosParam {
291  public:
292 
294  GetNonzerosParamParam(const Sparsity& sp, const MX& x, const MX& inner,
295  const MX& outer) :
296  GetNonzerosParam(sp, x, inner, outer) {}
297 
299  ~GetNonzerosParamParam() override {}
300 
304  size_t sz_iw() const override;
305 
307  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
308 
312  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
313  const std::vector<bool>& unique={}) const override;
314 
318  void ad_forward(const std::vector<std::vector<MX> >& fseed,
319  std::vector<std::vector<MX> >& fsens) const override;
320 
324  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
325  std::vector<std::vector<MX> >& asens) const override;
326 
330  std::string disp(const std::vector<std::string>& arg) const override;
331 
335  void generate(CodeGenerator& g,
336  const std::vector<casadi_int>& arg,
337  const std::vector<casadi_int>& res,
338  const std::vector<bool>& arg_is_ref,
339  std::vector<bool>& res_is_ref) const override;
340 
342  Dict info() const override { return {}; }
343 
344 
348  void serialize_type(SerializingStream& s) const override;
349 
353  explicit GetNonzerosParamParam(DeserializingStream& s);
354  };
355 
356 } // namespace casadi
358 
359 #endif // CASADI_GETNONZEROS_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.