unary_mx.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 "unary_mx.hpp"
27 #include "casadi_misc.hpp"
28 #include "global_options.hpp"
29 #include "serializing_stream.hpp"
30 #include <sstream>
31 #include <vector>
32 
33 namespace casadi {
34 
35  UnaryMX::UnaryMX(Operation op, MX x) : op_(op) {
36  // Put a densifying node in between if necessary
37  if (!operation_checker<F00Checker>(op_)) {
38  x = densify(x);
39  }
40 
41  set_dep(x);
42  set_sparsity(x->sparsity());
43  }
44 
45  std::string UnaryMX::disp(const std::vector<std::string>& arg) const {
46  return casadi_math<double>::print(op_, arg.at(0));
47  }
48 
49  int UnaryMX::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
50  double dummy = std::numeric_limits<double>::quiet_NaN();
51  casadi_math<double>::fun(op_, arg[0], dummy, res[0], nnz());
52  return 0;
53  }
54 
55  int UnaryMX::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
56  SXElem dummy = 0;
57  casadi_math<SXElem>::fun(op_, arg[0], dummy, res[0], nnz());
58  return 0;
59  }
60 
61  void UnaryMX::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
62  MX dummy;
63  casadi_math<MX>::fun(op_, arg[0], dummy, res[0]);
64  }
65 
66  void UnaryMX::eval_linear(const std::vector<std::array<MX, 3> >& arg,
67  std::vector<std::array<MX, 3> >& res) const {
68  MX dummy[3];
69  casadi_math<MX>::fun_linear(op_, arg[0].data(), dummy, res[0].data());
70  }
71 
72  void UnaryMX::ad_forward(const std::vector<std::vector<MX> >& fseed,
73  std::vector<std::vector<MX> >& fsens) const {
74  // Get partial derivatives
75  MX pd[2];
76  MX dummy; // Function value, dummy second argument
77  casadi_math<MX>::der(op_, dep(), dummy, shared_from_this<MX>(), pd);
78 
79  // Propagate forward seeds
80  for (casadi_int d=0; d<fsens.size(); ++d) {
81  fsens[d][0] = pd[0]*fseed[d][0];
82  }
83  }
84 
85  void UnaryMX::ad_reverse(const std::vector<std::vector<MX> >& aseed,
86  std::vector<std::vector<MX> >& asens) const {
87  // Get partial derivatives
88  MX pd[2];
89  MX dummy; // Function value, dummy second argument
90  casadi_math<MX>::der(op_, dep(), dummy, shared_from_this<MX>(), pd);
91 
92  // Propagate adjoint seeds
93  for (casadi_int d=0; d<aseed.size(); ++d) {
94  asens[d][0] += pd[0]*aseed[d][0];
95  }
96  }
97 
98  int UnaryMX::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
99  copy_fwd(arg[0], res[0], nnz());
100  return 0;
101  }
102 
103  int UnaryMX::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
104  copy_rev(arg[0], res[0], nnz());
105  return 0;
106  }
107 
109  const std::vector<casadi_int>& arg,
110  const std::vector<casadi_int>& res,
111  const std::vector<bool>& arg_is_ref,
112  std::vector<bool>& res_is_ref) const {
113  std::string r, x;
114  if (nnz()==1) {
115  // Scalar assignment
116  r = g.workel(res[0]);
117  x = g.workel(arg[0]);
118  } else {
119  // Vector assignment
120  g.local("cs", "const casadi_real", "*");
121  g.local("rr", "casadi_real", "*");
122  g.local("i", "casadi_int");
123  g << "for (i=0, rr=" << g.work(res[0], nnz(), false) << ", cs="
124  << g.work(arg[0], nnz(), arg_is_ref[0])
125  << "; i<" << sparsity().nnz() << "; ++i) ";
126  r = "*rr++";
127  x = "*cs++";
128  }
129 
130  // Output the operation
131  g << r << " = " << g.print_op(op_, " " + x + " ") << ";\n";
132  }
133 
134  MX UnaryMX::get_unary(casadi_int op) const {
136 
137  switch (op_) {
138  case OP_NEG:
139  if (op==OP_NEG) return dep();
140  else if (op==OP_SQ) return dep()->get_unary(OP_SQ);
141  else if (op==OP_FABS) return dep()->get_unary(OP_FABS);
142  else if (op==OP_COS) return dep()->get_unary(OP_COS);
143  break;
144  case OP_SQRT:
145  if (op==OP_SQ) return dep();
146  else if (op==OP_FABS) return shared_from_this<MX>();
147  break;
148  case OP_SQ:
149  if (op==OP_SQRT) return dep()->get_unary(OP_FABS);
150  else if (op==OP_FABS) return shared_from_this<MX>();
151  break;
152  case OP_EXP:
153  if (op==OP_LOG) return dep();
154  else if (op==OP_FABS) return shared_from_this<MX>();
155  break;
156  case OP_LOG:
157  if (op==OP_EXP) return dep();
158  break;
159  case OP_FABS:
160  if (op==OP_FABS) return shared_from_this<MX>();
161  else if (op==OP_SQ) return dep()->get_unary(OP_SQ);
162  else if (op==OP_COS) return dep()->get_unary(OP_COS);
163  break;
164  case OP_INV:
165  if (op==OP_INV) return dep();
166  break;
167  default: break; // no rule
168  }
169 
170  // Fallback to default implementation
171  return MXNode::get_unary(op);
172  }
173 
174  MX UnaryMX::_get_binary(casadi_int op, const MX& y, bool scX, bool scY) const {
175  switch (op_) {
176  case OP_NEG:
177  if (op==OP_ADD) return y->_get_binary(OP_SUB, dep(), scY, scX);
178  else if (op==OP_MUL) return -dep()->_get_binary(OP_MUL, y, scX, scY);
179  else if (op==OP_DIV) return -dep()->_get_binary(OP_DIV, y, scX, scY);
180  break;
181  case OP_INV:
182  if (op==OP_MUL) return y->_get_binary(OP_DIV, dep(), scY, scX);
183  break;
184  case OP_TWICE:
185  if (op==OP_SUB && MX::is_equal(y, dep(), maxDepth())) return dep();
186  break;
187  case OP_SQ:
188  if (op==OP_ADD && y.op()==OP_SQ) /*sum of squares:*/
189  if ((dep().op()==OP_SIN && y->dep().op()==OP_COS) ||
190  (dep().op()==OP_COS && y->dep()->op()==OP_SIN)) /* sin^2(x)+sin^2(y) */
191  if (MX::is_equal(dep()->dep(), y->dep()->dep(), maxDepth())) /*sin^2(x) + cos^2(x) */
192  return MX::ones(y.sparsity());
193  break;
194  default: break; // no rule
195  }
196 
197  // Fallback to default implementation
198  return MXNode::_get_binary(op, y, scX, scY);
199  }
200 
203  s.pack("UnaryMX::op", static_cast<int>(op_));
204  }
205 
207  int op;
208  s.unpack("UnaryMX::op", op);
209  op_ = Operation(op);
210  }
211 
212 } // namespace casadi
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string print_op(casadi_int op, const std::string &a0)
Print an operation to a c file.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string workel(casadi_int n) const
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
static MX ones(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries one.
static bool simplification_on_the_fly
Indicates whether simplifications should be made on the fly.
Node class for MX objects.
Definition: mx_node.hpp:51
static void copy_fwd(const bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities forward through a copy operation.
Definition: mx_node.cpp:1241
static bool maxDepth()
Get equality checking depth.
Definition: mx_node.hpp:342
static void copy_rev(bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities backwards through a copy operation.
Definition: mx_node.cpp:1247
virtual MX get_unary(casadi_int op) const
Get a unary operation.
Definition: mx_node.cpp:778
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:372
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
virtual casadi_int op() const =0
Get the operation.
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:226
virtual MX _get_binary(casadi_int op, const MX &y, bool scX, bool scY) const
Get a binary operation operation (matrix-matrix)
Definition: mx_node.cpp:843
MX - Matrix expression.
Definition: mx.hpp:92
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:592
static bool is_equal(const MX &x, const MX &y, casadi_int depth=0)
Definition: mx.cpp:838
casadi_int op() const
Get operation type.
Definition: mx.cpp:822
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148
Operation op_
operation
Definition: unary_mx.hpp:142
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: unary_mx.cpp:85
MX _get_binary(casadi_int op, const MX &y, bool scX, bool scY) const override
Get a binary operation operation.
Definition: unary_mx.cpp:174
void eval_linear(const std::vector< std::array< MX, 3 > > &arg, std::vector< std::array< MX, 3 > > &res) const override
Evaluate the MX node on a const/linear/nonlinear partition.
Definition: unary_mx.cpp:66
casadi_int op() const override
Get the operation.
Definition: unary_mx.hpp:104
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: unary_mx.cpp:98
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: unary_mx.cpp:61
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: unary_mx.cpp:49
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: unary_mx.cpp:55
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: unary_mx.cpp:201
MX get_unary(casadi_int op) const override
Get a unary operation.
Definition: unary_mx.cpp:134
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: unary_mx.cpp:108
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: unary_mx.cpp:103
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: unary_mx.cpp:45
UnaryMX(Operation op, MX x)
Constructor is private, use "create" below.
Definition: unary_mx.cpp:35
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: unary_mx.cpp:72
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
Operation
Enum for quick access to any node.
Definition: calculus.hpp:60
@ OP_COS
Definition: calculus.hpp:68
@ OP_SQRT
Definition: calculus.hpp:67
@ OP_INV
Definition: calculus.hpp:73
@ OP_EXP
Definition: calculus.hpp:66
@ OP_SIN
Definition: calculus.hpp:68
@ OP_TWICE
Definition: calculus.hpp:67
@ OP_SUB
Definition: calculus.hpp:65
@ OP_FABS
Definition: calculus.hpp:71
@ OP_LOG
Definition: calculus.hpp:66
@ OP_ADD
Definition: calculus.hpp:65
@ OP_DIV
Definition: calculus.hpp:65
@ OP_NEG
Definition: calculus.hpp:66
@ OP_MUL
Definition: calculus.hpp:65
@ OP_SQ
Definition: calculus.hpp:67
static void der(unsigned char op, const T &x, const T &y, const T &f, T *d)
Evaluate a built in derivative function.
Definition: calculus.hpp:1375
static void fun_linear(unsigned char op, const T *x, const T *y, T *f)
Evaluate function on a const/linear/nonlinear partition.
Definition: calculus.hpp:1552
static std::string print(unsigned char op, const std::string &x, const std::string &y)
Print.
Definition: calculus.hpp:1641
static void fun(unsigned char op, const T &x, const T &y, T &f)
Evaluate a built in function (scalar-scalar)
Definition: calculus.hpp:1289