26 #ifndef CASADI_MX_NODE_HPP
27 #define CASADI_MX_NODE_HPP
30 #include "shared_object_internal.hpp"
31 #include "sx_elem.hpp"
32 #include "calculus.hpp"
33 #include "code_generator.hpp"
40 class SerializingStream;
41 class DeserializingStream;
50 class CASADI_EXPORT
MXNode :
public SharedObjectInternal {
70 virtual bool is_zero()
const {
return false;}
75 virtual bool is_one()
const {
return false;}
85 virtual bool is_value(
double val)
const {
return false;}
90 virtual bool is_eye()
const {
return false;}
105 void can_inline(std::map<const MXNode*, casadi_int>& nodeind)
const;
111 std::vector<std::string>& intermed)
const;
116 virtual std::string
disp(
const std::vector<std::string>& arg)
const = 0;
142 const std::vector<casadi_int>& arg,
143 const std::vector<casadi_int>& res)
const;
148 virtual int eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const;
158 virtual void eval_mx(
const std::vector<MX>& arg, std::vector<MX>& res)
const;
163 virtual void ad_forward(
const std::vector<std::vector<MX> >& fseed,
164 std::vector<std::vector<MX> >& fsens)
const;
169 virtual void ad_reverse(
const std::vector<std::vector<MX> >& aseed,
170 std::vector<std::vector<MX> >& asens)
const;
175 virtual int sp_forward(
const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w)
const;
180 virtual int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w)
const;
185 virtual const std::string&
name()
const;
195 void disp(std::ostream& stream,
bool more)
const override;
210 virtual void primitives(std::vector<MX>::iterator& it)
const;
267 virtual casadi_int
op()
const = 0;
302 virtual bool is_equal(
const MXNode* node, casadi_int depth)
const {
return false;}
319 const MX&
dep(casadi_int ind=0)
const {
return dep_.at(ind);}
329 virtual casadi_int
nout()
const {
return 1;}
344 for (casadi_int i=0;i<dep_.size();++i) {
345 if (dep_[i].sparsity()!=arg[i].sparsity()) {
353 casadi_int
numel()
const {
return sparsity().numel(); }
354 casadi_int
nnz(casadi_int i=0)
const {
return sparsity(i).nnz(); }
355 casadi_int
size1()
const {
return sparsity().size1(); }
356 casadi_int
size2()
const {
return sparsity().size2(); }
357 std::pair<casadi_int, casadi_int>
size()
const {
return sparsity().size();}
360 virtual casadi_int
ind()
const;
374 virtual size_t sz_arg()
const {
return n_dep();}
379 virtual size_t sz_res()
const {
return nout();}
384 virtual size_t sz_iw()
const {
return 0;}
389 virtual size_t sz_w()
const {
return 0;}
428 virtual std::vector<MX>
get_horzsplit(
const std::vector<casadi_int>& output_offset)
const;
440 virtual std::vector<MX>
get_vertsplit(
const std::vector<casadi_int>& output_offset)
const;
446 virtual std::vector<MX>
get_diagsplit(
const std::vector<casadi_int>& offset1,
447 const std::vector<casadi_int>& offset2)
const;
467 const std::vector<casadi_int>& dim_c,
const std::vector<casadi_int>& dim_a,
468 const std::vector<casadi_int>& dim_b,
469 const std::vector<casadi_int>& c,
const std::vector<casadi_int>& a,
470 const std::vector<casadi_int>& b)
const;
700 const std::vector<casadi_int>& offset,
701 const std::vector<double>& coeffs,
702 const std::vector<casadi_int>& degree,
704 const std::vector<casadi_int>& lookup_mode)
const;
707 const std::vector<casadi_int>& offset,
708 const std::vector<casadi_int>& degree,
710 const std::vector<casadi_int>& lookup_mode)
const;
734 static void copy_fwd(
const bvec_t* arg, bvec_t* res, casadi_int len);
739 static void copy_rev(bvec_t* arg, bvec_t* res, casadi_int len);
Helper class for C code generation.
Helper class for Serialization.
std::pair< casadi_int, casadi_int > size() const
Get the shape.
Node class for MX objects.
static MXNode * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
virtual MX get_nz_ref(const Slice &inner, const MX &outer) const
Get the nonzeros of matrix, parametrically.
virtual MX get_bilin(const MX &x, const MX &y) const
Bilinear form.
virtual MX get_horzcat(const std::vector< MX > &x) const
Create a horizontal concatenation node.
virtual MX get_norm_1() const
1-norm
virtual bool has_output() const
Check if a multiple output node.
virtual int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const
Evaluate symbolically (SX)
std::string class_name() const override
Get name of public class.
virtual MX get_repmat(casadi_int m, casadi_int n) const
Create a repeated matrix node.
virtual MX get_nzassign(const MX &y, const MX &inner, const Slice &outer) const
Assign the nonzeros of a matrix to another matrix, parametrically.
~MXNode() override=0
Destructor.
virtual MX get_nzassign(const MX &y, const MX &nz) const
Assign the nonzeros of a matrix to another matrix, parametrically.
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
void disp(std::ostream &stream, bool more) const override
Print a description.
virtual MX get_det() const
Determinant.
virtual bool is_zero() const
Check if identically zero.
virtual size_t sz_arg() const
Get required length of arg field.
MXNode(DeserializingStream &s)
Deserializing constructor.
virtual MX get_nzassign(const MX &y, const std::vector< casadi_int > &nz) const
Assign the nonzeros of a matrix to another matrix.
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
virtual const std::string & name() const
Get the name.
virtual int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const
Propagate sparsity forward.
virtual void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const
Evaluate symbolically (MX)
virtual const Sparsity & sparsity(casadi_int oind) const
Get the sparsity of output oind.
static bool is_equal(const MXNode *x, const MXNode *y, casadi_int depth)
Check if two nodes are equivalent up to a given depth.
virtual void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res) const
Generate code for the operation.
virtual MX get_inv() const
Inverse.
virtual Dict info() const
virtual MX get_einstein(const MX &A, const MX &B, const std::vector< casadi_int > &dim_c, const std::vector< casadi_int > &dim_a, const std::vector< casadi_int > &dim_b, const std::vector< casadi_int > &c, const std::vector< casadi_int > &a, const std::vector< casadi_int > &b) const
Einstein product and addition.
virtual const Function & which_function() const
Get called function.
void can_inline(std::map< const MXNode *, casadi_int > &nodeind) const
Find out which nodes can be inlined.
virtual bool is_valid_input() const
Check if valid function input.
virtual MX get_nz_ref(const MX &inner, const Slice &outer) const
Get the nonzeros of matrix, parametrically.
virtual MX get_repsum(casadi_int m, casadi_int n) const
Create a repeated sum node.
static bool maxDepth()
Get equality checking depth.
virtual MX get_subassign(const MX &y, const Slice &i, const Slice &j) const
Get submatrix assignment.
virtual MX get_nzassign(const MX &y, const Slice &inner, const MX &outer) const
Assign the nonzeros of a matrix to another matrix, parametrically.
MX get_bspline(const std::vector< double > &knots, const std::vector< casadi_int > &offset, const std::vector< double > &coeffs, const std::vector< casadi_int > °ree, casadi_int m, const std::vector< casadi_int > &lookup_mode) const
BSpline.
virtual size_t sz_w() const
Get required length of w field.
virtual bool __nonzero__() const
Check the truth value of this node.
virtual MX join_primitives(std::vector< MX >::const_iterator &it) const
Join an expression along symbolic primitives.
virtual bool is_one() const
Check if identically one.
static void copy_rev(bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities backwards through a copy operation.
virtual bool is_binary() const
Check if binary operation.
virtual Matrix< casadi_int > mapping() const
Get an IM representation of a GetNonzeros or SetNonzeros node.
virtual void add_dependency(CodeGenerator &g) const
Add a dependent function.
virtual MX get_nzadd(const MX &y, const std::vector< casadi_int > &nz) const
Add the nonzeros of a matrix to another matrix.
virtual void split_primitives(const MX &x, std::vector< MX >::iterator &it) const
Split up an expression along symbolic primitives.
virtual void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const
Calculate forward mode directional derivatives.
virtual void split_primitives(const DM &x, std::vector< DM >::iterator &it) const
Split up an expression along symbolic primitives.
virtual MX get_subref(const Slice &i, const Slice &j) const
Get submatrix reference.
virtual double to_double() const
Get the value (only for scalar constant nodes)
virtual MX _get_binary(casadi_int op, const MX &y, bool scX, bool scY) const
Get a binary operation operation (matrix-matrix)
MX get_binary(casadi_int op, const MX &y) const
Get a binary operation operation.
virtual MX get_transpose() const
Transpose.
virtual MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz) const
Get the nonzeros of matrix.
virtual casadi_int n_inplace() const
Can the operation be performed inplace (i.e. overwrite the result)
virtual bool has_duplicates() const
Detect duplicate symbolic expressions.
virtual MX get_solve_triu_unity(const MX &r, bool tr) const
Solve a system of linear equations, upper triangular A, unity diagonal.
std::pair< casadi_int, casadi_int > size() const
virtual MX get_sparsity_cast(const Sparsity &sp) const
Sparsity cast.
virtual void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const
Calculate reverse mode directional derivatives.
Sparsity sparsity_
The sparsity pattern.
virtual void reset_input() const
Reset the marker for an input expression.
virtual DM get_DM() const
Get the value (only for constant nodes)
virtual MX get_nzadd(const MX &y, const MX &inner, const MX &outer) const
Add the nonzeros of a matrix to another matrix, parametrically.
virtual MX get_nzadd(const MX &y, const MX &nz) const
Add the nonzeros of a matrix to another matrix, parametrically.
virtual MX get_nzassign(const MX &y, const MX &inner, const MX &outer) const
Assign the nonzeros of a matrix to another matrix, parametrically.
virtual MX get_norm_fro() const
Frobenius norm.
casadi_int numel() const
Get shape.
const Sparsity & sparsity() const
Get the sparsity.
virtual DM join_primitives(std::vector< DM >::const_iterator &it) const
Join an expression along symbolic primitives.
virtual size_t sz_res() const
Get required length of res field.
virtual MX get_rank1(const MX &alpha, const MX &x, const MX &y) const
Bilinear form.
casadi_int nnz(casadi_int i=0) const
bool matches_sparsity(const std::vector< T > &arg) const
virtual void codegen_incref(CodeGenerator &g, std::set< void * > &added) const
Codegen incref.
virtual MX get_unary(casadi_int op) const
Get a unary operation.
virtual int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const
Propagate sparsity backwards.
virtual casadi_int nout() const
Number of outputs.
virtual bool is_value(double val) const
Check if a certain value.
virtual MX get_nzadd(const MX &y, const Slice &inner, const MX &outer) const
Add the nonzeros of a matrix to another matrix, parametrically.
virtual void primitives(std::vector< MX >::iterator &it) const
Get symbolic primitives.
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
std::vector< MX > dep_
dependencies - functions that have to be evaluated before this one
virtual bool is_unary() const
Check if unary operation.
virtual void codegen_decref(CodeGenerator &g, std::set< void * > &added) const
Codegen decref.
void set_dep(const MX &dep1, const MX &dep2)
Set binary dependencies.
MX get_monitor(const std::string &comment) const
Monitor.
virtual MX get_mac(const MX &y, const MX &z) const
Matrix multiplication and addition.
casadi_int n_dep() const
Number of dependencies.
virtual MX get_nz_ref(const MX &nz) const
Get the nonzeros of matrix, parametrically.
virtual MX get_solve(const MX &r, bool tr, const Linsol &linear_solver) const
Solve a system of linear equations.
virtual casadi_int n_primitives() const
Get the number of symbolic primitives.
virtual std::vector< MX > get_horzsplit(const std::vector< casadi_int > &output_offset) const
Create a horizontal split node.
virtual void split_primitives(const SX &x, std::vector< SX >::iterator &it) const
Split up an expression along symbolic primitives.
MX get_convexify(const Dict &opts) const
Convexify.
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
virtual MX get_project(const Sparsity &sp) const
Create set sparse.
virtual casadi_int op() const =0
Get the operation.
virtual MX get_diagcat(const std::vector< MX > &x) const
Create a diagonal concatenation node.
MX get_bspline(const MX &coeffs, const std::vector< double > &knots, const std::vector< casadi_int > &offset, const std::vector< casadi_int > °ree, casadi_int m, const std::vector< casadi_int > &lookup_mode) const
BSpline.
virtual MX get_output(casadi_int oind) const
Get an output.
virtual MX get_nz_ref(const MX &inner, const MX &outer) const
Get the nonzeros of matrix, parametrically.
virtual MX get_logsumexp() const
Logsumexp.
virtual MX get_solve_triu(const MX &r, bool tr) const
Solve a system of linear equations, upper triangular A.
virtual bool has_refcount() const
Is reference counting needed in codegen?
virtual MX get_mmax() const
Max.
virtual casadi_int offset() const
virtual bool is_minus_one() const
Check if identically minus one.
static std::map< casadi_int, MXNode *(*)(DeserializingStream &)> deserialize_map
MX get_low(const MX &v, const Dict &options) const
Find.
virtual casadi_int which_output() const
Get function output.
virtual std::vector< MX > get_diagsplit(const std::vector< casadi_int > &offset1, const std::vector< casadi_int > &offset2) const
Create a diagonal split node.
void set_dep(const std::vector< MX > &dep)
Set multiple dependencies.
virtual MX get_norm_inf() const
Infinity norm.
bool sameOpAndDeps(const MXNode *node, casadi_int depth) const
Checks if two nodes have the same operation and have.
virtual MX get_vertcat(const std::vector< MX > &x) const
Create a vertical concatenation node (vectors only)
virtual MX get_solve_tril(const MX &r, bool tr) const
Solve a system of linear equations, lower triangular A.
void set_dep(const MX &dep)
Set unary dependency.
MX get_assert(const MX &y, const std::string &fail_message) const
Assertion.
virtual MX get_mmin() const
Min.
virtual int eval(const double **arg, double **res, casadi_int *iw, double *w) const
Evaluate numerically.
virtual casadi_int ind() const
void set_dep(const MX &dep1, const MX &dep2, const MX &dep3)
Set ternary dependencies.
virtual std::string disp(const std::vector< std::string > &arg) const =0
Print expression.
virtual SX join_primitives(std::vector< SX >::const_iterator &it) const
Join an expression along symbolic primitives.
virtual MX get_norm_2() const
Spectral norm.
virtual MX get_dot(const MX &y) const
Inner product.
virtual casadi_int segment() const
virtual MX get_reshape(const Sparsity &sp) const
Reshape.
virtual bool is_output() const
Check if evaluation output.
virtual bool is_equal(const MXNode *node, casadi_int depth) const
virtual MX get_nzadd(const MX &y, const MX &inner, const Slice &outer) const
Add the nonzeros of a matrix to another matrix, parametrically.
static void copy_fwd(const bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities forward through a copy operation.
virtual size_t sz_iw() const
Get required length of iw field.
virtual bool is_eye() const
Check if identity matrix.
virtual std::vector< MX > get_vertsplit(const std::vector< casadi_int > &output_offset) const
Create a vertical split node (vectors only)
static MX to_matrix(const MX &x, const Sparsity &sp)
Convert scalar to matrix.
T join_primitives_gen(typename std::vector< T >::const_iterator &it) const
Join an expression along symbolic primitives (template)
void serialize(SerializingStream &s) const
Serialize an object.
virtual MX get_solve_tril_unity(const MX &r, bool tr) const
Solve a system of linear equations, lower triangular A, unity diagnal.
std::string print_compact(std::map< const MXNode *, casadi_int > &nodeind, std::vector< std::string > &intermed) const
Print compact.
static casadi_int get_max_depth()
Get the depth to which equalities are being checked for simplifications.
Sparse matrix class. SX and DM are specializations.
Helper class for Serialization.
Class representing a Slice.
std::pair< casadi_int, casadi_int > size() const
Get the shape.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.