casadi_call.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_CALL_HPP
27 #define CASADI_CALL_HPP
28 
29 #include "multiple_output.hpp"
30 #include "function.hpp"
31 
33 
34 namespace casadi {
35 
40  class CASADI_EXPORT Call : public MultipleOutput {
41  public:
45  static std::vector<MX> create(const Function& fcn, const std::vector<MX>& arg);
46 
50  static MX create_call(const Function& fcn, const std::vector<MX>& arg);
51 
55  MX get_output(casadi_int oind) const override;
56 
60  ~Call() override {}
61 
65  static MX projectArg(const MX& x, const Sparsity& sp, casadi_int i);
66 
70  std::string disp(const std::vector<std::string>& arg) const override;
71 
75  void add_dependency(CodeGenerator& g) const override;
76 
80  bool has_refcount() const override;
81 
85  void codegen_incref(CodeGenerator& g, std::set<void*>& added) const override;
86 
90  void codegen_decref(CodeGenerator& g, std::set<void*>& added) const override;
91 
95  void generate(CodeGenerator& g,
96  const std::vector<casadi_int>& arg,
97  const std::vector<casadi_int>& res,
98  const std::vector<bool>& arg_is_ref,
99  std::vector<bool>& res_is_ref) const override;
100 
102  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
103 
105  int eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const override;
106 
110  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const override;
111 
115  void ad_forward(const std::vector<std::vector<MX> >& fseed,
116  std::vector<std::vector<MX> >& fsens) const override;
117 
121  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
122  std::vector<std::vector<MX> >& asens) const override;
123 
127  int sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
128 
132  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
133 
137  const Function& which_function() const override { return fcn_;}
138 
142  casadi_int which_output() const override { return -1;}
143 
147  casadi_int nout() const override;
148 
152  const Sparsity& sparsity(casadi_int oind) const override;
153 
157  casadi_int op() const override { return OP_CALL;}
158 
162  size_t sz_arg() const override;
163 
167  size_t sz_res() const override;
168 
172  size_t sz_iw() const override;
173 
177  size_t sz_w() const override;
178 
182  void serialize_body(SerializingStream& s) const override;
183 
187  static MXNode* deserialize(DeserializingStream& s) { return new Call(s); }
188 
189  protected:
193  explicit Call(const Function& fcn, const std::vector<MX>& arg);
194 
198  explicit Call(DeserializingStream& s);
199 
203  static MX common_cond(const std::vector<std::vector<MX>>& seed);
204 
205  // Function to be evaluated
206  Function fcn_;
207 
209  mutable WeakCache<casadi_int, MX> cache_;
210  };
211 
212 } // namespace casadi
214 
215 #endif // CASADI_CALL_HPP
The casadi namespace.
Definition: archiver.hpp:32