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  ~Call() override {}
51 
55  static MX projectArg(const MX& x, const Sparsity& sp, casadi_int i);
56 
60  std::string disp(const std::vector<std::string>& arg) const override;
61 
65  void add_dependency(CodeGenerator& g) const override;
66 
70  bool has_refcount() const override;
71 
75  void codegen_incref(CodeGenerator& g, std::set<void*>& added) const override;
76 
80  void codegen_decref(CodeGenerator& g, std::set<void*>& added) const override;
81 
85  void generate(CodeGenerator& g,
86  const std::vector<casadi_int>& arg,
87  const std::vector<casadi_int>& res) const override;
88 
90  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
91 
93  int eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const override;
94 
98  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const override;
99 
103  void ad_forward(const std::vector<std::vector<MX> >& fseed,
104  std::vector<std::vector<MX> >& fsens) const override;
105 
109  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
110  std::vector<std::vector<MX> >& asens) const override;
111 
115  int sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
116 
120  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
121 
125  const Function& which_function() const override { return fcn_;}
126 
130  casadi_int which_output() const override { return -1;}
131 
135  casadi_int nout() const override;
136 
140  const Sparsity& sparsity(casadi_int oind) const override;
141 
145  casadi_int op() const override { return OP_CALL;}
146 
150  size_t sz_arg() const override;
151 
155  size_t sz_res() const override;
156 
160  size_t sz_iw() const override;
161 
165  size_t sz_w() const override;
166 
170  void serialize_body(SerializingStream& s) const override;
171 
175  static MXNode* deserialize(DeserializingStream& s) { return new Call(s); }
176 
177  protected:
181  explicit Call(const Function& fcn, const std::vector<MX>& arg);
182 
186  explicit Call(DeserializingStream& s);
187 
191  static MX common_cond(const std::vector<std::vector<MX>>& seed);
192 
193  // Function to be evaluated
194  Function fcn_;
195  };
196 
197 } // namespace casadi
199 
200 #endif // CASADI_CALL_HPP
The casadi namespace.