switch.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_SWITCH_HPP
27 #define CASADI_SWITCH_HPP
28 
29 #include "function_internal.hpp"
30 
32 
33 namespace casadi {
34 
39  class CASADI_EXPORT Switch : public FunctionInternal {
40  public:
41 
45  Switch(const std::string& name,
46  const std::vector<Function>& f, const Function& f_def);
47 
51  ~Switch() override;
52 
56  std::string class_name() const override {return "Switch";}
57 
59 
62  size_t get_n_in() override;
63  size_t get_n_out() override;
65 
67 
70  Sparsity get_sparsity_in(casadi_int i) override;
71  Sparsity get_sparsity_out(casadi_int i) override;
73 
77  void init(const Dict& opts) override;
78 
82  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
83 
87  int eval_sx(const SXElem** arg, SXElem** res,
88  casadi_int* iw, SXElem* w, void* mem) const override;
89 
91 
94  bool has_forward(casadi_int nfwd) const override { return true;}
95  Function get_forward(casadi_int nfwd, const std::string& name,
96  const std::vector<std::string>& inames,
97  const std::vector<std::string>& onames,
98  const Dict& opts) const override;
100 
102 
105  bool has_reverse(casadi_int nadj) const override { return true;}
106  Function get_reverse(casadi_int nadj, const std::string& name,
107  const std::vector<std::string>& inames,
108  const std::vector<std::string>& onames,
109  const Dict& opts) const override;
111 
115  void disp_more(std::ostream& stream) const override;
116 
120  void codegen_declarations(CodeGenerator& g) const override;
121 
125  bool has_codegen() const override { return true;}
126 
130  void codegen_body(CodeGenerator& g) const override;
131 
132  // Function to be evaluated for each case
133  std::vector<Function> f_;
134 
135  // Default case;
136  Function f_def_;
137 
138  // Sparsity projection needed?
139  bool project_in_, project_out_;
140 
144  void serialize_body(SerializingStream &s) const override;
145 
149  static ProtoFunction* deserialize(DeserializingStream& s) { return new Switch(s); }
150 
152  Dict info() const override;
153 
154  // Get all embedded functions, recursively
155  void find(std::map<FunctionInternal*, Function>& all_fun, casadi_int max_depth) const override;
156 
157  protected:
161  explicit Switch(DeserializingStream& s);
162  };
163 
164 } // namespace casadi
166 
167 #endif // CASADI_SWITCH_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.