casadi_low.cpp
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 #include "casadi_low.hpp"
27 
28 namespace casadi {
29 
30  Low::Low(const MX& v, const MX& p, const Dict& opts) {
31  casadi_assert_dev(v.is_vector() && v.is_dense());
32  casadi_assert_dev(p.is_vector() && v.is_dense());
33  set_dep(v, p);
35 
36  std::string lookup_mode = "auto";
37  for (auto&& e : opts) {
38  if (e.first=="lookup_mode") {
39  lookup_mode = e.second.to_string();
40  } else {
41  casadi_error("Unrecognized option: " + str(e.first));
42  }
43  }
44 
45  lookup_mode_ = interpret_lookup_mode(lookup_mode, v.numel());
46  }
47 
48  std::string Low::lookup_mode_from_enum(casadi_int lookup_mode) {
49  switch (lookup_mode) {
50  case LOOKUP_LINEAR:
51  return "linear";
52  case LOOKUP_EXACT:
53  return "exact";
54  case LOOKUP_BINARY:
55  return "binary";
56  default:
57  casadi_assert_dev(false);
58  }
59  }
60 
61  casadi_int Low::interpret_lookup_mode(const std::string& lookup_mode, casadi_int n) {
62  if (lookup_mode=="auto") {
63  if (n>100) return interpret_lookup_mode("binary", n);
64  return interpret_lookup_mode("linear", n);
65  } else if (lookup_mode=="binary") {
66  return LOOKUP_BINARY;
67  } else if (lookup_mode=="linear") {
68  return LOOKUP_LINEAR;
69  } else if (lookup_mode=="exact") {
70  return LOOKUP_EXACT;
71  } else {
72  casadi_error("Invalid lookup mode '" + lookup_mode + "'. "
73  "Available modes: linear|binary|exact|auto");
74  }
75  }
76 
77  std::string Low::disp(const std::vector<std::string>& arg) const {
78  return "low(" + arg.at(0) + ", " + arg.at(1) + ")";
79  }
80 
81  int Low::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
82  for (casadi_int i=0;i<dep(1).nnz();++i) {
83  res[0][i] = casadi_low(arg[1][i], arg[0], dep(0).nnz(), lookup_mode_);
84  }
85  return 0;
86  }
87 
88  void Low::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
89  res[0] = low(arg[0], arg[1]);
90  }
91 
92  void Low::ad_forward(const std::vector<std::vector<MX> >& fseed,
93  std::vector<std::vector<MX> >& fsens) const {
94  for (casadi_int d=0; d<fsens.size(); ++d) {
95  fsens[d][0] = 0;
96  }
97  }
98 
99  void Low::ad_reverse(const std::vector<std::vector<MX> >& aseed,
100  std::vector<std::vector<MX> >& asens) const {
101  }
102 
103  int Low::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
104  std::fill_n(res[0], nnz(), 0);
105  return 0;
106  }
107 
108  int Low::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
109  std::fill_n(res[0], nnz(), 0);
110  return 0;
111  }
112 
114  const std::vector<casadi_int>& arg,
115  const std::vector<casadi_int>& res,
116  const std::vector<bool>& arg_is_ref,
117  std::vector<bool>& res_is_ref) const {
118  casadi_int n = dep(1).nnz();
119  casadi_int ng = dep(0).nnz();
120  g.local("cr", "const casadi_real", "*");
121  g.local("rr", "casadi_real", "*");
122  g << "for (cr=" << g.work(arg[1], n, arg_is_ref[1]) << ", rr=" << g.work(res[0], n, false);
123  g << ";cr!=" << g.work(arg[1], n, arg_is_ref[1]) << "+" << n << ";++cr) ";
124  g << "*rr++ = ";
125  g << g.low("*cr", g.work(arg[0], ng, arg_is_ref[0]), ng, lookup_mode_) << "\n";
126  }
127 
130  s.pack("Low::lookup_mode", static_cast<casadi_int>(lookup_mode_));
131  }
132 
134  casadi_int lookup_mode;
135  s.unpack("Low::lookup_mode", lookup_mode);
136  lookup_mode_ = static_cast<casadi_int>(lookup_mode);
137  }
138 
139 } // namespace casadi
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string low(const std::string &x, const std::string &grid, casadi_int ng, casadi_int lookup_mode)
low
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
casadi_int numel() const
Get the number of elements.
bool is_dense() const
Check if the matrix expression is dense.
bool is_vector() const
Check if the matrix is a row or column vector.
casadi_int nnz() const
Get the number of (structural) non-zero elements.
static std::string lookup_mode_from_enum(casadi_int lookup_mode)
Definition: casadi_low.cpp:48
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: casadi_low.cpp:92
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: casadi_low.cpp:128
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: casadi_low.cpp:81
Low(const MX &v, const MX &p, const Dict &opts)
Constructor.
Definition: casadi_low.cpp:30
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: casadi_low.cpp:108
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: casadi_low.cpp:88
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
Definition: casadi_low.cpp:113
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: casadi_low.cpp:99
static casadi_int interpret_lookup_mode(const std::string &lookup_mode, casadi_int n)
Definition: casadi_low.cpp:61
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: casadi_low.cpp:103
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: casadi_low.cpp:77
Node class for MX objects.
Definition: mx_node.hpp:51
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:389
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:354
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
Definition: mx_node.cpp:523
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:222
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:226
MX - Matrix expression.
Definition: mx.hpp:92
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:592
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
casadi_int casadi_low(T1 x, const T1 *grid, casadi_int ng, casadi_int lookup_mode)
constexpr casadi_int LOOKUP_BINARY
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
constexpr casadi_int LOOKUP_EXACT
constexpr casadi_int LOOKUP_LINEAR