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 
142 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
143  static std::mutex mutex_solvers_;
144 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
145 
147  static const std::string infix_;
148 
149  // Number of dimensions
150  casadi_int ndim_;
151 
152  // Number of outputs
153  casadi_int m_;
154 
155  // Number of inputs
156  casadi_int batch_x_;
157 
158  // Input grid
159  std::vector<double> grid_;
160 
161  // Offset for each dimension
162  std::vector<casadi_int> offset_;
163 
164  // Values at gridpoints
165  std::vector<double> values_;
166 
167  // Lookup modes
168  std::vector<std::string> lookup_modes_;
169 
173  void serialize_body(SerializingStream &s) const override;
177  void serialize_type(SerializingStream &s) const override;
178 
182  std::string serialize_base_function() const override { return "Interpolant"; }
186  static ProtoFunction* deserialize(DeserializingStream& s);
187 
191  bool has_parametric_values() const { return values_.empty(); }
192 
196  bool has_parametric_grid() const { return grid_.empty(); }
197 
198  casadi_int arg_values() const;
199  casadi_int arg_grid() const;
200 
204  casadi_int coeff_size() const;
205  static casadi_int coeff_size(const std::vector<casadi_int>& offset, casadi_int m);
206 
207  protected:
208 
209  bool arg_values(casadi_int i) const;
210  bool arg_grid(casadi_int i) const;
211 
215  explicit Interpolant(DeserializingStream& s);
216 
217 
218  };
219 
220 } // namespace casadi
222 
223 #endif // CASADI_INTERPOLANT_IMPL_HPP
Helper class for Serialization.
Internal class for Function.
Function object.
Definition: function.hpp:60
std::vector< std::string > lookup_modes_
static const Options options_
Options.
std::string serialize_base_function() const override
String used to identify the immediate FunctionInternal subclass.
std::vector< casadi_int > offset_
size_t get_n_out() override
Number of function inputs and outputs.
std::vector< double > grid_
bool has_parametric_grid() const
Is parametric?
std::string class_name() const override
Get type name.
std::vector< double > values_
size_t get_n_in() override
Number of function inputs and outputs.
bool get_diff_in(casadi_int i) override
Which inputs are differentiable?
static const std::string infix_
Infix.
bool has_parametric_values() const
Is parametric?
const Options & get_options() const override
Options.
static std::map< std::string, Plugin > solvers_
Collection of solvers.
Interface for accessing input and output data structures.
Base class for FunctionInternal and LinsolInternal.
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:106
The casadi namespace.
Definition: archiver.cpp:28
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Options metadata for a class.
Definition: options.hpp:40