List of all members | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends
casadi::MX Class Reference

MX - Matrix expression. More...

#include <mx.hpp>

Detailed Description

The MX class is used to build up trees made up from MXNodes. It is a more general graph representation than the scalar expression, SX, and much less efficient for small objects. On the other hand, the class allows much more general operations than does SX, in particular matrix valued operations and calls to arbitrary differentiable functions.

The MX class is designed to have identical syntax with the Matrix<> template class, and uses DM (i.e. Matrix<double>) as its internal representation of the values at a node. By keeping the syntaxes identical, it is possible to switch from one class to the other, as well as inlining MX functions to SXElem functions.

Note that an operation is always "lazy", making a matrix multiplication will create a matrix multiplication node, not perform the actual multiplication.

Author
Joel Andersson
Date
2010-2011

Extra doc: https://github.com/casadi/casadi/wiki/L_py

Definition at line 88 of file mx.hpp.

Inheritance diagram for casadi::MX:
Inheritance graph
[legend]
Collaboration diagram for casadi::MX:
Collaboration graph
[legend]

Public Types

typedef MX ScalarType
 
typedef GenericMatrix< MXB
 Base class. More...
 
typedef std::map< std::string, MXMXDict
 Readability typedef. More...
 
using internal_base_type = SharedObjectInternal
 
using base_type = SharedObject
 

Public Member Functions

 MX ()
 Default constructor. More...
 
 MX (casadi_int nrow, casadi_int ncol)
 Create a sparse matrix with all structural zeros. More...
 
 MX (const std::pair< casadi_int, casadi_int > &rc)
 Create a sparse matrix with all structural zeros. More...
 
 MX (const Sparsity &sp)
 Create a sparse matrix from a sparsity pattern. More...
 
 MX (const Sparsity &sp, const MX &val)
 Construct matrix with a given sparsity and nonzeros. More...
 
 MX (const Sparsity &sp, const std::string &fname)
 Construct matrix with a given sparsity and a file with nonzeros. More...
 
 MX (const Matrix< double > &val, const std::string &name)
 Construct matrix with a given sparsity and nonzeros,. More...
 
 MX (double x)
 Create scalar constant (also implicit type conversion) More...
 
 MX (const std::vector< double > &x)
 Create vector constant (also implicit type conversion) More...
 
 MX (const Matrix< double > &x)
 Create sparse matrix constant (also implicit type conversion) More...
 
 ~MX ()
 Destructor. More...
 
const Sparsitysparsity () const
 Get the sparsity pattern. More...
 
bool __nonzero__ () const
 Returns the truth value of an MX expression. More...
 
Sparsity get_sparsity () const
 Get an owning reference to the sparsity pattern. More...
 
std::vector< MXget_nonzeros () const
 Get nonzeros as list of scalar MXes. More...
 
