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  bool SX::is_call() const;
69 
70  template<>
71  std::vector<SXElem> SX::call(const Function& f, const std::vector<SXElem>& dep);
72 
73  template<>
74  bool SX::is_output() const;
75 
76  template<>
77  bool SX::has_output() const;
78 
79  template<>
80  SX SX::get_output(casadi_int oind) const;
81 
82  template<>
83  Function SX::which_function() const;
84 
85  template<>
86  casadi_int SX::which_output() const;
87 
88  template<>
89  casadi_int SX::op() const;
90 
91  template<>
92  bool SX::is_op(casadi_int op) const;
93 
94  template<> bool SX::has_duplicates() const;
95 
96  template<> void SX::reset_input() const;
97 
98  template<>
99  std::string SX::name() const;
100  template<>
101  SX SX::dep(casadi_int ch) const;
102 
103  template<>
104  casadi_int SX::n_dep() const;
105 
106  template<>
107  void SX::expand(const SX& ex2, SX& ww, SX& tt);
108 
109  template<>
110  SX SX::pw_const(const SX& t, const SX& tval, const SX& val);
111 
112  template<>
113  SX SX::pw_lin(const SX& t, const SX& tval, const SX& val);
114 
115  template<>
116  SX SX::gauss_quadrature(const SX& f, const SX& x, const SX& a, const SX& b, casadi_int order,
117  const SX& w);
118 
119  template<>
120  SX SX::simplify(const SX& x);
121 
122  template<>
123  std::vector<SX>
124  SX::substitute(const std::vector<SX>& ex, const std::vector<SX>& v, const std::vector<SX>& vdef);
125 
126  template<>
127  SX SX::substitute(const SX& ex, const SX& v, const SX& vdef);
128 
129  template<>
130  void SX::substitute_inplace(const std::vector<SX>& v, std::vector<SX>& vdef,
131  std::vector<SX>& ex, bool reverse);
132 
133  template<>
134  void SX::extract_parametric(const SX &expr, const SX& par,
135  SX& expr_ret, std::vector<SX>& symbols, std::vector<SX>& parametric,
136  const Dict& opts);
137 
138  template<>
139  void SX::separate_linear(const SX &expr,
140  const SX &sym_lin, const SX &sym_const,
141  SX& expr_const, SX& expr_lin, SX& expr_nonlin);
142 
143  template<>
144  std::vector<SX> SX::cse(const std::vector<SX>& e);
145 
146  template<>
147  bool SX::depends_on(const SX &x, const SX &arg);
148 
149  template<>
150  bool SX::contains_all(const std::vector<SX>& v, const std::vector<SX> &n);
151 
152  template<>
153  bool SX::contains_any(const std::vector<SX>& v, const std::vector<SX> &n);
154 
155  template<>
156  SX SX::jacobian(const SX &f, const SX &x, const Dict& opts);
157  template<>
158  SX SX::hessian(const SX &ex, const SX &arg, SX &g, const Dict& opts);
159  template<>
160  SX SX::hessian(const SX &ex, const SX &arg, const Dict& opts);
161 
162  template<>
163  std::vector<std::vector<SX> >
164  SX::forward(const std::vector<SX> &ex, const std::vector<SX> &arg,
165  const std::vector<std::vector<SX> > &v, const Dict& opts);
166 
167  template<>
168  std::vector<std::vector<SX> >
169  SX::reverse(const std::vector<SX> &ex, const std::vector<SX> &arg,
170  const std::vector<std::vector<SX> > &v, const Dict& opts);
171 
172  template<>
173  std::vector<bool> SX::which_depends(const SX &expr, const SX &var, casadi_int order, bool tr);
174 
175  template<>
176  Sparsity SX::jacobian_sparsity(const SX &f, const SX &x);
177 
178  template<>
179  SX SX::taylor(const SX& f, const SX& x,
180  const SX& a, casadi_int order);
181 
182  template<>
183  SX SX::mtaylor(const SX& f, const SX& x, const SX& a, casadi_int order,
184  const std::vector<casadi_int>& order_contributions);
185 
186  template<>
187  SX SX::mtaylor(const SX& f, const SX& x, const SX& a, casadi_int order);
188 
189  template<>
190  casadi_int SX::n_nodes(const SX& x);
191 
192  template<>
193  std::string
194  SX::print_operator(const SX& X, const std::vector<std::string>& args);
195 
196  template<>
197  std::vector<SX> SX::symvar(const SX& x);
198 
199  template<>
200  void SX::extract(std::vector<SX>& ex, std::vector<SX>& v_sx,
201  std::vector<SX>& vdef_sx, const Dict& opts);
202 
203  template<>
204  void SX::shared(std::vector<SX>& ex,
205  std::vector<SX>& v_sx,
206  std::vector<SX> & vdef_sx,
207  const std::string& v_prefix,
208  const std::string& v_suffix);
209 
210  template<>
211  SX SX::poly_coeff(const SX& ex, const SX& x);
212 
213  template<>
214  SX SX::poly_roots(const SX& p);
215 
216  template<>
217  SX SX::eig_symbolic(const SX& m);
218 
219  template<>
220  void SX::print_split(casadi_int nnz, const SXElem* nonzeros, std::vector<std::string>& nz,
221  std::vector<std::string>& inter);
222 
223  template<> std::vector<SX> SX::get_input(const Function& f);
224  template<> std::vector<SX> SX::get_free(const Function& f);
225  template<>
226  Dict CASADI_EXPORT SX::info() const;
227 
228  template<>
229  void CASADI_EXPORT SX::to_file(const std::string& filename, const Sparsity& sp,
230  const SXElem* nonzeros, const std::string& format_hint);
231 
232 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
233  template<>
234  std::mutex& SX::get_mutex_temp();
235 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
236 
237 #ifndef CASADI_SX_INSTANTIATOR_CPP
238  extern template class Matrix<SXElem>;
239 #endif // CASADI_SX_INSTANTIATOR_CPP
240 
241 } // namespace casadi
242 
243 
244 #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.
Function which_function() const
Get function - only valid when is_call() is true.
casadi_int element_hash() const
Returns a number that is unique for a given symbolic scalar.
casadi_int which_output() const
Get the index of evaluation output - only valid when is_output() is true.
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_call() const
Check if function call.
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.
bool is_output() const
Check if evaluation output.
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.
bool has_output() const
Check if a multiple output node.
casadi_int op() const
Get operation type.
bool is_valid_input() const
Check if matrix can be used to define function inputs.
Matrix< Scalar > get_output(casadi_int oind) const
Get an output.
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 void extract_parametric(const Matrix< Scalar > &expr, const Matrix< Scalar > &par, Matrix< Scalar > &expr_ret, std::vector< Matrix< Scalar > > &symbols, std::vector< Matrix< Scalar > > &parametric, const Dict &opts=Dict())
Extract purely parametric parts from an expression graph.
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 void separate_linear(const Matrix< Scalar > &expr, const Matrix< Scalar > &sym_lin, const Matrix< Scalar > &sym_const, Matrix< Scalar > &expr_const, Matrix< Scalar > &expr_lin, Matrix< Scalar > &expr_nonlin)
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 bool contains_all(const std::vector< Matrix< Scalar > > &v, const std::vector< Matrix< Scalar > > &n)
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 bool contains_any(const std::vector< Matrix< Scalar > > &v, const std::vector< Matrix< Scalar > > &n)
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.
Definition: archiver.hpp:32
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.