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,
53  const std::vector<bool>& unique) const {
54  }
55 
56  void SymbolicMX::ad_forward(const std::vector<std::vector<MX> >& fseed,
57  std::vector<std::vector<MX> >& fsens) const {
58  }
59 
60  void SymbolicMX::ad_reverse(const std::vector<std::vector<MX> >& aseed,
61  std::vector<std::vector<MX> >& asens) const {
62  }
63 
64  const std::string& SymbolicMX::name() const {
65  return name_;
66  }
67 
68  int SymbolicMX::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
69  std::fill_n(res[0], nnz(), 0);
70  return 0;
71  }
72 
73  int SymbolicMX::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
74  std::fill_n(res[0], nnz(), 0);
75  return 0;
76  }
77 
79  if (this->temp!=0) {
80  casadi_warning("Duplicate expression: " + name());
81  return true;
82  } else {
83  this->temp = 1;
84  return false;
85  }
86  }
87 
88  void SymbolicMX::reset_input() const {
89  this->temp = 0;
90  }
91 
94  s.pack("SymbolicMX::name", name_);
95  }
96 
98  s.unpack("SymbolicMX::name", name_);
99  }
100 
101 } // 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:819
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:427
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
Definition: mx_node.cpp:530
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:224
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:1028
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:60
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: symbolic_mx.cpp:92
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:73
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:56
void reset_input() const override
Reset the marker for an input expression.
Definition: symbolic_mx.cpp:88
SymbolicMX(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Constructors.
Definition: symbolic_mx.cpp:32
bool has_duplicates() const override
Detect duplicate symbolic expressions.
Definition: symbolic_mx.cpp:78
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: symbolic_mx.cpp:52
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:68
const std::string & name() const override
Get the name.
Definition: symbolic_mx.cpp:64
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t