void erase (const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
 Erase a submatrix (leaving structural zeros in its place) More...
 
void erase (const std::vector< casadi_int > &rr, bool ind1=false)
 Erase a submatrix (leaving structural zeros in its place) More...
 
void enlarge (casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
 Enlarge matrix. More...
 
MX operator- () const
 
MX dep (casadi_int ch=0) const
 Get the nth dependency as MX. More...
 
casadi_int n_out () const
 Number of outputs. More...
 
MX get_output (casadi_int oind) const
 Get an output. More...
 
casadi_int n_dep () const
 Get the number of dependencies of a binary SXElem. More...
 
std::string name () const
 Get the name. More...
 
 operator double () const
 Get the value (only for scalar constant nodes) More...
 
 operator Matrix< double > () const
 Get the value (only for constant nodes) More...
 
bool is_symbolic () const
 Check if symbolic. More...
 
bool is_constant () const
 Check if constant. More...
 
bool is_call () const
 Check if evaluation. More...
 
Function which_function () const
 Get function - only valid when is_call() is true. More...
 
bool is_output () const
 Check if evaluation output. More...
 
bool has_output () const
 Check if a multiple output node. More...
 
casadi_int which_output () const
 Get the index of evaluation output - only valid when is_output() is true. More...
 
bool is_op (casadi_int op) const
 Is it a certain operation. More...
 
bool is_multiplication () const
 Check if multiplication. More...
 
bool is_commutative () const
 Check if commutative operation. More...
 
bool is_norm () const
 Check if norm. More...
 
bool is_valid_input () const
 Check if matrix can be used to define function inputs. More...
 
casadi_int n_primitives () const
 Get the number of primitives for MXFunction inputs/outputs. More...
 
std::vector< MXprimitives () const
 Get primitives. More...
 
bool has_duplicates () const
 Detect duplicate symbolic expressions. More...
 
void reset_input () const
 Reset the marker for an input expression. More...
 
bool is_eye () const
 check if identity More...
 
bool is_zero () const
 check if zero (note that false negative answers are possible) More...
 
bool is_one () const
 check if zero (note that false negative answers are possible) More...
 
bool is_minus_one () const
 check if zero (note that false negative answers are possible) More...
 
bool is_transpose () const
 Is the expression a transpose? More...
 
bool is_regular () const
 Checks if expression does not contain NaN or Inf. More...
 
bool is_binary () const
 Is binary operation. More...
 
bool is_unary () const
 Is unary operation. More...
 
casadi_int op () const
 Get operation type. More...
 
Dict info () const
 
void serialize (SerializingStream &s) const
 Serialize an object. More...
 
casadi_int get_temp () const
 
void set_temp (casadi_int t) const
 Set the temporary variable. More...
 
MXNodeget () const
 Get a const pointer to the node. More...
 
MX printme (const MX &b) const
 
MX attachAssert (const MX &y, const std::string &fail_message="") const
 returns itself, but with an assertion attached More...
 
MX monitor (const std::string &comment) const
 Monitor an expression. More...
 
MX T () const
 Transpose the matrix. More...
 
Matrix< casadi_int > mapping () const
 Get an IM representation of a GetNonzeros or SetNonzeros node. More...
 
void eval_mx (const std::vector< MX > &arg, std::vector< MX > &res) const
 Evaluate the MX node with new symbolic dependencies. More...
 
 MX (const Sparsity &sp, double val, bool dummy)
 Construct constant matrix with a given sparsity and values. More...
 
casadi_int nnz () const
 Get the number of (structural) non-zero elements. More...
 
casadi_int nnz_lower () const
 Get the number of non-zeros in the lower triangular half. More...
 
casadi_int nnz_upper () const
 Get the number of non-zeros in the upper triangular half. More...
 
casadi_int nnz_diag () const
 Get get the number of non-zeros on the diagonal. More...
 
casadi_int numel () const
 Get the number of elements. More...
 
casadi_int size1 () const
 Get the first dimension (i.e. number of rows) More...
 
casadi_int rows () const
 Get the number of rows, Octave-style syntax. More...
 
casadi_int size2 () const
 Get the second dimension (i.e. number of columns) More...
 
casadi_int columns () const
 Get the number of columns, Octave-style syntax. More...
 
std::string dim (bool with_nz=false) const
 Get string representation of dimensions. More...
 
std::pair< casadi_int, casadi_int > size () const
 Get the shape. More...
 
casadi_int size (casadi_int axis) const
 Get the size along a particular dimensions. More...
 
bool is_empty (bool both=false) const
 Check if the sparsity is empty, i.e. if one of the dimensions is zero. More...
 
bool is_dense () const
 Check if the matrix expression is dense. More...
 
bool is_scalar (bool scalar_and_dense=false) const
 Check if the matrix expression is scalar. More...
 
bool is_square () const
 Check if the matrix expression is square. More...
 
bool is_vector () const
 Check if the matrix is a row or column vector. More...
 
bool is_row () const
 Check if the matrix is a row vector (i.e. size1()==1) More...
 
bool is_column () const
 Check if the matrix is a column vector (i.e. size2()==1) More...
 
bool is_triu () const
 Check if the matrix is upper triangular. More...
 
bool is_tril () const
 Check if the matrix is lower triangular. More...
 
const MX nz (const K &k) const
 Get vector nonzero or slice of nonzeros. More...
 
NonZeros< MX, K > nz (const K &k)
 Access vector nonzero or slice of nonzeros. More...
 
const MX operator() (const RR &rr) const
 Get vector element or slice. More...
 
const MX operator() (const RR &rr, const CC &cc) const
 Get Matrix element or slice. More...
 
SubIndex< MX, RR > operator() (const RR &rr)
 Access Matrix elements (one argument) More...
 
SubMatrix< MX, RR, CC > operator() (const RR &rr, const CC &cc)
 Access Matrix elements (two arguments) More...
 
std::string class_name () const
 Get class name. More...
 
void disp (std::ostream &stream, bool more=false) const
 Print a description of the object. More...
 
std::string get_str (bool more=false) const
 Get string representation. More...
 
void print_ptr (std::ostream &stream=casadi::uout()) const
 
void own (SharedObjectInternal *node)
 
void assign (SharedObjectInternal *node)
 Assign the node to a node class pointer without reference counting. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
void swap (GenericShared &other)
 Swap content with another instance. More...
 
std::string debug_repr () const
 
bool is_null () const
 Is a null pointer? More...
 
casadi_int __hash__ () const
 Returns a number that is unique for a given Node. More...
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 
MXNodeoperator-> ()
 Access a member of the node. More...
 
const MXNodeoperator-> () const
 Const access a member of the node. More...
 
std::vector< MXsplit_primitives (const MX &x) const
 Split up an expression along symbolic primitives. More...
 
std::vector< SXsplit_primitives (const SX &x) const
 Split up an expression along symbolic primitives. More...
 
std::vector< DMsplit_primitives (const DM &x) const
 Split up an expression along symbolic primitives. More...
 
MX join_primitives (const std::vector< MX > &v) const
 Join an expression along symbolic primitives. More...
 
SX join_primitives (const std::vector< SX > &v) const
 Join an expression along symbolic primitives. More...
 
DM join_primitives (const std::vector< DM > &v) const
 Join an expression along symbolic primitives. More...
 
void get (MX &m, bool ind1, const Slice &rr) const
 
void get (MX &m, bool ind1, const Matrix< casadi_int > &rr) const
 
void get (MX &m, bool ind1, const Sparsity &sp) const
 
void get (MX &m, bool ind1, const MX &rr) const
 
void get (MX &m, bool ind1, const casadi_int rr) const
 
void get (MX &m, bool ind1, const Slice &rr, const Slice &cc) const
 
void get (MX &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc) const
 
void get (MX &m, bool ind1, const Slice &rr, casadi_int cc) const
 
void get (MX &m, bool ind1, const Matrix< casadi_int > &rr, const Slice &cc) const
 
void get (MX &m, bool ind1, casadi_int rr, const Slice &cc) const
 
void get (MX &m, bool ind1, const Matrix< casadi_int > &rr, const Matrix< casadi_int > &cc) const
 
void get (MX &m, bool ind1, casadi_int rr, casadi_int cc) const
 
void get (MX &m, bool ind1, const MX &rr, const Slice &cc) const
 
void get (MX &m, bool ind1, const Slice &rr, const MX &cc) const
 
void get (MX &m, bool ind1, const MX &rr, const MX &cc) const
 
void set (const MX &m, bool ind1, const Slice &rr)
 
void set (const MX &m, bool ind1, const Matrix< casadi_int > &rr)
 
void set (const MX &m, bool ind1, const Sparsity &sp)
 
void set (const MX &m, bool ind1, const Slice &rr, const Slice &cc)
 
void set (const MX &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc)
 
void set (const MX &m, bool ind1, const Matrix< casadi_int > &rr, const Slice &cc)
 
void set (const MX &m, bool ind1, const Matrix< casadi_int > &rr, const Matrix< casadi_int > &cc)
 
void get_nz (MX &m, bool ind1, const Slice &kk) const
 
void get_nz (MX &m, bool ind1, const Matrix< casadi_int > &kk) const
 
void get_nz (MX &m, bool ind1, const MX &kk) const
 
void get_nz (MX &m, bool ind1, casadi_int kk) const
 
void get_nz (MX &m, bool ind1, const MX &inner, const Slice &outer) const
 
void get_nz (MX &m, bool ind1, const Slice &inner, const MX &outer) const
 
void get_nz (MX &m, bool ind1, const MX &inner, const MX &outer) const
 
void set_nz (const MX &m, bool ind1, const Slice &kk)
 
void set_nz (const MX &m, bool ind1, const Matrix< casadi_int > &kk)
 
void set_nz (const MX &m, bool ind1, const MX &kk)
 
void set_nz (const MX &m, bool ind1, casadi_int kk)
 
void ad_forward (const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const
 Called from MXFunction. More...
 
void ad_reverse (const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const
 Called from MXFunction. More...
 
std::vector< casadi_int > get_row () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
std::vector< casadi_int > get_colind () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
const casadi_int * row () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
casadi_int row (casadi_int el) const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
const casadi_int * colind () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
casadi_int colind (casadi_int col) const
 Get the sparsity pattern. See the Sparsity class for details. More...
 

Static Public Member Functions

static std::string type_name ()
 Get type name. More...
 
static MX create (MXNode *node)
 Create from node. More...
 
static std::vector< MXcreateMultipleOutput (MXNode *node)
 Create from node (multiple-outputs) More...
 
static MX deserialize (DeserializingStream &s)
 Deserialize with type disambiguation. More...
 
static MX eye (casadi_int n)
 Identity matrix. More...
 
static DM bspline_dual (const std::vector< double > &x, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, const Dict &opts=Dict())
 
static MX interpn_linear (const std::vector< MX > &x, const MX &v, const std::vector< MX > &xq, const Dict &opts=Dict())
 Low-level access to inlined linear interpolation. More...
 
static void set_max_depth (casadi_int eq_depth=1)
 Set or reset the depth to which equalities are being checked for simplifications. More...
 
static casadi_int get_max_depth ()
 Get the depth to which equalities are being checked for simplifications. More...
 
static bool test_cast (const SharedObjectInternal *ptr)
 Check if a particular cast is allowed. More...
 
static std::vector< MXget_input (const Function &f)
 Get function inputs. More...
 
static std::vector< MXget_free (const Function &f)
 Get free variables. More...
 
static MX _sym (const std::string &name, const Sparsity &sp)
 
static MX logsumexp (const MX &x)
 
static MX binary (casadi_int op, const MX &x, const MX &y)
 Create nodes by their ID. More...
 
static MX unary (casadi_int op, const MX &x)
 Create nodes by their ID. More...
 
static MX inf (const Sparsity &sp)
 create a matrix with all inf More...
 
static MX inf (casadi_int nrow=1, casadi_int ncol=1)
 create a matrix with all inf More...
 
static MX inf (const std::pair< casadi_int, casadi_int > &rc)
 create a matrix with all inf More...
 
static MX nan (const Sparsity &sp)
 create a matrix with all nan More...
 
static MX nan (casadi_int nrow=1, casadi_int ncol=1)
 create a matrix with all nan More...
 
static MX nan (const std::pair< casadi_int, casadi_int > &rc)
 create a matrix with all nan More...
 
static MX einstein (const MX &A, const MX &B, const MX &C, const std::vector< casadi_int > &dim_a, const std::vector< casadi_int > &dim_b, const std::vector< casadi_int > &dim_c, const std::vector< casadi_int > &a, const std::vector< casadi_int > &b, const std::vector< casadi_int > &c)
 Computes an einstein dense tensor contraction. More...
 
static MX einstein (const MX &A, const MX &B, const std::vector< casadi_int > &dim_a, const std::vector< casadi_int > &dim_b, const std::vector< casadi_int > &dim_c, const std::vector< casadi_int > &a, const std::vector< casadi_int > &b, const std::vector< casadi_int > &c)
 Computes an einstein dense tensor contraction. More...
 
static bool is_equal (const MX &x, const MX &y, casadi_int depth=0)
 
static MX mmin (const MX &x)
 
static MX mmax (const MX &x)
 
static MX horzcat (const std::vector< MX > &x)
 
static MX diagcat (const std::vector< MX > &x)
 
static MX vertcat (const std::vector< MX > &x)
 
static std::vector< MXhorzsplit (const MX &x, const std::vector< casadi_int > &offset)
 
static std::vector< MXdiagsplit (const MX &x, const std::vector< casadi_int > &offset1, const std::vector< casadi_int > &offset2)
 
static std::vector< MXvertsplit (const MX &x, const std::vector< casadi_int > &offset)
 
static MX blockcat (const std::vector< std::vector< MX > > &v)
 
static MX mtimes (const MX &x, const MX &y)
 
static MX mac (const MX &x, const MX &y, const MX &z)
 
static MX reshape (const MX &x, casadi_int nrow, casadi_int ncol)
 
static MX reshape (const MX &x, const Sparsity &sp)
 
static MX sparsity_cast (const MX &x, const Sparsity &sp)
 
static MX kron (const MX &x, const MX &b)
 
static MX repmat (const MX &x, casadi_int n, casadi_int m=1)
 
static MX jacobian (const MX &f, const MX &x, const Dict &opts=Dict())
 
static MX hessian (const MX &f, const MX &x, const Dict &opts=Dict())
 
static MX hessian (const MX &f, const MX &x, MX &g, const Dict &opts=Dict())
 
static std::vector< std::vector< MX > > forward (const std::vector< MX > &ex, const std::vector< MX > &arg, const std::vector< std::vector< MX > > &v, const Dict &opts=Dict())
 
static std::vector< std::vector< MX > > reverse (const std::vector< MX > &ex, const std::vector< MX > &arg, const std::vector< std::vector< MX > > &v, const Dict &opts=Dict())
 
static std::vector< bool > which_depends (const MX &expr, const MX &var, casadi_int order=1, bool tr=false)
 
static Sparsity jacobian_sparsity (const MX &f, const MX &x)
 
static MX substitute (const MX &ex, const MX &v, const MX &vdef)
 
static std::vector< MXsubstitute (const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
 
static void substitute_inplace (const std::vector< MX > &v, std::vector< MX > &vdef, std::vector< MX > &ex, bool reverse)
 
static MX solve (const MX &a, const MX &b)
 
static MX solve (const MX &a, const MX &b, const std::string &lsolver, const Dict &dict=Dict())
 
static MX inv_minor (const MX &A)
 
static MX inv_node (const MX &A)
 
static MX inv (const MX &A, const std::string &lsolver="qr", const Dict &dict=Dict())
 
static MX pinv (const MX &A, const std::string &lsolver="qr", const Dict &dict=Dict())
 
static MX expm_const (const MX &A, const MX &t)
 
static MX expm (const MX &A)
 
static casadi_int n_nodes (const MX &x)
 
static std::string print_operator (const MX &x, const std::vector< std::string > &args)
 
static void extract (std::vector< MX > &ex, std::vector< MX > &v, std::vector< MX > &vdef, const Dict &opts=Dict())
 
static void shared (std::vector< MX > &ex, std::vector< MX > &v, std::vector< MX > &vdef, const std::string &v_prefix, const std::string &v_suffix)
 
static MX if_else (const MX &cond, const MX &if_true, const MX &if_false, bool short_circuit=false)
 
static MX conditional (const MX &ind, const std::vector< MX > &x, const MX &x_default, bool short_circuit=false)
 
static bool depends_on (const MX &x, const MX &arg)
 
static bool contains_all (const std::vector< MX > &v, const std::vector< MX > &n)
 
static bool contains_any (const std::vector< MX > &v, const std::vector< MX > &n)
 
static MX simplify (const MX &x)
 
static MX dot (const MX &x, const MX &y)
 
static MX mrdivide (const MX &a, const MX &b)
 
static MX mldivide (const MX &a, const MX &b)
 
static MX norm_2 (const MX &x)
 
static MX norm_fro (const MX &x)
 
static MX norm_1 (const MX &x)
 
static MX norm_inf (const MX &x)
 
static MX unite (const MX &A, const MX &B)
 
static MX trace (const MX &x)
 
static MX diag (const MX &x)
 
static MX sum2 (const MX &x)
 
static MX sum1 (const MX &x)
 
static MX polyval (const MX &p, const MX &x)
 
static MX det (const MX &x)
 
static std::vector< MXsymvar (const MX &x)
 
static MX nullspace (const MX &A)
 
static MX repsum (const MX &x, casadi_int n, casadi_int m=1)
 
static MX densify (const MX &x, const MX &val=0)
 
static MX _bilin (const MX &A, const MX &x, const MX &y)
 
static MX _rank1 (const MX &A, const MX &alpha, const MX &x, const MX &y)
 
static MX project (const MX &x, const Sparsity &sp, bool intersect=false)
 
static MX cumsum (const MX &x, casadi_int axis=-1)
 
static MX _logsumexp (const MX &x)
 
static std::vector< MXcse (const std::vector< MX > &e)
 
static void extract_parametric (const MX &expr, const MX &par, MX &expr_ret, std::vector< MX > &symbols, std::vector< MX > &parametric, const Dict &opts)
 
static void separate_linear (const MX &expr, const MX &sym_lin, const MX &sym_const, MX &expr_const, MX &expr_lin, MX &expr_nonlin)
 
static MX find (const MX &x)
 
static MX low (const MX &v, const MX &p, const Dict &options=Dict())
 
static MX graph_substitute (const MX &x, const std::vector< MX > &v, const std::vector< MX > &vdef)
 
static MX graph_substitute (const MX &x, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
 
static std::vector< MXgraph_substitute (const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
 
static std::vector< MXgraph_substitute (const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
 
static MX matrix_expand (const MX &e, const std::vector< MX > &boundary, const Dict &options)
 
static std::vector< MXmatrix_expand (const std::vector< MX > &e, const std::vector< MX > &boundary, const Dict &options)
 
static MX lift (const MX &x, const MX &x_guess)
 
static DM evalf (const MX &m)
 
static MX bspline (const MX &x, const DM &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
 
static MX bspline (const MX &x, const MX &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
 
static MX convexify (const MX &H, const Dict &opts=Dict())
 
static MX stop_diff (const MX &expr, casadi_int order)
 
static MX stop_diff (const MX &expr, const MX &var, casadi_int order)
 
static std::vector< MXdifference (const std::vector< MX > &a, const std::vector< MX > &b)
 
static MX interp1d (const std::vector< double > &x, const MX &v, const std::vector< double > &xq, const std::string &mode, bool equidistant)
 Functions called by friend functions defined here. More...
 
static casadi_int sprank (const MX &x)
 Functions called by friend functions defined here. More...
 
static casadi_int norm_0_mul (const MX &x, const MX &y)
 Functions called by friend functions defined here. More...
 
static MX tril (const MX &x, bool includeDiagonal=true)
 Functions called by friend functions defined here. More...
 
static MX triu (const MX &x, bool includeDiagonal=true)
 Functions called by friend functions defined here. More...
 
static MX sumsqr (const MX &x)
 Functions called by friend functions defined here. More...
 
static MX linspace (const MX &a, const MX &b, casadi_int nsteps)
 Functions called by friend functions defined here. More...
 
static MX cross (const MX &a, const MX &b, casadi_int dim=-1)
 Functions called by friend functions defined here. More...
 
static MX skew (const MX &a)
 Functions called by friend functions defined here. More...
 
static MX inv_skew (const MX &a)
 Functions called by friend functions defined here. More...
 
static MX tril2symm (const MX &x)
 Functions called by friend functions defined here. More...
 
static MX triu2symm (const MX &x)
 Functions called by friend functions defined here. More...
 
static MX repsum (const MX &x, casadi_int n, casadi_int m=1)
 Functions called by friend functions defined here. More...
 
static MX diff (const MX &x, casadi_int n=1, casadi_int axis=-1)
 Functions called by friend functions defined here. More...
 
static bool is_linear (const MX &expr, const MX &var)
 Functions called by friend functions defined here. More...
 
static bool is_quadratic (const MX &expr, const MX &var)
 Functions called by friend functions defined here. More...
 
static void quadratic_coeff (const MX &expr, const MX &var, MX &A, MX &b, MX &c, bool check)
 Functions called by friend functions defined here. More...
 
static void linear_coeff (const MX &expr, const MX &var, MX &A, MX &b, bool check)
 Functions called by friend functions defined here. More...
 
static MX mpower (const MX &x, const MX &y)
 
static MX soc (const MX &x, const MX &y)
 
static MX linearize (const MX &f, const MX &x, const MX &x0, const Dict &opts=Dict())
 
static MX gradient (const MX &ex, const MX &arg, const Dict &opts=Dict())
 
static MX tangent (const MX &ex, const MX &arg, const Dict &opts=Dict())
 
static MX jtimes (const MX &ex, const MX &arg, const MX &v, bool tr=false, const Dict &opts=Dict())
 
static MX bilin (const MX &A, const MX &x, const MX &y)
 Calculate bilinear/quadratic form x^T A y. More...
 
static MX rank1 (const MX &A, const MX &alpha, const MX &x, const MX &y)
 Make a rank-1 update to a matrix A. More...
 
Construct symbolic primitives

The "sym" function is intended to work in a similar way as "sym" used in the Symbolic Toolbox for Matlab but instead creating a CasADi symbolic primitive.

static MX sym (const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
 Create an nrow-by-ncol symbolic primitive. More...
 
static MX sym (const std::string &name, const std::pair< casadi_int, casadi_int > &rc)
 Construct a symbolic primitive with given dimensions. More...
 
static MX sym (const std::string &name, const Sparsity &sp)
 Create symbolic primitive with a given sparsity pattern. More...
 
static std::vector< MXsym (const std::string &name, const Sparsity &sp, casadi_int p)
 Create a vector of length p with with matrices. More...
 
static std::vector< MXsym (const std::string &name, casadi_int nrow, casadi_int ncol, casadi_int p)
 Create a vector of length p with nrow-by-ncol symbolic primitives. More...
 
static std::vector< std::vector< MX > > sym (const std::string &name, const Sparsity &sp, casadi_int p, casadi_int r)
 Create a vector of length r of vectors of length p with. More...
 
static std::vector< std::vector< MX > > sym (const std::string &name, casadi_int nrow, casadi_int ncol, casadi_int p, casadi_int r)
 Create a vector of length r of vectors of length p. More...
 
static MX zeros (casadi_int nrow=1, casadi_int ncol=1)
 Create a dense matrix or a matrix with specified sparsity with all entries zero. More...
 
static MX zeros (const Sparsity &sp)
 Create a dense matrix or a matrix with specified sparsity with all entries zero. More...
 
static MX zeros (const std::pair< casadi_int, casadi_int > &rc)
 Create a dense matrix or a matrix with specified sparsity with all entries zero. More...
 
static MX ones (casadi_int nrow=1, casadi_int ncol=1)
 Create a dense matrix or a matrix with specified sparsity with all entries one. More...
 
static MX ones (const Sparsity &sp)
 Create a dense matrix or a matrix with specified sparsity with all entries one. More...
 
static MX ones (const std::pair< casadi_int, casadi_int > &rc)
 Create a dense matrix or a matrix with specified sparsity with all entries one. More...
 

Protected Member Functions

void count_up ()
 
void count_down ()
 

Static Private Member Functions

static MX printme (const MX &x, const MX &y)
 

Friends

MX find (const MX &x)
 Find first nonzero, returned as row index. More...
 
MX low (const MX &v, const MX &p, const Dict &options=Dict())
 Find first nonzero. More...
 
MX graph_substitute (const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
 Substitute single expression in graph. More...
 
MX graph_substitute (const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
 
std::vector< MXgraph_substitute (const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
 Substitute multiple expressions in graph. More...
 
std::vector< MXgraph_substitute (const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
 
MX matrix_expand (const MX &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
 Expand MX graph to SXFunction call. More...
 
std::vector< MXmatrix_expand (const std::vector< MX > &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
 Expand MX graph to SXFunction call. More...
 
MX bspline (const MX &x, const DM &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
 
MX bspline (const MX &x, const MX &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
 
DM bspline_dual (const std::vector< double > &x, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, const Dict &opts=Dict())
 
MX convexify (const MX &H, const Dict &opts=Dict())
 
MX lift (const MX &x, const MX &x_guess)
 Lift the expression. More...
 
MX inv_node (const MX &x)
 Inverse node. More...
 
DM evalf (const MX &expr)
 Evaluates the expression numerically. More...
 
MX stop_diff (const MX &expr, casadi_int order)
 Stop derivatives of an expression wrt to all its symbolic variables. More...
 
MX no_grad (const MX &expr)
 Stop first derivatives of an expression wrt to all its symbolic variables. More...
 
MX no_hess (const MX &expr)
 Stop second derivatives of an expression wrt to all its symbolic variables. More...
 
MX stop_diff (const MX &expr, const MX &var, casadi_int order)
 Stop derivatives of an expression wrt to a select set of symbolic variables. More...
 
std::vector< MXdifference (const std::vector< MX > &a, const std::vector< MX > &b)
 

Member Typedef Documentation

◆ B

Definition at line 189 of file mx.hpp.

◆ base_type

Definition at line 103 of file shared_object.hpp.

◆ internal_base_type

Definition at line 102 of file shared_object.hpp.

◆ MXDict

typedef std::map<std::string, MX> casadi::MX::MXDict

Definition at line 964 of file mx.hpp.

◆ ScalarType

Scalar type

Definition at line 185 of file mx.hpp.

Constructor & Destructor Documentation

◆ MX() [1/11]

casadi::MX::MX ( )

◆ MX() [2/11]

casadi::MX::MX ( casadi_int  nrow,
casadi_int  ncol 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_q1

Definition at line 108 of file mx.cpp.

108  {
109  own(ConstantMX::create(Sparsity(nrow, ncol), 0));
110  }
static ConstantMX * create(const Sparsity &sp, casadi_int val)

References casadi::ConstantMX::create(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

◆ MX() [3/11]

casadi::MX::MX ( const std::pair< casadi_int, casadi_int > &  rc)
explicit

◆ MX() [4/11]

casadi::MX::MX ( const Sparsity sp)
explicit

Same as MX::ones(sparsity)

Extra doc: https://github.com/casadi/casadi/wiki/L_q3

Definition at line 104 of file mx.cpp.

104  {
105  own(ConstantMX::create(sp, 1));
106  }

References casadi::ConstantMX::create(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

◆ MX() [5/11]

casadi::MX::MX ( const Sparsity sp,
const MX val 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_q4

Definition at line 83 of file mx.cpp.

83  {
84  if (sp.is_reshape(val.sparsity())) {
85  *this = reshape(val, sp);
86  } else if (val.is_scalar()) {
87  // Dense matrix if val dense
88  if (val.is_dense()) {
89  if (val.is_constant()) {
90  own(ConstantMX::create(sp, static_cast<double>(val)));
91  } else {
92  *this = val->get_nzref(sp, std::vector<casadi_int>(sp.nnz(), 0));
93  }
94  } else {
95  // Empty matrix
96  own(ConstantMX::create(Sparsity(sp.size()), 0));
97  }
98  } else {
99  casadi_assert_dev(val.is_column() && sp.nnz()==val.size1());
100  *this = densify(val)->get_nzref(sp, range(sp.nnz()));
101  }
102  }
virtual MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz) const
Get the nonzeros of matrix.
Definition: mx_node.cpp:657
static MX reshape(const MX &x, casadi_int nrow, casadi_int ncol)
Definition: mx.cpp:1242
static MX densify(const MX &x, const MX &val=0)
Definition: mx.cpp:894
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.

References casadi::ConstantMX::create(), densify(), casadi::MXNode::get_nzref(), casadi::GenericMatrix< MatType >::is_column(), is_constant(), casadi::GenericMatrix< MatType >::is_dense(), casadi::Sparsity::is_reshape(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::Sparsity::nnz(), casadi::GenericShared< SharedObject, SharedObjectInternal >::own(), casadi::range(), reshape(), casadi::Sparsity::size(), casadi::GenericMatrix< MatType >::size1(), and sparsity().

◆ MX() [6/11]

casadi::MX::MX ( const Sparsity sp,
const std::string &  fname 
)

◆ MX() [7/11]

casadi::MX::MX ( const Matrix< double > &  val,
const std::string &  name 
)

configurable in codegen via a pool

Extra doc: https://github.com/casadi/casadi/wiki/L_2aa

Definition at line 124 of file mx.cpp.

124  {
125  own(ConstantMX::create(val, name));
126  }
std::string name() const
Get the name.
Definition: mx.cpp:762

References casadi::ConstantMX::create(), name(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

◆ MX() [8/11]

casadi::MX::MX ( double  x)

Extra doc: https://github.com/casadi/casadi/wiki/L_q6

Definition at line 71 of file mx.cpp.

71  {
73  }
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
Definition: sparsity.cpp:1012

References casadi::ConstantMX::create(), casadi::Sparsity::dense(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

◆ MX() [9/11]

casadi::MX::MX ( const std::vector< double > &  x)

◆ MX() [10/11]

casadi::MX::MX ( const Matrix< double > &  x)

◆ ~MX()

casadi::MX::~MX ( )

Extra doc: https://github.com/casadi/casadi/wiki/L_q9

Definition at line 56 of file mx.cpp.

56  {
57  }

◆ MX() [11/11]

casadi::MX::MX ( const Sparsity sp,
double  val,
bool  dummy 
)

Definition at line 116 of file mx.cpp.

116  {
117  own(ConstantMX::create(sp, val));
118  }

References casadi::ConstantMX::create(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

Member Function Documentation

◆ __hash__()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::__hash__
inherited

If the Object does not point to any node, "0" is returned.

Extra doc: https://github.com/casadi/casadi/wiki/L_av

Definition at line 124 of file generic_shared_impl.hpp.

137  {
138  return reinterpret_cast<casadi_int>(get());
139  }

◆ __nonzero__()

bool casadi::MX::__nonzero__ ( ) const

Definition at line 143 of file mx.cpp.

143  {
144  return (*this)->__nonzero__();
145  }

◆ _bilin()

MX casadi::MX::_bilin ( const MX A,
const MX x,
const MX y 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2723 of file mx.cpp.

2723  {
2724  return A->get_bilin(x, y);
2725  }

References casadi::MXNode::get_bilin().

◆ _logsumexp()

MX casadi::MX::_logsumexp ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2731 of file mx.cpp.

2731  {
2732  return x->get_logsumexp();
2733  }

References casadi::MXNode::get_logsumexp().

◆ _rank1()

MX casadi::MX::_rank1 ( const MX A,
const MX alpha,
const MX x,
const MX y 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2727 of file mx.cpp.

2727  {
2728  return A->get_rank1(alpha, x, y);
2729  }

References casadi::MXNode::get_rank1().

◆ _sym()

MX casadi::MX::_sym ( const std::string &  name,
const Sparsity sp 
)
static

Definition at line 917 of file mx.cpp.

917  {
918  if (sp.nnz()==0) {
919  return MX::zeros(sp);
920  } else {
921  return MX::create(new SymbolicMX(name, sp));
922  }
923  }
static MX zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
static MX create(MXNode *node)
Create from node.
Definition: mx.cpp:67

References create(), name(), casadi::Sparsity::nnz(), and casadi::GenericMatrix< MX >::zeros().

◆ ad_forward()

void casadi::MX::ad_forward ( const std::vector< std::vector< MX > > &  fseed,
std::vector< std::vector< MX > > &  fsens 
) const

Extra doc: https://github.com/casadi/casadi/wiki/L_ro

Definition at line 2745 of file mx.cpp.

2746  {
2747  try {
2748  (*this)->ad_forward(fseed, fsens);
2749  } catch (std::exception& e) {
2750  CASADI_THROW_ERROR_OBJ("ad_forward", e.what());
2751  }
2752  }

◆ ad_reverse()

void casadi::MX::ad_reverse ( const std::vector< std::vector< MX > > &  aseed,
std::vector< std::vector< MX > > &  asens 
) const

Extra doc: https://github.com/casadi/casadi/wiki/L_ro

Definition at line 2754 of file mx.cpp.

2755  {
2756  try {
2757  (*this)->ad_reverse(aseed, asens);
2758  } catch (std::exception& e) {
2759  CASADI_THROW_ERROR_OBJ("ad_reverse", e.what());
2760  }
2761  }

◆ assign()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::assign ( Internal *  node)
inherited

improper use will cause memory leaks!

Extra doc: https://github.com/casadi/casadi/wiki/L_at

Definition at line 97 of file generic_shared_impl.hpp.

75  {
76  node = node_;
77  }

◆ attachAssert()

MX casadi::MX::attachAssert ( const MX y,
const std::string &  fail_message = "" 
) const

If y does not evaluate to 1, a runtime error is raised

Extra doc: https://github.com/casadi/casadi/wiki/L_rg

Examples
misc/assertion.py.

Definition at line 723 of file mx.cpp.

723  {
724  casadi_assert(y.is_scalar(),
725  "Error in attachAssert: assertion expression y must be scalar, "
726  "but got " + y.dim());
727  return(*this)->get_assert(y, fail_message);
728  }

References casadi::GenericMatrix< MatType >::dim(), and casadi::GenericMatrix< MatType >::is_scalar().

◆ bilin()

static MX casadi::GenericMatrix< MX >::bilin ( const MX A,
const MX x,
const MX y 
)
staticinherited
Parameters
[in]ycan be omitted, in which case x^T A x is calculated

Extra doc: https://github.com/casadi/casadi/wiki/L_1bo

◆ binary()

MX casadi::MX::binary ( casadi_int  op,
const MX x,
const MX y 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r1

Definition at line 513 of file mx.cpp.

513  {
514  // Check, correct dimensions
515  if (x.size()!=y.size() && !x.is_scalar() && !y.is_scalar()) {
516  // x and y are horizontal multiples of each other?
517  if (!x.is_empty() && !y.is_empty()) {
518  if (x.size1() == y.size1() && x.size2() % y.size2() == 0) {
519  return binary(op, x, repmat(y, 1, x.size2() / y.size2()));
520  } else if (y.size1() == x.size1() && y.size2() % x.size2() == 0) {
521  return binary(op, repmat(x, 1, y.size2() / x.size2()), y);
522  }
523  }
524  // x and y are empty horizontal multiples of each other?
525  if (x.size1()==0 && y.size1()==0 && x.size2()>0 && y.size2()>0) {
526  if (x.size2() % y.size2() == 0) {
527  return MX(0, x.size2());
528  } else if (y.size2() % x.size2() == 0) {
529  return MX(0, y.size2());
530  }
531  }
532  // Dimension mismatch
533  casadi_error("Dimension mismatch for " + casadi_math<double>::print(op, "x", "y") +
534  ", x is " + x.dim() + ", while y is " + y.dim());
535  }
536  // Call internal class
537  return x->get_binary(op, y);
538  }
static MX repmat(const MX &x, casadi_int n, casadi_int m=1)
Definition: mx.cpp:1989
MX()
Default constructor.
Definition: mx.cpp:59
static MX binary(casadi_int op, const MX &x, const MX &y)
Create nodes by their ID.
Definition: mx.cpp:513
casadi_int op() const
Get operation type.
Definition: mx.cpp:822
static std::string print(unsigned char op, const std::string &x, const std::string &y)
Print.
Definition: calculus.hpp:1641

References casadi::GenericMatrix< MatType >::dim(), casadi::MXNode::get_binary(), casadi::GenericMatrix< MatType >::is_empty(), casadi::GenericMatrix< MatType >::is_scalar(), MX(), op(), repmat(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MatType >::size1(), and casadi::GenericMatrix< MatType >::size2().

Referenced by printme().

◆ blockcat()

MX casadi::MX::blockcat ( const std::vector< std::vector< MX > > &  v)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1197 of file mx.cpp.

1197  {
1198  // Quick return if no block rows
1199  if (v.empty()) return MX(0, 0);
1200 
1201  // Make sure same number of block columns
1202  casadi_int ncols = v.front().size();
1203  for (auto&& e : v) {
1204  casadi_assert(e.size()==ncols, "blockcat: Inconsistent number of block columns");
1205  }
1206 
1207  // Quick return if no block columns
1208  if (v.front().empty()) return MX(0, 0);
1209 
1210  // Horizontally concatenate all columns for each row, then vertically concatenate rows
1211  std::vector<MX> rows;
1212  for (auto&& e : v) {
1213  rows.push_back(horzcat(e));
1214  }
1215  return vertcat(rows);
1216  }
casadi_int rows() const
Get the number of rows, Octave-style syntax.
static MX horzcat(const std::vector< MX > &x)
Definition: mx.cpp:1053
static MX vertcat(const std::vector< MX > &x)
Definition: mx.cpp:1099

References horzcat(), MX(), casadi::GenericMatrix< MX >::rows(), and vertcat().

Referenced by casadi::Expm::get_forward(), casadi::Nlpsol::get_forward(), casadi::Expm::get_reverse(), casadi::Nlpsol::get_reverse(), and kron().

◆ bspline() [1/2]

MX casadi::MX::bspline ( const MX x,
const DM coeffs,
const std::vector< std::vector< double > > &  knots,
const std::vector< casadi_int > &  degree,
casadi_int  m,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for this class

Definition at line 2116 of file mx.cpp.

2121  {
2122  return BSpline::create(x, knots, coeffs.nonzeros(), degree, m, opts);
2123  }
static MX create(const MX &x, const std::vector< std::vector< double > > &knots, const std::vector< double > &coeffs, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts)
Definition: bspline.cpp:268

References casadi::BSpline::create(), and casadi::Matrix< Scalar >::nonzeros().

Referenced by casadi::Function::bspline(), casadi::BSplineInterpolant::construct_graph(), and casadi::BSplineCommon::jac().

◆ bspline() [2/2]

MX casadi::MX::bspline ( const MX x,
const MX coeffs,
const std::vector< std::vector< double > > &  knots,
const std::vector< casadi_int > &  degree,
casadi_int  m,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for this class

Definition at line 2125 of file mx.cpp.

2129  {
2130  return BSplineParametric::create(x, coeffs, knots, degree, m, opts);
2131  }
static MX create(const MX &x, const MX &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts)
Definition: bspline.cpp:305

References casadi::BSplineParametric::create().

◆ bspline_dual()

DM casadi::MX::bspline_dual ( const std::vector< double > &  x,
const std::vector< std::vector< double > > &  knots,
const std::vector< casadi_int > &  degree,
const Dict opts = Dict() 
)
static

Definition at line 2133 of file mx.cpp.

2136  {
2137  return BSpline::dual(x, knots, degree, opts);
2138  }
static DM dual(const std::vector< double > &x, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, const Dict &opts)
Definition: bspline.cpp:445

References casadi::BSpline::dual().

Referenced by casadi::BSplineInterpolant::construct_graph().

◆ class_name()

std::string casadi::SharedObject::class_name ( ) const
inherited

◆ colind() [1/2]

const casadi_int* casadi::GenericMatrix< MX >::colind ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b8

Definition at line 198 of file generic_matrix.hpp.

198 { return sparsity().colind(); }
Sparsity sparsity() const
Get the sparsity pattern.
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
Definition: sparsity.cpp:168

◆ colind() [2/2]

casadi_int casadi::GenericMatrix< MX >::colind ( casadi_int  col) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b8

Definition at line 201 of file generic_matrix.hpp.

201 { return sparsity().colind(col); }

◆ columns()

casadi_int casadi::GenericMatrix< MX >::columns ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1av

Definition at line 124 of file generic_matrix.hpp.

124 {return size2();}
casadi_int size2() const
Get the second dimension (i.e. number of columns)

◆ conditional()

MX casadi::MX::conditional ( const MX ind,
const std::vector< MX > &  x,
const MX x_default,
bool  short_circuit = false 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1294 of file mx.cpp.

1295  {
1296  if (short_circuit) {
1297  // Get symbolic primitives
1298  std::vector<MX> arg = x;
1299  arg.push_back(x_default);
1300  arg = symvar(veccat(arg));
1301 
1302  // Form functions for cases
1303  std::vector<Function> f(x.size());
1304  for (casadi_int k=0; k<x.size(); ++k) {
1305  std::stringstream ss;
1306  ss << "f_case" << k;
1307  f[k] = Function(ss.str(), arg, {x[k]});
1308  }
1309  Function f_default("f_default", arg, {x_default});
1310 
1311  // Form Switch
1312  Function sw = Function::conditional("switch", f, f_default);
1313 
1314  // Call the Switch
1315  std::vector<MX> sw_arg;
1316  sw_arg.push_back(ind);
1317  sw_arg.insert(sw_arg.end(), arg.begin(), arg.end());
1318  return sw(sw_arg).at(0);
1319  } else {
1320  MX ret = x_default;
1321  for (casadi_int k=0; k<x.size(); ++k) {
1322  ret = if_else(ind==static_cast<double>(k), x[k], ret);
1323  }
1324  return ret;
1325  }
1326  }
static Function conditional(const std::string &name, const std::vector< Function > &f, const Function &f_def, const Dict &opts=Dict())
Constuct a switch function.
Definition: function.cpp:765
static std::vector< MX > symvar(const MX &x)
Definition: mx.cpp:1938
static MX if_else(const MX &cond, const MX &if_true, const MX &if_false, bool short_circuit=false)
Definition: mx.cpp:1272
static MatType veccat(const std::vector< MatType > &x)

References casadi::Function::conditional(), if_else(), symvar(), and casadi::SparsityInterface< MatType >::veccat().

◆ contains_all()

bool casadi::MX::contains_all ( const std::vector< MX > &  v,
const std::vector< MX > &  n 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2076 of file mx.cpp.

2076  {
2077  if (n.empty()) return true;
2078 
2079  // Set to contain all nodes
2080  std::set<MXNode*> l;
2081  for (const MX& e : v) l.insert(e.get());
2082 
2083  size_t l_unique = l.size();
2084 
2085  for (const MX& e : n) l.insert(e.get());
2086 
2087  return l.size()==l_unique;
2088  }

◆ contains_any()

bool casadi::MX::contains_any ( const std::vector< MX > &  v,
const std::vector< MX > &  n 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2090 of file mx.cpp.

2090  {
2091  if (n.empty()) return true;
2092 
2093  // Set to contain all nodes
2094  std::set<MXNode*> l;
2095  for (const MX& e : v) l.insert(e.get());
2096 
2097  size_t l_unique = l.size();
2098 
2099  std::set<MXNode*> r;
2100  for (const MX& e : n) r.insert(e.get());
2101 
2102  size_t r_unique = r.size();
2103  for (const MX& e : n) l.insert(e.get());
2104 
2105  return l.size()<l_unique+r_unique;
2106  }

◆ convexify()

MX casadi::MX::convexify ( const MX H,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for this class

Definition at line 2140 of file mx.cpp.

2141  {
2142  return H->get_convexify(opts);
2143  }

References casadi::MXNode::get_convexify().

◆ count_down()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_down
protectedinherited

Definition at line 134 of file generic_shared_impl.hpp.

42  {
43 #ifdef WITH_EXTRA_CHECKS
44  casadi_assert_dev(Function::call_depth_==0);
45 #endif // WITH_EXTRA_CHECKS
46  if (!node) return;
47  if (node->weak_ref_) {
48 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
49  auto mutex = node->weak_ref_->get_mutex();
50  // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
51  std::lock_guard<std::mutex> lock(*mutex);
52  // Could it be that this mutex is destroyed when the lock goes out of scope?
53 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
54 
55  if (--static_cast<Internal*>(node)->count == 0) {
56  delete node;
57  node = nullptr;
58  }
59  } else {
60  if (--static_cast<Internal*>(node)->count == 0) {
61  delete node;
62  node = nullptr;
63  }
64  }
65  }

◆ count_up()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_up
protectedinherited

Definition at line 133 of file generic_shared_impl.hpp.

32  {
33 #ifdef WITH_EXTRA_CHECKS
34  casadi_assert_dev(Function::call_depth_==0);
35 #endif // WITH_EXTRA_CHECKS
36 
37  if (node) static_cast<Internal*>(node)->count++;
38 
39  }

◆ create()

MX casadi::MX::create ( MXNode node)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_qa

Definition at line 67 of file mx.cpp.

67  {
68  return MX(node, false, false, false, false);
69  }

References MX().

Referenced by casadi::MXNode::_get_binary(), _sym(), casadi::GetNonzerosParam::create(), casadi::SetNonzerosParam< Add >::create(), casadi::SetNonzeros< Add >::create(), casadi::GetNonzeros::create(), casadi::Call::create_call(), createMultipleOutput(), deserialize(), casadi::MXNode::get_assert(), casadi::MXNode::get_bilin(), casadi::MXNode::get_bspline(), casadi::MXNode::get_convexify(), casadi::MXNode::get_det(), casadi::MXNode::get_diagcat(), casadi::MXNode::get_dot(), casadi::MXNode::get_einstein(), casadi::MXNode::get_find(), casadi::MXNode::get_horzcat(), casadi::MXNode::get_inv(), casadi::MXNode::get_logsumexp(), casadi::MXNode::get_low(), casadi::MXNode::get_mac(), casadi::MXNode::get_mmax(), casadi::MXNode::get_mmin(), casadi::MXNode::get_monitor(), casadi::MXNode::get_norm_1(), casadi::MXNode::get_norm_2(), casadi::MXNode::get_norm_fro(), casadi::MXNode::get_norm_inf(), casadi::Constant< Value >::get_nzref(), casadi::Call::get_output(), casadi::MultipleOutput::get_output(), casadi::MXNode::get_project(), casadi::Constant< Value >::get_project(), casadi::MXNode::get_rank1(), casadi::MXNode::get_repmat(), casadi::MXNode::get_repsum(), casadi::MXNode::get_reshape(), casadi::Constant< Value >::get_reshape(), casadi::MXNode::get_solve(), casadi::MXNode::get_solve_tril(), casadi::MXNode::get_solve_tril_unity(), casadi::MXNode::get_solve_triu(), casadi::MXNode::get_solve_triu_unity(), casadi::MXNode::get_sparsity_cast(), casadi::MXNode::get_subassign(), casadi::MXNode::get_subref(), casadi::MXNode::get_transpose(), casadi::Constant< Value >::get_transpose(), casadi::MXNode::get_unary(), casadi::MXNode::get_vertcat(), inf(), casadi::MXFunction::init(), and nan().

◆ createMultipleOutput()

std::vector< MX > casadi::MX::createMultipleOutput ( MXNode node)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_qb

Definition at line 128 of file mx.cpp.

128  {
129  casadi_assert_dev(dynamic_cast<MultipleOutput*>(node) != nullptr);
130  MX x = MX::create(node);
131  std::vector<MX> ret(x->nout());
132  for (casadi_int i=0; i<ret.size(); ++i) {
133  ret[i] = x.get_output(i);
134  if (ret[i].is_empty(true)) {
135  ret[i] = MX(0, 0);
136  } else if (ret[i].nnz()==0) {
137  ret[i] = MX(ret[i].size());
138  }
139  }
140  return ret;
141  }
bool is_empty(bool both=false) const
Check if the sparsity is empty, i.e. if one of the dimensions is zero.
std::pair< casadi_int, casadi_int > size() const
Get the shape.
casadi_int nnz() const
Get the number of (structural) non-zero elements.

References create(), get_output(), casadi::GenericMatrix< MX >::is_empty(), MX(), casadi::GenericMatrix< MX >::nnz(), casadi::MXNode::nout(), and casadi::GenericMatrix< MX >::size().

Referenced by casadi::Call::create(), casadi::MXNode::get_diagsplit(), casadi::MXNode::get_horzsplit(), and casadi::MXNode::get_vertsplit().

◆ cross()

MX casadi::GenericMatrix< MX >::cross ( const MX a,
const MX b,
casadi_int  dim = -1 
)
staticinherited

Definition at line 227 of file generic_matrix.hpp.

1394  {
1395  casadi_assert(a.size1()==b.size1() && a.size2()==b.size2(),
1396  "cross(a, b): Inconsistent dimensions. Dimension of a ("
1397  + a.dim() + " ) must equal that of b (" + b.dim() + ").");
1398 
1399  casadi_assert(a.size1()==3 || a.size2()==3,
1400  "cross(a, b): One of the dimensions of a should have length 3, but got "
1401  + a.dim() + ".");
1402  casadi_assert(dim==-1 || dim==1 || dim==2,
1403  "cross(a, b, dim): Dim must be 1, 2 or -1 (automatic).");
1404 
1405  std::vector<MatType> ret(3);
1406 
1407  bool t = a.size1()==3;
1408 
1409  if (dim==1) t = true;
1410  if (dim==2) t = false;
1411 
1412  MatType a1 = t ? a(0, Slice()) : a(Slice(), 0);
1413  MatType a2 = t ? a(1, Slice()) : a(Slice(), 1);
1414  MatType a3 = t ? a(2, Slice()) : a(Slice(), 2);
1415 
1416  MatType b1 = t ? b(0, Slice()) : b(Slice(), 0);
1417  MatType b2 = t ? b(1, Slice()) : b(Slice(), 1);
1418  MatType b3 = t ? b(2, Slice()) : b(Slice(), 2);
1419 
1420  ret[0] = a2*b3-a3*b2;
1421  ret[1] = a3*b1-a1*b3;
1422  ret[2] = a1*b2-a2*b1;
1423 
1424  return t ? vertcat(ret) : horzcat(ret);
1425  }
std::string dim(bool with_nz=false) const
Get string representation of dimensions.
friend MatType vertcat(const std::vector< MatType > &v)
Concatenate a list of matrices vertically.
friend MatType horzcat(const std::vector< MatType > &v)
Concatenate a list of matrices horizontally.

◆ cse()

std::vector< MX > casadi::MX::cse ( const std::vector< MX > &  e)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2179 of file mx.cpp.

2179  {
2180  std::vector<MX> orig = e;
2181  bool updated = true;
2182  while (updated) {
2183  Function f("f", std::vector<MX>{}, orig,
2184  {{"live_variables", false}, {"max_io", 0}, {"cse", false}, {"allow_free", true}});
2185  MXFunction *ff = f.get<MXFunction>();
2186 
2187  // Symbolic work, non-differentiated
2188  std::vector<MX> swork(ff->workloc_.size()-1);
2189 
2190  // Allocate storage for split outputs
2191  std::vector<std::vector<MX> > res_split(orig.size());
2192  for (casadi_int i=0; i<orig.size(); ++i) res_split[i].resize(orig[i].n_primitives());
2193 
2194  std::vector<MX> arg1, res1;
2195  std::vector<MX> res(orig.size());
2196 
2197  std::unordered_map<std::string, MX > cache;
2198  IncrementalSerializerMX s;
2199 
2200  std::unordered_map<FunctionInternal*, Function> function_cache;
2201 
2202  // Loop over computational nodes in forward order
2203  casadi_int alg_counter = 0;
2204  for (auto it=ff->algorithm_.begin(); it!=ff->algorithm_.end(); ++it, ++alg_counter) {
2205  if (it->op == OP_INPUT) {
2206  // pass
2207  } else if (it->op==OP_OUTPUT) {
2208  // Collect the results
2209  res_split.at(it->data->ind()).at(it->data->segment()) = swork[it->arg.front()];
2210  } else if (it->op==OP_PARAMETER) {
2211  // Fetch parameter
2212  MX& target = swork[it->res.front()];
2213  target = it->data;
2214  cache[s.pack(target)] = target;
2215  } else {
2216 
2217  // Arguments of the operation
2218  arg1.resize(it->arg.size());
2219  for (casadi_int i=0; i<arg1.size(); ++i) {
2220  casadi_int el = it->arg[i]; // index of the argument
2221  arg1[i] = el<0 ? MX(it->data->dep(i).size()) : swork[el];
2222  }
2223 
2224  // Perform the operation
2225  res1.resize(it->res.size());
2226  it->data->eval_mx(arg1, res1);
2227 
2228  // Get the result
2229  for (casadi_int i=0; i<res1.size(); ++i) {
2230  casadi_int el = it->res[i]; // index of the output
2231 
2232  MX& out_i = res1[i];
2233 
2234  // Default assumption is that out_i is not an output node
2235  casadi_int output_node = -1;
2236 
2237  if (out_i.is_output()) {
2238  output_node = out_i.which_output();
2239  // First pack/cache the parent (MultipleOutput node e.g. Call, Horzsplit)
2240  out_i = out_i.dep(0);
2241 
2242  // If we are a call node,
2243  if (out_i.op()==OP_CALL) {
2244  FunctionInternal* key = out_i.which_function().get();
2245  auto itk = function_cache.find(key);
2246  if (itk==function_cache.end()) {
2247  function_cache[key] = out_i.which_function();
2248  } else {
2249  out_i = Call::create_call(function_cache[key], out_i->dep_);
2250  }
2251  }
2252  }
2253 
2254  while (true) {
2255  // Replace out_i by a cached variant if possible
2256  std::string key = s.pack(out_i);
2257 
2258  auto itk = cache.find(key);
2259  if (itk==cache.end()) {
2260  cache[key] = out_i;
2261  } else {
2262  out_i = itk->second;
2263  }
2264 
2265  if (output_node==-1) {
2266  break; // Job is done
2267  } else {
2268  // Recreate the output node on top of the parent
2269  out_i = out_i.get_output(output_node);
2270  output_node = -1;
2271  // Loop once more
2272  }
2273  }
2274 
2275  if (el>=0) swork[el] = out_i;
2276  }
2277  }
2278  }
2279 
2280  // Join split outputs
2281  for (casadi_int i=0; i<res.size(); ++i) res[i] = orig[i].join_primitives(res_split[i]);
2282 
2283  std::vector<MX> subs_from;
2284  std::vector<MX> subs_to;
2285  for (const auto& e : function_cache) {
2286  e.second->merge(res, subs_from, subs_to);
2287  }
2288  orig = graph_substitute(res, subs_from, subs_to, updated);
2289  }
2290 
2291  return orig;
2292  }
static MX create_call(const Function &fcn, const std::vector< MX > &arg)
Create function call node.
casadi_int n_primitives() const
Get the number of primitives for MXFunction inputs/outputs.
Definition: mx.cpp:929
static MX graph_substitute(const MX &x, const std::vector< MX > &v, const std::vector< MX > &vdef)
Definition: mx.cpp:1450
MX join_primitives(const std::vector< MX > &v) const
Join an expression along symbolic primitives.
Definition: mx.cpp:965
@ OP_OUTPUT
Definition: calculus.hpp:82
@ OP_INPUT
Definition: calculus.hpp:82
@ OP_PARAMETER
Definition: calculus.hpp:85
@ OP_CALL
Definition: calculus.hpp:88

References casadi::Call::create_call(), dep(), casadi::MXNode::dep_, casadi::FunctionInternal::find(), casadi::Function::get(), get_output(), graph_substitute(), is_output(), join_primitives(), MX(), n_primitives(), op(), casadi::OP_CALL, casadi::OP_INPUT, casadi::OP_OUTPUT, casadi::OP_PARAMETER, which_function(), and which_output().

◆ cumsum()

MX casadi::MX::cumsum ( const MX x,
casadi_int  axis = -1 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 678 of file mx.cpp.

678  {
679  if (axis==-1) axis = x.is_row();
680  MX r = axis==0 ? x.T() : x;
681  Sparsity sl = r(Slice(), 0).sparsity();
682  MX acc = MX::sym("acc", sl);
683  MX u = MX::sym("u", sl);
684 
685  Function f("f", {acc, u}, {acc+u});
686  f = f.mapaccum(r.size2());
687  MX ret = f(std::vector<MX>{0, r})[0];
688 
689  return axis==0 ? ret.T() : ret;
690  }
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.

References casadi::GenericMatrix< MatType >::is_row(), casadi::Function::mapaccum(), casadi::GenericMatrix< MatType >::size2(), sparsity(), casadi::GenericMatrix< MX >::sym(), and T().

◆ debug_repr()

std::string casadi::GenericShared< SharedObject , SharedObjectInternal >::debug_repr
inherited

Definition at line 113 of file generic_shared_impl.hpp.

80  {
81  if (node) {
82  return node->debug_repr(node);
83  } else {
84  return "NULL";
85  }
86  }
std::string debug_repr(const Internal *) const

◆ densify()

MX casadi::MX::densify ( const MX x,
const MX val = 0 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 894 of file mx.cpp.

894  {
895  casadi_assert_dev(val.is_scalar());
896  if (x.is_dense()) {
897  return x; // Already ok
898  } else if (val->is_zero()) {
899  return project(x, Sparsity::dense(x.size()));
900  } else {
901  MX ret = MX::repmat(val, x.size());
902  ret(x.sparsity()) = x;
903  return ret;
904  }
905  }
static MX project(const MX &x, const Sparsity &sp, bool intersect=false)
Definition: mx.cpp:877

References casadi::Sparsity::dense(), casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::MXNode::is_zero(), project(), repmat(), casadi::GenericMatrix< MatType >::size(), and sparsity().

Referenced by MX(), and set().

◆ dep()

MX casadi::MX::dep ( casadi_int  ch = 0) const

◆ depends_on()

bool casadi::MX::depends_on ( const MX x,
const MX arg 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2057 of file mx.cpp.

2057  {
2058  if (x.nnz()==0) return false;
2059 
2060  // Construct a temporary algorithm
2061  Function temp("tmp_depends_on", {arg}, {x}, Dict{{"max_io", 0}, {"allow_free", true}});
2062 
2063  // Perform a single dependency sweep
2064  std::vector<bvec_t> t_in(arg.nnz(), 1), t_out(x.nnz());
2065  temp({get_ptr(t_in)}, {get_ptr(t_out)});
2066 
2067  // Loop over results
2068  for (casadi_int i=0; i<t_out.size(); ++i) {
2069  if (t_out[i]) return true;
2070  }
2071 
2072  return false;
2073  }
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.

References casadi::get_ptr(), and casadi::GenericMatrix< MatType >::nnz().

◆ deserialize()

MX casadi::MX::deserialize ( DeserializingStream s)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r0

Definition at line 834 of file mx.cpp.

834  {
835  return MX::create(MXNode::deserialize(s));
836  }
static MXNode * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
Definition: mx_node.cpp:540

References create(), and casadi::MXNode::deserialize().

◆ det()

MX casadi::MX::det ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1922 of file mx.cpp.

1922  {
1923  return x->get_det();
1924  }

References casadi::MXNode::get_det().

◆ diag()

MX casadi::MX::diag ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1363 of file mx.cpp.

1363  {
1364  // Nonzero mapping
1365  std::vector<casadi_int> mapping;
1366 
1367  // Get the sparsity
1368  Sparsity sp = x.sparsity().get_diag(mapping);
1369 
1370  // Create a reference to the nonzeros
1371  return x->get_nzref(sp, mapping);
1372  }
Matrix< casadi_int > mapping() const
Get an IM representation of a GetNonzeros or SetNonzeros node.
Definition: mx.cpp:857
const Sparsity & sparsity() const
Const access the sparsity - reference to data member.
Sparsity get_diag(std::vector< casadi_int > &mapping) const
Definition: sparsity.cpp:611

References casadi::Sparsity::get_diag(), casadi::MXNode::get_nzref(), mapping(), and sparsity().

◆ diagcat()

MX casadi::MX::diagcat ( const std::vector< MX > &  x)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1089 of file mx.cpp.

1089  {
1090  // Quick return if empty or single element
1091  if (x.empty()) return MX();
1092  if (x.size()==1) return x.front();
1093  // Call recursively if any 0-by-0 matrices
1094  if (has_empty(x, true)) return diagcat(trim_empty(x, true));
1095  // Create diagcat node
1096  return x.front()->get_diagcat(x);
1097  }
static MX diagcat(const std::vector< MX > &x)
Definition: mx.cpp:1089
std::vector< MX > trim_empty(const std::vector< MX > &x, bool both=false)
Definition: mx.cpp:1045
bool has_empty(const std::vector< MX > &x, bool both=false)
Definition: mx.cpp:1038

References casadi::has_empty(), MX(), and casadi::trim_empty().

◆ diagsplit()

std::vector< MX > casadi::MX::diagsplit ( const MX x,
const std::vector< casadi_int > &  offset1,
const std::vector< casadi_int > &  offset2 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1157 of file mx.cpp.

1158  {
1159  // Consistency check
1160  casadi_assert_dev(!offset1.empty());
1161  casadi_assert_dev(offset1.front()==0);
1162  casadi_assert_dev(offset1.back()==x.size1());
1163  casadi_assert_dev(is_monotone(offset1));
1164 
1165  // Consistency check
1166  casadi_assert_dev(!offset2.empty());
1167  casadi_assert_dev(offset2.front()==0);
1168  casadi_assert_dev(offset2.back()==x.size2());
1169  casadi_assert_dev(is_monotone(offset2));
1170 
1171  return x->get_diagsplit(offset1, offset2);
1172  }
bool is_monotone(const std::vector< T > &v)
Check if the vector is monotone.

References casadi::MXNode::get_diagsplit(), casadi::is_monotone(), casadi::GenericMatrix< MatType >::size1(), and casadi::GenericMatrix< MatType >::size2().

◆ diff()

MX casadi::GenericMatrix< MX >::diff ( const MX x,
casadi_int  n = 1,
casadi_int  axis = -1 
)
staticinherited

Definition at line 233 of file generic_matrix.hpp.

1774  {
1775  casadi_assert(axis==-1 || axis==0 || axis==1, "Axis argument invalid");
1776  casadi_assert(n>=1, "n argument invalid");
1777 
1778  MatType ret = x;
1779  for (casadi_int i=0;i<n;++i) {
1780  // Matlab's special case
1781  if (axis==-1 && ret.is_scalar()) return MatType();
1782 
1783  casadi_int local_axis = (axis==-1) ? ret.is_row() : axis;
1784  if (local_axis==0) {
1785  if (ret.size1()<=1) {
1786  ret = MatType::zeros(0, ret.size2());
1787  } else {
1788  ret = ret(Slice(1, ret.size1()), Slice())-ret(Slice(0, ret.size1()-1), Slice());
1789  }
1790  } else {
1791  if (ret.size2()<=1) {
1792  ret = MatType::zeros(ret.size1(), 0);
1793  } else {
1794  ret = ret(Slice(), Slice(1, ret.size2()))-ret(Slice(), Slice(0, ret.size2()-1));
1795  }
1796  }
1797  }
1798  return ret;
1799  }

◆ difference()

std::vector< MX > casadi::MX::difference ( const std::vector< MX > &  a,
const std::vector< MX > &  b 
)
static

Functions called by friend functions defined for this class

Definition at line 2613 of file mx.cpp.

2613  {
2614  // Create a set of MXNodes from b
2615  std::set<MXNode*> bs;
2616  for (const auto& e : b) {
2617  if (!e.is_null()) bs.insert(e.get());
2618  }
2619  std::vector<MX> ret;
2620  for (auto&& e : a) {
2621  // If the element is not in the set, add it to the return vector
2622  if (bs.find(e.get())==bs.end()) {
2623  ret.push_back(e);
2624  }
2625  }
2626  return ret;
2627  }

Referenced by stop_diff().

◆ dim()

std::string casadi::GenericMatrix< MX >::dim ( bool  with_nz = false) const
inherited

The representation is e.g. "4x5" or "4x5,10nz"

Extra doc: https://github.com/casadi/casadi/wiki/L_1aw

Definition at line 131 of file generic_matrix.hpp.

1343  {
1344  return sparsity().dim(with_nz);
1345  }
std::string dim(bool with_nz=false) const
Get the dimension as a string.
Definition: sparsity.cpp:587

◆ disp()

void casadi::SharedObject::disp ( std::ostream &  stream,
bool  more = false 
) const
inherited

Definition at line 35 of file shared_object.cpp.

35  {
36  if (is_null()) {
37  stream << "NULL";
38  } else {
39  (*this)->disp(stream, more);
40  }
41  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::is_null().

Referenced by casadi::Nlpsol::disp_more(), and casadi::CsparseInterface::nfact().

◆ dot()

MX casadi::MX::dot ( const MX x,
const MX y 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 715 of file mx.cpp.

715  {
716  return x->get_dot(y);
717  }

References casadi::MXNode::get_dot().

◆ einstein() [1/2]

MX casadi::MX::einstein ( const MX A,
const MX B,
const MX C,
const std::vector< casadi_int > &  dim_a,
const std::vector< casadi_int > &  dim_b,
const std::vector< casadi_int > &  dim_c,
const std::vector< casadi_int > &  a,
const std::vector< casadi_int > &  b,
const std::vector< casadi_int > &  c 
)
static

Computes the product: C_c = A_a + B_b where a b c are index/einstein notation in an encoded form

For example, an matrix-matrix product may be written as: C_ij = A_ik B_kj

The encoded form uses strictly negative numbers to indicate labels. For the above example, we would have: a {-1, -3} b {-3, -2} c {-1 -2}

Extra doc: https://github.com/casadi/casadi/wiki/L_r5

Definition at line 662 of file mx.cpp.

666  {
667  return C->get_einstein(A, B, dim_c, dim_a, dim_b, c, a, b);
668  }
GenericMatrix< MX > B
Base class.
Definition: mx.hpp:189

References casadi::C.

Referenced by casadi::Einstein::ad_forward(), and casadi::Einstein::ad_reverse().

◆ einstein() [2/2]

MX casadi::MX::einstein ( const MX A,
const MX B,
const std::vector< casadi_int > &  dim_a,
const std::vector< casadi_int > &  dim_b,
const std::vector< casadi_int > &  dim_c,
const std::vector< casadi_int > &  a,
const std::vector< casadi_int > &  b,
const std::vector< casadi_int > &  c 
)
static

Computes the product: C_c = A_a + B_b where a b c are index/einstein notation in an encoded form

For example, an matrix-matrix product may be written as: C_ij = A_ik B_kj

The encoded form uses strictly negative numbers to indicate labels. For the above example, we would have: a {-1, -3} b {-3, -2} c {-1 -2}

Extra doc: https://github.com/casadi/casadi/wiki/L_r5

Definition at line 670 of file mx.cpp.

674  {
675  return MX::zeros(product(dim_c), 1)->get_einstein(A, B, dim_c, dim_a, dim_b, c, a, b);
676  }
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.
Definition: mx_node.cpp:582
T product(const std::vector< T > &values)
product

References casadi::MXNode::get_einstein(), casadi::product(), and casadi::GenericMatrix< MX >::zeros().

◆ enlarge()

void casadi::MX::enlarge ( casadi_int  nrow,
casadi_int  ncol,
const std::vector< casadi_int > &  rr,
const std::vector< casadi_int > &  cc,
bool  ind1 = false 
)

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

Extra doc: https://github.com/casadi/casadi/wiki/L_qg

Definition at line 642 of file mx.cpp.

644  {
645  Sparsity sp = sparsity();
646  sp.enlarge(nrow, ncol, rr, cc, ind1);
647 
648  MX ret = (*this)->get_nzref(sp, range(nnz())); // FIXME?
649  *this = ret;
650  }
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:592

References casadi::Sparsity::enlarge(), casadi::MXNode::get_nzref(), casadi::GenericMatrix< MX >::nnz(), casadi::range(), and sparsity().

◆ erase() [1/2]

void casadi::MX::erase ( const std::vector< casadi_int > &  rr,
bool  ind1 = false 
)

Erase elements of a matrix

Extra doc: https://github.com/casadi/casadi/wiki/L_qf

Definition at line 628 of file mx.cpp.

628  {
629  // Get sparsity of the new matrix
630  Sparsity sp = sparsity();
631 
632  // Erase from sparsity pattern
633  std::vector<casadi_int> mapping = sp.erase(rr, ind1);
634 
635  // Create new matrix
636  if (mapping.size()!=nnz()) {
637  MX ret = (*this)->get_nzref(sp, mapping);
638  *this = ret;
639  }
640  }
void erase(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Erase a submatrix (leaving structural zeros in its place)

References casadi::Sparsity::erase(), casadi::MXNode::get_nzref(), mapping(), casadi::GenericMatrix< MX >::nnz(), casadi::GenericMatrix< MatType >::size(), and sparsity().

◆ erase() [2/2]

void casadi::MX::erase ( const std::vector< casadi_int > &  rr,
const std::vector< casadi_int > &  cc,
bool  ind1 = false 
)

Erase rows and/or columns of a matrix

Extra doc: https://github.com/casadi/casadi/wiki/L_qe

Definition at line 596 of file mx.cpp.

596  {
597  // Get sparsity of the new matrix
598  Sparsity sp = sparsity();
599 
600  // Erase from sparsity pattern
601  std::vector<casadi_int> mapping = sp.erase(rr, cc, ind1);
602 
603  // Create new matrix
604  if (mapping.size()!=nnz()) {
605  MX ret = (*this)->get_nzref(sp, mapping);
606  *this = ret;
607  }
608  }

References casadi::Sparsity::erase(), casadi::MXNode::get_nzref(), mapping(), casadi::GenericMatrix< MX >::nnz(), casadi::GenericMatrix< MatType >::size(), and sparsity().

Referenced by set().

◆ eval_mx()

void casadi::MX::eval_mx ( const std::vector< MX > &  arg,
std::vector< MX > &  res 
) const

Extra doc: https://github.com/casadi/casadi/wiki/L_rn

Definition at line 2736 of file mx.cpp.

2736  {
2737  try {
2738  res.resize((*this)->nout());
2739  (*this)->eval_mx(arg, res);
2740  } catch (std::exception& e) {
2741  CASADI_THROW_ERROR_OBJ("eval_mx", e.what());
2742  }
2743  }

◆ evalf()

DM casadi::MX::evalf ( const MX m)
static

Functions called by friend functions defined for this class

Definition at line 739 of file mx.cpp.

739  {
740  Function f("f", std::vector<MX>{}, {m}, {{"allow_free", true}});
741  return f(std::vector<DM>{})[0];
742  }

◆ expm()

MX casadi::MX::expm ( const MX A)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2046 of file mx.cpp.

2046  {
2047  Function ret = expmsol("mysolver", "slicot", A.sparsity());
2048  return ret(std::vector<MX>{A, 1})[0];
2049  }
Function expmsol(const std::string &name, const std::string &solver, const Sparsity &A, const Dict &opts)
Definition: expm.cpp:44

References casadi::expmsol(), and sparsity().

◆ expm_const()

MX casadi::MX::expm_const ( const MX A,
const MX t 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2039 of file mx.cpp.

2039  {
2040  Dict opts;
2041  opts["const_A"] = true;
2042  Function ret = expmsol("mysolver", "slicot", A.sparsity(), opts);
2043  return ret(std::vector<MX>{A, t})[0];
2044  }

References casadi::expmsol(), and sparsity().

◆ extract()

void casadi::MX::extract ( std::vector< MX > &  ex,
std::vector< MX > &  v,
std::vector< MX > &  vdef,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1642 of file mx.cpp.

1643  {
1644  try {
1645  // Read options
1646  std::string v_prefix = "v_", v_suffix = "";
1647  bool lift_shared = true, lift_calls = false;
1648  casadi_int v_ind = 0;
1649  for (auto&& op : opts) {
1650  if (op.first == "prefix") {
1651  v_prefix = std::string(op.second);
1652  } else if (op.first == "suffix") {
1653  v_suffix = std::string(op.second);
1654  } else if (op.first == "lift_shared") {
1655  lift_shared = op.second;
1656  } else if (op.first == "lift_calls") {
1657  lift_calls = op.second;
1658  } else if (op.first == "offset") {
1659  v_ind = op.second;
1660  } else {
1661  casadi_error("No such option: " + std::string(op.first));
1662  }
1663  }
1664  // Sort the expression
1665  Function f("tmp_extract", std::vector<MX>{}, ex, Dict{{"max_io", 0}, {"allow_free", true}});
1666  auto *ff = f.get<MXFunction>();
1667  // Get references to the internal data structures
1668  const std::vector<MXAlgEl>& algorithm = ff->algorithm_;
1669  std::vector<MX> work(ff->workloc_.size()-1);
1670  // Count how many times an expression has been used
1671  std::vector<casadi_int> usecount(work.size(), 0);
1672  // Remember the origin of every calculation
1673  std::vector<std::pair<casadi_int, casadi_int> > origin(work.size(), std::make_pair(-1, -1));
1674  // Which evaluations to replace
1675  std::vector<std::pair<casadi_int, casadi_int> > replace;
1676  // Evaluate the algorithm to identify which evaluations to replace
1677  casadi_int k=0;
1678  for (auto it=algorithm.begin(); it<algorithm.end(); ++it, ++k) {
1679  // Increase usage counters
1680  switch (it->op) {
1681  case OP_CONST:
1682  case OP_PARAMETER:
1683  break;
1684  default: // Unary operation, binary operation or output
1685  for (casadi_int c=0; c<it->arg.size(); ++c) {
1686  // Identify nodes used more than once
1687  if (lift_calls && it->op == OP_CALL) {
1688  // If not already marked for replacing
1689  if (usecount.at(it->arg[c]) >= 0) {
1690  replace.push_back(origin.at(it->arg[c]));
1691  usecount.at(it->arg[c]) = -1; // Do not replace again
1692  }
1693  } else if (lift_shared && work[it->arg[c]].op() != OP_PARAMETER
1694  && work[it->arg[c]].op() != OP_CONST) {
1695  if (usecount.at(it->arg[c]) == 0) {
1696  // First time node is used
1697  usecount.at(it->arg[c]) = 1;
1698  } else if (usecount.at(it->arg[c]) == 1) {
1699  // Second time node is used
1700  replace.push_back(origin.at(it->arg[c]));
1701  usecount.at(it->arg[c]) = -1; // Do not replace again
1702  }
1703  }
1704  }
1705  }
1706  // Perform the operation
1707  switch (it->op) {
1708  case OP_OUTPUT:
1709  break;
1710  case OP_CONST:
1711  usecount[it->res.front()] = -1; // Never extract constants
1712  break;
1713  default:
1714  for (casadi_int c=0; c<it->res.size(); ++c) {
1715  if (it->res[c]>=0) {
1716  work[it->res[c]] = it->data.get_output(c);
1717  origin[it->res[c]] = std::make_pair(k, c);
1718  if (lift_calls && it->op == OP_CALL) {
1719  // If function call, replace right away
1720  replace.push_back(origin.at(it->res[c]));
1721  usecount.at(it->res[c]) = -1; // Do not replace again
1722  } else {
1723  usecount.at(it->res[c]) = 0; // Not (yet) extracted
1724  }
1725  }
1726  }
1727  break;
1728  }
1729  }
1730  // New variables and definitions
1731  v.clear();
1732  v.reserve(replace.size());
1733  vdef.clear();
1734  vdef.reserve(replace.size());
1735  // Quick return
1736  if (replace.empty()) return;
1737  // Sort the elements to be replaced in the order of appearence in the algorithm
1738  sort(replace.begin(), replace.end());
1739  std::vector<std::pair<casadi_int, casadi_int> >::const_iterator replace_it=replace.begin();
1740  // Arguments for calling the atomic operations
1741  std::vector<MX> oarg, ores;
1742  // Evaluate the algorithm
1743  k = 0;
1744  for (auto it=algorithm.begin(); it<algorithm.end(); ++it, ++k) {
1745  switch (it->op) {
1746  case OP_OUTPUT:
1747  casadi_assert(it->data->segment()==0, "Not implemented");
1748  ex[it->data->ind()] = work[it->arg.front()];
1749  break;
1750  case OP_CONST:
1751  work[it->res.front()] = it->data;
1752  break;
1753  default:
1754  {
1755  if (it->op == OP_PARAMETER) {
1756  // Free parameter
1757  work[it->res.front()] = it->data;
1758  } else {
1759  // Arguments of the operation
1760  oarg.resize(it->arg.size());
1761  for (casadi_int i=0; i<oarg.size(); ++i) {
1762  casadi_int el = it->arg[i];
1763  oarg[i] = el<0 ? MX(it->data->dep(i).size()) : work.at(el);
1764  }
1765  // Perform the operation
1766  ores.resize(it->res.size());
1767  it->data->eval_mx(oarg, ores);
1768  // Get the result
1769  for (casadi_int i=0; i<ores.size(); ++i) {
1770  casadi_int el = it->res[i];
1771  if (el>=0) work.at(el) = ores[i];
1772  }
1773  }
1774  // Possibly replace results with new variables
1775  for (casadi_int c=0; c<it->res.size(); ++c) {
1776  // Output index
1777  casadi_int ind = it->res[c];
1778  // In the list of nodes for replacing?
1779  bool replace_node = replace_it != replace.end()
1780  && replace_it->first==k && replace_it->second==c;
1781  // Call node (introduce variable for outputs, even if unused)
1782  bool output_node = lift_calls && it->op == OP_CALL;
1783  // Skip if no reason to replace
1784  if (!replace_node && !output_node) continue;
1785  // Create a new variable
1786  Sparsity v_sp = it->op == OP_PARAMETER ? it->data.sparsity() : ores.at(c).sparsity();
1787  v.push_back(MX::sym(v_prefix + std::to_string(v_ind++) + v_suffix, v_sp));
1788  // Add definition of new variable
1789  if (ind >= 0) {
1790  // Replace existing call
1791  casadi_assert(replace_node, "Consistency check");
1792  // Store the result
1793  vdef.push_back(work[ind]);
1794  // Use in calculations
1795  work[ind] = v.back();
1796  // Go to the next element to be replaced
1797  replace_it++;
1798  } else {
1799  // New node corresponding to an output
1800  casadi_assert(output_node, "Consistency check");
1801  // Store the result
1802  vdef.push_back(ores.at(c));
1803  }
1804  }
1805  }
1806  }
1807  }
1808  // Ensure all nodes have been replaced
1809  casadi_assert(replace_it == replace.end(), "Consistency check failed");
1810  } catch (std::exception& e) {
1811  CASADI_THROW_ERROR("extract", e.what());
1812  }
1813  }
CASADI_EXPORT std::string replace(const std::string &s, const std::string &p, const std::string &r)
Replace all occurences of p with r in s.
void sort(const std::vector< T > &values, std::vector< T > &sorted_values, std::vector< casadi_int > &indices, bool invert_indices=false)
Sort the data in a vector.
@ OP_CONST
Definition: calculus.hpp:79

References casadi::MXFunction::algorithm_, casadi::Function::get(), MX(), op(), casadi::OP_CALL, casadi::OP_CONST, casadi::OP_OUTPUT, casadi::OP_PARAMETER, casadi::replace(), casadi::sort(), and casadi::GenericMatrix< MX >::sym().

Referenced by shared().

◆ extract_parametric()

void casadi::MX::extract_parametric ( const MX expr,
const MX par,
MX expr_ret,
std::vector< MX > &  symbols,
std::vector< MX > &  parametric,
const Dict opts 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2324 of file mx.cpp.

2326  {
2327  std::string v_prefix = "e_";
2328  std::string v_suffix = "";
2329  bool extract_trivial = false;
2330  casadi_int v_offset = 0;
2331  for (auto&& op : opts) {
2332  if (op.first == "prefix") {
2333  v_prefix = std::string(op.second);
2334  } else if (op.first == "suffix") {
2335  v_suffix = std::string(op.second);
2336  } else if (op.first == "offset") {
2337  v_offset = op.second;
2338  } else if (op.first == "extract_trivial") {
2339  extract_trivial = op.second;
2340  } else {
2341  casadi_error("No such option: " + std::string(op.first));
2342  }
2343  }
2344  Function f("f", {par}, {expr}, {{"live_variables", false},
2345  {"max_io", 0}, {"allow_free", true}});
2346  MXFunction *ff = f.get<MXFunction>();
2347 
2348  // Work vector
2349  std::vector< MX > w(ff->workloc_.size()-1);
2350 
2351  // Status of the expression:
2352  // 0: dependant on constants only
2353  // 1: dependant on parameters/constants only
2354  // 2: dependant on non-parameters
2355  std::vector< char > expr_status(ff->workloc_.size()-1, 0);
2356 
2357  // Split up inputs analogous to symbolic primitives
2358  std::vector<MX> arg_split = par.split_primitives(par);
2359 
2360  // Allocate storage for split outputs
2361  std::vector<MX> res_split;
2362  res_split.resize(expr.n_primitives());
2363 
2364  // Scratch space for node inputs/outputs
2365  std::vector<MX > arg1, res1;
2366 
2367  // Map of registered symbols
2368  std::map<MXNode*, MX> symbol_map;
2369 
2370  // Flat list of registerd symbols and parametric expressions
2371  std::vector<MX> symbol_v, parametric_v;
2372 
2373  // Loop over computational nodes in forward order
2374  casadi_int alg_counter = 0;
2375  for (auto it=ff->algorithm_.begin(); it!=ff->algorithm_.end(); ++it, ++alg_counter) {
2376  if (it->op == OP_INPUT) {
2377  w[it->res.front()] = arg_split.at(it->data->segment());
2378  expr_status[it->res.front()] = 1;
2379  } else if (it->op==OP_OUTPUT) {
2380  MX arg = w[it->arg.front()];
2381  if (expr_status[it->arg.front()]==1) {
2382  arg = register_symbol(arg, symbol_map, symbol_v, parametric_v,
2383  extract_trivial, v_offset, v_prefix, v_suffix);
2384  }
2385  // Collect the results
2386  res_split.at(it->data->segment()) = arg;
2387  } else if (it->op==OP_CONST) {
2388  // Fetch constant
2389  w[it->res.front()] = it->data;
2390  expr_status[it->res.front()] = 0;
2391  } else if (it->op==OP_PARAMETER) {
2392  // Free variables
2393  w[it->res.front()] = it->data;
2394  expr_status[it->res.front()] = 2;
2395  } else {
2396  // Arguments of the operation
2397  arg1.resize(it->arg.size());
2398  for (casadi_int i=0; i<arg1.size(); ++i) {
2399  casadi_int el = it->arg[i]; // index of the argument
2400  arg1[i] = el<0 ? MX(it->data->dep(i).size()) : w[el];
2401  }
2402 
2403  // Check worst case status of inputs
2404  char max_status = 0;
2405  for (casadi_int i=0; i<arg1.size(); ++i) {
2406  casadi_int el = it->arg[i]; // index of the argument
2407  if (el>=0) {
2408  max_status = std::max(max_status, expr_status[it->arg[i]]);
2409  }
2410  }
2411  bool any_tainted = max_status==2;
2412 
2413  if (any_tainted) {
2414  // Loop over all inputs
2415  for (casadi_int i=0; i<arg1.size(); ++i) {
2416  casadi_int el = it->arg[i]; // index of the argument
2417 
2418  // For each parametric input being mixed into a non-parametric expression
2419  if (el>=0 && expr_status[el]==1) {
2420 
2421  arg1[i] = register_symbol(w[el], symbol_map, symbol_v, parametric_v,
2422  extract_trivial, v_offset, v_prefix, v_suffix);
2423  }
2424  }
2425  }
2426 
2427  // Perform the operation
2428  res1.resize(it->res.size());
2429  it->data->eval_mx(arg1, res1);
2430 
2431  // Get the result
2432  for (casadi_int i=0; i<res1.size(); ++i) {
2433  casadi_int el = it->res[i]; // index of the output
2434  if (el>=0) {
2435  w[el] = res1[i];
2436  // Update expression status
2437  expr_status[el] = max_status;
2438  }
2439  }
2440  }
2441  }
2442 
2443  // Join split outputs
2444  expr_ret = expr.join_primitives(res_split);
2445 
2446  symbols = symbol_v;
2447  parametric = parametric_v;
2448  }
MX register_symbol(const MX &node, std::map< MXNode *, MX > &symbol_map, std::vector< MX > &symbol_v, std::vector< MX > &parametric_v, bool extract_trivial, casadi_int v_offset, const std::string &v_prefix, const std::string &v_suffix)
Definition: mx.cpp:2294

References join_primitives(), MX(), n_primitives(), op(), casadi::OP_CONST, casadi::OP_INPUT, casadi::OP_OUTPUT, casadi::OP_PARAMETER, casadi::register_symbol(), and split_primitives().

◆ eye()

MX casadi::MX::eye ( casadi_int  n)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r4

Definition at line 580 of file mx.cpp.

580  {
581  return MX(DM::eye(n));
582  }
static Matrix< double > eye(casadi_int n)
create an n-by-n identity matrix

References casadi::Matrix< double >::eye(), and MX().

Referenced by casadi::OptiNode::canon_expr(), and inv().

◆ find()

MX casadi::MX::find ( const MX x)
static

Functions called by friend functions defined for this class

Definition at line 2108 of file mx.cpp.

2108  {
2109  return x->get_find();
2110  }

References casadi::MXNode::get_find().

Referenced by casadi::DaeBuilderInternal::create(), graph_substitute(), casadi::DaeBuilderInternal::hess_v_v_from_calls(), casadi::DaeBuilderInternal::jac_vdef_v_from_calls(), and casadi::register_symbol().

◆ forward()

std::vector< std::vector< MX > > casadi::MX::forward ( const std::vector< MX > &  ex,
const std::vector< MX > &  arg,
const std::vector< std::vector< MX > > &  v,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1851 of file mx.cpp.

1853  {
1854  try {
1855  // Read options
1856  bool always_inline = true;
1857  bool never_inline = false;
1858 
1859  Dict h_opts;
1860  Dict opts_remainder = extract_from_dict(opts, "helper_options", h_opts);
1861  h_opts["allow_free"] = true;
1862  for (auto&& op : opts_remainder) {
1863  if (op.first=="always_inline") {
1864  always_inline = op.second;
1865  } else if (op.first=="never_inline") {
1866  never_inline = op.second;
1867  } else {
1868  casadi_error("No such option: " + std::string(op.first));
1869  }
1870  }
1871  // Call internal function on a temporary object
1872  Function temp("forward_temp", arg, ex, h_opts);
1873  std::vector<std::vector<MX> > ret;
1874  temp->call_forward(arg, ex, v, ret, always_inline, never_inline);
1875  return ret;
1876  } catch (std::exception& e) {
1877  CASADI_THROW_ERROR("forward", e.what());
1878  }
1879  }
Dict extract_from_dict(const Dict &d, const std::string &key, T &value)

References casadi::FunctionInternal::call_forward(), casadi::extract_from_dict(), and op().

◆ get() [1/16]

MXNode * casadi::MX::get ( ) const

◆ get() [2/16]

void casadi::MX::get ( MX m,
bool  ind1,
casadi_int  rr,
casadi_int  cc 
) const
inline

Get a submatrix, two arguments

Definition at line 498 of file mx.hpp.

499  {
500  get(m, ind1, Matrix<casadi_int>(rr), Matrix<casadi_int>(cc));
501  }
MXNode * get() const
Get a const pointer to the node.
Definition: mx.cpp:544
template class CASADI_EXPORT Matrix< casadi_int >

◆ get() [3/16]

void casadi::MX::get ( MX m,
bool  ind1,
casadi_int  rr,
const Slice cc 
) const
inline

Get a submatrix, two arguments

Definition at line 493 of file mx.hpp.

493  {
494  get(m, ind1, Matrix<casadi_int>(rr), cc);
495  }

◆ get() [4/16]

void casadi::MX::get ( MX m,
bool  ind1,
const casadi_int  rr 
) const
inline

Get a submatrix, single argument

Definition at line 480 of file mx.hpp.

480  {
481  get(m, ind1, Matrix<casadi_int>(rr));
482  }

◆ get() [5/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Matrix< casadi_int > &  rr 
) const

Get a submatrix, single argument

Definition at line 182 of file mx.cpp.

182  {
183  // If the indexed matrix is dense, use nonzero indexing
184  if (is_dense()) {
185  return get_nz(m, ind1, rr);
186  }
187 
188  // If indexed matrix was a row/column vector, make sure that the result is too
189  bool tr = (is_column() && rr.is_row()) || (is_row() && rr.is_column());
190 
191  // Get the sparsity pattern - does bounds checking
192  std::vector<casadi_int> mapping;
193  Sparsity sp = sparsity().sub(rr.nonzeros(), tr ? rr.sparsity().T() : rr.sparsity(),
194  mapping, ind1);
195 
196  // Create return MX
197  m = (*this)->get_nzref(sp, mapping);
198  }
bool is_dense() const
Check if the matrix expression is dense.
bool is_column() const
Check if the matrix is a column vector (i.e. size2()==1)
bool is_row() const
Check if the matrix is a row vector (i.e. size1()==1)
void get_nz(MX &m, bool ind1, const Slice &kk) const
Definition: mx.cpp:390
std::vector< Scalar > & nonzeros()
Sparsity sub(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, std::vector< casadi_int > &mapping, bool ind1=false) const
Get a submatrix.
Definition: sparsity.cpp:334
Sparsity T() const
Transpose the matrix.
Definition: sparsity.cpp:394

References get_nz(), casadi::MXNode::get_nzref(), casadi::GenericMatrix< MatType >::is_column(), casadi::GenericMatrix< MX >::is_column(), casadi::GenericMatrix< MX >::is_dense(), casadi::GenericMatrix< MX >::is_row(), casadi::GenericMatrix< MatType >::is_row(), mapping(), casadi::Matrix< Scalar >::nonzeros(), casadi::Matrix< Scalar >::sparsity(), sparsity(), casadi::Sparsity::sub(), and casadi::Sparsity::T().

◆ get() [6/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Matrix< casadi_int > &  rr,
const Matrix< casadi_int > &  cc 
) const

Get a submatrix, two arguments

Definition at line 162 of file mx.cpp.

162  {
163  // Make sure dense vectors
164  casadi_assert(rr.is_dense() && rr.is_vector(),
165  "Marix::get: First index must be a dense vector");
166  casadi_assert(cc.is_dense() && cc.is_vector(),
167  "Marix::get: Second index must be a dense vector");
168 
169  // Get the sparsity pattern - does bounds checking
170  std::vector<casadi_int> mapping;
171  Sparsity sp = sparsity().sub(rr.nonzeros(), cc.nonzeros(), mapping, ind1);
172 
173  // Create return MX
174  m = (*this)->get_nzref(sp, mapping);
175  }
bool is_vector() const
Check if the matrix is a row or column vector.

References casadi::MXNode::get_nzref(), casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_vector(), mapping(), casadi::Matrix< Scalar >::nonzeros(), sparsity(), and casadi::Sparsity::sub().

◆ get() [7/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Matrix< casadi_int > &  rr,
const Slice cc 
) const

Get a submatrix, two arguments

Definition at line 157 of file mx.cpp.

157  {
158  // Fall back on (IM, IM)
159  get(m, ind1, rr, cc.all(size2(), ind1));
160  }

References casadi::Slice::all(), get(), and casadi::GenericMatrix< MX >::size2().

◆ get() [8/16]

void casadi::MX::get ( MX m,
bool  ind1,
const MX rr 
) const

Get a submatrix, single argument

Definition at line 208 of file mx.cpp.

208  {
209  casadi_assert(is_dense(), "Parametric slicing only supported for dense matrices."
210  "Got " + dim(true) + " instead.");
211  get_nz(m, ind1, rr);
212  }

References casadi::GenericMatrix< MX >::dim(), get_nz(), and casadi::GenericMatrix< MX >::is_dense().

◆ get() [9/16]

void casadi::MX::get ( MX m,
bool  ind1,
const MX rr,
const MX cc 
) const

Get a submatrix, two arguments

Definition at line 224 of file mx.cpp.

224  {
225  casadi_assert(is_dense(), "Parametric slicing only supported for dense matrices.");
226  m = (*this)->get_nz_ref(ind1 ? rr-1 : rr, floor(ind1 ? cc-1 : cc)*size1());
227  }
casadi_int size1() const
Get the first dimension (i.e. number of rows)
static MX floor(const MX &x)
Round down to nearest integer: x -> floor(x)

References casadi::GenericExpression< MX >::floor(), casadi::MXNode::get_nz_ref(), casadi::GenericMatrix< MX >::is_dense(), and casadi::GenericMatrix< MX >::size1().

◆ get() [10/16]

void casadi::MX::get ( MX m,
bool  ind1,
const MX rr,
const Slice cc 
) const

Get a submatrix, two arguments

Definition at line 219 of file mx.cpp.

219  {
220  casadi_assert(is_dense(), "Parametric slicing only supported for dense matrices.");
221  m = (*this)->get_nz_ref(ind1 ? rr-1 : rr, cc.apply(size2())*size1());
222  }

References casadi::Slice::apply(), casadi::MXNode::get_nz_ref(), casadi::GenericMatrix< MX >::is_dense(), casadi::GenericMatrix< MX >::size1(), and casadi::GenericMatrix< MX >::size2().

◆ get() [11/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Slice rr 
) const

Get a submatrix, single argument

Definition at line 177 of file mx.cpp.

177  {
178  // Fall back on IM
179  get(m, ind1, rr.all(numel(), ind1));
180  }
casadi_int numel() const
Get the number of elements.

References casadi::Slice::all(), get(), and casadi::GenericMatrix< MX >::numel().

◆ get() [12/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Slice rr,
casadi_int  cc 
) const
inline

Get a submatrix, two arguments

Definition at line 489 of file mx.hpp.

489  {
490  get(m, ind1, rr, Matrix<casadi_int>(cc));
491  }

◆ get() [13/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Slice rr,
const Matrix< casadi_int > &  cc 
) const

Get a submatrix, two arguments

Definition at line 152 of file mx.cpp.

152  {
153  // Fall back on (IM, IM)
154  get(m, ind1, rr.all(size1(), ind1), cc);
155  }

References casadi::Slice::all(), get(), and casadi::GenericMatrix< MX >::size1().

◆ get() [14/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Slice rr,
const MX cc 
) const

Get a submatrix, two arguments

Definition at line 214 of file mx.cpp.

214  {
215  casadi_assert(is_dense(), "Parametric slicing only supported for dense matrices. ");
216  m = (*this)->get_nz_ref(rr.apply(size1()), floor(ind1 ? cc-1 : cc)*size1());
217  }

References casadi::Slice::apply(), casadi::GenericExpression< MX >::floor(), casadi::MXNode::get_nz_ref(), casadi::GenericMatrix< MX >::is_dense(), and casadi::GenericMatrix< MX >::size1().

◆ get() [15/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Slice rr,
const Slice cc 
) const

Get a submatrix, two arguments

Definition at line 147 of file mx.cpp.

147  {
148  // Fall back on (IM, IM)
149  return get(m, ind1, rr.all(size1(), ind1), cc.all(size2(), ind1));
150  }

References casadi::Slice::all(), get(), casadi::GenericMatrix< MX >::size1(), and casadi::GenericMatrix< MX >::size2().

◆ get() [16/16]

void casadi::MX::get ( MX m,
bool  ind1,
const Sparsity sp 
) const

Get a submatrix, single argument

Definition at line 200 of file mx.cpp.

200  {
201  casadi_assert(size()==sp.size(),
202  "get(Sparsity sp): shape mismatch. This matrix has shape "
203  + str(size()) + ", but supplied sparsity index has shape "
204  + str(sp.size()) + ".");
205  m = project(*this, sp);
206  }
std::string str(const T &v)
String representation, any type.

References project(), casadi::GenericMatrix< MX >::size(), casadi::Sparsity::size(), and casadi::str().

◆ get_colind()

std::vector<casadi_int> casadi::GenericMatrix< MX >::get_colind ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b8

Definition at line 195 of file generic_matrix.hpp.

195 { return sparsity().get_colind(); }
std::vector< casadi_int > get_colind() const
Get the column index for each column.
Definition: sparsity.cpp:364

◆ get_free()

std::vector< MX > casadi::MX::get_free ( const Function f)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_rm

Definition at line 2719 of file mx.cpp.

2719  {
2720  return f.free_mx();
2721  }

References casadi::Function::free_mx().

◆ get_input()

std::vector< MX > casadi::MX::get_input ( const Function f)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_rl

Definition at line 2715 of file mx.cpp.

2715  {
2716  return f.mx_in();
2717  }

References casadi::Function::mx_in().

◆ get_max_depth()

casadi_int casadi::MX::get_max_depth ( )
static

Extra doc: https://github.com/casadi/casadi/wiki/L_rk

Definition at line 913 of file mx.cpp.

913  {
914  return eq_depth_;
915  }

Referenced by casadi::MXNode::maxDepth().

◆ get_nonzeros()

std::vector< MX > casadi::MX::get_nonzeros ( ) const

Since MX is not a containter, the scalar MXes may be complex When the expression satisfies is_valid_input, the results may be simple

For example: vertcat(x,y).nonzeros() will return {x,y}

See also
expr.nz[:]

Extra doc: https://github.com/casadi/casadi/wiki/L_2bh

Definition at line 610 of file mx.cpp.

610  {
611  std::vector<MX> ret;
612  std::vector<MX> p = primitives();
613  for (const MX& e : p) {
614  if (e.is_scalar()) {
615  ret.push_back(e);
616  } else {
617  // Get nonzeros sparsity cast
618  MX nz;
619  e.get_nz(nz, 0, Slice());
620  for (casadi_int i=0; i<nz.nnz(); ++i) {
621  ret.push_back(nz(i));
622  }
623  }
624  }
625  return ret;
626  }
const MX nz(const K &k) const
Get vector nonzero or slice of nonzeros.
std::vector< MX > primitives() const
Get primitives.
Definition: mx.cpp:933

References get_nz(), casadi::GenericMatrix< MatType >::nnz(), casadi::GenericMatrix< MX >::nz(), and primitives().

◆ get_nz() [1/7]

void casadi::MX::get_nz ( MX m,
bool  ind1,
casadi_int  kk 
) const
inline

Get a set of nonzeros

Definition at line 527 of file mx.hpp.

527  {
528  get_nz(m, ind1, Matrix<casadi_int>(kk));
529  }

◆ get_nz() [2/7]

void casadi::MX::get_nz ( MX m,
bool  ind1,
const Matrix< casadi_int > &  kk 
) const

Get a set of nonzeros

Definition at line 395 of file mx.cpp.

395  {
396  // If indexed matrix was a row/column vector, make sure that the result is too
397  bool tr = (is_column() && kk.is_row()) || (is_row() && kk.is_column());
398 
399  // Quick return if no entries
400  if (kk.nnz()==0) {
401  m = MX::zeros(tr ? kk.sparsity().T() : kk.sparsity());
402  return;
403  }
404 
405  // Check bounds
406  casadi_int sz = nnz();
407  casadi_assert_in_range(kk.nonzeros(), -sz+ind1, sz+ind1);
408 
409  // Handle index-1, negative indices
410  if (ind1 || *std::min_element(kk->begin(), kk->end())<0) {
411  Matrix<casadi_int> kk_mod = kk;
412  for (auto&& i : kk_mod.nonzeros()) {
413  casadi_assert(!(ind1 && i<=0),
414  "Matlab is 1-based, but requested index " + str(i) + ". "
415  "Note that negative slices are disabled in the Matlab interface. "
416  "Possibly you may want to use 'end'.");
417  if (ind1) i--;
418  if (i<0) i += sz;
419  }
420  get_nz(m, false, kk_mod); // Call recursively
421  return;
422  }
423 
424  // Return reference to the nonzeros
425  m = (*this)->get_nzref(tr ? kk.sparsity().T() : kk.sparsity(), kk.nonzeros());
426  }

References get_nz(), casadi::MXNode::get_nzref(), casadi::GenericMatrix< MatType >::is_column(), casadi::GenericMatrix< MX >::is_column(), casadi::GenericMatrix< MX >::is_row(), casadi::GenericMatrix< MatType >::is_row(), casadi::GenericMatrix< MX >::nnz(), casadi::GenericMatrix< MatType >::nnz(), casadi::Matrix< Scalar >::nonzeros(), casadi::Matrix< Scalar >::sparsity(), casadi::str(), casadi::Sparsity::T(), and casadi::GenericMatrix< MX >::zeros().

◆ get_nz() [3/7]

void casadi::MX::get_nz ( MX m,
bool  ind1,
const MX inner,
const MX outer 
) const

Get a set of nonzeros

Definition at line 433 of file mx.cpp.

433  {
434  // Create return MX
435  m = (*this)->get_nz_ref(ind1 ? inner-1.0: inner, ind1 ? outer-1.0: outer);
436  }

References casadi::MXNode::get_nz_ref().

◆ get_nz() [4/7]

void casadi::MX::get_nz ( MX m,
bool  ind1,
const MX inner,
const Slice outer 
) const

Get a set of nonzeros

Definition at line 443 of file mx.cpp.

443  {
444  // Create return MX
445  m = (*this)->get_nz_ref(ind1 ? inner-1.0: inner, ind1 ? outer-1: outer);
446  }

References casadi::MXNode::get_nz_ref().

◆ get_nz() [5/7]

void casadi::MX::get_nz ( MX m,
bool  ind1,
const MX kk 
) const

Get a set of nonzeros

Definition at line 428 of file mx.cpp.

428  {
429  // Create return MX
430  m = (*this)->get_nz_ref(ind1 ? kk-1.0 : kk);
431  }

References casadi::MXNode::get_nz_ref().

◆ get_nz() [6/7]

void casadi::MX::get_nz ( MX m,
bool  ind1,
const Slice inner,
const MX outer 
) const

Get a set of nonzeros

Definition at line 438 of file mx.cpp.

438  {
439  // Create return MX
440  m = (*this)->get_nz_ref(ind1 ? inner-1: inner, ind1 ? outer-1.0: outer);
441  }

References casadi::MXNode::get_nz_ref().

◆ get_nz() [7/7]

void casadi::MX::get_nz ( MX m,
bool  ind1,
const Slice kk 
) const

Get a set of nonzeros

Definition at line 390 of file mx.cpp.

390  {
391  // Fallback on IM
392  get_nz(m, ind1, kk.all(nnz(), ind1));
393  }

References casadi::Slice::all(), and casadi::GenericMatrix< MX >::nnz().

Referenced by get(), casadi::get_boor(), get_nonzeros(), get_nz(), casadi::interpn_G(), and interpn_linear().

◆ get_output()

MX casadi::MX::get_output ( casadi_int  oind) const

Extra doc: https://github.com/casadi/casadi/wiki/L_ql

Definition at line 873 of file mx.cpp.

873  {
874  return (*this)->get_output(oind);
875  }

References casadi::MXNode::get_output().

Referenced by createMultipleOutput(), cse(), and graph_substitute().

◆ get_row()

std::vector<casadi_int> casadi::GenericMatrix< MX >::get_row ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b8

Definition at line 194 of file generic_matrix.hpp.

194 { return sparsity().get_row(); }
std::vector< casadi_int > get_row() const
Get the row for each non-zero entry.
Definition: sparsity.cpp:372

◆ get_sparsity()

Sparsity casadi::MX::get_sparsity ( ) const
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_qd

Definition at line 205 of file mx.hpp.

205 { return sparsity();}

◆ get_str()

std::string casadi::SharedObject::get_str ( bool  more = false) const
inlineinherited

Definition at line 91 of file shared_object.hpp.

91  {
92  std::stringstream ss;
93  disp(ss, more);
94  return ss.str();
95  }
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.

◆ get_temp()

casadi_int casadi::MX::get_temp ( ) const

Get the temporary variable

Definition at line 861 of file mx.cpp.

861  {
862  return (*this)->temp;
863  }

◆ getCount()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::getCount
inherited

Definition at line 103 of file generic_shared_impl.hpp.

127  {
128  return (*this)->getCount();
129  }

◆ gradient()

static MX casadi::GenericMatrix< MX >::gradient ( const MX ex,
const MX arg,
const Dict opts = Dict() 
)
staticinherited

Functions called by friend functions defined here

◆ graph_substitute() [1/4]

MX casadi::MX::graph_substitute ( const MX x,
const std::vector< MX > &  v,
const std::vector< MX > &  vdef 
)
static

Functions called by friend functions defined for this class

Definition at line 1450 of file mx.cpp.

1451  {
1452  return graph_substitute(std::vector<MX>{x}, v, vdef).at(0);
1453  }

Referenced by cse(), graph_substitute(), and matrix_expand().

◆ graph_substitute() [2/4]

MX casadi::MX::graph_substitute ( const MX x,
const std::vector< MX > &  v,
const std::vector< MX > &  vdef,
bool &  updated 
)
static

Functions called by friend functions defined for this class

Definition at line 1455 of file mx.cpp.

1456  {
1457  return graph_substitute(std::vector<MX>{x}, v, vdef, updated).at(0);
1458  }

References graph_substitute().

◆ graph_substitute() [3/4]

std::vector< MX > casadi::MX::graph_substitute ( const std::vector< MX > &  ex,
const std::vector< MX > &  v,
const std::vector< MX > &  vdef 
)
static

Functions called by friend functions defined for this class

Definition at line 1460 of file mx.cpp.

1462  {
1463  bool updated;
1464  return graph_substitute(ex, v, vdef, updated);
1465  }

References graph_substitute().

◆ graph_substitute() [4/4]

std::vector< MX > casadi::MX::graph_substitute ( const std::vector< MX > &  ex,
const std::vector< MX > &  v,
const std::vector< MX > &  vdef,
bool &  updated 
)
static

Functions called by friend functions defined for this class

Definition at line 1466 of file mx.cpp.

1469  {
1470  casadi_assert(v.size()==vdef.size(),
1471  "Mismatch in the number of expression to substitute: "
1472  + str(v.size()) + " <-> " + str(vdef.size()) + ".");
1473 
1474  updated = false;
1475 
1476  // Quick return if all equal
1477  bool all_equal = true;
1478  for (casadi_int k=0; k<v.size(); ++k) {
1479  if (v[k].size()!=vdef[k].size() || !is_equal(v[k], vdef[k])) {
1480  all_equal = false;
1481  break;
1482  }
1483  }
1484  if (all_equal) return ex;
1485 
1486  // Validate dimensions
1487  for (casadi_int i=0;i<v.size();++i) {
1488  casadi_assert(v[i].size()==vdef[i].size(),
1489  "Inconsistent shapes for i = " + str(i) + ": v[i] " + v[i].dim() +
1490  " <-> vdef[i] " + vdef[i].dim());
1491  }
1492 
1493  // Sort the expression
1494  Dict opts({{"max_io", 0}, {"allow_free", true}});
1495  Function f("tmp_graph_substitute", std::vector<MX>{}, ex, opts);
1496  MXFunction *ff = f.get<MXFunction>();
1497 
1498  // Get references to the internal data structures
1499  const std::vector<MXAlgEl>& algorithm = ff->algorithm_;
1500  std::vector<MX> swork(ff->workloc_.size()-1);
1501 
1502  // A boolean vector indicated whoch nodes are tainted by substitutions
1503  std::vector<bool> tainted(swork.size());
1504 
1505  // Temporary std::stringstream
1506  std::stringstream ss;
1507 
1508  // Construct lookup table for expressions,
1509  // giving priority to first occurances
1510  std::map<const MXNode*, casadi_int> expr_lookup;
1511  for (casadi_int i=0;i<v.size();++i) {
1512  auto it = expr_lookup.find(v[i].operator->());
1513  if (it==expr_lookup.end()) expr_lookup[v[i].operator->()] = i;
1514  }
1515 
1516  // Construct found map
1517  std::vector<bool> expr_found(v.size(), false);
1518 
1519  // Allocate output vector
1520  std::vector<MX> f_out(f.n_out());
1521  std::vector<MX> oarg, ores;
1522 
1523  // expr_lookup iterator
1524  std::map<const MXNode*, casadi_int>::const_iterator it_lookup;
1525 
1526  // Allocate storage for split outputs
1527  std::vector<std::vector<MX>> out_split(ex.size());
1528  for (casadi_int i = 0; i < out_split.size(); ++i) out_split[i].resize(ex[i].n_primitives());
1529 
1530  for (auto it=algorithm.begin(); it!=algorithm.end(); ++it) {
1531 
1532  if (it->op != OP_OUTPUT) {
1533  // Check if it->data points to a supplied expr
1534  it_lookup = expr_lookup.find((it->data).operator->());
1535 
1536  if (it_lookup!=expr_lookup.end()) {
1537  // Fill in that expression in-place
1538  MX e = vdef[it_lookup->second];
1539 
1540  // If node is of a MultipleOutput type
1541  if (e->has_output()) {
1542  for (casadi_int i=0;i<it->res.size();++i) {
1543  casadi_int k = it->res[i];
1544  if (k!=-1) {
1545  swork[k] = e.get_output(i);
1546  tainted[k] = true;
1547  }
1548  }
1549  } else {
1550  swork[it->res.front()] = e;
1551  tainted[it->res.front()] = true;
1552  }
1553  expr_found[it_lookup->second] = true;
1554  continue;
1555  } else if (it->data->has_output()) {
1556  bool any_tainted = false;
1557  // Loop over all oputputs of MultiOutput
1558  for (casadi_int i=0;i<it->res.size();++i) {
1559  // Create Output node (cached)
1560  casadi_int k = it->res[i];
1561  if (k!=-1) {
1562  MX out = it->data.get_output(i);
1563  // Check if out points to a supplied expr
1564  it_lookup = expr_lookup.find(out.operator->());
1565  if (it_lookup!=expr_lookup.end()) {
1566  // Fill in that expression in-place
1567  MX e = vdef[it_lookup->second];
1568  swork[k] = e;
1569  tainted[k] = true;
1570  any_tainted = true;
1571  expr_found[it_lookup->second] = true;
1572  }
1573  }
1574  }
1575  if (any_tainted) continue;
1576  }
1577  }
1578 
1579  switch (it->op) {
1580  case OP_INPUT:
1581  tainted[it->res.front()] = false;
1582  break;
1583  case OP_PARAMETER:
1584  swork[it->res.front()] = it->data;
1585  tainted[it->res.front()] = false;
1586  break;
1587  case OP_OUTPUT:
1588  out_split.at(it->data->ind()).at(it->data->segment()) = swork[it->arg.front()];
1589  break;
1590  default:
1591  {
1592  bool node_tainted = false;
1593 
1594  // Arguments of the operation
1595  oarg.resize(it->arg.size());
1596  for (casadi_int i=0; i<oarg.size(); ++i) {
1597  casadi_int el = it->arg[i];
1598  if (el>=0) node_tainted = node_tainted || tainted[el];
1599  oarg[i] = el<0 ? MX(it->data->dep(i).size()) : swork.at(el);
1600  }
1601 
1602  // Perform the operation
1603  ores.resize(it->res.size());
1604  if (!node_tainted) {
1605  if (it->data.has_output()) {
1606  for (casadi_int i=0;i<it->res.size();++i) {
1607  ores.at(i) = it->data.get_output(i);
1608  }
1609  } else {
1610  ores.at(0) = it->data;
1611  }
1612  } else {
1613  it->data->eval_mx(oarg, ores);
1614  }
1615 
1616  // Get the result
1617  for (casadi_int i=0; i<ores.size(); ++i) {
1618  casadi_int el = it->res[i];
1619  if (el>=0) swork.at(el) = ores[i];
1620  if (el>=0) tainted[el] = node_tainted;
1621  }
1622  }
1623  }
1624  }
1625 
1626  // Join primitives
1627  for (size_t k = 0; k < out_split.size(); ++k) {
1628  f_out[k] = ex[k].join_primitives(out_split.at(k));
1629  }
1630 
1631  bool all_found=true;
1632  for (casadi_int i=0;i<v.size();++i) {
1633  all_found = all_found && expr_found[i];
1634  }
1635 
1636  updated = any(expr_found);
1637 
1638  return f_out;
1639 
1640  }
static bool is_equal(const MX &x, const MX &y, casadi_int depth=0)
Definition: mx.cpp:838
bool any(const std::vector< bool > &v)
Check if any arguments are true.
Definition: casadi_misc.cpp:84

References casadi::MXFunction::algorithm_, casadi::any(), casadi::GenericMatrix< MX >::dim(), find(), get_output(), casadi::MXNode::get_output(), casadi::MXNode::has_output(), is_equal(), MX(), n_primitives(), casadi::OP_INPUT, casadi::OP_OUTPUT, casadi::OP_PARAMETER, casadi::GenericMatrix< MX >::size(), and casadi::str().

◆ has_duplicates()

bool casadi::MX::has_duplicates ( ) const

If there are symbolic primitives appearing more than once, the function will return true and the names of the duplicate expressions will be passed to casadi_warning. Note: Will mark the node using MX::set_temp. Make sure to call reset_input() after usage.

Extra doc: https://github.com/casadi/casadi/wiki/L_qs

Definition at line 989 of file mx.cpp.

989  {
990  return (*this)->has_duplicates();
991  }

◆ has_output()

bool casadi::MX::has_output ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_284

Definition at line 786 of file mx.cpp.

786  {
787  return (*this)->has_output();
788  }

◆ hessian() [1/2]

MX casadi::MX::hessian ( const MX f,
const MX x,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1834 of file mx.cpp.

1834  {
1835  MX g;
1836  return hessian(f, x, g, opts);
1837  }
static MX hessian(const MX &f, const MX &x, const Dict &opts=Dict())
Definition: mx.cpp:1834

◆ hessian() [2/2]

MX casadi::MX::hessian ( const MX f,
const MX x,
MX g,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1839 of file mx.cpp.

1839  {
1840  try {
1841  Dict all_opts = opts;
1842  g = gradient(f, x, opts);
1843  if (!opts.count("symmetric")) all_opts["symmetric"] = true;
1844  return jacobian(g, x, all_opts);
1845  } catch (std::exception& e) {
1846  CASADI_THROW_ERROR("hessian", e.what());
1847  }
1848  }
static MX jacobian(const MX &f, const MX &x, const Dict &opts=Dict())
Definition: mx.cpp:1822
friend MX gradient(const MX &ex, const MX &arg, const Dict &opts=Dict())
Calculate the gradient of an expression.

References casadi::GenericMatrix< MX >::gradient, and jacobian().

◆ horzcat()

MX casadi::MX::horzcat ( const std::vector< MX > &  x)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1053 of file mx.cpp.

1053  {
1054  // Check dimensions
1055  if (x.size()>1) {
1056  std::vector<MX> ne = trim_empty(x, true);
1057  for (casadi_int i=0;i<ne.size();i++) {
1058  casadi_assert(ne[i].size1()==ne[0].size1(),
1059  "horzcat dimension mismatch x[" + str(i) + "]:" + ne[i].dim() +
1060  " and x[0]: " + ne[0].dim() + ".");
1061  }
1062  }
1063 
1064  if (x.empty()) {
1065  return MX(1, 0);
1066  } else if (x.size()==1) {
1067  return x.front();
1068  } else if (has_empty(x)) {
1069  std::vector<MX> ret = trim_empty(x);
1070  if (ret.empty()) {
1071  // We still want horzcat(zeros(0,5),zeros(0,5)) -> zeros(0,10)
1072  ret = trim_empty(x, true);
1073  casadi_int s = 0;
1074  casadi_int nrow = 0;
1075  for (casadi_int i=0;i<ret.size();++i) {
1076  s+= ret[i].size2();
1077  casadi_assert_dev(nrow==0 || nrow==ret[i].size1());
1078  nrow = ret[i].size1();
1079  }
1080  return MX::zeros(nrow, s);
1081  } else {
1082  return horzcat(ret);
1083  }
1084  } else {
1085  return x.front()->get_horzcat(x);
1086  }
1087  }
static MX ne(const MX &x, const MX &y)
Logical not equal to: (x,y) -> x != y.

References casadi::GenericMatrix< MX >::dim(), casadi::has_empty(), MX(), casadi::GenericExpression< MX >::ne(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MX >::size1(), casadi::str(), casadi::trim_empty(), and casadi::GenericMatrix< MX >::zeros().

Referenced by blockcat(), and vertcat().

◆ horzsplit()

std::vector< MX > casadi::MX::horzsplit ( const MX x,
const std::vector< casadi_int > &  offset 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1140 of file mx.cpp.

1140  {
1141  // Consistency check
1142  casadi_assert_dev(!offset.empty());
1143  casadi_assert_dev(offset.front()==0);
1144  casadi_assert_dev(offset.back()==x.size2());
1145  casadi_assert_dev(is_monotone(offset));
1146 
1147  // Trivial return if possible
1148  if (offset.size()==1) {
1149  return std::vector<MX>(0);
1150  } else if (offset.size()==2) {
1151  return std::vector<MX>(1, x);
1152  } else {
1153  return x->get_horzsplit(offset);
1154  }
1155  }
static std::vector< casadi_int > offset(const std::vector< MatType > &v, bool vert=true)

References casadi::MXNode::get_horzsplit(), casadi::is_monotone(), casadi::SparsityInterface< MatType >::offset(), and casadi::GenericMatrix< MatType >::size2().

Referenced by vertsplit().

◆ if_else()

MX casadi::MX::if_else ( const MX cond,
const MX if_true,
const MX if_false,
bool  short_circuit = false 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1272 of file mx.cpp.

1272  {
1273  if (short_circuit) {
1274  // Get symbolic primitives
1275  std::vector<MX> arg = symvar(veccat(std::vector<MX>{x_true, x_false}));
1276 
1277  // Form functions for cases
1278  Function f_true("f_true", arg, {x_true});
1279  Function f_false("f_false", arg, {x_false});
1280 
1281  // Form Switch
1282  Function sw = Function::if_else("switch", f_true, f_false);
1283 
1284  // Call the Switch
1285  std::vector<MX> sw_arg;
1286  sw_arg.push_back(cond);
1287  sw_arg.insert(sw_arg.end(), arg.begin(), arg.end());
1288  return sw(sw_arg).at(0);
1289  } else {
1290  return if_else_zero(cond, x_true) + if_else_zero(!cond, x_false);
1291  }
1292  }
static Function if_else(const std::string &name, const Function &f_true, const Function &f_false, const Dict &opts=Dict())
Constructor (if-else)
Definition: function.cpp:810
static MX if_else_zero(const MX &x, const MX &y)
Conditional assignment: (x,y) -> x ? y : 0.

References casadi::Function::if_else(), casadi::GenericExpression< MX >::if_else_zero(), symvar(), and casadi::SparsityInterface< MatType >::veccat().

Referenced by conditional().

◆ inf() [1/3]

MX casadi::MX::inf ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r2

Definition at line 556 of file mx.cpp.

556  {
557  return inf(Sparsity::dense(nrow, ncol));
558  }
static MX inf(const Sparsity &sp)
create a matrix with all inf
Definition: mx.cpp:564

References casadi::Sparsity::dense(), and inf().

◆ inf() [2/3]

MX casadi::MX::inf ( const Sparsity sp)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r2

Definition at line 564 of file mx.cpp.

564  {
565  return create(ConstantMX::create(sp, std::numeric_limits<double>::infinity()));
566  }

References casadi::ConstantMX::create(), and create().

Referenced by inf(), and casadi::init_gen().

◆ inf() [3/3]

MX casadi::MX::inf ( const std::pair< casadi_int, casadi_int > &  rc)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r2

Definition at line 560 of file mx.cpp.

560  {
561  return inf(rc.first, rc.second);
562  }

References inf().

◆ info()

Dict casadi::MX::info ( ) const

Obtain information about node

Definition at line 826 of file mx.cpp.

826  {
827  return (*this)->info();
828  }

Referenced by casadi::MXFunction::export_code_body().

◆ interp1d()

MX casadi::GenericMatrix< MX >::interp1d ( const std::vector< double > &  x,
const MX v,
const std::vector< double > &  xq,
const std::string &  mode,
bool  equidistant 
)
staticinherited

Definition at line 213 of file generic_matrix.hpp.

1432  {
1433 
1434  bool mode_floor = false;
1435  bool mode_ceil = false;
1436  if (mode=="floor") {
1437  mode_floor = true;
1438  } else if (mode=="ceil") {
1439  mode_ceil = true;
1440  } else if (mode=="linear") {
1441  //
1442  } else {
1443  casadi_error("interp1d(x, v, xq, mode): "
1444  "Mode must be 'floor', 'ceil' or 'linear'. Got '" + mode + "' instead.");
1445  }
1446 
1447  casadi_assert(is_increasing(x), "interp1d(x, v, xq): x must be increasing.");
1448 
1449  casadi_assert(x.size()==v.size1(),
1450  "interp1d(x, v, xq): dimensions mismatch. v expected to have " + str(x.size()) + " rows,"
1451  " but got " + str(v.size1()) + " instead.");
1452 
1453  // Need at least two elements
1454  casadi_assert(x.size()>=2, "interp1d(x, v, xq): x must be at least length 2.");
1455 
1456  // Vectors to compose a sparse matrix
1457  std::vector<double> val;
1458  std::vector<casadi_int> colind(1, 0);
1459  std::vector<casadi_int> row;
1460 
1461  // Number of nonzeros in to-be composed matrix
1462  casadi_int nnz = 0;
1463  for (casadi_int i=0;i<xq.size();++i) {
1464  // Obtain index corresponding to xq[i]
1465  double ind = index_interp1d(x, xq[i], equidistant);
1466 
1467  if (mode_floor) ind = floor(ind);
1468  if (mode_ceil) ind = ceil(ind);
1469 
1470  // Split into integer and fractional part
1471  double int_partd;
1472  double frac_part = modf(ind, &int_partd);
1473  casadi_int int_part = static_cast<casadi_int>(int_partd);
1474 
1475  if (frac_part==0) {
1476  // Create a single entry
1477  val.push_back(1);
1478  row.push_back(int_part);
1479  nnz+=1;
1480  colind.push_back(nnz);
1481  } else {
1482  // Create a double entry
1483  val.push_back(1-frac_part);
1484  val.push_back(frac_part);
1485  row.push_back(int_part);
1486  row.push_back(int_part+1);
1487  nnz+=2;
1488  colind.push_back(nnz);
1489  }
1490  }
1491 
1492  // Construct sparsity for composed matrix
1493  Sparsity sp(x.size(), xq.size() , colind, row);
1494 
1495  return MatType::mtimes(MatType(sp, val).T(), v);
1496 
1497  }
const casadi_int * colind() const
const casadi_int * row() const
double index_interp1d(const std::vector< double > &x, double xq, bool equidistant)
bool is_increasing(const std::vector< T > &v)
Check if the vector is strictly increasing.

◆ interpn_linear()

MX casadi::MX::interpn_linear ( const std::vector< MX > &  x,
const MX v,
const std::vector< MX > &  xq,
const Dict opts = Dict() 
)
static

Usually, you want to be using 'interpolant' instead.

Accepts lookup_mode option.

Extra doc: https://github.com/casadi/casadi/wiki/L_r6

Definition at line 2659 of file mx.cpp.

2660  {
2661 
2662  casadi_int n_dim = x.size();
2663  std::vector<std::string> lookup_mode(n_dim, "auto");
2664  for (auto&& op : opts) {
2665  if (op.first=="lookup_mode") {
2666  lookup_mode = op.second;
2667  } else {
2668  casadi_error("Unknown option '" + op.first + "'.");
2669  }
2670  }
2671 
2672  casadi_assert_dev(xq.size()==n_dim);
2673  casadi_assert_dev(v.is_vector());
2674 
2675  // Extract grid dimensions
2676  std::vector<casadi_int> x_dims;
2677  for (auto e : x) x_dims.push_back(e.numel());
2678 
2679  // Determine multipicity of output
2680  casadi_int n_out = v.numel()/product(x_dims);
2681  casadi_assert(n_out*product(x_dims)==v.numel(),
2682  "Dimension mismatch: coefficients (" + str(v.numel()) + ") should be "
2683  "an integer multiple of product-of-dimensions (" + str(product(x_dims)) + ").");
2684 
2685  // Dimension check xq
2686  casadi_int nq = xq[0].numel();
2687  for (auto e : xq) {
2688  casadi_assert_dev(e.is_vector() && e.numel()==nq);
2689  }
2690 
2691  // Compute stride vector
2692  std::vector<casadi_int> strides;
2693  strides.push_back(n_out);
2694  for (auto d : x_dims) strides.push_back(strides.back()*d);
2695 
2696  // Pre-compute lower index and normalized coordinate
2697  // (Allows for more sub-expression sharing)
2698  std::vector<MX> xis, Ls, Lps;
2699  for (casadi_int i=0;i<n_dim;++i) {
2700  MX L = low(x[i], xq[i], {{"lookup_mode", lookup_mode[i]}});
2701  MX Lp = L+1;
2702  MX xl, xu;
2703  x[i].get_nz(xl, false, L);
2704  x[i].get_nz(xu, false, Lp);
2705  xis.push_back((xq[i]-xl)/(xu-xl));
2706  Ls.push_back(L);
2707  Lps.push_back(Lp);
2708  }
2709 
2710  Slice I(0, n_out);
2711 
2712  return interpn_G(n_dim, v, xis, Ls, Lps, strides, I);
2713  }
casadi_int n_out() const
Number of outputs.
Definition: mx.cpp:869
static MX low(const MX &v, const MX &p, const Dict &options=Dict())
Definition: mx.cpp:2112
MX interpn_G(casadi_int i, const MX &v, const std::vector< MX > &xis, const std::vector< MX > &L, const std::vector< MX > &Lp, const std::vector< casadi_int > &strides, const Slice &I, const MX &offset=0)
Definition: mx.cpp:2629

References get_nz(), casadi::interpn_G(), casadi::GenericMatrix< MatType >::is_vector(), low(), n_out(), casadi::GenericMatrix< MatType >::numel(), op(), casadi::product(), casadi::GenericMatrix< MatType >::size(), and casadi::str().

Referenced by casadi::LinearInterpolant::do_inline().

◆ inv()

MX casadi::MX::inv ( const MX A,
const std::string &  lsolver = "qr",
const Dict dict = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1934 of file mx.cpp.

1934  {
1935  return solve(x, MX::eye(x.size1()), lsolver, dict);
1936  }
static MX eye(casadi_int n)
Identity matrix.
Definition: mx.cpp:580
static MX solve(const MX &a, const MX &b)
Definition: mx.cpp:2007

References eye(), casadi::GenericMatrix< MatType >::size1(), and solve().

◆ inv_minor()

MX casadi::MX::inv_minor ( const MX A)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1930 of file mx.cpp.

1930  {
1931  casadi_error("Not implemented");
1932  }

◆ inv_node()

MX casadi::MX::inv_node ( const MX A)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1926 of file mx.cpp.

1926  {
1927  return x->get_inv();
1928  }

References casadi::MXNode::get_inv().

◆ inv_skew()

MX casadi::GenericMatrix< MX >::inv_skew ( const MX a)
staticinherited

Definition at line 229 of file generic_matrix.hpp.

1511  {
1512  casadi_assert(a.size1()==3 && a.size2()==3,
1513  "inv_skew(a): Expecting 3-by-3 matrix, got " + a.dim() + ".");
1514 
1515  return 0.5*vertcat(std::vector<MatType>({a(2, 1)-a(1, 2), a(0, 2)-a(2, 0), a(1, 0)-a(0, 1)}));
1516  }

◆ is_binary()

bool casadi::MX::is_binary ( ) const

Definition at line 814 of file mx.cpp.

814  {
815  return (*this)->is_binary();
816  }

Referenced by casadi::MXFunction::export_code_body(), and is_commutative().

◆ is_call()

bool casadi::MX::is_call ( ) const

Definition at line 774 of file mx.cpp.

774  {
775  return (*this)->op()==OP_CALL;
776  }

References casadi::OP_CALL.

Referenced by casadi::BlazingSplineFunction::merge().

◆ is_column()

bool casadi::GenericMatrix< MX >::is_column ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b5

Definition at line 178 of file generic_matrix.hpp.

178 { return sparsity().is_column();}
bool is_column() const
Check if the pattern is a column vector (i.e. size2()==1)
Definition: sparsity.cpp:285

◆ is_commutative()

bool casadi::MX::is_commutative ( ) const

Definition at line 850 of file mx.cpp.

850  {
851  if (is_unary()) return true;
852  casadi_assert(is_binary() || is_unary(),
853  "MX::is_commutative: must be binary or unary operation");
854  return operation_checker<CommChecker>(op());
855  }
bool is_binary() const
Is binary operation.
Definition: mx.cpp:814
bool is_unary() const
Is unary operation.
Definition: mx.cpp:818

References is_binary(), is_unary(), and op().

◆ is_constant()

bool casadi::MX::is_constant ( ) const

◆ is_dense()

bool casadi::GenericMatrix< MX >::is_dense ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b0

Definition at line 153 of file generic_matrix.hpp.

153 { return sparsity().is_dense();}
bool is_dense() const
Is dense?
Definition: sparsity.cpp:273

◆ is_empty()

bool casadi::GenericMatrix< MX >::is_empty ( bool  both = false) const
inlineinherited

(or optionally both dimensions)

Extra doc: https://github.com/casadi/casadi/wiki/L_1az

Definition at line 148 of file generic_matrix.hpp.

148 { return sparsity().is_empty(both);}
bool is_empty(bool both=false) const
Check if the sparsity is empty.
Definition: sparsity.cpp:144

◆ is_equal()

bool casadi::MX::is_equal ( const MX x,
const MX y,
casadi_int  depth = 0 
)
static

Functions called by friend functions defined for GenericExpression

Definition at line 838 of file mx.cpp.

838  {
839  return MXNode::is_equal(x.get(), y.get(), depth);
840  }
static bool is_equal(const MXNode *x, const MXNode *y, casadi_int depth)
Check if two nodes are equivalent up to a given depth.
Definition: mx_node.cpp:1256

References get(), and casadi::MXNode::is_equal().

Referenced by casadi::BinaryMX< ScX, ScY >::_get_binary(), casadi::UnaryMX::_get_binary(), casadi::Call::common_cond(), casadi::MXNode::get_binary(), graph_substitute(), casadi::BinaryMX< ScX, ScY >::is_equal(), casadi::MXNode::sameOpAndDeps(), and substitute().

◆ is_eye()

bool casadi::MX::is_eye ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qu

Definition at line 997 of file mx.cpp.

997  {
998  return (*this)->is_eye();
999  }

Referenced by mac().

◆ is_linear()

bool casadi::GenericMatrix< MX >::is_linear ( const MX expr,
const MX var 
)
staticinherited

Definition at line 235 of file generic_matrix.hpp.

1743  {
1744  return !any(MatType::which_depends(expr, var, 2, true));
1745  }

◆ is_minus_one()

bool casadi::MX::is_minus_one ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qx

Definition at line 1013 of file mx.cpp.

1013  {
1014  return (*this)->is_value(-1);
1015  }

◆ is_multiplication()

bool casadi::MX::is_multiplication ( ) const

Definition at line 798 of file mx.cpp.

798  {
799  return (*this)->op()==OP_MTIMES;
800  }
@ OP_MTIMES
Definition: calculus.hpp:100

References casadi::OP_MTIMES.

◆ is_norm()

bool casadi::MX::is_norm ( ) const

Definition at line 802 of file mx.cpp.

802  {
803  return dynamic_cast<const Norm*>(get())!=nullptr;
804  }

References get().

◆ is_null()

Definition at line 117 of file generic_shared_impl.hpp.

109  {
110  return node==nullptr;
111  }

◆ is_one()

bool casadi::MX::is_one ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qw

Definition at line 1009 of file mx.cpp.

1009  {
1010  return (*this)->is_one();
1011  }

◆ is_op()

bool casadi::MX::is_op ( casadi_int  op) const

Definition at line 794 of file mx.cpp.

794  {
795  return (*this)->op()==op;
796  }

References op().

Referenced by casadi::OptiNode::canon_expr(), and casadi::DaeBuilderInternal::when().

◆ is_output()

bool casadi::MX::is_output ( ) const

Definition at line 782 of file mx.cpp.

782  {
783  return (*this)->is_output();
784  }

Referenced by casadi::DaeBuilderInternal::create(), cse(), and casadi::DaeBuilderInternal::jac_vdef_v_from_calls().

◆ is_quadratic()

bool casadi::GenericMatrix< MX >::is_quadratic ( const MX expr,
const MX var 
)
staticinherited

Definition at line 236 of file generic_matrix.hpp.

1748  {
1749  return is_linear(gradient(expr, var), var);
1750  }
static bool is_linear(const MX &expr, const MX &var)

◆ is_regular()

bool casadi::MX::is_regular ( ) const

Definition at line 1021 of file mx.cpp.

1021  {
1022  if (is_constant()) {
1023  return static_cast<DM>(*this).is_regular();
1024  } else {
1025  casadi_error("Cannot check regularity for symbolic MX");
1026  }
1027  }
bool is_constant() const
Check if constant.
Definition: mx.cpp:770
bool is_regular() const
Checks if expression does not contain NaN or Inf.

References is_constant(), and casadi::Matrix< Scalar >::is_regular().

◆ is_row()

bool casadi::GenericMatrix< MX >::is_row ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b4

Definition at line 173 of file generic_matrix.hpp.

173 { return sparsity().is_row();}
bool is_row() const
Check if the pattern is a row vector (i.e. size1()==1)
Definition: sparsity.cpp:281

◆ is_scalar()

bool casadi::GenericMatrix< MX >::is_scalar ( bool  scalar_and_dense = false) const
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b1

Definition at line 158 of file generic_matrix.hpp.

1348  {
1349  return sparsity().is_scalar(scalar_and_dense);
1350  }
bool is_scalar(bool scalar_and_dense=false) const
Is scalar?
Definition: sparsity.cpp:269

◆ is_square()

bool casadi::GenericMatrix< MX >::is_square ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b2

Definition at line 163 of file generic_matrix.hpp.

163 { return sparsity().is_square();}
bool is_square() const
Is square?
Definition: sparsity.cpp:293

◆ is_symbolic()

bool casadi::MX::is_symbolic ( ) const

◆ is_transpose()

bool casadi::MX::is_transpose ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qy

Definition at line 1017 of file mx.cpp.

1017  {
1018  return op()==OP_TRANSPOSE;
1019  }
@ OP_TRANSPOSE
Definition: calculus.hpp:106

References op(), and casadi::OP_TRANSPOSE.

◆ is_tril()

bool casadi::GenericMatrix< MX >::is_tril ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b7

Definition at line 188 of file generic_matrix.hpp.

188 { return sparsity().is_tril();}
bool is_tril(bool strictly=false) const
Is lower triangular?
Definition: sparsity.cpp:321

◆ is_triu()

bool casadi::GenericMatrix< MX >::is_triu ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b6

Definition at line 183 of file generic_matrix.hpp.

183 { return sparsity().is_triu();}
bool is_triu(bool strictly=false) const
Is upper triangular?
Definition: sparsity.cpp:325

◆ is_unary()

bool casadi::MX::is_unary ( ) const

Definition at line 818 of file mx.cpp.

818  {
819  return (*this)->is_unary();
820  }

Referenced by casadi::MXFunction::export_code_body(), and is_commutative().

◆ is_valid_input()

bool casadi::MX::is_valid_input ( ) const

Valid inputs for MXFunctions are combinations of Reshape, concatenations and SymbolicMX

Extra doc: https://github.com/casadi/casadi/wiki/L_qn

Definition at line 925 of file mx.cpp.

925  {
926  return (*this)->is_valid_input();
927  }

Referenced by casadi::DaeBuilderInternal::der(), casadi::OptiNode::set_domain(), and casadi::OptiNode::to_function().

◆ is_vector()

bool casadi::GenericMatrix< MX >::is_vector ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b3

Definition at line 168 of file generic_matrix.hpp.

168 { return sparsity().is_vector();}
bool is_vector() const
Check if the pattern is a row or column vector.
Definition: sparsity.cpp:289

◆ is_zero()

bool casadi::MX::is_zero ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qv

Definition at line 1001 of file mx.cpp.

1001  {
1002  if (nnz()==0) {
1003  return true;
1004  } else {
1005  return (*this)->is_zero();
1006  }
1007  }

References casadi::GenericMatrix< MX >::nnz().

Referenced by casadi::MXNode::_get_binary(), casadi::MXNode::get_dot(), casadi::MXNode::get_einstein(), casadi::MXNode::get_mac(), and mac().

◆ jacobian()

MX casadi::MX::jacobian ( const MX f,
const MX x,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1822 of file mx.cpp.

1822  {
1823  try {
1824  Dict h_opts;
1825  Dict opts_remainder = extract_from_dict(opts, "helper_options", h_opts);
1826  h_opts["allow_free"] = true;
1827  Function h("helper_jacobian_MX", {x}, {f}, h_opts);
1828  return h.get<MXFunction>()->jac(opts_remainder).at(0);
1829  } catch (std::exception& e) {
1830  CASADI_THROW_ERROR("jacobian", e.what());
1831  }
1832  }

References casadi::extract_from_dict(), and casadi::Function::get().

Referenced by hessian(), and casadi::Scpgen::init().

◆ jacobian_sparsity()

Sparsity casadi::MX::jacobian_sparsity ( const MX f,
const MX x 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1918 of file mx.cpp.

1918  {
1919  return _jacobian_sparsity(f, x);
1920  }
Sparsity _jacobian_sparsity(const MatType &expr, const MatType &var)

References casadi::_jacobian_sparsity().

◆ join_primitives() [1/3]

DM casadi::MX::join_primitives ( const std::vector< DM > &  v) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qr

Definition at line 981 of file mx.cpp.

981  {
982  casadi_assert(v.size()==n_primitives(), "Wrong number of primitives supplied");
983  std::vector<DM>::const_iterator it=v.begin();
984  DM ret = (*this)->join_primitives(it);
985  casadi_assert_dev(it==v.end());
986  return ret;
987  }

References n_primitives().

◆ join_primitives() [2/3]

MX casadi::MX::join_primitives ( const std::vector< MX > &  v) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qr

Definition at line 965 of file mx.cpp.

965  {
966  casadi_assert(v.size()==n_primitives(), "Wrong number of primitives supplied");
967  std::vector<MX>::const_iterator it=v.begin();
968  MX ret = (*this)->join_primitives(it);
969  casadi_assert_dev(it==v.end());
970  return ret;
971  }

References casadi::MXNode::join_primitives(), and n_primitives().

Referenced by cse(), casadi::DaeBuilderInternal::der(), extract_parametric(), and separate_linear().

◆ join_primitives() [3/3]

SX casadi::MX::join_primitives ( const std::vector< SX > &  v) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qr

Definition at line 973 of file mx.cpp.

973  {
974  casadi_assert(v.size()==n_primitives(), "Wrong number of primitives supplied");
975  std::vector<SX>::const_iterator it=v.begin();
976  SX ret = (*this)->join_primitives(it);
977  casadi_assert_dev(it==v.end());
978  return ret;
979  }
Matrix< SXElem > SX
Definition: sx_fwd.hpp:32

References n_primitives().

◆ jtimes()

static MX casadi::GenericMatrix< MX >::jtimes ( const MX ex,
const MX arg,
const MX v,
bool  tr = false,
const Dict opts = Dict() 
)
staticinherited

Functions called by friend functions defined here

◆ kron()

MX casadi::MX::kron ( const MX x,
const MX b 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1974 of file mx.cpp.

1974  {
1975  const Sparsity &a_sp = a.sparsity();
1976  MX filler(b.size());
1977  std::vector< std::vector< MX > > blocks(a.size1(), std::vector< MX >(a.size2(), filler));
1978  for (casadi_int i=0; i<a.size1(); ++i) {
1979  for (casadi_int j=0; j<a.size2(); ++j) {
1980  casadi_int k = a_sp.get_nz(i, j);
1981  if (k!=-1) {
1982  blocks[i][j] = a.nz(k)*b;
1983  }
1984  }
1985  }
1986  return blockcat(blocks);
1987  }
static MX blockcat(const std::vector< std::vector< MX > > &v)
Definition: mx.cpp:1197

References blockcat(), casadi::Sparsity::get_nz(), casadi::GenericMatrix< MatType >::nz(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), and sparsity().

◆ lift()

MX casadi::MX::lift ( const MX x,
const MX x_guess 
)
static

Functions called by friend functions defined for this class

Definition at line 734 of file mx.cpp.

734  {
735  casadi_assert_dev(x.sparsity()==x_guess.sparsity());
736  return x->_get_binary(OP_LIFT, x_guess, false, false);
737  }
@ OP_LIFT
Definition: calculus.hpp:191

References casadi::MXNode::_get_binary(), casadi::OP_LIFT, and sparsity().

◆ linear_coeff()

void casadi::GenericMatrix< MX >::linear_coeff ( const MX expr,
const MX var,
MX A,
MX b,
bool  check 
)
staticinherited

Definition at line 239 of file generic_matrix.hpp.

1765  {
1766  casadi_assert(expr.is_vector(), "'linear_coeff' only defined for vector expressions.");
1767  if (check)
1768  casadi_assert(is_linear(expr, var), "'linear_coeff' called on non-linear expression.");
1769  A = substitute(jacobian(expr, var), var, 0);
1770  b = vec(substitute(expr, var, 0));
1771  }
static MatType vec(const MatType &x)
friend MX substitute(const MX &ex, const MX &v, const MX &vdef)
Substitute variable v with expression vdef in an expression ex.
friend MX jacobian(const MX &ex, const MX &arg, const Dict &opts=Dict())
Calculate Jacobian.

◆ linearize()

static MX casadi::GenericMatrix< MX >::linearize ( const MX f,
const MX x,
const MX x0,
const Dict opts = Dict() 
)
staticinherited

Functions called by friend functions defined here

◆ linspace()

MX casadi::GenericMatrix< MX >::linspace ( const MX a,
const MX b,
casadi_int  nsteps 
)
staticinherited

Definition at line 226 of file generic_matrix.hpp.

1381  {
1382  std::vector<MatType> ret(nsteps);
1383  ret[0] = a;
1384  MatType step = (b-a)/static_cast<MatType>(nsteps-1);
1385 
1386  for (casadi_int i=1; i<nsteps-1; ++i)
1387  ret[i] = a + i * step;
1388 
1389  ret[nsteps-1] = b;
1390  return vertcat(ret);
1391  }

◆ logsumexp()

static MX casadi::GenericMatrix< MX >::logsumexp ( const MX x)
staticinherited

◆ low()

MX casadi::MX::low ( const MX v,
const MX p,
const Dict options = Dict() 
)
static

Functions called by friend functions defined for this class

Definition at line 2112 of file mx.cpp.

2112  {
2113  return p->get_low(v, options);
2114  }

References casadi::MXNode::get_low().

Referenced by interpn_linear().

◆ mac()

MX casadi::MX::mac ( const MX x,
const MX y,
const MX z 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 692 of file mx.cpp.

692  {
693  if (x.is_scalar() || y.is_scalar()) {
694  // Use element-wise multiplication if at least one factor scalar
695  return z + x*y;
696  }
697 
698  // Check matching dimensions
699  casadi_assert(x.size2()==y.size1(),
700  "Matrix product with incompatible dimensions. Lhs is "
701  + x.dim() + " and rhs is " + y.dim() + ".");
702 
703  // Check if we can simplify the product
704  if (x.is_eye()) {
705  return y + z;
706  } else if (y.is_eye()) {
707  return x + z;
708  } else if (x.is_zero() || y.is_zero()) {
709  return z;
710  } else {
711  return x->get_mac(y, z);
712  }
713  }

References casadi::GenericMatrix< MatType >::dim(), casadi::MXNode::get_mac(), is_eye(), casadi::GenericMatrix< MatType >::is_scalar(), is_zero(), casadi::GenericMatrix< MatType >::size1(), and casadi::GenericMatrix< MatType >::size2().

Referenced by mtimes().

◆ mapping()

Matrix< casadi_int > casadi::MX::mapping ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_ri

Definition at line 857 of file mx.cpp.

857  {
858  return (*this)->mapping();
859  }

Referenced by diag(), erase(), get(), and unite().

◆ matrix_expand() [1/2]

MX casadi::MX::matrix_expand ( const MX e,
const std::vector< MX > &  boundary,
const Dict options 
)
static

Functions called by friend functions defined for this class

Definition at line 1943 of file mx.cpp.

1943  {
1944  return matrix_expand(std::vector<MX>{e}, boundary, options).at(0);
1945  }
static MX matrix_expand(const MX &e, const std::vector< MX > &boundary, const Dict &options)
Definition: mx.cpp:1943

◆ matrix_expand() [2/2]

std::vector< MX > casadi::MX::matrix_expand ( const std::vector< MX > &  e,
const std::vector< MX > &  boundary,
const Dict options 
)
static

Functions called by friend functions defined for this class

Definition at line 1947 of file mx.cpp.

1949  {
1950 
1951  // Create symbols for boundary nodes
1952  std::vector<MX> syms(boundary.size());
1953 
1954  for (casadi_int i=0;i<syms.size();++i) {
1955  syms[i] = MX::sym("x", boundary[i].sparsity());
1956  }
1957 
1958  // Substitute symbols for boundary nodes
1959  std::vector<MX> ret = graph_substitute(e, boundary, syms);
1960 
1961  // Obtain list of dependents
1962  std::vector<MX> v = symvar(veccat(ret));
1963 
1964  // Construct an MXFunction with it
1965  Function f("tmp_matrix_expand", v, ret, Dict{{"max_io", 0}, {"allow_free", true}});
1966 
1967  // Expand to SXFunction
1968  Function s = f.expand("expand_" + f.name(), options);
1969  std::vector<MX> r;
1970  s.call(graph_substitute(v, syms, boundary), r);
1971  return r;
1972  }

References casadi::Function::call(), casadi::Function::expand(), graph_substitute(), sparsity(), casadi::GenericMatrix< MX >::sym(), symvar(), and casadi::SparsityInterface< MatType >::veccat().

◆ mldivide()

MX casadi::MX::mldivide ( const MX a,
const MX b 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 749 of file mx.cpp.

749  {
750  if (a.is_scalar() || b.is_scalar()) return b/a;
751  return solve(a, b);
752  }

References casadi::GenericMatrix< MatType >::is_scalar(), and solve().

◆ mmax()

MX casadi::MX::mmax ( const MX x)
static

Functions called by friend functions defined for GenericExpression

Definition at line 846 of file mx.cpp.

846  {
847  return x->get_mmax();
848  }

References casadi::MXNode::get_mmax().

◆ mmin()

MX casadi::MX::mmin ( const MX x)
static

Functions called by friend functions defined for GenericExpression

Definition at line 842 of file mx.cpp.

842  {
843  return x->get_mmin();
844  }

References casadi::MXNode::get_mmin().

◆ monitor()

MX casadi::MX::monitor ( const std::string &  comment) const

Returns itself, but with the side effect of printing the nonzeros along with a comment

Extra doc: https://github.com/casadi/casadi/wiki/L_rh

Definition at line 730 of file mx.cpp.

730  {
731  return(*this)->get_monitor(comment);
732  }

References casadi::MXNode::get_monitor().

◆ mpower()

static MX casadi::GenericMatrix< MX >::mpower ( const MX x,
const MX y 
)
staticinherited

Functions called by friend functions defined here

◆ mrdivide()

MX casadi::MX::mrdivide ( const MX a,
const MX b 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 744 of file mx.cpp.

744  {
745  if (a.is_scalar() || b.is_scalar()) return b/a;
746  return solve(a.T(), b.T()).T();
747  }
MX T() const
Transpose the matrix.
Definition: mx.cpp:1029

References casadi::GenericMatrix< MatType >::is_scalar(), solve(), and T().

◆ mtimes()

MX casadi::MX::mtimes ( const MX x,
const MX y 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 652 of file mx.cpp.

652  {
653  if (x.is_scalar() || y.is_scalar()) {
654  // Use element-wise multiplication if at least one factor scalar
655  return x*y;
656  } else {
657  MX z = MX::zeros(Sparsity::mtimes(x.sparsity(), y.sparsity()));
658  return mac(x, y, z);
659  }
660  }
static MX mac(const MX &x, const MX &y, const MX &z)
Definition: mx.cpp:692
static Sparsity mtimes(const Sparsity &x, const Sparsity &y)
Enlarge matrix.
Definition: sparsity.cpp:430

References casadi::GenericMatrix< MatType >::is_scalar(), mac(), casadi::Sparsity::mtimes(), sparsity(), and casadi::GenericMatrix< MX >::zeros().

Referenced by pinv(), solve(), sum1(), and sum2().

◆ n_dep()

casadi_int casadi::MX::n_dep ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qm

Definition at line 758 of file mx.cpp.

758  {
759  return (*this)->n_dep();
760  }

Referenced by casadi::DaeBuilderInternal::create().

◆ n_nodes()

casadi_int casadi::MX::n_nodes ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1374 of file mx.cpp.

1374  {
1375  Dict opts{{"max_io", 0}, {"cse", false}, {"allow_free", true}};
1376  Function f("tmp_n_nodes", std::vector<MX>{}, {x}, opts);
1377  return f.n_nodes();
1378  }

References casadi::Function::n_nodes().

◆ n_out()

casadi_int casadi::MX::n_out ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qk

Definition at line 869 of file mx.cpp.

869  {
870  return (*this)->nout();
871  }

Referenced by casadi::DaeBuilderInternal::create(), and interpn_linear().

◆ n_primitives()

casadi_int casadi::MX::n_primitives ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qo

Definition at line 929 of file mx.cpp.

929  {
930  return (*this)->n_primitives();
931  }

Referenced by cse(), extract_parametric(), graph_substitute(), join_primitives(), primitives(), separate_linear(), and split_primitives().

◆ name()

std::string casadi::MX::name ( ) const

◆ nan() [1/3]

MX casadi::MX::nan ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r3

Definition at line 568 of file mx.cpp.

568  {
569  return nan(Sparsity::dense(nrow, ncol));
570  }
static MX nan(const Sparsity &sp)
create a matrix with all nan
Definition: mx.cpp:576

References casadi::Sparsity::dense(), and nan().

◆ nan() [2/3]

MX casadi::MX::nan ( const Sparsity sp)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r3

Definition at line 576 of file mx.cpp.

576  {
577  return create(ConstantMX::create(sp, std::numeric_limits<double>::quiet_NaN()));
578  }

References casadi::ConstantMX::create(), and create().

Referenced by casadi::MXNode::_get_binary(), nan(), and casadi::qpsol_nlp().

◆ nan() [3/3]

MX casadi::MX::nan ( const std::pair< casadi_int, casadi_int > &  rc)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r3

Definition at line 572 of file mx.cpp.

572  {
573  return nan(rc.first, rc.second);
574  }

References nan().

◆ nnz()

casadi_int casadi::GenericMatrix< MX >::nnz
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1an

Definition at line 84 of file generic_matrix.hpp.

1298  {
1299  return sparsity().nnz();
1300  }
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148

◆ nnz_diag()

casadi_int casadi::GenericMatrix< MX >::nnz_diag
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1aq

Definition at line 99 of file generic_matrix.hpp.

1313  {
1314  return sparsity().nnz_diag();
1315  }
casadi_int nnz_diag() const
Number of non-zeros on the diagonal, i.e. the number of elements (i, j) with j==i.
Definition: sparsity.cpp:360

◆ nnz_lower()

casadi_int casadi::GenericMatrix< MX >::nnz_lower
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1ao

Definition at line 89 of file generic_matrix.hpp.

1303  {
1304  return sparsity().nnz_lower();
1305  }
casadi_int nnz_lower(bool strictly=false) const
Number of non-zeros in the lower triangular half,.
Definition: sparsity.cpp:352

◆ nnz_upper()

casadi_int casadi::GenericMatrix< MX >::nnz_upper
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1ap

Definition at line 94 of file generic_matrix.hpp.

1308  {
1309  return sparsity().nnz_upper();
1310  }
casadi_int nnz_upper(bool strictly=false) const
Number of non-zeros in the upper triangular half,.
Definition: sparsity.cpp:356

◆ norm_0_mul()

static casadi_int casadi::GenericMatrix< MX >::norm_0_mul ( const MX x,
const MX y 
)
inlinestaticinherited

Definition at line 216 of file generic_matrix.hpp.

216  {
217  return Sparsity::norm_0_mul(x.sparsity(), y.sparsity());
218  }

◆ norm_1()

MX casadi::MX::norm_1 ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1230 of file mx.cpp.

1230  {
1231  return x->get_norm_1();
1232  }

References casadi::MXNode::get_norm_1().

◆ norm_2()

MX casadi::MX::norm_2 ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1218 of file mx.cpp.

1218  {
1219  if (x.is_vector()) {
1220  return norm_fro(x);
1221  } else {
1222  return x->get_norm_2();
1223  }
1224  }
static MX norm_fro(const MX &x)
Definition: mx.cpp:1226

References casadi::MXNode::get_norm_2(), casadi::GenericMatrix< MatType >::is_vector(), and norm_fro().

◆ norm_fro()

MX casadi::MX::norm_fro ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1226 of file mx.cpp.

1226  {
1227  return x->get_norm_fro();
1228  }

References casadi::MXNode::get_norm_fro().

Referenced by norm_2().

◆ norm_inf()

MX casadi::MX::norm_inf ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1234 of file mx.cpp.

1234  {
1235  return x->get_norm_inf();
1236  }

References casadi::MXNode::get_norm_inf().

◆ nullspace()

MX casadi::MX::nullspace ( const MX A)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2051 of file mx.cpp.

2051  {
2052  SX A_sx = SX::sym("A", A.sparsity());
2053  Function f("nullspace", {A_sx}, {SX::nullspace(A_sx)});
2054  return f(A).at(0);
2055  }
static Matrix< Scalar > nullspace(const Matrix< Scalar > &x)

References casadi::Matrix< Scalar >::nullspace(), sparsity(), and casadi::GenericMatrix< Matrix< Scalar > >::sym().

◆ numel()

casadi_int casadi::GenericMatrix< MX >::numel
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1ar

Definition at line 104 of file generic_matrix.hpp.

1318  {
1319  return sparsity().numel();
1320  }
casadi_int numel() const
The total number of elements, including structural zeros, i.e. size2()*size1()
Definition: sparsity.cpp:132

◆ nz() [1/2]

NonZeros<MX , K> casadi::GenericMatrix< MX >::nz ( const K &  k)
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1bc

Definition at line 258 of file generic_matrix.hpp.

258  {
259  return NonZeros<MatType, K>(self(), k);
260  }

◆ nz() [2/2]

const MX casadi::GenericMatrix< MX >::nz ( const K &  k) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1bb

Definition at line 248 of file generic_matrix.hpp.

248  {
249  MatType ret;
250  self().get_nz(ret, false, k);
251  return ret;
252  }

◆ ones() [1/3]

static MX casadi::GenericMatrix< MX >::ones ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1di

Definition at line 1275 of file generic_matrix.hpp.

1275  {
1276  return ones(Sparsity::dense(nrow, ncol));
1277  }
static MX ones(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries one.

◆ ones() [2/3]

static MX casadi::GenericMatrix< MX >::ones ( const Sparsity sp)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1di

Definition at line 1278 of file generic_matrix.hpp.

1278 { return MatType(sp, 1, false);}

◆ ones() [3/3]

static MX casadi::GenericMatrix< MX >::ones ( const std::pair< casadi_int, casadi_int > &  rc)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1di

Definition at line 1279 of file generic_matrix.hpp.

1279  {
1280  return ones(rc.first, rc.second);
1281  }

◆ op()

casadi_int casadi::MX::op ( ) const

◆ operator double()

casadi::MX::operator double ( ) const
explicit

Definition at line 806 of file mx.cpp.

806  {
807  return (*this)->to_double();
808  }

References casadi::MXNode::to_double().

◆ operator Matrix< double >()

casadi::MX::operator Matrix< double > ( ) const
explicit

◆ operator()() [1/4]

SubIndex<MX , RR> casadi::GenericMatrix< MX >::operator() ( const RR &  rr)
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1bf

Definition at line 286 of file generic_matrix.hpp.

286  {
287  return SubIndex<MatType, RR>(self(), rr);
288  }

◆ operator()() [2/4]

const MX casadi::GenericMatrix< MX >::operator() ( const RR &  rr) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1bd

Definition at line 266 of file generic_matrix.hpp.

266  {
267  MatType ret;
268  self().get(ret, false, rr);
269  return ret;
270  }

◆ operator()() [3/4]

SubMatrix<MX , RR, CC> casadi::GenericMatrix< MX >::operator() ( const RR &  rr,
const CC &  cc 
)
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1bg

Definition at line 294 of file generic_matrix.hpp.

294  {
295  return SubMatrix<MatType, RR, CC>(self(), rr, cc);
296  }

◆ operator()() [4/4]

const MX casadi::GenericMatrix< MX >::operator() ( const RR &  rr,
const CC &  cc 
) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1be

Definition at line 276 of file generic_matrix.hpp.

276  {
277  MatType ret;
278  self().get(ret, false, rr, cc);
279  return ret;
280  }

◆ operator-()

MX casadi::MX::operator- ( ) const

Definition at line 584 of file mx.cpp.

584  {
585  if ((*this)->op()==OP_NEG) {
586  return (*this)->dep(0);
587  } else {
588  return (*this)->get_unary(OP_NEG);
589  }
590  }
@ OP_NEG
Definition: calculus.hpp:66

References casadi::MXNode::dep(), casadi::MXNode::get_unary(), and casadi::OP_NEG.

◆ operator->() [1/2]

MXNode * casadi::MX::operator-> ( )

Extra doc: https://github.com/casadi/casadi/wiki/L_qh

Definition at line 548 of file mx.cpp.

548  {
549  return static_cast<MXNode*>(SharedObject::operator->());
550  }
SharedObjectInternal * operator->() const
Access a member function or object.

References casadi::GenericShared< SharedObject, SharedObjectInternal >::operator->().

◆ operator->() [2/2]

const MXNode * casadi::MX::operator-> ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qi

Definition at line 552 of file mx.cpp.

552  {
553  return static_cast<const MXNode*>(SharedObject::operator->());
554  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::operator->().

◆ own()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::own ( Internal *  node)
inherited

Assign the node to a node class pointer (or null)

Definition at line 90 of file generic_shared_impl.hpp.

◆ pinv()

MX casadi::MX::pinv ( const MX A,
const std::string &  lsolver = "qr",
const Dict dict = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2031 of file mx.cpp.

2031  {
2032  if (A.size1()>=A.size2()) {
2033  return solve(mtimes(A.T(), A), A.T(), lsolver, dict);
2034  } else {
2035  return solve(mtimes(A, A.T()), A, lsolver, dict).T();
2036  }
2037  }
static MX mtimes(const MX &x, const MX &y)
Definition: mx.cpp:652

References mtimes(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), solve(), and T().

◆ polyval()

MX casadi::MX::polyval ( const MX p,
const MX x 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1388 of file mx.cpp.

1388  {
1389  casadi_assert(p.is_dense(), "polynomial coefficients vector must be a vector");
1390  casadi_assert(p.is_column() && p.nnz()>0, "polynomial coefficients must be a vector");
1391  MX ret = p.nz(0);
1392  for (casadi_int i=1; i<p.nnz(); ++i) {
1393  ret = ret*x + p.nz(i);
1394  }
1395  return ret;
1396  }

References casadi::GenericMatrix< MatType >::is_column(), casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::nnz(), and casadi::GenericMatrix< MatType >::nz().

◆ primitives()

std::vector< MX > casadi::MX::primitives ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qp

Definition at line 933 of file mx.cpp.

933  {
934  std::vector<MX> ret(n_primitives());
935  std::vector<MX>::iterator it=ret.begin();
936  (*this)->primitives(it);
937  casadi_assert_dev(it==ret.end());
938  return ret;
939  }

References n_primitives().

Referenced by casadi::DaeBuilderInternal::der(), get_nonzeros(), casadi::OptiNode::set_domain(), and casadi::OptiNode::to_function().

◆ print_operator()

std::string casadi::MX::print_operator ( const MX x,
const std::vector< std::string > &  args 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1398 of file mx.cpp.

1398  {
1399  return x->disp(args);
1400  }

References casadi::MXNode::disp().

◆ print_ptr()

void casadi::SharedObject::print_ptr ( std::ostream &  stream = casadi::uout()) const
inherited

Print the pointer to the internal class

Definition at line 43 of file shared_object.cpp.

43  {
44  stream << get();
45  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::get().

◆ printme()

MX casadi::MX::printme ( const MX b) const

Definition at line 719 of file mx.cpp.

719  {
720  return binary(OP_PRINTME, *this, b);
721  }
@ OP_PRINTME
Definition: calculus.hpp:190

References binary(), and casadi::OP_PRINTME.

◆ project()

MX casadi::MX::project ( const MX x,
const Sparsity sp,
bool  intersect = false 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 877 of file mx.cpp.

877  {
878  try {
879  if (x.is_empty() || (sp==x.sparsity())) {
880  return x;
881  } else {
882  casadi_assert(sp.size()==x.size(), "Cannot project " + x.dim() + " to " + sp.dim());
883  if (intersect) {
884  return x->get_project(sp.intersect(x.sparsity()));
885  } else {
886  return x->get_project(sp);
887  }
888  }
889  } catch (std::exception& e) {
890  CASADI_THROW_ERROR("project", e.what());
891  }
892  }

References casadi::GenericMatrix< MatType >::dim(), casadi::Sparsity::dim(), casadi::MXNode::get_project(), casadi::Sparsity::intersect(), casadi::GenericMatrix< MatType >::is_empty(), casadi::GenericMatrix< MatType >::size(), casadi::Sparsity::size(), and sparsity().

Referenced by densify(), get(), set(), and set_nz().

◆ quadratic_coeff()

void casadi::GenericMatrix< MX >::quadratic_coeff ( const MX expr,
const MX var,
MX A,
MX b,
MX c,
bool  check 
)
staticinherited

Definition at line 237 of file generic_matrix.hpp.

1754  {
1755  casadi_assert(expr.is_scalar(), "'quadratic_coeff' only defined for scalar expressions.");
1756  A = hessian(expr, var);
1757  b = substitute(jacobian(expr, var), var, 0).T();
1758  if (check)
1759  casadi_assert(!depends_on(A, var), "'quadratic_coeff' called on non-quadratic expression.");
1760  c = substitute(expr, var, 0);
1761  }
friend bool depends_on(const MX &f, const MX &arg)
Check if expression depends on the argument.
friend MX hessian(const MX &ex, const MX &arg, const Dict &opts=Dict())
Hessian and (optionally) gradient.

◆ rank1()

static MX casadi::GenericMatrix< MX >::rank1 ( const MX A,
const MX alpha,
const MX x,
const MX y 
)
staticinherited

Calculates A + 1/2 * alpha * x*y'

Extra doc: https://github.com/casadi/casadi/wiki/L_1bp

◆ repmat()

MX casadi::MX::repmat ( const MX x,
casadi_int  n,
casadi_int  m = 1 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1989 of file mx.cpp.

1989  {
1990  if (n==0 && m==0) {
1991  return MX();
1992  } else if (n==0) {
1993  return MX(0, x.size2()*m);
1994  } else if (m==0) {
1995  return MX(x.size1()*n, 0);
1996  } else if (n==1 && m==1) {
1997  return x;
1998  } else {
1999  return x->get_repmat(n, m);
2000  }
2001  }

References casadi::MXNode::get_repmat(), MX(), casadi::GenericMatrix< MatType >::size1(), and casadi::GenericMatrix< MatType >::size2().

Referenced by binary(), densify(), and set().

◆ repsum() [1/2]

MX casadi::GenericMatrix< MX >::repsum ( const MX x,
casadi_int  n,
casadi_int  m = 1 
)
staticinherited

Definition at line 232 of file generic_matrix.hpp.

1529  {
1530  casadi_assert_dev(x.size1() % n==0);
1531  casadi_assert_dev(x.size2() % m==0);
1532  std::vector< std::vector< MatType> > s =
1533  blocksplit(x, x.size1()/n, x.size2()/m);
1534  MatType sum = 0;
1535  for (casadi_int i=0;i<s.size();++i) {
1536  for (casadi_int j=0;j<s[i].size();++j) {
1537  sum = sum + s[i][j];
1538  }
1539  }
1540  return sum;
1541  }
static std::vector< std::vector< MatType > > blocksplit(const MatType &x, const std::vector< casadi_int > &vert_offset, const std::vector< casadi_int > &horz_offset)
friend MatType sum(const MatType &x)
Returns summation of all elements.

◆ repsum() [2/2]

MX casadi::MX::repsum ( const MX x,
casadi_int  n,
casadi_int  m = 1 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2003 of file mx.cpp.

2003  {
2004  return x->get_repsum(n, m);
2005  }

References casadi::MXNode::get_repsum().

◆ reset_input()

void casadi::MX::reset_input ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qt

Definition at line 993 of file mx.cpp.

993  {
994  (*this)->reset_input();
995  }

◆ reshape() [1/2]

MX casadi::MX::reshape ( const MX x,
casadi_int  nrow,
casadi_int  ncol 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1242 of file mx.cpp.

1242  {
1243  // Quick return if trivial
1244  if (nrow==x.size1() && ncol==x.size2()) return x;
1245 
1246  // Reshape the sparsity pattern
1247  return reshape(x, Sparsity::reshape(x.sparsity(), nrow, ncol));
1248  }
static Sparsity reshape(const Sparsity &x, casadi_int nrow, casadi_int ncol)
Enlarge matrix.
Definition: sparsity.cpp:260

References casadi::Sparsity::reshape(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), and sparsity().

Referenced by MX().

◆ reshape() [2/2]

MX casadi::MX::reshape ( const MX x,
const Sparsity sp 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1250 of file mx.cpp.

1250  {
1251  casadi_assert(sp.is_reshape(x.sparsity()), "Reshape mismatch");
1252 
1253  // Quick return if trivial
1254  if (sp==x.sparsity()) return x;
1255 
1256  // Call internal method
1257  return x->get_reshape(sp);
1258  }

References casadi::MXNode::get_reshape(), casadi::Sparsity::is_reshape(), and sparsity().

◆ reverse()

std::vector< std::vector< MX > > casadi::MX::reverse ( const std::vector< MX > &  ex,
const std::vector< MX > &  arg,
const std::vector< std::vector< MX > > &  v,
const Dict opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1882 of file mx.cpp.

1884  {
1885  try {
1886  // Read options
1887  bool always_inline = true;
1888  bool never_inline = false;
1889 
1890 
1891  Dict h_opts;
1892  Dict opts_remainder = extract_from_dict(opts, "helper_options", h_opts);
1893  h_opts["allow_free"] = true;
1894 
1895  for (auto&& op : opts_remainder) {
1896  if (op.first=="always_inline") {
1897  always_inline = op.second;
1898  } else if (op.first=="never_inline") {
1899  never_inline = op.second;
1900  } else {
1901  casadi_error("No such option: " + std::string(op.first));
1902  }
1903  }
1904  // Call internal function on a temporary object
1905  Function temp("reverse_temp", arg, ex, h_opts);
1906  std::vector<std::vector<MX> > ret;
1907  temp->call_reverse(arg, ex, v, ret, always_inline, never_inline);
1908  return ret;
1909  } catch (std::exception& e) {
1910  CASADI_THROW_ERROR("reverse", e.what());
1911  }
1912  }

References casadi::FunctionInternal::call_reverse(), casadi::extract_from_dict(), and op().

Referenced by substitute_inplace().

◆ row() [1/2]

const casadi_int* casadi::GenericMatrix< MX >::row ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b8

Definition at line 197 of file generic_matrix.hpp.

197 { return sparsity().row(); }
const casadi_int * row() const
Get a reference to row-vector,.
Definition: sparsity.cpp:164

◆ row() [2/2]

casadi_int casadi::GenericMatrix< MX >::row ( casadi_int  el) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1b8

Definition at line 200 of file generic_matrix.hpp.

200 { return sparsity().row(el); }

◆ rows()

casadi_int casadi::GenericMatrix< MX >::rows ( ) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1at

Definition at line 114 of file generic_matrix.hpp.

114 {return size1();}

◆ separate_linear()

void casadi::MX::separate_linear ( const MX expr,
const MX sym_lin,
const MX sym_const,
MX expr_const,
MX expr_lin,
MX expr_nonlin 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2450 of file mx.cpp.

2452  {
2453 
2454  std::vector<MX> in = {sym_const, sym_lin};
2455  std::vector<MX> out = {expr};
2456 
2457  Function f("f", in, out, {{"live_variables", false},
2458  {"max_io", 0}, {"allow_free", true}});
2459  MXFunction *ff = f.get<MXFunction>();
2460 
2461  // Each work vector element has (const, lin, nonlin) part
2462  std::vector< std::array<MX, 3> > w(ff->workloc_.size()-1);
2463 
2464  // Split up inputs analogous to symbolic primitives
2465  std::vector<std::vector<MX> > arg_split(in.size());
2466  for (casadi_int i=0; i<in.size(); ++i) arg_split[i] = in[i].split_primitives(in[i]);
2467 
2468  // Allocate storage for split outputs
2469  std::array<std::vector<MX>, 3> res_split;
2470  for (int k=0;k<3;++k) {
2471  res_split[k].resize(expr.n_primitives());
2472  }
2473 
2474  std::vector<std::array<MX, 3> > arg1, res1;
2475 
2476  std::array<MX, 3> res;
2477 
2478  // Loop over computational nodes in forward order
2479  casadi_int alg_counter = 0;
2480  for (auto it=ff->algorithm_.begin(); it!=ff->algorithm_.end(); ++it, ++alg_counter) {
2481  if (it->op == OP_INPUT) {
2482  MX null = MX::zeros(arg_split.at(it->data->ind()).at(it->data->segment()).sparsity());
2483  w[it->res.front()][0] = null;
2484  w[it->res.front()][1] = null;
2485  w[it->res.front()][2] = null;
2486  w[it->res.front()][it->data->ind()] = arg_split.at(it->data->ind()).at(it->data->segment());
2487  } else if (it->op==OP_OUTPUT) {
2488  // Collect the results
2489  for (int i=0;i<3;++i) {
2490  res_split.at(i).at(it->data->segment()) = w[it->arg.front()][i];
2491  }
2492  } else if (it->op==OP_CONST) {
2493  // Fetch constant
2494  w[it->res.front()][0] = it->data;
2495  w[it->res.front()][1] = MX::zeros(it->data->sparsity());
2496  w[it->res.front()][2] = MX::zeros(it->data->sparsity());
2497  } else if (it->op==OP_PARAMETER) {
2498  // Fetch parameter
2499  w[it->res.front()][0] = MX::zeros(it->data->sparsity());
2500  w[it->res.front()][1] = MX::zeros(it->data->sparsity());
2501  w[it->res.front()][2] = it->data;
2502  } else {
2503  // Arguments of the operation
2504  arg1.resize(it->arg.size());
2505  for (casadi_int i=0; i<arg1.size(); ++i) {
2506  casadi_int el = it->arg[i]; // index of the argument
2507  for (int k=0;k<3;++k) {
2508  arg1[i][k] = el<0 ? MX(it->data->dep(i).size()) : w[el][k];
2509  }
2510  }
2511 
2512  // Perform the operation
2513  res1.clear();
2514  res1.resize(it->res.size());
2515  for (casadi_int i=0;i<it->res.size();++i) {
2516  for (int k=0;k<3;++k) {
2517  res1[i][k] = MX::zeros(it->data->sparsity());
2518  }
2519  }
2520  it->data->eval_linear(arg1, res1);
2521 
2522  // Get the result
2523  for (casadi_int i=0; i<res1.size(); ++i) {
2524  casadi_int el = it->res[i]; // index of the output
2525  for (int k=0;k<3;++k) {
2526  if (el>=0) w[el][k] = res1[i][k];
2527  }
2528  }
2529  }
2530  }
2531 
2532  // Join split outputs
2533  for (int k=0;k<3;++k) {
2534  res[k] = expr.join_primitives(res_split[k]);
2535  }
2536  expr_const = res[0];
2537  expr_lin = res[1];
2538  expr_nonlin = res[2];
2539  }
std::vector< MX > split_primitives(const MX &x) const
Split up an expression along symbolic primitives.
Definition: mx.cpp:941

References join_primitives(), MX(), n_primitives(), casadi::OP_CONST, casadi::OP_INPUT, casadi::OP_OUTPUT, casadi::OP_PARAMETER, split_primitives(), and casadi::GenericMatrix< MX >::zeros().

◆ serialize()

void casadi::MX::serialize ( SerializingStream s) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qz

Definition at line 830 of file mx.cpp.

830  {
831  return (*this)->serialize(s);
832  }

◆ set() [1/7]

void casadi::MX::set ( const MX m,
bool  ind1,
const Matrix< casadi_int > &  rr 
)

Set a submatrix, single argument

Definition at line 309 of file mx.cpp.

309  {
310  // Assert dimensions of assigning matrix
311  if (rr.sparsity() != m.sparsity()) {
312  if (rr.size() == m.size()) {
313  // Remove submatrix to be replaced
314  erase(rr.nonzeros(), ind1);
315 
316  // Find the intersection between rr's and m's sparsity patterns
317  Sparsity sp = rr.sparsity() * m.sparsity();
318 
319  // Project both matrices to this sparsity
320  return set(project(m, sp), ind1, Matrix<casadi_int>::project(rr, sp));
321  } else if (m.is_scalar()) {
322  // m scalar means "set all"
323  if (m.is_dense()) {
324  return set(MX(rr.sparsity(), m), ind1, rr);
325  } else {
326  return set(MX(rr.size()), ind1, rr);
327  }
328  } else if (rr.size1() == m.size2() && rr.size2() == m.size1()
329  && std::min(m.size1(), m.size2()) == 1) {
330  // m is transposed if necessary
331  return set(m.T(), ind1, rr);
332  } else {
333  // Error otherwise
334  casadi_error("Dimension mismatch. lhs is " + str(rr.size())
335  + ", while rhs is " + str(m.size()));
336  }
337  }
338 
339  // Dimensions of this
340  casadi_int sz1 = size1(), sz2 = size2(), sz = nnz(), nel = numel(), rrsz = rr.nnz();
341 
342  // Quick return if nothing to set
343  if (rrsz==0) return;
344 
345  // Check bounds
346  casadi_assert_in_range(rr.nonzeros(), -nel+ind1, nel+ind1);
347 
348  // Dense mode
349  if (is_dense() && m.is_dense()) {
350  return set_nz(m, ind1, rr);
351  }
352 
353  // Construct new sparsity pattern
354  std::vector<casadi_int> new_row=sparsity().get_row(), new_col=sparsity().get_col();
355  std::vector<casadi_int> nz(rr.nonzeros());
356  new_row.reserve(sz+rrsz);
357  new_col.reserve(sz+rrsz);
358  nz.reserve(rrsz);
359  for (std::vector<casadi_int>::iterator i=nz.begin(); i!=nz.end(); ++i) {
360  if (ind1) (*i)--;
361  if (*i<0) *i += nel;
362  new_row.push_back(*i % sz1);
363  new_col.push_back(*i / sz1);
364  }
365  Sparsity sp = Sparsity::triplet(sz1, sz2, new_row, new_col);
366 
367  // If needed, update pattern
368  if (sp != sparsity()) *this = project(*this, sp);
369 
370  // Find the nonzeros corresponding to rr
371  sparsity().get_nz(nz);
372 
373  // Create a nonzero assignment node
374  *this = m->get_nzassign(*this, nz);
375  }
void erase(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Erase a submatrix (leaving structural zeros in its place)
Definition: mx.cpp:596
void set_nz(const MX &m, bool ind1, const Slice &kk)
Definition: mx.cpp:448
void set(const MX &m, bool ind1, const Slice &rr)
Definition: mx.cpp:304
static Matrix< casadi_int > project(const Matrix< casadi_int > &x, const Sparsity &sp, bool intersect=false)
casadi_int get_nz(casadi_int rr, casadi_int cc) const
Get the index of an existing non-zero element.
Definition: sparsity.cpp:246
std::vector< casadi_int > get_col() const
Get the column for each non-zero entry.
Definition: sparsity.cpp:368
static Sparsity triplet(casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, std::vector< casadi_int > &mapping, bool invert_mapping)
Create a sparsity pattern given the nonzeros in sparse triplet form *.
Definition: sparsity.cpp:1127

References erase(), casadi::Sparsity::get_col(), casadi::Sparsity::get_nz(), casadi::MXNode::get_nzassign(), casadi::Sparsity::get_row(), casadi::GenericMatrix< MX >::is_dense(), casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_scalar(), MX(), casadi::GenericMatrix< MatType >::nnz(), casadi::GenericMatrix< MX >::nnz(), casadi::Matrix< Scalar >::nonzeros(), casadi::GenericMatrix< MX >::numel(), casadi::GenericMatrix< MX >::nz(), project(), set(), set_nz(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MX >::size1(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MX >::size2(), casadi::GenericMatrix< MatType >::size2(), casadi::Matrix< Scalar >::sparsity(), sparsity(), casadi::str(), T(), and casadi::Sparsity::triplet().

◆ set() [2/7]

void casadi::MX::set ( const MX m,
bool  ind1,
const Matrix< casadi_int > &  rr,
const Matrix< casadi_int > &  cc 
)

Set a submatrix, two arguments

Definition at line 244 of file mx.cpp.

244  {
245  // Row vector rr (e.g. in MATLAB) is transposed to column vector
246  if (rr.size1()==1 && rr.size2()>1) {
247  return set(m, ind1, rr.T(), cc);
248  }
249 
250  // Row vector cc (e.g. in MATLAB) is transposed to column vector
251  if (cc.size1()==1 && cc.size2()>1) {
252  return set(m, ind1, rr, cc.T());
253  }
254 
255  // Make sure rr and cc are dense vectors
256  casadi_assert(rr.is_dense() && rr.is_column(),
257  "MX::set: First index not dense vector");
258  casadi_assert(cc.is_dense() && cc.is_column(),
259  "MX::set: Second index not dense vector");
260 
261  // Assert dimensions of assigning matrix
262  if (rr.size1() != m.size1() || cc.size1() != m.size2()) {
263  if (m.is_scalar()) {
264  // m scalar means "set all"
265  return set(repmat(m, rr.size1(), cc.size1()), ind1, rr, cc);
266  } else if (rr.size1() == m.size2() && cc.size1() == m.size1()
267  && std::min(m.size1(), m.size2()) == 1) {
268  // m is transposed if necessary
269  return set(m.T(), ind1, rr, cc);
270  } else {
271  // Error otherwise
272  casadi_error("Dimension mismatch. lhs is " + str(rr.size1()) + "-by-"
273  + str(cc.size1()) + ", while rhs is " + str(m.size()));
274  }
275  }
276 
277  // Dimensions
278  casadi_int sz1 = size1(), sz2 = size2();
279 
280  // Report out-of-bounds
281  casadi_assert_in_range(rr.nonzeros(), -sz1+ind1, sz1+ind1);
282  casadi_assert_in_range(cc.nonzeros(), -sz2+ind1, sz2+ind1);
283 
284  // If we are assigning with something sparse, first remove existing entries
285  if (!m.is_dense()) {
286  erase(rr.nonzeros(), cc.nonzeros(), ind1);
287  }
288 
289  // Collect all assignments
290  IM el = IM::zeros(m.sparsity());
291  for (casadi_int j=0; j<el.size2(); ++j) { // Loop over columns of m
292  casadi_int this_j = cc->at(j) - ind1; // Corresponding column in this
293  if (this_j<0) this_j += sz2;
294  for (casadi_int k=el.colind(j); k<el.colind(j+1); ++k) { // Loop over rows of m
295  casadi_int i = m.row(k);
296  casadi_int this_i = rr->at(i) - ind1; // Corresponding row in this
297  if (this_i<0) this_i += sz1;
298  el->at(k) = this_i + this_j*sz1;
299  }
300  }
301  return set(m, false, el);
302  }
Matrix< Scalar > T() const
Transpose the matrix.
Matrix< casadi_int > IM
Definition: im_fwd.hpp:31

References casadi::GenericMatrix< MatType >::colind(), erase(), casadi::GenericMatrix< MatType >::is_column(), casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::Matrix< Scalar >::nonzeros(), repmat(), casadi::GenericMatrix< MatType >::row(), set(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MX >::size1(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MX >::size2(), casadi::GenericMatrix< MatType >::size2(), sparsity(), casadi::str(), casadi::Matrix< Scalar >::T(), T(), and casadi::GenericMatrix< MatType >::zeros().

◆ set() [3/7]

void casadi::MX::set ( const MX m,
bool  ind1,
const Matrix< casadi_int > &  rr,
const Slice cc 
)

Set a submatrix, two arguments

Definition at line 239 of file mx.cpp.

239  {
240  // Fall back on (IM, IM)
241  set(m, ind1, rr, cc.all(size2(), ind1));
242  }

References casadi::Slice::all(), set(), and casadi::GenericMatrix< MX >::size2().

◆ set() [4/7]

void casadi::MX::set ( const MX m,
bool  ind1,
const Slice rr 
)

Set a submatrix, single argument

Definition at line 304 of file mx.cpp.

304  {
305  // Fall back on IM
306  set(m, ind1, rr.all(size1(), ind1));
307  }

References casadi::Slice::all(), and casadi::GenericMatrix< MX >::size1().

Referenced by set().

◆ set() [5/7]

void casadi::MX::set ( const MX m,
bool  ind1,
const Slice rr,
const Matrix< casadi_int > &  cc 
)

Set a submatrix, two arguments

Definition at line 234 of file mx.cpp.

234  {
235  // Fall back on (IM, IM)
236  set(m, ind1, rr.all(size1(), ind1), cc);
237  }

References casadi::Slice::all(), set(), and casadi::GenericMatrix< MX >::size1().

◆ set() [6/7]

void casadi::MX::set ( const MX m,
bool  ind1,
const Slice rr,
const Slice cc 
)

Set a submatrix, two arguments

Definition at line 229 of file mx.cpp.

229  {
230  // Fall back on (IM, IM)
231  set(m, ind1, rr.all(size1(), ind1), cc.all(size2(), ind1));
232  }

References casadi::Slice::all(), set(), casadi::GenericMatrix< MX >::size1(), and casadi::GenericMatrix< MX >::size2().

◆ set() [7/7]

void casadi::MX::set ( const MX m,
bool  ind1,
const Sparsity sp 
)

Set a submatrix, single argument

Definition at line 377 of file mx.cpp.

377  {
378  casadi_assert(size()==sp.size(),
379  "set(Sparsity sp): shape mismatch. This matrix has shape "
380  + str(size()) + ", but supplied sparsity index has shape "
381  + str(sp.size()) + ".");
382  std::vector<casadi_int> ii = sp.find();
383  if (m.is_scalar()) {
384  (*this)(ii) = densify(m);
385  } else {
386  (*this)(ii) = densify(m(ii));
387  }
388  }

References densify(), casadi::Sparsity::find(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::GenericMatrix< MX >::size(), casadi::Sparsity::size(), and casadi::str().

◆ set_max_depth()

void casadi::MX::set_max_depth ( casadi_int  eq_depth = 1)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_rj

Definition at line 909 of file mx.cpp.

909  {
910  eq_depth_ = eq_depth;
911  }

◆ set_nz() [1/4]

void casadi::MX::set_nz ( const MX m,
bool  ind1,
casadi_int  kk 
)
inline

Set a set of nonzeros

Definition at line 540 of file mx.hpp.

540 { set_nz(m, ind1, Matrix<casadi_int>(kk)); }

References set_nz().

Referenced by set_nz().

◆ set_nz() [2/4]

void casadi::MX::set_nz ( const MX m,
bool  ind1,
const Matrix< casadi_int > &  kk 
)

Set a set of nonzeros

Definition at line 453 of file mx.cpp.

453  {
454  casadi_assert(kk.nnz()==m.nnz() || m.nnz()==1,
455  "MX::set_nz: length of non-zero indices (" + str(kk.nnz()) + ") " +
456  "must match size of rhs (" + str(m.nnz()) + ").");
457 
458  // Assert dimensions of assigning matrix
459  if (kk.sparsity() != m.sparsity()) {
460  if (m.is_scalar()) {
461  // m scalar means "set all"
462  if (!m.is_dense()) return; // Nothing to set
463  return set_nz(MX(kk.sparsity(), m), ind1, kk);
464  } else if (kk.size() == m.size()) {
465  // Project sparsity if needed
466  return set_nz(project(m, kk.sparsity()), ind1, kk);
467  } else if (kk.size1() == m.size2() && kk.size2() == m.size1()
468  && std::min(m.size1(), m.size2()) == 1) {
469  // m is transposed if necessary
470  return set_nz(m.T(), ind1, kk);
471  } else {
472  // Error otherwise
473  casadi_error("Dimension mismatch. lhs is " + str(kk.size())
474  + ", while rhs is " + str(m.size()));
475  }
476  }
477 
478  // Call recursively if points both objects point to the same node
479  if (this==&m) {
480  MX m_copy = m;
481  return set_nz(m_copy, ind1, kk);
482  }
483 
484  // Check bounds
485  casadi_int sz = nnz();
486  casadi_assert_in_range(kk.nonzeros(), -sz+ind1, sz+ind1);
487 
488  // Quick return if no assignments to be made
489  if (kk.nnz()==0) return;
490 
491  // Handle index-1, negative indices
492  if (ind1 || *std::min_element(kk->begin(), kk->end())<0) {
493  Matrix<casadi_int> kk_mod = kk;
494  for (auto&& i : kk_mod.nonzeros()) {
495  casadi_assert(!(ind1 && i<=0),
496  "Matlab is 1-based, but requested index " + str(i) + ". "
497  "Note that negative slices are disabled in the Matlab interface. "
498  "Possibly you may want to use 'end'.");
499  if (ind1) i--;
500  if (i<0) i += sz;
501  }
502  return set_nz(m, false, kk_mod); // Call recursively
503  }
504 
505  // Create a nonzero assignment node
506  *this = m->get_nzassign(*this, kk.nonzeros());
507  }

References casadi::MXNode::get_nzassign(), casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_scalar(), MX(), casadi::GenericMatrix< MX >::nnz(), casadi::GenericMatrix< MatType >::nnz(), casadi::Matrix< Scalar >::nonzeros(), project(), set_nz(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), casadi::Matrix< Scalar >::sparsity(), sparsity(), casadi::str(), and T().

◆ set_nz() [3/4]

void casadi::MX::set_nz ( const MX m,
bool  ind1,
const MX kk 
)

Set a set of nonzeros

Definition at line 509 of file mx.cpp.

509  {
510  *this = m->get_nzassign(*this, ind1 ? kk-1 : kk);
511  }

References casadi::MXNode::get_nzassign().

◆ set_nz() [4/4]

void casadi::MX::set_nz ( const MX m,
bool  ind1,
const Slice kk 
)

Set a set of nonzeros

Definition at line 448 of file mx.cpp.

448  {
449  // Fallback on IM
450  set_nz(m, ind1, kk.all(nnz(), ind1));
451  }

References casadi::Slice::all(), and casadi::GenericMatrix< MX >::nnz().

Referenced by set(), and set_nz().

◆ set_temp()

void casadi::MX::set_temp ( casadi_int  t) const

Definition at line 865 of file mx.cpp.

865  {
866  (*this)->temp = t;
867  }

◆ shared()

void casadi::MX::shared ( std::vector< MX > &  ex,
std::vector< MX > &  v,
std::vector< MX > &  vdef,
const std::string &  v_prefix,
const std::string &  v_suffix 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1815 of file mx.cpp.

1816  {
1817  // Call new, more generic function
1818  return extract(ex, v, vdef, Dict{{"lift_shared", true}, {"lift_calls", false},
1819  {"prefix", v_prefix}, {"suffix", v_suffix}});
1820  }
static void extract(std::vector< MX > &ex, std::vector< MX > &v, std::vector< MX > &vdef, const Dict &opts=Dict())
Definition: mx.cpp:1642

References extract().

◆ simplify()

MX casadi::MX::simplify ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1238 of file mx.cpp.

1238  {
1239  return x;
1240  }

◆ size() [1/2]

std::pair< casadi_int, casadi_int > casadi::GenericMatrix< MX >::size
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1ax

Definition at line 136 of file generic_matrix.hpp.

1333  {
1334  return sparsity().size();
1335  }
std::pair< casadi_int, casadi_int > size() const
Get the shape.
Definition: sparsity.cpp:152

◆ size() [2/2]

casadi_int casadi::GenericMatrix< MX >::size ( casadi_int  axis) const
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1ay

Definition at line 141 of file generic_matrix.hpp.

1338  {
1339  return sparsity().size(axis);
1340  }

◆ size1()

casadi_int casadi::GenericMatrix< MX >::size1
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1as

Definition at line 109 of file generic_matrix.hpp.

1323  {
1324  return sparsity().size1();
1325  }
casadi_int size1() const
Get the number of rows.
Definition: sparsity.cpp:124

◆ size2()

casadi_int casadi::GenericMatrix< MX >::size2
inherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1au

Definition at line 119 of file generic_matrix.hpp.

1328  {
1329  return sparsity().size2();
1330  }
casadi_int size2() const
Get the number of columns.
Definition: sparsity.cpp:128

◆ skew()

MX casadi::GenericMatrix< MX >::skew ( const MX a)
staticinherited

Definition at line 228 of file generic_matrix.hpp.

1500  {
1501  casadi_assert(a.is_vector() && (a.size1()==3 || a.size2()==3),
1502  "skew(a): Expecting 3-vector, got " + a.dim() + ".");
1503 
1504  MatType x = a(0);
1505  MatType y = a(1);
1506  MatType z = a(2);
1507  return blockcat(std::vector< std::vector<MatType> >({{0, -z, y}, {z, 0, -x}, {-y, x, 0}}));
1508  }
friend MatType blockcat(const std::vector< std::vector< MatType > > &v)
Construct a matrix from a list of list of blocks.

◆ soc()

static MX casadi::GenericMatrix< MX >::soc ( const MX x,
const MX y 
)
staticinherited

Functions called by friend functions defined here

◆ solve() [1/2]

MX casadi::MX::solve ( const MX a,
const MX b 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2007 of file mx.cpp.

2007  {
2008  if (a.is_triu()) {
2009  // A is upper triangular
2010  return a->get_solve_triu(b, false);
2011  } else if (a.is_tril()) {
2012  // A is lower triangular
2013  return a->get_solve_tril(b, false);
2014  } else if (a.sparsity().is_orthonormal()) {
2015  // A is orthonormal -> inv(A)==A.T
2016  MX nz = sparsity_cast(a, Sparsity::dense(a.nnz()));
2017  const Sparsity& Q = a.sparsity();
2018  return mtimes(MX(Q, 1/nz).T(), b);
2019  } else {
2020  // Fall-back to QR factorization
2021  return solve(a, b, "qr");
2022  }
2023  }
static MX sparsity_cast(const MX &x, const Sparsity &sp)
Definition: mx.cpp:1260

References casadi::Sparsity::dense(), casadi::MXNode::get_solve_tril(), casadi::MXNode::get_solve_triu(), casadi::Sparsity::is_orthonormal(), casadi::GenericMatrix< MatType >::is_tril(), casadi::GenericMatrix< MatType >::is_triu(), mtimes(), MX(), casadi::GenericMatrix< MatType >::nnz(), casadi::GenericMatrix< MX >::nz(), casadi::Q, sparsity(), sparsity_cast(), and T().

Referenced by casadi::Nlpsol::get_forward(), casadi::Nlpsol::get_reverse(), inv(), mldivide(), mrdivide(), pinv(), and solve().

◆ solve() [2/2]

MX casadi::MX::solve ( const MX a,
const MX b,
const std::string &  lsolver,
const Dict dict = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 2025 of file mx.cpp.

2025  {
2026  if (a.sparsity().is_orthonormal()) return solve(a, b);
2027  Linsol mysolver("tmp_solve", lsolver, a.sparsity(), dict);
2028  return mysolver.solve(a, b, false);
2029  }

References casadi::Sparsity::is_orthonormal(), casadi::Linsol::solve(), solve(), and sparsity().

◆ sparsity()

const Sparsity & casadi::MX::sparsity ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qc

Definition at line 592 of file mx.cpp.

592  {
593  return (*this)->sparsity();
594  }

Referenced by casadi::MXNode::_get_binary(), casadi::Constant< Value >::_get_binary(), casadi::UnaryMX::_get_binary(), casadi::GetNonzeros::ad_forward(), casadi::SetNonzeros< Add >::ad_forward(), casadi::GetNonzeros::ad_reverse(), casadi::Solve< Tr >::ad_reverse(), casadi::SparsityCast::ad_reverse(), casadi::Assertion::Assertion(), casadi::BinaryMX< ScX, ScY >::BinaryMX(), casadi::OptiNode::canon_expr(), casadi::Convexify::Convexify(), casadi::GetNonzerosParam::create(), casadi::SetNonzeros< Add >::create(), casadi::GetNonzeros::create(), cumsum(), densify(), diag(), casadi::Diagsplit::Diagsplit(), casadi::Dot::Dot(), casadi::dplesol(), enlarge(), erase(), casadi::GetNonzeros::eval_mx(), casadi::SetNonzeros< Add >::eval_mx(), expm(), expm_const(), casadi::MXFunction::export_code_body(), casadi::MXFunction::generate_lifted(), get(), casadi::MXNode::get_binary(), casadi::Variable::get_der(), casadi::MXNode::get_dot(), casadi::Nlpsol::get_forward(), casadi::MXNode::get_mac(), casadi::Nlpsol::get_reverse(), casadi::HorzRepmat::HorzRepmat(), casadi::HorzRepsum::HorzRepsum(), casadi::Horzsplit::Horzsplit(), casadi::Scpgen::init(), kron(), lift(), casadi::Low::Low(), matrix_expand(), casadi::Monitor::Monitor(), mtimes(), casadi::Multiplication::Multiplication(), MX(), nullspace(), casadi::Horzcat::off(), casadi::Vertcat::off(), casadi::Diagcat::off(), casadi::DaeBuilderInternal::output(), casadi::OptiNode::parameter(), casadi::MXFunction::print_arg(), project(), casadi::Rank1::Rank1(), casadi::register_symbol(), reshape(), set(), set_nz(), casadi::SetNonzeros< Add >::SetNonzeros(), casadi::SetNonzerosParam< Add >::SetNonzerosParam(), casadi::simpleIRK(), solve(), casadi::Solve< Tr >::Solve(), casadi::Bilin::sp_forward(), casadi::Bilin::sp_reverse(), casadi::Project::sp_reverse(), sparsity_cast(), casadi::Transpose::Transpose(), unite(), casadi::OptiNode::variable(), and casadi::Vertsplit::Vertsplit().

◆ sparsity_cast()

MX casadi::MX::sparsity_cast ( const MX x,
const Sparsity sp 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1260 of file mx.cpp.

1260  {
1261  casadi_assert(x.nnz()==sp.nnz(),
1262  "Mismatching nonzero count: " + str(x.nnz()) + " versus " +
1263  str(sp.nnz()) + ".");
1264 
1265  // Quick return if trivial
1266  if (sp==x.sparsity()) return x;
1267 
1268  // Call internal method
1269  return x->get_sparsity_cast(sp);
1270  }

References casadi::MXNode::get_sparsity_cast(), casadi::GenericMatrix< MatType >::nnz(), casadi::Sparsity::nnz(), sparsity(), and casadi::str().

Referenced by solve().

◆ split_primitives() [1/3]

std::vector< DM > casadi::MX::split_primitives ( const DM x) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qq

Definition at line 957 of file mx.cpp.

957  {
958  std::vector<DM> ret(n_primitives());
959  std::vector<DM>::iterator it=ret.begin();
960  (*this)->split_primitives(x, it);
961  casadi_assert_dev(it==ret.end());
962  return ret;
963  }

References n_primitives().

◆ split_primitives() [2/3]

std::vector< MX > casadi::MX::split_primitives ( const MX x) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qq

Definition at line 941 of file mx.cpp.

941  {
942  std::vector<MX> ret(n_primitives());
943  std::vector<MX>::iterator it=ret.begin();
944  (*this)->split_primitives(x, it);
945  casadi_assert_dev(it==ret.end());
946  return ret;
947  }

References n_primitives().

Referenced by extract_parametric(), and separate_linear().

◆ split_primitives() [3/3]

std::vector< SX > casadi::MX::split_primitives ( const SX x) const

Extra doc: https://github.com/casadi/casadi/wiki/L_qq

Definition at line 949 of file mx.cpp.

949  {
950  std::vector<SX> ret(n_primitives());
951  std::vector<SX>::iterator it=ret.begin();
952  (*this)->split_primitives(x, it);
953  casadi_assert_dev(it==ret.end());
954  return ret;
955  }

References n_primitives().

◆ sprank()

static casadi_int casadi::GenericMatrix< MX >::sprank ( const MX x)
inlinestaticinherited

Definition at line 215 of file generic_matrix.hpp.

215 { return Sparsity::sprank(x.sparsity());}

◆ stop_diff() [1/2]

MX casadi::MX::stop_diff ( const MX expr,
casadi_int  order 
)
static

Functions called by friend functions defined for this class

Definition at line 2541 of file mx.cpp.

2541  {
2542  std::vector<MX> s = symvar(expr);
2543  MX x = veccat(s);
2544  Dict options;
2545  options["never_inline"] = true;
2546 
2547  Dict inline_options;
2548  inline_options["never_inline"] = false;
2549  inline_options["always_inline"] = true;
2550  Dict der_options = Dict{{"forward_options", inline_options},
2551  {"reverse_options", inline_options},
2552  {"jacobian_options", inline_options}};
2553  if (order==1) {
2554  options["is_diff_in"] = std::vector<bool>{false};
2555  options["is_diff_out"] = std::vector<bool>{true};
2556  options = combine(options, der_options);
2557  } else if (order==2) {
2558  options["der_options"] = der_options;
2559  options["forward_options"] = Dict{{"is_diff_in", std::vector<bool>{false, true, true} },
2560  {"is_diff_out", std::vector<bool>{true}}};
2561  options["reverse_options"] = Dict{{"is_diff_in", std::vector<bool>{false, true, true} },
2562  {"is_diff_out", std::vector<bool>{true}}};
2563  options["jacobian_options"] = Dict{{"is_diff_in", std::vector<bool>{false, true} },
2564  {"is_diff_out", std::vector<bool>{false}}};
2565  } else {
2566  casadi_error("stop_diff: order must be 1 or 2, got " + str(order) + ".");
2567  }
2568 
2569  Function FS("FS", {x}, {expr}, {"x"}, {"z"}, options);
2570  return FS(std::vector<MX>{x})[0];
2571  }
Dict combine(const Dict &first, const Dict &second, bool recurse)
Combine two dicts. First has priority.

References casadi::combine(), casadi::str(), symvar(), and casadi::SparsityInterface< MatType >::veccat().

◆ stop_diff() [2/2]

MX casadi::MX::stop_diff ( const MX expr,
const MX var,
casadi_int  order 
)
static

Functions called by friend functions defined for this class

Definition at line 2573 of file mx.cpp.

2573  {
2574  casadi_warning("stop_diff(expr, var, order) is not well tested.");
2575  std::vector<MX> xv = symvar(var);
2576  std::vector<MX> s = symvar(expr);
2577  std::vector<MX> yv = difference(s, xv);
2578 
2579  MX x = veccat(xv);
2580  MX y = veccat(yv);
2581 
2582  Dict options;
2583  options["never_inline"] = true;
2584 
2585  Dict inline_options;
2586  inline_options["never_inline"] = false;
2587  inline_options["always_inline"] = true;
2588  Dict der_options = Dict{{"forward_options", inline_options},
2589  {"reverse_options", inline_options},
2590  {"jacobian_options", inline_options}};
2591  if (order==1) {
2592  options["is_diff_in"] = std::vector<bool>{false, true};
2593  options["is_diff_out"] = std::vector<bool>{true};
2594  options = combine(options, der_options);
2595  } else if (order==2) {
2596  options["der_options"] = der_options;
2597  options["forward_options"] = Dict{{"is_diff_in",
2598  std::vector<bool>{false, true, false, true, true} },
2599  {"is_diff_out", std::vector<bool>{true}}};
2600  options["reverse_options"] = Dict{{"is_diff_in",
2601  std::vector<bool>{false, true, false, true}},
2602  {"is_diff_out", std::vector<bool>{false, true}}};
2603  options["jacobian_options"] = Dict{{"is_diff_in", std::vector<bool>{false, true, true}},
2604  {"is_diff_out", std::vector<bool>{true}}};
2605  } else {
2606  casadi_error("stop_diff: order must be 1 or 2, got " + str(order) + ".");
2607  }
2608 
2609  Function FS("FS", {x, y}, {expr}, {"x", "y"}, {"z"}, options);
2610  return FS(std::vector<MX>{x, y})[0];
2611  }
static std::vector< MX > difference(const std::vector< MX > &a, const std::vector< MX > &b)
Definition: mx.cpp:2613

References casadi::combine(), difference(), casadi::str(), symvar(), and casadi::SparsityInterface< MatType >::veccat().

◆ substitute() [1/2]

MX casadi::MX::substitute ( const MX ex,
const MX v,
const MX vdef 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1424 of file mx.cpp.

1424  {
1425  return substitute(std::vector<MX>{ex}, std::vector<MX>{v}, std::vector<MX>{vdef}).front();
1426  }
static MX substitute(const MX &ex, const MX &v, const MX &vdef)
Definition: mx.cpp:1424

Referenced by casadi::DaeBuilderInternal::transition().

◆ substitute() [2/2]

std::vector< MX > casadi::MX::substitute ( const std::vector< MX > &  ex,
const std::vector< MX > &  v,
const std::vector< MX > &  vdef 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1428 of file mx.cpp.

1429  {
1430  // Assert consistent dimensions
1431  casadi_assert_dev(v.size()==vdef.size());
1432 
1433  // Quick return if all equal
1434  bool all_equal = true;
1435  for (casadi_int k=0; k<v.size(); ++k) {
1436  if (v[k].size()!=vdef[k].size() || !is_equal(v[k], vdef[k])) {
1437  all_equal = false;
1438  break;
1439  }
1440  }
1441  if (all_equal) return ex;
1442 
1443  // Otherwise, evaluate symbolically
1444  Function F("tmp_substitute", v, ex, Dict{{"max_io", 0}, {"allow_free", true}});
1445  std::vector<MX> ret;
1446  F.call(vdef, ret, true);
1447  return ret;
1448  }

References casadi::Function::call(), is_equal(), and casadi::GenericMatrix< MX >::size().

◆ substitute_inplace()

void casadi::MX::substitute_inplace ( const std::vector< MX > &  v,
std::vector< MX > &  vdef,
std::vector< MX > &  ex,
bool  reverse 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1402 of file mx.cpp.

1403  {
1404  casadi_assert(v.size()==vdef.size(),
1405  "Mismatch in the number of expression to substitute.");
1406  for (casadi_int k=0; k<v.size(); ++k) {
1407  casadi_assert(v[k].is_symbolic(),
1408  "Variable " + str(k) + " is not symbolic");
1409  casadi_assert(v[k].size() == vdef[k].size(),
1410  "Inconsistent shape for variable " + str(k) + ".");
1411  }
1412  casadi_assert(reverse==false, "Not implemented");
1413 
1414  // quick return if nothing to replace
1415  if (v.empty()) return;
1416 
1417  // implemented in MXFunction
1418  std::vector<MX> f_out = vdef;
1419  f_out.insert(f_out.end(), ex.begin(), ex.end());
1420  Function temp("tmp_substitute_inplace", {v}, f_out, Dict{{"max_io", 0}, {"allow_free", true}});
1421  temp.get<MXFunction>()->substitute_inplace(vdef, ex);
1422  }
static void substitute_inplace(const std::vector< MX > &v, std::vector< MX > &vdef, std::vector< MX > &ex, bool reverse)
Definition: mx.cpp:1402
static std::vector< std::vector< MX > > reverse(const std::vector< MX > &ex, const std::vector< MX > &arg, const std::vector< std::vector< MX > > &v, const Dict &opts=Dict())
Definition: mx.cpp:1882
bool is_symbolic() const
Check if symbolic.
Definition: mx.cpp:766

References casadi::Function::get(), is_symbolic(), reverse(), casadi::GenericMatrix< MX >::size(), and casadi::str().

◆ sum1()

MX casadi::MX::sum1 ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1384 of file mx.cpp.

1384  {
1385  return mtimes(MX::ones(1, x.size1()), x);
1386  }

References mtimes(), casadi::GenericMatrix< MX >::ones(), and casadi::GenericMatrix< MatType >::size1().

◆ sum2()

MX casadi::MX::sum2 ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1380 of file mx.cpp.

1380  {
1381  return mtimes(x, MX::ones(x.size2(), 1));
1382  }

References mtimes(), casadi::GenericMatrix< MX >::ones(), and casadi::GenericMatrix< MatType >::size2().

◆ sumsqr()

static MX casadi::GenericMatrix< MX >::sumsqr ( const MX x)
inlinestaticinherited

Definition at line 225 of file generic_matrix.hpp.

225 { return dot(x, x);}
friend MX dot(const MX &x, const MX &y)
Inner product of two matrices.

◆ swap()

Definition at line 106 of file generic_shared_impl.hpp.

120  {
121  GenericShared<Shared, Internal> temp = *this;
122  *this = other;
123  other = temp;
124  }

◆ sym() [1/7]

static std::vector<MX > casadi::GenericMatrix< MX >::sym ( const std::string &  name,
casadi_int  nrow,
casadi_int  ncol,
casadi_int  p 
)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1de

Definition at line 1234 of file generic_matrix.hpp.

1235  {
1236  return sym(name, Sparsity::dense(nrow, ncol), p);
1237  }

◆ sym() [2/7]

static std::vector<std::vector<MX > > casadi::GenericMatrix< MX >::sym ( const std::string &  name,
casadi_int  nrow,
casadi_int  ncol,
casadi_int  p,
casadi_int  r 
)
inlinestaticinherited

with nrow-by-ncol symbolic primitives

Extra doc: https://github.com/casadi/casadi/wiki/L_1dg

Definition at line 1253 of file generic_matrix.hpp.

1253  {
1254  return sym(name, Sparsity::dense(nrow, ncol), p, r);
1255  }

◆ sym() [3/7]

static MX casadi::GenericMatrix< MX >::sym ( const std::string &  name,
casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1da

Definition at line 1206 of file generic_matrix.hpp.

1206  {
1207  return sym(name, Sparsity::dense(nrow, ncol));
1208  }

◆ sym() [4/7]

static MX casadi::GenericMatrix< MX >::sym ( const std::string &  name,
const Sparsity sp 
)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1dc

Definition at line 1220 of file generic_matrix.hpp.

1220  {
1221  return MatType::_sym(name, sp);
1222  }

◆ sym() [5/7]

std::vector< MX > casadi::GenericMatrix< MX >::sym ( const std::string &  name,
const Sparsity sp,
casadi_int  p 
)
staticinherited

with symbolic primitives of given sparsity

Extra doc: https://github.com/casadi/casadi/wiki/L_1dd

Definition at line 1229 of file generic_matrix.hpp.

1356  {
1357  std::vector<MatType> ret(p);
1358  std::stringstream ss;
1359  for (casadi_int k=0; k<p; ++k) {
1360  ss.str("");
1361  ss << name << k;
1362  ret[k] = sym(ss.str(), sp);
1363  }
1364  return ret;
1365  }

◆ sym() [6/7]

std::vector< std::vector< MX > > casadi::GenericMatrix< MX >::sym ( const std::string &  name,
const Sparsity sp,
casadi_int  p,
casadi_int  r 
)
staticinherited

symbolic primitives with given sparsity

Extra doc: https://github.com/casadi/casadi/wiki/L_1df

Definition at line 1245 of file generic_matrix.hpp.

1370  {
1371  std::vector<std::vector<MatType> > ret(r);
1372  for (casadi_int k=0; k<r; ++k) {
1373  std::stringstream ss;
1374  ss << name << "_" << k;
1375  ret[k] = sym(ss.str(), sp, p);
1376  }
1377  return ret;
1378  }

◆ sym() [7/7]

static MX casadi::GenericMatrix< MX >::sym ( const std::string &  name,
const std::pair< casadi_int, casadi_int > &  rc 
)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1db

Definition at line 1213 of file generic_matrix.hpp.

1213  {
1214  return sym(name, rc.first, rc.second);
1215  }

◆ symvar()

std::vector< MX > casadi::MX::symvar ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1938 of file mx.cpp.

1938  {
1939  Function f("f", std::vector<MX>{}, {x}, {{"allow_free", true}});
1940  return f.free_mx();
1941  }

References casadi::Function::free_mx().

Referenced by conditional(), if_else(), matrix_expand(), casadi::OptiNode::set_initial(), casadi::OptiNode::set_linear_scale(), casadi::OptiNode::set_value(), stop_diff(), casadi::OptiNode::symvar(), and casadi::OptiNode::update_user_dict().

◆ T()

MX casadi::MX::T ( ) const

◆ tangent()

static MX casadi::GenericMatrix< MX >::tangent ( const MX ex,
const MX arg,
const Dict opts = Dict() 
)
staticinherited

Functions called by friend functions defined here

◆ test_cast()

bool casadi::MX::test_cast ( const SharedObjectInternal ptr)
static

Definition at line 1033 of file mx.cpp.

1033  {
1034  return dynamic_cast<const MXNode*>(ptr)!=nullptr;
1035  }

◆ trace()

MX casadi::MX::trace ( const MX x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1354 of file mx.cpp.

1354  {
1355  casadi_assert(x.is_square(), "trace: must be square");
1356  MX res(0);
1357  for (casadi_int i=0; i < x.size2(); i ++) {
1358  res += x(i, i);
1359  }
1360  return res;
1361  }

References casadi::GenericMatrix< MatType >::is_square(), and casadi::GenericMatrix< MatType >::size2().

◆ tril()

static MX casadi::GenericMatrix< MX >::tril ( const MX x,
bool  includeDiagonal = true 
)
inlinestaticinherited

Definition at line 219 of file generic_matrix.hpp.

219  {
220  return project(x, Sparsity::tril(x.sparsity(), includeDiagonal));
221  }
friend MX project(const MX &A, const Sparsity &sp, bool intersect=false)
Create a new matrix with a given sparsity pattern but with the.

◆ tril2symm()

MX casadi::GenericMatrix< MX >::tril2symm ( const MX x)
staticinherited

Definition at line 230 of file generic_matrix.hpp.

1520  {
1521  casadi_assert(x.is_square(),
1522  "Shape error in tril2symm. Expecting square shape but got " + x.dim());
1523  casadi_assert(x.nnz_upper()-x.nnz_diag()==0,
1524  "Sparsity error in tril2symm. Found above-diagonal entries in argument: " + x.dim());
1525  return x + x.T() - diag(diag(x));
1526  }
friend MX diag(const MX &A)
Get the diagonal of a matrix or construct a diagonal.

◆ triu()

static MX casadi::GenericMatrix< MX >::triu ( const MX x,
bool  includeDiagonal = true 
)
inlinestaticinherited

Definition at line 222 of file generic_matrix.hpp.

222  {
223  return project(x, Sparsity::triu(x.sparsity(), includeDiagonal));
224  }

◆ triu2symm()

MX casadi::GenericMatrix< MX >::triu2symm ( const MX x)
staticinherited

Definition at line 231 of file generic_matrix.hpp.

1544  {
1545  casadi_assert(x.is_square(),
1546  "Shape error in triu2symm. Expecting square shape but got " + x.dim());
1547  casadi_assert(x.nnz_lower()-x.nnz_diag()==0,
1548  "Sparsity error in triu2symm. Found below-diagonal entries in argument: " + x.dim());
1549  return x + x.T() - diag(diag(x));
1550  }

◆ type_name()

static std::string casadi::MX::type_name ( )
inlinestatic

Extra doc: https://github.com/casadi/casadi/wiki/L_pz

Definition at line 97 of file mx.hpp.

97 {return "MX";}

◆ unary()

MX casadi::MX::unary ( casadi_int  op,
const MX x 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_r1

Definition at line 540 of file mx.cpp.

540  {
541  return x->get_unary(Operation(op));
542  }
Operation
Enum for quick access to any node.
Definition: calculus.hpp:60

References casadi::MXNode::get_unary(), and op().

◆ unite()

MX casadi::MX::unite ( const MX A,
const MX B 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1328 of file mx.cpp.

1328  {
1329  // Join the sparsity patterns
1330  std::vector<unsigned char> mapping;
1331  Sparsity sp = A.sparsity().unite(B.sparsity(), mapping);
1332 
1333  // Split up the mapping
1334  std::vector<casadi_int> nzA, nzB;
1335 
1336  // Copy sparsity
1337  for (casadi_int k=0; k<mapping.size(); ++k) {
1338  if (mapping[k]==1) {
1339  nzA.push_back(k);
1340  } else if (mapping[k]==2) {
1341  nzB.push_back(k);
1342  } else {
1343  throw CasadiException("Pattern intersection not empty");
1344  }
1345  }
1346 
1347  // Create mapping
1348  MX ret = MX::zeros(sp);
1349  ret = A->get_nzassign(ret, nzA);
1350  ret = B->get_nzassign(ret, nzB);
1351  return ret;
1352  }
Sparsity unite(const Sparsity &y, std::vector< unsigned char > &mapping) const
Union of two sparsity patterns.
Definition: sparsity.cpp:409

References casadi::MXNode::get_nzassign(), mapping(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MatType >::sparsity(), sparsity(), casadi::Sparsity::unite(), and casadi::GenericMatrix< MX >::zeros().

◆ vertcat()

MX casadi::MX::vertcat ( const std::vector< MX > &  x)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1099 of file mx.cpp.

1099  {
1100  // Check dimensions
1101  if (x.size()>1) {
1102  std::vector<MX> ne = trim_empty(x, true);
1103  for (casadi_int i=0;i<ne.size();i++) {
1104  casadi_assert(ne[i].size2()==ne[0].size2(),
1105  "vertcat dimension mismatch x[" + str(i) + "]:" + ne[i].dim() +
1106  " and x[0]: " + ne[0].dim() + ".");
1107  }
1108  }
1109 
1110  if (x.empty()) {
1111  return MX(0, 1);
1112  } else if (x.size()==1) {
1113  return x.front();
1114  } else if (has_empty(x)) {
1115  std::vector<MX> ret = trim_empty(x);
1116  if (ret.empty()) {
1117  // We still want vertcat(zeros(5,0),zeros(5,0)) -> zeros(10,0)
1118  ret = trim_empty(x, true);
1119  casadi_int s = 0;
1120  casadi_int ncol = 0;
1121  for (casadi_int i=0;i<ret.size();++i) {
1122  s+= ret[i].size1();
1123  casadi_assert_dev(ncol==0 || ret[i].size2()==ncol);
1124  ncol = ret[i].size2();
1125  }
1126  return MX::zeros(s, ncol);
1127  } else {
1128  return vertcat(ret);
1129  }
1130  } else if (!x.front().is_column()) {
1131  // Vertcat operation only supports vectors, rewrite using horzcat
1132  std::vector<MX> xT = x;
1133  for (std::vector<MX>::iterator i=xT.begin(); i!=xT.end(); ++i) *i = i->T();
1134  return horzcat(xT).T();
1135  } else {
1136  return x.front()->get_vertcat(x);
1137  }
1138  }
virtual MX get_vertcat(const std::vector< MX > &x) const
Create a vertical concatenation node (vectors only)
Definition: mx_node.cpp:1123

References casadi::GenericMatrix< MX >::dim(), casadi::MXNode::get_vertcat(), casadi::has_empty(), horzcat(), MX(), casadi::GenericExpression< MX >::ne(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MX >::size2(), casadi::str(), T(), casadi::trim_empty(), and casadi::GenericMatrix< MX >::zeros().

Referenced by blockcat(), casadi::Nlpsol::get_forward(), casadi::Nlpsol::get_reverse(), and casadi::Blocksqp::init().

◆ vertsplit()

std::vector< MX > casadi::MX::vertsplit ( const MX x,
const std::vector< casadi_int > &  offset 
)
static

Functions called by friend functions defined for SparsityInterface

Definition at line 1174 of file mx.cpp.

1174  {
1175  if (x.is_column()) {
1176  // Consistency check
1177  casadi_assert_dev(!offset.empty());
1178  casadi_assert_dev(offset.front()==0);
1179  casadi_assert_dev(offset.back()==x.size1());
1180  casadi_assert_dev(is_monotone(offset));
1181 
1182  // Trivial return if possible
1183  if (offset.size()==1) {
1184  return std::vector<MX>();
1185  } else if (offset.size()==2) {
1186  return std::vector<MX>(1, x);
1187  } else {
1188  return x->get_vertsplit(offset);
1189  }
1190  } else {
1191  std::vector<MX> ret = horzsplit(x.T(), offset);
1192  for (auto&& e : ret) e = e.T();
1193  return ret;
1194  }
1195  }
static std::vector< MX > horzsplit(const MX &x, const std::vector< casadi_int > &offset)
Definition: mx.cpp:1140

References casadi::MXNode::get_vertsplit(), horzsplit(), casadi::GenericMatrix< MatType >::is_column(), casadi::is_monotone(), casadi::SparsityInterface< MatType >::offset(), casadi::GenericMatrix< MatType >::size1(), and T().

◆ weak()

Extra doc: https://github.com/casadi/casadi/wiki/L_aw

Definition at line 131 of file generic_shared_impl.hpp.

132  {
133  return (*this)->weak();
134  }

◆ which_depends()

std::vector< bool > casadi::MX::which_depends ( const MX expr,
const MX var,
casadi_int  order = 1,
bool  tr = false 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 1914 of file mx.cpp.

1914  {
1915  return _which_depends(expr, var, order, tr);
1916  }
std::vector< bool > _which_depends(const MatType &expr, const MatType &var, casadi_int order, bool tr)

References casadi::_which_depends().

◆ which_function()

Function casadi::MX::which_function ( ) const

Definition at line 778 of file mx.cpp.

778  {
779  return (*this)->which_function();
780  }

Referenced by casadi::DaeBuilderInternal::create(), cse(), and casadi::BlazingSplineFunction::merge().

◆ which_output()

casadi_int casadi::MX::which_output ( ) const

Definition at line 790 of file mx.cpp.

790  {
791  return (*this)->which_output();
792  }

Referenced by casadi::DaeBuilderInternal::create(), cse(), and casadi::DaeBuilderInternal::jac_vdef_v_from_calls().

◆ zeros() [1/3]

static MX casadi::GenericMatrix< MX >::zeros ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1dh

Definition at line 1262 of file generic_matrix.hpp.

1262  {
1263  return zeros(Sparsity::dense(nrow, ncol));
1264  }

◆ zeros() [2/3]

static MX casadi::GenericMatrix< MX >::zeros ( const Sparsity sp)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1dh

Definition at line 1265 of file generic_matrix.hpp.

1265 { return MatType(sp, 0, false);}

◆ zeros() [3/3]

static MX casadi::GenericMatrix< MX >::zeros ( const std::pair< casadi_int, casadi_int > &  rc)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_1dh

Definition at line 1266 of file generic_matrix.hpp.

1266  {
1267  return zeros(rc.first, rc.second);
1268  }

The documentation for this class was generated from the following files: