symbolic_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 "symbolic_mx.hpp"
27 #include "casadi_misc.hpp"
28 #include "serializing_stream.hpp"
29 
30 namespace casadi {
31 
32  SymbolicMX::SymbolicMX(const std::string& name, casadi_int nrow, casadi_int ncol) : name_(name) {
33  set_sparsity(Sparsity::dense(nrow, ncol));
34  }
35 
36  SymbolicMX::SymbolicMX(const std::string& name, const Sparsity & sp) : name_(name) {
37  set_sparsity(sp);
38  }
39 
40  std::string SymbolicMX::disp(const std::vector<std::string>& arg) const {
41  return name_;
42  }
43 
44  int SymbolicMX::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
45  return 0;
46  }
47 
48  int SymbolicMX::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
49  return 0;
50  }
51 
52  void SymbolicMX::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
53  }
54 
55  void SymbolicMX::ad_forward(const std::vector<std::vector<MX> >& fseed,
56  std::vector<std::vector<MX> >& fsens) const {
57  }
58 
59  void SymbolicMX::ad_reverse(const std::vector<std::vector<MX> >& aseed,
60  std::vector<std::vector<MX> >& asens) const {
61  }
62 
63  const std::string& SymbolicMX::name() const {
64  return name_;
65  }
66 
67  int SymbolicMX::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
68  std::fill_n(res[0], nnz(), 0);
69  return 0;
70  }
71 
72  int SymbolicMX::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
73  std::fill_n(res[0], nnz(), 0);
74  return 0;
75  }
76 
78  if (this->temp!=0) {
79  casadi_warning("Duplicate expression: " + name());
80  return true;
81  } else {
82  this->temp = 1;
83  return false;
84  }
85  }
86 
87  void SymbolicMX::reset_input() const {
88  this->temp = 0;
89  }
90 
93  s.pack("SymbolicMX::name", name_);
94  }
95 
97  s.unpack("SymbolicMX::name", name_);
98  }
99 
100 } // namespace casadi
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
Node class for MX objects.
Definition: mx_node.hpp:51
casadi_int temp
Definition: mx_node.hpp:757
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:389
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
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.
General sparsity class.
Definition: sparsity.hpp:106
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
Definition: sparsity.cpp:1012
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: symbolic_mx.cpp:59
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: symbolic_mx.cpp:91
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: symbolic_mx.cpp:40
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: symbolic_mx.cpp:48
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: symbolic_mx.cpp:44
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: symbolic_mx.cpp:72
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: symbolic_mx.cpp:55
void reset_input() const override
Reset the marker for an input expression.
Definition: symbolic_mx.cpp:87
SymbolicMX(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Constructors.
Definition: symbolic_mx.cpp:32
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: symbolic_mx.cpp:52
bool has_duplicates() const override
Detect duplicate symbolic expressions.
Definition: symbolic_mx.cpp:77
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: symbolic_mx.cpp:67
const std::string & name() const override
Get the name.
Definition: symbolic_mx.cpp:63
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t