26 #include "linear_interpolant.hpp"
31 int CASADI_INTERPOLANT_LINEAR_EXPORT
34 plugin->name =
"linear";
36 plugin->version = CASADI_VERSION;
52 "Sets, for each grid dimenion, the lookup algorithm used to find the correct index. "
53 "'linear' uses a for-loop + break; "
54 "'exact' uses floored division (only for uniform grids)."}}
60 const std::vector<double>& grid,
61 const std::vector<casadi_int>& offset,
62 const std::vector<double>& values,
87 eval(
const double** arg,
double** res, casadi_int* iw,
double* w,
void* mem)
const {
88 setup(mem, arg, res, iw, w);
101 g <<
" if (res[0]) {\n"
109 const std::vector<std::string>& inames,
110 const std::vector<std::string>& onames,
111 const Dict& opts)
const {
120 const std::vector<std::string>& inames,
121 const std::vector<std::string>& onames,
122 const Dict& opts)
const {
123 std::vector<MX> args =
mx_in();
124 std::vector<MX> res(
n_out_);
125 for (casadi_int i=0;i<
n_out_;++i)
138 alloc_w(2*m->ndim_ + m->m_,
true);
143 eval(
const double** arg,
double** res, casadi_int* iw,
double* w,
void* mem)
const {
150 values, arg[0],
get_ptr(m->lookup_mode_), m->m_, iw, w);
171 grid, g.
constant(m->offset_), values,
172 "arg[0]", g.
constant(m->lookup_mode_), m->m_,
"iw",
"w") <<
"\n";
181 s.
version(
"LinearInterpolant", 1);
183 s.
unpack(
"LinearInterpolant::type", type);
188 casadi_error(
"LinearInterpolant::deserialize error");
199 s.
version(
"LinearInterpolant", 1);
200 s.
pack(
"LinearInterpolant::type",
'f');
206 m->PluginInterface<Interpolant>::serialize_type(s);
207 s.
version(
"LinearInterpolant", 1);
208 s.
pack(
"LinearInterpolant::type",
'j');
213 const std::vector<double>& grid,
214 const std::vector<casadi_int>& offset,
215 const std::vector<double>& values,
220 casadi_int ndim = offset.size()-1;
225 g =
MX::sym(
"g", offset.back());
230 if (values.empty()) {
238 std::vector<MX> args = {x};
239 std::vector<std::string> arg_names = {
"x"};
242 arg_names.push_back(
"g");
244 if (values.empty()) {
246 arg_names.push_back(
"c");
249 return Function(name, args, {f.
T()}, arg_names, {
"f"});
Helper class for C code generation.
std::string arg(casadi_int i) const
Refer to argument.
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
std::string interpn(const std::string &res, casadi_int ndim, const std::string &grid, const std::string &offset, const std::string &values, const std::string &x, const std::string &lookup_mode, casadi_int m, const std::string &iw, const std::string &w)
Multilinear interpolation.
std::string interpn_grad(const std::string &grad, casadi_int ndim, const std::string &grid, const std::string &offset, const std::string &values, const std::string &x, const std::string &lookup_mode, casadi_int m, const std::string &iw, const std::string &w)
Multilinear interpolation - calculate gradient.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void alloc_iw(size_t sz_iw, bool persistent=false)
Ensure required length of iw field.
void init(const Dict &opts) override
Initialize.
virtual const std::vector< MX > mx_in() const
Get function input(s) and output(s)
casadi_int size2_out(casadi_int ind) const
Input/output dimensions.
casadi_int size1_out(casadi_int ind) const
Input/output dimensions.
void serialize_type(SerializingStream &s) const override
Serialize type information.
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
virtual Function get_jacobian(const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const
Return Jacobian of all input elements with respect to all output elements.
void setup(void *mem, const double **arg, double **res, casadi_int *iw, double *w) const
Set the (persistent and temporary) work vectors.
Function derivative_of_
If the function is the derivative of another function.
FunctionInternal * get() const
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.
std::vector< std::string > lookup_modes_
static const Options options_
Options.
casadi_int coeff_size() const
Size of the flattened coefficients vector.
void init(const Dict &opts) override
Initialize.
std::vector< casadi_int > offset_
std::vector< double > grid_
bool has_parametric_grid() const
Is parametric?
std::vector< double > values_
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
casadi_int arg_values() const
casadi_int arg_grid() const
bool has_parametric_values() const
Is parametric?
void serialize_type(SerializingStream &s) const override
Serialize type information.
static std::vector< casadi_int > interpret_lookup_mode(const std::vector< std::string > &modes, const std::vector< double > &grid, const std::vector< casadi_int > &offset, const std::vector< casadi_int > &margin_left=std::vector< casadi_int >(), const std::vector< casadi_int > &margin_right=std::vector< casadi_int >())
Convert from (optional) lookup modes labels to enum.
bool has_parametric_values() const
Is parametric?
void serialize_type(SerializingStream &s) const override
Serialize type information.
void init(const Dict &opts) override
Initialize.
int eval(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Evaluate numerically.
~LinearInterpolantJac() override
Destructor.
void codegen_body(CodeGenerator &g) const override
Generate code for the body of the C function.
bool has_parametric_grid() const
Function get_jacobian(const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Full Jacobian.
'linear' plugin for Interpolant Implements a multilinear interpolant: For 1D, the interpolating polyn...
int eval(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Evaluate numerically.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
void serialize_type(SerializingStream &s) const override
Serialize type information.
static Interpolant * creator(const std::string &name, const std::vector< double > &grid, const std::vector< casadi_int > &offset, const std::vector< double > &values, casadi_int m)
Create a new Interpolant.
static const Options options_
Options.
std::vector< casadi_int > lookup_mode_
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void init(const Dict &opts) override
Initialize.
void codegen_body(CodeGenerator &g) const override
Generate code for the body of the C function.
Function get_jacobian(const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Full Jacobian.
~LinearInterpolant() override
static Function do_inline(const std::string &name, const std::vector< double > &grid, const std::vector< casadi_int > &offset, const std::vector< double > &values, casadi_int m, const Dict &opts)
LinearInterpolant(const std::string &name, const std::vector< double > &grid, const std::vector< casadi_int > &offset, const std::vector< double > &values, casadi_int m)
static const std::string meta_doc
A documentation string.
MX T() const
Transpose the matrix.
static MX interpn_linear(const std::vector< MX > &x, const MX &v, const std::vector< MX > &xq, const Dict &opts=Dict())
Low-level access to inlined linear interpolation.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
Base class for FunctionInternal and LinsolInternal.
void construct(const Dict &opts)
Construct.
void clear_mem()
Clear all memory (called from destructor)
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
T1 casadi_interpn(casadi_int ndim, const T1 *grid, const casadi_int *offset, const T1 *values, const T1 *x, casadi_int *iw, T1 *w)
int CASADI_INTERPOLANT_LINEAR_EXPORT casadi_register_interpolant_linear(Interpolant::Plugin *plugin)
void CASADI_INTERPOLANT_LINEAR_EXPORT casadi_load_interpolant_linear()
void casadi_interpn_grad(T1 *grad, casadi_int ndim, const T1 *grid, const casadi_int *offset, const T1 *values, const T1 *x, casadi_int *iw, T1 *w)
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.