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  void generate(CodeGenerator& g,
81  const std::vector<casadi_int>& arg,
82  const std::vector<casadi_int>& res,
83  const std::vector<bool>& arg_is_ref,
84  std::vector<bool>& res_is_ref) const override;
85 
87  int eval(const double** arg, double** res, casadi_int* iw, double* w) const override;
88 
90  int eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const override;
91 
95  void eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
96  const std::vector<bool>& unique={}) const override;
97 
101  void ad_forward(const std::vector<std::vector<MX> >& fseed,
102  std::vector<std::vector<MX> >& fsens) const override;
103 
107  void ad_reverse(const std::vector<std::vector<MX> >& aseed,
108  std::vector<std::vector<MX> >& asens) const override;
109 
113  int sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
114 
118  int eval_activity(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
119 
123  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const override;
124 
128  const Function& which_function() const override { return fcn_;}
129 
133  casadi_int which_output() const override { return -1;}
134 
138  casadi_int nout() const override;
139 
143  const Sparsity& sparsity(casadi_int oind) const override;
144 
148  casadi_int op() const override { return OP_CALL;}
149 
153  size_t sz_arg() const override;
154 
158  size_t sz_res() const override;
159 
163  size_t sz_iw() const override;
164 
168  size_t sz_w() const override;
169 
173  void serialize_body(SerializingStream& s) const override;
174 
178  static MXNode* deserialize(DeserializingStream& s) { return new Call(s); }
179 
180  protected:
184  explicit Call(const Function& fcn, const std::vector<MX>& arg);
185 
189  explicit Call(DeserializingStream& s);
190 
194  static MX common_cond(const std::vector<std::vector<MX>>& seed);
195 
196  // Function to be evaluated
197  Function fcn_;
198 
200  mutable WeakCache<casadi_int, MX> cache_;
201  };
202 
203 } // namespace casadi
205 
206 #endif // CASADI_CALL_HPP
The casadi namespace.
Definition: archiver.hpp:32