sx.hpp
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 #ifndef CASADI_SX_HPP
26 #define CASADI_SX_HPP
27 
28 #include "sx_fwd.hpp"
29 #include "matrix_decl.hpp"
30 
31 namespace casadi {
32 
33 template<> inline std::string matrixName<SXElem>() { return "SX"; }
34 
35 
36 
37  template<>
38  bool SX::__nonzero__() const;
39  template<>
40  void SX::set_max_depth(casadi_int eq_depth);
41  template<>
42  casadi_int SX::get_max_depth();
43  template<>
44  SX SX::_sym(const std::string& name, const Sparsity& sp);
45 
46  template<>
47  bool SX::is_regular() const;
48 
49  template<>
50  bool SX::is_smooth() const;
51 
52  template<>
53  casadi_int SX::element_hash() const;
54 
55  template<>
56  bool SX::is_leaf() const;
57 
58  template<>
59  bool SX::is_commutative() const;
60 
61  template<>
62  bool SX::is_valid_input() const;
63 
64  template<>
65  bool SX::is_symbolic() const;
66 
67  template<>
68  casadi_int SX::op() const;
69 
70  template<>
71  bool SX::is_op(casadi_int op) const;
72 
73  template<> bool SX::has_duplicates() const;
74 
75  template<> void SX::reset_input() const;
76 
77  template<>
78  std::string SX::name() const;
79  template<>
80  SX SX::dep(casadi_int ch) const;
81 
82  template<>
83  casadi_int SX::n_dep() const;
84 
85  template<>
86  void SX::expand(const SX& ex2, SX& ww, SX& tt);
87 
88  template<>
89  SX SX::pw_const(const SX& t, const SX& tval, const SX& val);
90 
91  template<>
92  SX SX::pw_lin(const SX& t, const SX& tval, const SX& val);
93 
94  template<>
95  SX SX::gauss_quadrature(const SX& f, const SX& x, const SX& a, const SX& b, casadi_int order,
96  const SX& w);
97 
98  template<>
99  SX SX::simplify(const SX& x);
100 
101  template<>
102  std::vector<SX>
103  SX::substitute(const std::vector<SX>& ex, const std::vector<SX>& v, const std::vector<SX>& vdef);
104 
105  template<>
106  SX SX::substitute(const SX& ex, const SX& v, const SX& vdef);
107 
108  template<>
109  void SX::substitute_inplace(const std::vector<SX>& v, std::vector<SX>& vdef,
110  std::vector<SX>& ex, bool reverse);
111 
112  template<>
113  std::vector<SX> SX::cse(const std::vector<SX>& e);
114 
115  template<>
116  bool SX::depends_on(const SX &x, const SX &arg);
117 
118  template<>
119  SX SX::jacobian(const SX &f, const SX &x, const Dict& opts);
120  template<>
121  SX SX::hessian(const SX &ex, const SX &arg, SX &g, const Dict& opts);
122  template<>
123  SX SX::hessian(const SX &ex, const SX &arg, const Dict& opts);
124 
125  template<>
126  std::vector<std::vector<SX> >
127  SX::forward(const std::vector<SX> &ex, const std::vector<SX> &arg,
128  const std::vector<std::vector<SX> > &v, const Dict& opts);
129 
130  template<>
131  std::vector<std::vector<SX> >
132  SX::reverse(const std::vector<SX> &ex, const std::vector<SX> &arg,
133  const std::vector<std::vector<SX> > &v, const Dict& opts);
134 
135  template<>
136  std::vector<bool> SX::which_depends(const SX &expr, const SX &var, casadi_int order, bool tr);
137 
138  template<>
139  Sparsity SX::jacobian_sparsity(const SX &f, const SX &x);
140 
141  template<>
142  SX SX::taylor(const SX& f, const SX& x,
143  const SX& a, casadi_int order);
144 
145  template<>
146  SX SX::mtaylor(const SX& f, const SX& x, const SX& a, casadi_int order,
147  const std::vector<casadi_int>& order_contributions);
148 
149  template<>
150  SX SX::mtaylor(const SX& f, const SX& x, const SX& a, casadi_int order);
151 
152  template<>
153  casadi_int SX::n_nodes(const SX& x);
154 
155  template<>
156  std::string
157  SX::print_operator(const SX& X, const std::vector<std::string>& args);
158 
159  template<>
160  std::vector<SX> SX::symvar(const SX& x);
161 
162  template<>
163  void SX::extract(std::vector<SX>& ex, std::vector<SX>& v_sx,
164  std::vector<SX>& vdef_sx, const Dict& opts);
165 
166  template<>
167  void SX::shared(std::vector<SX>& ex,
168  std::vector<SX>& v_sx,
169  std::vector<SX> & vdef_sx,
170  const std::string& v_prefix,
171  const std::string& v_suffix);
172 
173  template<>
174  SX SX::poly_coeff(const SX& ex, const SX& x);
175 
176  template<>
177  SX SX::poly_roots(const SX& p);
178 
179  template<>
180  SX SX::eig_symbolic(const SX& m);
181 
182  template<>
183  void SX::print_split(casadi_int nnz, const SXElem* nonzeros, std::vector<std::string>& nz,
184  std::vector<std::string>& inter);
185 
186  template<> std::vector<SX> SX::get_input(const Function& f);
187  template<> std::vector<SX> SX::get_free(const Function& f);
188  template<>
189  Dict CASADI_EXPORT SX::info() const;
190 
191  template<>
192  void CASADI_EXPORT SX::to_file(const std::string& filename, const Sparsity& sp,
193  const SXElem* nonzeros, const std::string& format_hint);
194 
195 
196 #ifndef CASADI_SX_INSTANTIATOR_CPP
197  extern template class Matrix<SXElem>;
198 #endif // CASADI_SX_INSTANTIATOR_CPP
199 
200 } // namespace casadi
201 
202 
203 #endif // CASADI_SX_HPP
void print_split(std::vector< std::string > &nz, std::vector< std::string > &inter) const
Get strings corresponding to the nonzeros and the interdependencies.
bool is_regular() const
Checks if expression does not contain NaN or Inf.
static void set_max_depth(casadi_int eq_depth=1)
Set or reset the depth to which equalities are being checked for simplifications.
casadi_int element_hash() const
Returns a number that is unique for a given symbolic scalar.
bool is_commutative() const
Check whether a binary SX is commutative.
Dict info() const
bool is_symbolic() const
Check if symbolic (Dense)
static std::vector< Matrix< Scalar > > get_free(const Function &f)
Get free.
static casadi_int get_max_depth()
Get the depth to which equalities are being checked for simplifications.
bool is_smooth() const
Check if smooth.
void to_file(const std::string &filename, const std::string &format="") const
bool __nonzero__() const
Returns the truth value of a Matrix.
Definition: matrix_impl.hpp:70
bool is_op(casadi_int op) const
Is it a certain operation.
static std::vector< Matrix< Scalar > > get_input(const Function &f)
Get function input.
Matrix< Scalar > dep(casadi_int ch=0) const
Get expressions of the children of the expression.
casadi_int op() const
Get operation type.
bool is_valid_input() const
Check if matrix can be used to define function inputs.
casadi_int n_dep() const
Get the number of dependencies of a binary SXElem.
bool is_leaf() const
Check if SX is a leaf of the SX graph.
std::string name() const
Get name (only if symbolic scalar)
friend Matrix< Scalar > mtaylor(const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order=1)
multivariate Taylor series expansion
friend bool depends_on(const Matrix< Scalar > &f, const Matrix< Scalar > &arg)
Check if expression depends on the argument.
friend Matrix< Scalar > simplify(const Matrix< Scalar > &x)
Simplify an expression.
friend Matrix< Scalar > eig_symbolic(const Matrix< Scalar > &m)
Attempts to find the eigenvalues of a symbolic matrix.
friend std::vector< bool > which_depends(const Matrix< Scalar > &expr, const Matrix< Scalar > &var, casadi_int order, bool tr)
Find out which variables enter with some order.
friend Matrix< Scalar > pw_lin(const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)
t a scalar variable (e.g. time)
friend Matrix< Scalar > hessian(const Matrix< Scalar > &ex, const Matrix< Scalar > &arg, const Dict &opts=Dict())
Hessian and (optionally) gradient.
friend std::vector< std::vector< Matrix< Scalar > > > reverse(const std::vector< Matrix< Scalar > > &ex, const std::vector< Matrix< Scalar > > &arg, const std::vector< std::vector< Matrix< Scalar > > > &v, const Dict &opts=Dict())
Reverse directional derivative.
friend std::string print_operator(const Matrix< Scalar > &xb, const std::vector< std::string > &args)
Get a string representation for a binary MatType, using custom arguments.
friend casadi_int n_nodes(const Matrix< Scalar > &A)
friend std::vector< Matrix< Scalar > > symvar(const Matrix< Scalar > &x)
Get all symbols contained in the supplied expression.
friend void expand(const Matrix< Scalar > &ex, Matrix< Scalar > &weights, Matrix< Scalar > &terms)
Expand the expression as a weighted sum (with constant weights)
friend Matrix< Scalar > substitute(const Matrix< Scalar > &ex, const Matrix< Scalar > &v, const Matrix< Scalar > &vdef)
Substitute variable v with expression vdef in an expression ex.
friend Matrix< Scalar > poly_coeff(const Matrix< Scalar > &f, const Matrix< Scalar > &x)
extracts polynomial coefficients from an expression
friend Matrix< Scalar > pw_const(const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)
Create a piecewise constant function.
friend Matrix< Scalar > taylor(const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order=1)
univariate Taylor series expansion
friend Sparsity jacobian_sparsity(const Matrix< Scalar > &f, const Matrix< Scalar > &x)
Get the sparsity pattern of a jacobian.
friend Matrix< Scalar > poly_roots(const Matrix< Scalar > &p)
Attempts to find the roots of a polynomial.
friend void substitute_inplace(const std::vector< Matrix< Scalar > > &v, std::vector< Matrix< Scalar > > &inout_vdef, std::vector< Matrix< Scalar > > &inout_ex, bool reverse=false)
Inplace substitution with piggyback expressions.
friend void extract(std::vector< Matrix< Scalar > > &ex, std::vector< Matrix< Scalar > > &v, std::vector< Matrix< Scalar > > &vdef, const Dict &opts=Dict())
Introduce intermediate variables for selected nodes in a graph.
friend std::vector< std::vector< Matrix< Scalar > > > forward(const std::vector< Matrix< Scalar > > &ex, const std::vector< Matrix< Scalar > > &arg, const std::vector< std::vector< Matrix< Scalar > > > &v, const Dict &opts=Dict())
Forward directional derivative.
friend Matrix< Scalar > cse(const Matrix< Scalar > &e)
Common subexpression elimination.
friend Matrix< Scalar > gauss_quadrature(const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int order=5)
Integrate f from a to b using Gaussian quadrature with n points.
friend void shared(std::vector< Matrix< Scalar > > &ex, std::vector< Matrix< Scalar > > &v, std::vector< Matrix< Scalar > > &vdef, const std::string &v_prefix="v_", const std::string &v_suffix="")
Extract shared subexpressions from an set of expressions.
friend Matrix< Scalar > jacobian(const Matrix< Scalar > &ex, const Matrix< Scalar > &arg, const Dict &opts=Dict())
Calculate Jacobian.
The casadi namespace.
std::string matrixName< SXElem >()
Definition: sx.hpp:33
Matrix< SXElem > SX
Definition: sx_fwd.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.