interpolant_impl.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_INTERPOLANT_IMPL_HPP
27 #define CASADI_INTERPOLANT_IMPL_HPP
28 
29 #include "interpolant.hpp"
30 #include "function_internal.hpp"
31 #include "plugin_interface.hpp"
32 
34 
35 namespace casadi {
36 
40  class CASADI_EXPORT Interpolant
41  : public FunctionInternal, public PluginInterface<Interpolant> {
42  public:
44  Interpolant(const std::string& name,
45  const std::vector<double>& grid,
46  const std::vector<casadi_int>& offset,
47  const std::vector<double>& values,
48  casadi_int m);
49 
51  ~Interpolant() override;
52 
56  std::string class_name() const override {return "Interpolant";}
57 
59 
62  size_t get_n_in() override { return 1+has_parametric_values()+has_parametric_grid();}
63  size_t get_n_out() override { return 1;}
65 
69  bool get_diff_in(casadi_int i) override { return i==0; }
70 
72 
75  Sparsity get_sparsity_in(casadi_int i) override;
76  Sparsity get_sparsity_out(casadi_int i) override;
78 
80 
83  std::string get_name_in(casadi_int i) override;
84  std::string get_name_out(casadi_int i) override;
86 
88 
91  static const Options options_;
92  const Options& get_options() const override { return options_;}
94 
96  void init(const Dict& opts) override;
97 
99  static std::vector<casadi_int> interpret_lookup_mode(const std::vector<std::string>& modes,
100  const std::vector<double>& grid, const std::vector<casadi_int>& offset,
101  const std::vector<casadi_int>& margin_left=std::vector<casadi_int>(),
102  const std::vector<casadi_int>& margin_right=std::vector<casadi_int>());
103 
104  static void stack_grid(const std::vector< std::vector<double> >& grid,
105  std::vector<casadi_int>& offset, std::vector<double>& stacked);
106 
107  static void check_grid(const std::vector< std::vector<double> >& grid);
108  static void check_grid(const std::vector<casadi_int>& grid);
109 
110  static std::vector<double> meshgrid(const std::vector< std::vector<double> >& grid);
111 
112  // Creator function for internal class
113  typedef Interpolant* (*Creator)(const std::string& name,
114  const std::vector<double>& grid,
115  const std::vector<casadi_int>& offset,
116  const std::vector<double>& values,
117  casadi_int m);
118 
122  static Function construct(const std::string& solver, const std::string& name,
123  const std::vector<double>& grid,
124  const std::vector<casadi_int>& offset,
125  const std::vector<double>& values,
126  casadi_int m,
127  const Dict& opts);
128 
129  typedef Function (* DoInline)(const std::string& name,
130  const std::vector<double>& grid,
131  const std::vector<casadi_int>& offset,
132  const std::vector<double>& values,
133  casadi_int m,
134  const Dict& opts);
135 
136  // No static functions exposed
137  struct Exposed{ DoInline do_inline; };
138 
140  static std::map<std::string, Plugin> solvers_;
141 
143  static const std::string infix_;
144 
145  // Number of dimensions
146  casadi_int ndim_;
147 
148  // Number of outputs
149  casadi_int m_;
150 
151  // Number of inputs
152  casadi_int batch_x_;
153 
154  // Input grid
155  std::vector<double> grid_;
156 
157  // Offset for each dimension
158  std::vector<casadi_int> offset_;
159 
160  // Values at gridpoints
161  std::vector<double> values_;
162 
163  // Lookup modes
164  std::vector<std::string> lookup_modes_;
165 
169  void serialize_body(SerializingStream &s) const override;
173  void serialize_type(SerializingStream &s) const override;
174 
178  std::string serialize_base_function() const override { return "Interpolant"; }
182  static ProtoFunction* deserialize(DeserializingStream& s);
183 
187  bool has_parametric_values() const { return values_.empty(); }
188 
192  bool has_parametric_grid() const { return grid_.empty(); }
193 
194  casadi_int arg_values() const;
195  casadi_int arg_grid() const;
196 
200  casadi_int coeff_size() const;
201  static casadi_int coeff_size(const std::vector<casadi_int>& offset, casadi_int m);
202 
203  protected:
204 
205  bool arg_values(casadi_int i) const;
206  bool arg_grid(casadi_int i) const;
207 
211  explicit Interpolant(DeserializingStream& s);
212 
213 
214  };
215 
216 } // namespace casadi
218 
219 #endif // CASADI_INTERPOLANT_IMPL_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.