List of all members | Public Types | Public Member Functions | Static Public 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.

Each node is a matrix-valued operation (symbolic primitive, constant, binary operation, concatenation, matrix product, call to a Function, ...), so an expression such as A+1 is a small number of nodes regardless of the matrix sizes. The graph is immutable: every operation creates new nodes. Symbolic primitives are created with MX.sym; MX(...) creates constants.

>>> x = MX.sym("x", 2)
>>> A = MX.sym("A", 2, 2)
>>> e = A @ x + 1
>>> e
MX((ones(2x1)+mac(A,x,zeros(2x1))))
>>> e.shape, e.n_dep(), e.is_symbolic()
((2, 1), 2, False)
>>> f = Function("f", [A, x], [e])
>>> f(DM([[1, 2], [3, 4]]), DM([1, 1]))
DM([4, 8])

>> x = MX.sym('x', 2); A = MX.sym('A', 2, 2);
>> e = A*x + 1;
>> e
(ones(2x1)+mac(A,x,zeros(2x1)))
>> size(e), e.n_dep(), e.is_symbolic()
2 1
2
0
>> f = Function('f', {A, x}, {e});
>> f(DM([1 2; 3 4]), DM([1; 1]))
[4, 8]

julia> x = ca.MX.sym("x", 2); A = ca.MX.sym("A", 2, 2);
julia> e = A * x + 1;
julia> e
MX((ones(2x1)+mac(A,x,zeros(2x1))))
julia> size(e), ca.n_dep(e), ca.is_symbolic(e)
((2, 1), 2, false)
julia> f = ca.Function("f", [A, x], [e]);
julia> f([1 2; 3 4], [1, 1])
DM([4, 8])

See also
SX, DM, Function
Author
Joel Andersson
Date
2010-2011

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

Definition at line 141 of file mx.hpp.

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

Public Types

typedef std::map< std::string, MX > MXDict
 Readability typedef. More...
 
using internal_base_type = SharedObjectInternal
 
using base_type = SharedObject
 

Public Member Functions

 MX ()
 Default constructor: a 0-by-0 constant. More...
 
 MX (casadi_int nrow, casadi_int ncol)
 Create a sparse matrix with all structural zeros. More...
 
 MX (const Sparsity &sp)
 Create a constant with a given sparsity pattern and all nonzeros equal to one. More...
 
 MX (const Sparsity &sp, const MX &val)
 Construct a matrix with a given sparsity pattern and given nonzeros. More...
 
 MX (const Sparsity &sp, const std::string &fname)
 Construct a constant whose nonzeros are read from a text file. More...
 
 MX (const Matrix< double > &val, const std::string &name)
 Construct a named constant that generated code exposes as a modifiable pool. More...
 
 MX (double x)
 Create a 1-by-1 constant (also used for implicit conversion from a number) More...
 
 MX (const Matrix< double > &x)
 Create a constant from a DM (also used for implicit conversion) More...
 
bool __nonzero__ () const
 Truth value of a 1-by-1 constant. More...
 
Sparsity get_sparsity () const
 Get an owning reference to the sparsity pattern. More...
 
std::vector< MX > get_nonzeros () const
 Get the nonzeros as a list of 1-by-1 expressions. 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 elements (leaving structural zeros in their 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
 Unary minus. More...
 
MX operator+ () const
 Unary plus: returns the expression itself. More...
 
MX inv () const
 Element-wise inverse, 1/x. More...
 
MX dep (casadi_int ch=0) const
 Get the ch-th dependency (child node) of this node. More...
 
casadi_int n_out () const
 Number of outputs of this node. More...
 
MX get_output (casadi_int oind) const
 Get an output of a multiple-output node. More...
 
casadi_int n_dep () const
 Get the number of dependencies (child nodes) of this node. More...
 
std::string name () const
 Get the name of a symbolic primitive. More...
 
 operator double () const
 Get the value of a 1-by-1 constant node (float conversion) More...
 
 operator Matrix< double > () const
 Get the value of a constant node as a DM. More...
 
bool is_symbolic () const
 Check if the node is a symbolic primitive (created by MX.sym) More...
 
bool is_constant () const
 Check if the node is a constant. More...
 
bool is_integer () const
 Check if the node is a constant with integer-valued nonzeros. More...
 
bool is_doubled () const
 Check if the node is the sum of two equal expressions, or 2*x. More...
 
bool is_call () const
 Check if the node is a call to a Function. More...
 
Function which_function () const
 Get the Function of a call node. More...
 
bool is_output () const
 Check if the node selects one output of a multiple-output node. More...
 
bool has_output () const
 Check if this is a multiple-output node. More...
 
casadi_int which_output () const
 Get the output index of an OutputNode. More...
 
bool is_op (casadi_int op) const
 Check if the node is a certain operation. More...
 
bool is_multiplication () const
 Check if the node is a matrix product (mtimes) More...
 
bool is_commutative () const
 Check if a binary operation is commutative. More...
 
bool is_norm () const
 Check if the node is a norm (norm_1, norm_2, norm_inf or norm_fro) More...
 
bool is_valid_input () const
 Check if the expression can be used as a Function input. More...
 
casadi_int n_primitives () const
 Get the number of symbolic primitives in a valid Function input. More...
 
std::vector< MX > primitives () const
 Get the symbolic primitives of a valid Function input. More...
 
bool is_eye () const
 Check if the node is a constant identity matrix. More...
 
bool is_zero () const
 Check if the node is a constant zero (false negatives are possible) More...
 
bool is_one () const
 Check if the node is a constant with all nonzeros one (false negatives possible) More...
 
bool is_minus_one () const
 Check if the node is a constant with all nonzeros -1 (false negatives possible) More...
 
bool is_half () const
 Check if the node is a constant with all nonzeros 0.5 (false negatives possible) More...
 
bool is_value (double val) const
 Check if the node is a constant with all nonzeros equal to val. More...
 
bool is_inf () const
 Check if the node is a constant with all nonzeros +inf (false negatives possible) More...
 
bool is_minus_inf () const
 Check if the node is a constant with all nonzeros -inf (false negatives possible) More...
 
bool is_transpose () const
 Check if the node is a transpose operation. More...
 
bool is_regular () const
 Check that a constant node contains neither NaN nor Inf. More...
 
bool is_binary () const
 Check if the node is an elementwise binary operation. More...
 
bool is_nonnegative () const
 Check if a value is always nonnegative (false negatives are allowed) More...
 
bool is_unary () const
 Check if the node is an elementwise unary operation. More...
 
casadi_int op () const
 Get the operation type of the node. More...
 
Dict info () const
 Obtain information about the node as a dictionary. More...
 
void serialize (SerializingStream &s) const
 Serialize the expression to a stream. More...
 
MX printme (const MX &b) const
 Return the expression with the side effect of printing its value with a tag. More...
 
MX attachAssert (const MX &y, const std::string &fail_message="") const
 Return the expression with a run-time assertion attached. More...
 
MX monitor (const std::string &comment) const
 Return the expression with the side effect of printing its value. More...
 
MX dump (const std::string &base_filename, const Dict &opts=Dict()) const
 Return the expression with the side effect of writing its value to a file. More...
 
void reset_dump_count ()
 Reset the file counter of a dump node; raises an error on any other node. More...
 
MX T () const
 Transpose the matrix. More...
 
Matrix< casadi_int > mapping () const
 Get the nonzero mapping of a GetNonzeros or SetNonzeros node. More...
 
void eval_mx (const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique=std::vector< bool >()) const
 Re-apply the operation of this node to new dependencies. More...
 
casadi_int nnz () const
 Get the number of structural nonzeros. More...
 
casadi_int nnz_lower () const
 Get the number of structural nonzeros on or below the diagonal. More...
 
casadi_int nnz_upper () const
 Get the number of structural nonzeros on or above the diagonal. More...
 
casadi_int nnz_diag () const
 Get the number of structural nonzeros on the diagonal. More...
 
casadi_int numel () const
 Get the number of elements, size1()*size2() 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 (same as size1()) 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 (same as size2()) 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 as a pair (number of rows, number of columns) More...
 
casadi_int size (casadi_int axis) const
 Get the size along a particular dimension. More...
 
bool is_empty (bool both=false) const
 Check if the matrix is empty, i.e. if one of the dimensions is zero. More...
 
bool is_dense () const
 Check if the matrix is dense, i.e. nnz() equals numel() More...
 
bool is_scalar (bool scalar_and_dense=false) const
 Check if the matrix is scalar, i.e. 1-by-1. More...
 
bool is_square () const
 Check if the matrix is square, i.e. size1() equals size2() 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...
 
Sparsity sparsity () const
 Get the sparsity pattern. 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...
 
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...
 
std::vector< MX > split_primitives (const MX &x) const
 Split a value along the symbolic primitives of this expression. More...
 
std::vector< SX > split_primitives (const SX &x) const
 Split a value along the symbolic primitives of this expression. More...
 
std::vector< DM > split_primitives (const DM &x) const
 Split a value along the symbolic primitives of this expression. More...
 
MX join_primitives (const std::vector< MX > &v) const
 Join values along the symbolic primitives of this expression. More...
 
SX join_primitives (const std::vector< SX > &v) const
 Join values along the symbolic primitives of this expression. More...
 
DM join_primitives (const std::vector< DM > &v) const
 Join values along the symbolic primitives of this expression. More...
 
void get (MX &m, bool ind1, const Slice &rr) const
 Get a submatrix by linear index (what x[k] does) More...
 
void get (MX &m, bool ind1, const Matrix< casadi_int > &rr) const
 Get a submatrix by linear index (what x[k] does) More...
 
void get (MX &m, bool ind1, const Sparsity &sp) const
 Get a submatrix by linear index (what x[k] does) More...
 
void get (MX &m, bool ind1, const MX &rr) const
 Get a submatrix by linear index (what x[k] does) More...
 
void get (MX &m, bool ind1, const casadi_int rr) const
 Get a submatrix by linear index (what x[k] does) More...
 
void get (MX &m, bool ind1, const Slice &rr, const Slice &cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, const Slice &rr, casadi_int cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, const Matrix< casadi_int > &rr, const Slice &cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, casadi_int rr, const Slice &cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, const Matrix< casadi_int > &rr, const Matrix< casadi_int > &cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, casadi_int rr, casadi_int cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, const MX &rr, const Slice &cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, const Slice &rr, const MX &cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void get (MX &m, bool ind1, const MX &rr, const MX &cc) const
 Get a submatrix by row and column index (what x[i, j] does) More...
 
void set (const MX &m, bool ind1, const Slice &rr)
 Assign to a submatrix by linear index (what x[k] = m does) More...
 
void set (const MX &m, bool ind1, const Matrix< casadi_int > &rr)
 Assign to a submatrix by linear index (what x[k] = m does) More...
 
void set (const MX &m, bool ind1, const Sparsity &sp)
 Assign to a submatrix by linear index (what x[k] = m does) More...
 
void set (const MX &m, bool ind1, const Slice &rr, const Slice &cc)
 Assign to a submatrix by row and column index (what x[i, j] = m does) More...
 
void set (const MX &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc)
 Assign to a submatrix by row and column index (what x[i, j] = m does) More...
 
void set (const MX &m, bool ind1, const Matrix< casadi_int > &rr, const Slice &cc)
 Assign to a submatrix by row and column index (what x[i, j] = m does) More...
 
void set (const MX &m, bool ind1, const Matrix< casadi_int > &rr, const Matrix< casadi_int > &cc)
 Assign to a submatrix by row and column index (what x[i, j] = m does) More...
 
void get_nz (MX &m, bool ind1, const Slice &kk) const
 Get a set of nonzeros (what x.nz[k] does) More...
 
void get_nz (MX &m, bool ind1, const Matrix< casadi_int > &kk) const
 Get a set of nonzeros (what x.nz[k] does) More...
 
void get_nz (MX &m, bool ind1, const MX &kk) const
 Get a set of nonzeros (what x.nz[k] does) More...
 
void get_nz (MX &m, bool ind1, casadi_int kk) const
 Get a set of nonzeros (what x.nz[k] does) More...
 
void get_nz (MX &m, bool ind1, const MX &inner, const Slice &outer) const
 Get a set of nonzeros (what x.nz[k] does) More...
 
void get_nz (MX &m, bool ind1, const Slice &inner, const MX &outer) const
 Get a set of nonzeros (what x.nz[k] does) More...
 
void get_nz (MX &m, bool ind1, const MX &inner, const MX &outer) const
 Get a set of nonzeros (what x.nz[k] does) More...
 
void set_nz (const MX &m, bool ind1, const Slice &kk)
 Assign to a set of nonzeros (what x.nz[k] = m does) More...
 
void set_nz (const MX &m, bool ind1, const Matrix< casadi_int > &kk)
 Assign to a set of nonzeros (what x.nz[k] = m does) More...
 
void set_nz (const MX &m, bool ind1, const MX &kk)
 Assign to a set of nonzeros (what x.nz[k] = m does) More...
 
void set_nz (const MX &m, bool ind1, casadi_int kk)
 Assign to a set of nonzeros (what x.nz[k] = m does) 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...
 
casadi_int row (casadi_int el) 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 deserialize (DeserializingStream &s)
 Deserialize an expression from a stream, with type disambiguation. More...
 
static MX eye (casadi_int n)
 Identity matrix constant of size n-by-n, with a diagonal sparsity pattern. More...
 
static bool simplify_ref_count (std::vector< MX > &arg, std::vector< MX > &res, const Dict &opts=Dict())
 
static bool simplify_const_folding (std::vector< MX > &arg, std::vector< MX > &res, const Dict &opts=Dict())
 
static bool simplify_combine_terms (std::vector< MX > &arg, std::vector< MX > &res, const Dict &opts=Dict())
 
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())
 Evaluate the B-spline basis functions at given points. More...
 
static MX interpn_linear (const std::vector< MX > &x, const MX &v, const std::vector< MX > &xq, const Dict &opts=Dict())
 Low-level inlined multilinear interpolation. More...
 
static void set_max_depth (casadi_int eq_depth=1)
 Set the depth to which equalities are checked for simplifications. More...
 
static casadi_int get_max_depth ()
 Get the depth to which equalities are checked for simplifications. More...
 
static bool test_cast (const SharedObjectInternal *ptr)
 Check if a SharedObjectInternal pointer refers to an MX node (C++ only) More...
 
static std::vector< MX > get_input (const Function &f)
 Get the symbolic inputs of a Function, as MX. More...
 
static std::vector< MX > get_free (const Function &f)
 Get the free variables of a Function. More...
 
static MX logsumexp (const MX &x)
 
static MX binary (casadi_int op, const MX &x, const MX &y, bool unique_x=false, bool unique_y=false)
 Create an elementwise binary or unary node from an operation code. More...
 
static MX unary (casadi_int op, const MX &x, bool unique=false)
 Create an elementwise binary or unary node from an operation code. More...
 
static MX inf (const Sparsity &sp)
 Create a constant with all nonzeros +inf. More...
 
static MX inf (casadi_int nrow=1, casadi_int ncol=1)
 Create a constant with all nonzeros +inf. More...
 
static MX inf (const std::pair< casadi_int, casadi_int > &rc)
 Create a constant with all nonzeros +inf. More...
 
static MX nan (const Sparsity &sp)
 Create a constant with all nonzeros NaN. More...
 
static MX nan (casadi_int nrow=1, casadi_int ncol=1)
 Create a constant with all nonzeros NaN. More...
 
static MX nan (const std::pair< casadi_int, casadi_int > &rc)
 Create a constant with all nonzeros 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)
 Compute a dense tensor contraction in Einstein notation. 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)
 Compute a dense tensor contraction in Einstein notation. 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, as a pair (nrow, ncol) More...
 
static MX sym (const std::string &name, const Sparsity &sp)
 Create symbolic primitive with a given sparsity pattern. More...
 
static std::vector< MX > sym (const std::string &name, const Sparsity &sp, casadi_int p)
 Create a list of length p of symbolic primitives with a given sparsity. More...
 
static std::vector< MX > sym (const std::string &name, casadi_int nrow, casadi_int ncol, casadi_int p)
 Create a list of length p of 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 list of length r of lists of length p of symbolic primitives. 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 list of length r of lists of length p of nrow-by-ncol symbolic primitives. 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...
 

Friends

MX find (const MX &x)
 Find the index of the first nonzero entry of a vector. More...
 
MX low (const MX &v, const MX &p, const Dict &options=Dict())
 Find the interval of a sorted grid that contains each query value. More...
 
MX graph_substitute (const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
 Substitute nodes in the expression graph, by node identity. More...
 
MX graph_substitute (const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
 
std::vector< MX > graph_substitute (const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
 Substitute nodes in several expressions at once, by node identity. More...
 
std::vector< MX > graph_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 an MX expression into a call to an SX Function. More...
 
std::vector< MX > matrix_expand (const std::vector< MX > &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
 Expand several MX expressions into one call to an SX Function. 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())
 Evaluate a tensor-product B-spline with fixed coefficients. More...
 
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())
 Evaluate a B-spline whose coefficients are an expression. More...
 
MX bspline (const MX &x, const MX &coeffs, const std::vector< MX > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
 Evaluate a B-spline whose knots and coefficients are expressions. More...
 
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())
 Evaluate the B-spline basis functions at given points, see MX::bspline_dual. More...
 
MX convexify (const MX &H, const Dict &opts=Dict())
 Make a symmetric matrix positive definite at evaluation time. More...
 
MX lift (const MX &x, const MX &x_guess)
 Lift the expression. More...
 
MX inv_node (const MX &x)
 Symbolic matrix inverse node. More...
 
DM evalf (const MX &expr)
 Evaluate an expression without free symbols numerically. More...
 
MX stop_diff (const MX &expr, casadi_int order)
 Stop derivatives of an expression with respect to all its symbolic variables. More...
 
MX no_grad (const MX &expr)
 Treat an expression as constant for differentiation (stop first derivatives) More...
 
MX no_hess (const MX &expr)
 Stop second derivatives of an expression wrt all its symbolic variables. More...
 
MX stop_diff (const MX &expr, const MX &var, casadi_int order)
 Stop derivatives of an expression wrt a selected set of symbolic variables. More...
 
std::vector< MX > difference (const std::vector< MX > &a, const std::vector< MX > &b)
 Return all elements of a that do not occur in b, preserving order. More...
 

Member Typedef Documentation

◆ base_type

Definition at line 103 of file shared_object.hpp.

◆ internal_base_type

using casadi::SharedObject::internal_base_type = SharedObjectInternal
inherited

Definition at line 102 of file shared_object.hpp.

◆ MXDict

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

Definition at line 5053 of file mx.hpp.

Constructor & Destructor Documentation

◆ MX() [1/8]

casadi::MX::MX ( )

The result is an empty constant, not a symbolic primitive (use MX.sym for that).

>>> MX(), MX().shape, MX().is_constant()
(MX(0x0), (0, 0), True)

>> MX(), size(MX()), MX().is_constant()
0x0
0 0
1

julia> ca.MX(), size(ca.MX()), ca.is_constant(ca.MX())
(0x0, (0, 0), true)

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

◆ MX() [2/8]

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

The result is a constant of shape nrow-by-ncol without any nonzeros. It is the usual starting point for building a matrix by assignment: assigning to an element grows the sparsity pattern.

>>> Z = MX(2, 3)
>>> Z, Z.nnz(), Z.is_zero()
(MX(zeros(2x3,0nz)), 0, True)
>>> Z[1, 2] = MX.sym("x")
>>> Z.sparsity()
Sparsity(2x3,1nz)

>> Z = MX(2, 3);
>> Z, Z.nnz(), Z.is_zero()
zeros(2x3,0nz)
0
1
>> Z(2, 3) = MX.sym('x');
>> Z.sparsity()
2x3,1nz

julia> Z = ca.MX(2, 3);
julia> Z, ca.nnz(Z), ca.is_zero(Z)
(zeros(2x3,0nz), 0, true)
julia> Z[2, 3] = ca.MX.sym("x");
julia> ca.sparsity(Z)
Sparsity(2x3,1nz)

See also
MX.zeros, MX.sym

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

◆ MX() [3/8]

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

Same as MX::ones(sp).

>>> MX(Sparsity.lower(2))
MX(ones(2x2,3nz))
>>> evalf(MX(Sparsity.lower(2)))
DM(
[[1, 00],
 [1, 1]])

>> MX(Sparsity.lower(2))
ones(2x2,3nz)
>> evalf(MX(Sparsity.lower(2)))
[[1, 00],
 [1, 1]]

julia> ca.MX(ca.Sparsity.lower(2))
MX(ones(2x2,3nz))
julia> ca.evalf(ca.MX(ca.Sparsity.lower(2)))
DM(
[[1, 00],
 [1, 1]])

See also
MX.ones

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

◆ MX() [4/8]

casadi::MX::MX ( const Sparsity &  sp,
const MX &  val 
)
Parameters
spSparsity pattern of the result
valEither a scalar, which is repeated over all nonzeros of sp, or a column vector with exactly sp.nnz() entries, which become the nonzeros of the result in column-major order. A matrix whose shape is a reshape of sp is reshaped. Anything else is an error.

val may be symbolic; the result is then an expression, not a constant.

>>> v = MX.sym("v", 3)
>>> L = MX(Sparsity.lower(2), v)
>>> L.sparsity(), L.is_constant()
(Sparsity(2x2,3nz), False)
>>> Function("f", [v], [L])(DM([1, 2, 3]))
DM(
[[1, 00],
 [2, 3]])
>>> evalf(MX(Sparsity.lower(2), MX(7)))
DM(
[[7, 00],
 [7, 7]])

>> v = MX.sym('v', 3);
>> L = MX(Sparsity.lower(2), v);
>> L.sparsity(), L.is_constant()
2x2,3nz
0
>> f = Function('f', {v}, {L});
>> f(DM([1; 2; 3]))
[[1, 00],
 [2, 3]]
>> evalf(MX(Sparsity.lower(2), MX(7)))
[[7, 00],
 [7, 7]]

julia> v = ca.MX.sym("v", 3);
julia> L = ca.MX(ca.Sparsity.lower(2), v);
julia> ca.sparsity(L), ca.is_constant(L)
(2x2,3nz, false)
julia> ca.Function("f", [v], [L])([1, 2, 3])
DM(
[[1, 00],
 [2, 3]])
julia> ca.evalf(ca.MX(ca.Sparsity.lower(2), ca.MX(7)))
DM(
[[7, 00],
 [7, 7]])

See also
MX.sym, sparsity_cast

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

◆ MX() [5/8]

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

The file must contain exactly sp.nnz() whitespace-separated numbers, giving the nonzeros in column-major order; otherwise an error is raised. In generated C code the file is read at initialization time rather than embedded in the source. The node cannot be converted with to_DM.

Parameters
fnamePath of the file, read when the constructor runs

>>> try:
...   MX(Sparsity.lower(2), "no_such_file.txt")
... except Exception as e:
...   print("Cannot open file" in str(e))
True

>> try, MX(Sparsity.lower(2), 'no_such_file.txt'); catch e, disp('cannot open file'); end
cannot open file

julia> try
           ca.MX(ca.Sparsity.lower(2), "no_such_file.txt")
       catch e
           println(occursin("Cannot open file", sprint(showerror, e)))
       end
true

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

◆ MX() [6/8]

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

Behaves like the constant val during evaluation. In generated C code the nonzeros are placed in a named double pool so that they can be changed after compilation. The node cannot be converted with to_DM.

>>> p = MX(DM([1, 2]), "p")
>>> p, p.is_constant()
(MX(constant_pool('p'): [1, 2]), True)
>>> evalf(2*p)
DM([2, 4])

>> p = MX(DM([1; 2]), 'p');
>> p, p.is_constant()
constant_pool('p'): [1, 2]
1
>> evalf(2*p)
[2, 4]

julia> p = ca.MX(ca.DM([1, 2]), "p");
julia> p, ca.is_constant(p)
(constant_pool('p'): [1, 2], true)
julia> ca.evalf(2 * p)
DM([2, 4])

See also
CodeGenerator

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

◆ MX() [7/8]

casadi::MX::MX ( double  x)

>>> c = MX(3.5)
>>> c, c.is_constant(), c.shape, float(c)
(MX(3.5), True, (1, 1), 3.5)

>> c = MX(3.5);
>> c, c.is_constant(), size(c), c.to_double()
3.5
1
1 1
3.5000

julia> c = ca.MX(3.5);
julia> c, ca.is_constant(c), size(c), Float64(c)
(3.5, true, (1, 1), 3.5)

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

◆ MX() [8/8]

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

The sparsity pattern of x is kept. Numbers and numeric arrays are accepted through the DM conversion. SX cannot be converted to MX this way.

>>> c = MX(DM(Sparsity.diag(2), [1, 2]))
>>> c.sparsity(), c.is_constant()
(Sparsity(2x2,2nz), True)
>>> MX([1, 2, 3]).shape
(3, 1)
>>> MX(np.array([[1, 2], [3, 4]]))
MX(
[[1, 2],
 [3, 4]])

>> c = MX(DM(Sparsity.diag(2), [1; 2]));
>> c.sparsity(), c.is_constant()
2x2,2nz
1
>> size(MX([1; 2; 3]))
3 1
>> MX([1 2; 3 4])
[[1, 2],
 [3, 4]]

julia> c = ca.MX(ca.DM(ca.Sparsity.diag(2), [1, 2]));
julia> ca.sparsity(c), ca.is_constant(c)
(2x2,2nz, true)
julia> size(ca.MX([1, 2, 3]))
(3, 1)
julia> ca.MX([1 2; 3 4])
MX(
[[1, 2],
 [3, 4]])

See also
to_DM, evalf

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

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 123 of file generic_shared_impl.hpp.

◆ __nonzero__()

bool casadi::MX::__nonzero__ ( ) const

Nonzero constants are true, zero is false. Symbolic expressions and non-scalar constants raise an error: use a Function or evalf to evaluate first.

>>> bool(MX(2)), bool(MX(0))
(True, False)
>>> try:
...   bool(MX.sym("x"))
... except Exception as e:
...   print("Can only determine truth value of a numeric MX" in str(e))
True

julia> Bool(ca.MX(1)), Bool(ca.MX(0))
(true, false)
julia> try
           Bool(ca.MX.sym("x"))
       catch e
           println(occursin("constant scalar", sprint(showerror, e)))
       end
true

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

◆ attachAssert()

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

The result is equal to this expression, but when it is evaluated the scalar expression y is evaluated too and a runtime error with fail_message is raised unless y equals 1. Useful to check preconditions inside a Function. A non-scalar y raises an error at construction.

>>> x = MX.sym("x")
>>> e = x.attachAssert(x > 0, "x must be positive")
>>> e
MX(assertion(x, (0<x)))
>>> f = Function("f", [x], [e])
>>> f(1)
DM(1)
>>> try:
...   f(-1)
... except Exception as e:
...   print("Assertion error: x must be positive" in str(e))
True

>> x = MX.sym('x');
>> e = x.attachAssert(x > 0, 'x must be positive');
>> e
assertion(x, (0<x))
>> f = Function('f', {x}, {e});
>> f(1)
1
>> try, f(-1); catch err, disp('assertion error'); end
assertion error

julia> x = ca.MX.sym("x");
julia> e = ca.attachAssert(x, x > 0, "x must be positive");
julia> e
MX(assertion(x, (0<x)))
julia> f = ca.Function("f", [x], [e]);
julia> f(1)
DM(1)
julia> try
           f(-1)
       catch err
           println(occursin("Assertion error: x must be positive", sprint(showerror, err)))
       end
true

See also
monitor, printme

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

Examples
misc/assertion.py.

◆ bilin()

static MX casadi::GenericMatrix< MX >::bilin ( const MX &  A,
const MX &  x,
const MX &  y 
)
staticinherited

x and y must be vectors (row vectors are transposed, sparse vectors densified) with x.numel() == A.size1() and y.numel() == A.size2(); otherwise an error is raised. The result is scalar. When y is omitted, the quadratic form x^T A x is calculated.

>>> A = DM([[1, 2], [3, 4]])
>>> bilin(A, DM([1, 1]), DM([1, 2]))
DM(16)
>>> bilin(A, DM([1, 1]))
DM(10)
>>> bilin(MX.sym("A", 2, 2), MX.sym("x", 2), MX.sym("y", 2))
MX(bilin(A, x, y))

>> A = DM([1 2; 3 4]);
>> bilin(A, DM([1; 1]), DM([1; 2]))
16
>> bilin(A, DM([1; 1]))
10
>> bilin(MX.sym('A', 2, 2), MX.sym('x', 2), MX.sym('y', 2))
bilin(A, x, y)

julia> A = ca.DM([1 2; 3 4]);
julia> ca.bilin(A, ca.DM([1, 1]), ca.DM([1, 2]))
DM(16)
julia> ca.bilin(A, ca.DM([1, 1]))
DM(10)
julia> ca.bilin(ca.MX.sym("A", 2, 2), ca.MX.sym("x", 2), ca.MX.sym("y", 2))
MX(bilin(A, x, y))

See also
rank1, dot, mtimes

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

◆ binary()

static MX casadi::MX::binary ( casadi_int  op,
const MX &  x,
const MX &  y,
bool  unique_x = false,
bool  unique_y = false 
)
static

Low-level constructors behind the operators: MX.binary(OP_ADD, x, y) is x + y. Operands must have the same shape, or one of them is a scalar, or one is a horizontal repetition of the other (which is repmat-ed). The usual simplifications are applied. An invalid operation code is not checked.

Parameters
opOne of the OP_* constants
unique_x,unique_yInternal hints for in-place simplification; leave false

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> MX.binary(OP_ADD, x, y), MX.unary(OP_SIN, x)
(MX((x+y)), MX(sin(x)))
>>> MX.binary(OP_ADD, MX.sym("V", 2, 4), MX.sym("W", 2, 2))
MX((V+repmat(W, 2)))
>>> try:
...   MX.binary(OP_ADD, MX.sym("v", 2), MX.sym("w", 3))
... except Exception as e:
...   print("Dimension mismatch" in str(e))
True

>> x = MX.sym('x'); y = MX.sym('y');
>> MX.binary(OP_ADD, x, y), MX.unary(OP_SIN, x)
(x+y)
sin(x)
>> MX.binary(OP_ADD, MX.sym('V', 2, 4), MX.sym('W', 2, 2))
(V+repmat(W, 2))
>> v = MX.sym('v', 2); w = MX.sym('w', 3);
>> try, MX.binary(OP_ADD, v, w); catch e, disp('dimension mismatch'); end
dimension mismatch

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> ca.MX.binary(ca.OP_ADD, x, y), ca.MX.unary(ca.OP_SIN, x)
((x+y), sin(x))
julia> ca.MX.binary(ca.OP_ADD, ca.MX.sym("V", 2, 4), ca.MX.sym("W", 2, 2))
MX((V+repmat(W, 2)))
julia> try
           ca.MX.binary(ca.OP_ADD, ca.MX.sym("v", 2), ca.MX.sym("w", 3))
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

See also
op

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

◆ bspline_dual()

static 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

Returns the sparse matrix of basis function values such that bspline_dual(x, knots, degree) @ coeffs equals bspline(x, coeffs, knots, degree, 1) at the same points. Each row corresponds to one evaluation point and each row sums to one inside the knot range. Used to fit coefficients to data.

Parameters
xEvaluation points, flattened: n_points * n_dims numbers, point by point
knotsOne knot vector per dimension
degreeDegree per dimension
opts"lookup_mode": list with one of "linear", "exact", "binary" per dimension
Returns
n_points-by-n_coeffs DM

>>> knots = [[0, 0, 0, 0, 1, 2, 3, 3, 3, 3]]
>>> B = MX.bspline_dual([0.5, 1.5], knots, [3])
>>> B.shape, sum2(B)
((2, 6), DM([1, 1]))
>>> B @ DM(range(6))
DM([1.17708, 2.5])

>> knots = {[0 0 0 0 1 2 3 3 3 3]};
>> B = MX.bspline_dual([0.5, 1.5], knots, 3);
>> size(B), sum2(B)
2 6
[1, 1]
>> B * DM((0:5)')
[1.17708, 2.5]

julia> knots = [[0, 0, 0, 0, 1, 2, 3, 3, 3, 3]];
julia> B = ca.MX.bspline_dual([0.5, 1.5], knots, [3]);
julia> size(B), sum(B, dims=2)
((2, 6), [1, 1])
julia> B * ca.DM(0:5)
DM([1.17708, 2.5])

See also
bspline

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

◆ class_name()

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

◆ colind()

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

The pattern is stored in compressed column format: colind has size2()+1 entries, the nonzeros of column j are the entries colind(j) up to colind(j+1) (exclusive), and row(el) is the row index of nonzero el. Without argument, the full vectors are returned. Out-of-range indices raise an error.

>>> x = DM(Sparsity.diag(2), [5, 6])
>>> x.row(), x.colind()
([0, 1], [0, 1, 2])
>>> x.row(1), x.colind(2)
(1, 2)

>> x = DM(Sparsity.diag(2), [5; 6]);
>> x.row(), x.colind()
0 1
0 1 2
>> fprintf('%d %d\n', x.row(1), x.colind(2))
1 2

julia> x = ca.DM(ca.Sparsity.diag(2), [5, 6]);
julia> ca.row(x), ca.colind(x)
([0, 1], [0, 1, 2])
julia> ca.row(x, 1), ca.colind(x, 2)
(1, 2)

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

Definition at line 609 of file generic_matrix.hpp.

◆ columns()

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

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

Definition at line 295 of file generic_matrix.hpp.

◆ debug_repr()

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

Definition at line 112 of file generic_shared_impl.hpp.

◆ dep()

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

Together with n_dep, op and is_op this allows walking the expression graph. Symbolic primitives and constants have no dependencies.

Parameters
chIndex in [0, n_dep()); out of range raises an error

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> e = x*y + 1
>>> e.n_dep(), e.dep(0), e.dep(1)
(2, MX(1), MX((x*y)))
>>> e.dep(1).dep(0), x.n_dep()
(MX(x), 0)

>> x = MX.sym('x'); y = MX.sym('y');
>> e = x*y + 1;
>> e.n_dep(), e.dep(0), e.dep(1)
2
1
(x*y)
>> e.dep(1).dep(0), x.n_dep()
x
0

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> e = x * y + 1;
julia> ca.n_dep(e), ca.dep(e, 0), ca.dep(e, 1)
(2, 1, (x*y))
julia> ca.dep(ca.dep(e, 1), 0), ca.n_dep(x)
(x, 0)

See also
n_dep, op, is_op

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

◆ deserialize()

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

Counterpart of serialize; use StringDeserializer.unpack from Python.

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

◆ dim()

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

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

>>> DM.eye(3).dim(), DM.eye(3).dim(True), DM.ones(3, 3).dim(True)
('3x3', '3x3,3nz', '3x3,9nz')

>> fprintf('%s %s %s\n', DM.eye(3).dim(), DM.eye(3).dim(true), DM.ones(3, 3).dim(true))
3x3 3x3,3nz 3x3,9nz

julia> ca.dim(ca.DM.eye(3)), ca.dim(ca.DM.eye(3), true), ca.dim(ca.DM.ones(3, 3), true)
("3x3", "3x3,3nz", "3x3,9nz")

See also
size, nnz

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

◆ disp()

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

◆ dump()

MX casadi::MX::dump ( const std::string &  base_filename,
const Dict &  opts = Dict() 
) const

Each evaluation writes the value to a new file named after base_filename and a running counter. The value is unchanged. A matrix without nonzeros is returned as is.

Parameters
base_filenamePrefix of the file names
opts"dir" (directory, default "."), "format" (file format, default "mtx", Matrix Market), "verbose" (print the file name on each dump, default false). Unknown keys raise an error.
See also
monitor, reset_dump_count

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

◆ einstein() [1/2]

static 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 C_c += A_a * B_b, where a, b and c are index labels in Einstein notation: a label appearing in both a and b is summed over, the remaining labels index the result. Tensors are passed as flat column vectors in column-major (Fortran) order together with their dimensions. Labels are encoded as strictly negative integers. For the matrix product C_ij = A_ik B_kj one writes a = [-1, -3], b = [-3, -2], c = [-1, -2].

Parameters
A,BDense column vectors with product(dim_a), product(dim_b) entries
COptional accumulator with product(dim_c) entries, added to the result; when omitted the result starts from zero
dim_a,dim_b,dim_cTensor dimensions
a,b,cNegative labels, one per dimension
Returns
Column vector with product(dim_c) entries

Inconsistent dimensions or labels are not validated with a user-facing message. Also available for SX and DM.

>>> A = MX.sym("A", 6)
>>> B = MX.sym("B", 6)
>>> C = einstein(A, B, [2, 3], [3, 2], [2, 2], [-1, -3], [-3, -2], [-1, -2])
>>> C.shape
(4, 1)
>>> f = Function("f", [A, B], [C])
>>> f(DM(range(1, 7)), DM(range(1, 7)))
DM([22, 28, 49, 64])
>>> reshape(DM(range(1, 7)), 2, 3) @ reshape(DM(range(1, 7)), 3, 2)
DM(
[[22, 49],
 [28, 64]])

>> A = MX.sym('A', 6); B = MX.sym('B', 6);
>> C = einstein(A, B, [2 3], [3 2], [2 2], [-1 -3], [-3 -2], [-1 -2]);
>> size(C)
4 1
>> f = Function('f', {A, B}, {C});
>> f(DM((1:6)'), DM((1:6)'))
[22, 28, 49, 64]
>> reshape(DM((1:6)'), 2, 3) * reshape(DM((1:6)'), 3, 2)
[[22, 49],
 [28, 64]]

julia> A = ca.MX.sym("A", 6); B = ca.MX.sym("B", 6);
julia> C = ca.einstein(A, B, [2, 3], [3, 2], [2, 2], [-1, -3], [-3, -2], [-1, -2]);
julia> size(C)
(4, 1)
julia> f = ca.Function("f", [A, B], [C]);
julia> f(1:6, 1:6)
DM([22, 28, 49, 64])
julia> reshape(ca.DM(1:6), 2, 3) * reshape(ca.DM(1:6), 3, 2)
DM(
[[22, 49],
 [28, 64]])

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

◆ einstein() [2/2]

static 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 C_c += A_a * B_b, where a, b and c are index labels in Einstein notation: a label appearing in both a and b is summed over, the remaining labels index the result. Tensors are passed as flat column vectors in column-major (Fortran) order together with their dimensions. Labels are encoded as strictly negative integers. For the matrix product C_ij = A_ik B_kj one writes a = [-1, -3], b = [-3, -2], c = [-1, -2].

Parameters
A,BDense column vectors with product(dim_a), product(dim_b) entries
COptional accumulator with product(dim_c) entries, added to the result; when omitted the result starts from zero
dim_a,dim_b,dim_cTensor dimensions
a,b,cNegative labels, one per dimension
Returns
Column vector with product(dim_c) entries

Inconsistent dimensions or labels are not validated with a user-facing message. Also available for SX and DM.

>>> A = MX.sym("A", 6)
>>> B = MX.sym("B", 6)
>>> C = einstein(A, B, [2, 3], [3, 2], [2, 2], [-1, -3], [-3, -2], [-1, -2])
>>> C.shape
(4, 1)
>>> f = Function("f", [A, B], [C])
>>> f(DM(range(1, 7)), DM(range(1, 7)))
DM([22, 28, 49, 64])
>>> reshape(DM(range(1, 7)), 2, 3) @ reshape(DM(range(1, 7)), 3, 2)
DM(
[[22, 49],
 [28, 64]])

>> A = MX.sym('A', 6); B = MX.sym('B', 6);
>> C = einstein(A, B, [2 3], [3 2], [2 2], [-1 -3], [-3 -2], [-1 -2]);
>> size(C)
4 1
>> f = Function('f', {A, B}, {C});
>> f(DM((1:6)'), DM((1:6)'))
[22, 28, 49, 64]
>> reshape(DM((1:6)'), 2, 3) * reshape(DM((1:6)'), 3, 2)
[[22, 49],
 [28, 64]]

julia> A = ca.MX.sym("A", 6); B = ca.MX.sym("B", 6);
julia> C = ca.einstein(A, B, [2, 3], [3, 2], [2, 2], [-1, -3], [-3, -2], [-1, -2]);
julia> size(C)
(4, 1)
julia> f = ca.Function("f", [A, B], [C]);
julia> f(1:6, 1:6)
DM([22, 28, 49, 64])
julia> reshape(ca.DM(1:6), 2, 3) * reshape(ca.DM(1:6), 3, 2)
DM(
[[22, 49],
 [28, 64]])

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

◆ 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 
)

Makes the matrix nrow-by-ncol by inserting empty rows and columns, keeping the existing nonzeros. Modifies the expression in place.

Parameters
nrowNew number of rows
ncolNew number of columns
rrRows of the new matrix that receive the existing rows; one entry per existing row
ccColumns of the new matrix that receive the existing columns; one entry per existing column
ind1True for 1-based indices (MATLAB), default 0-based

>>> B = MX.sym("B", 2, 2)
>>> B.enlarge(3, 3, [0, 2], [0, 2])
>>> B.shape, B.nnz(), B.sparsity().row()
((3, 3), 4, [0, 2, 0, 2])

>> B = MX.sym('B', 2, 2);
>> B.enlarge(3, 3, [1, 3], [1, 3], true);
>> size(B), B.nnz(), B.sparsity().row()
3 3
4
0 2 0 2

julia> B = ca.MX.sym("B", 2, 2);
julia> ca.enlarge(B, 3, 3, [0, 2], [0, 2]);
julia> size(B), ca.nnz(B), ca.row(ca.sparsity(B))
((3, 3), 4, [0, 2, 0, 2])

See also
erase, Sparsity.enlarge

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

◆ erase() [1/2]

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

Same as the two-index overload, but rr holds linear (column-major) element indices.

>>> B = MX.sym("B", 3, 3)
>>> B.erase([0, 4])
>>> B.sparsity()
Sparsity(3x3,7nz)

>> B = MX.sym('B', 3, 3);
>> B.erase([1, 5], true);
>> B.sparsity()
3x3,7nz

julia> B = ca.MX.sym("B", 3, 3);
julia> ca.erase(B, [0, 4]);
julia> ca.sparsity(B)
Sparsity(3x3,7nz)

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

◆ erase() [2/2]

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

Removes the nonzeros at the intersection of rows rr and columns cc from the sparsity pattern. The shape is unchanged. Modifies the expression in place by replacing it with a new node that references the remaining nonzeros.

Parameters
rrRow indices to erase; out-of-range values raise an error
ccColumn indices to erase
ind1True for 1-based indices (MATLAB), default 0-based

>>> B = MX.sym("B", 3, 3)
>>> B.erase([0], [1])
>>> B.sparsity(), B.shape
(Sparsity(3x3,8nz), (3, 3))

>> B = MX.sym('B', 3, 3);
>> B.erase(1, 2, true);
>> B.sparsity(), size(B)
3x3,8nz
3 3

julia> B = ca.MX.sym("B", 3, 3);
julia> ca.erase(B, [0], [1]);
julia> ca.sparsity(B), size(B)
(3x3,8nz, (3, 3))

See also
enlarge, Sparsity.erase

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

◆ eval_mx()

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

Creates the same operation as this node, applied to arg instead of the node's own dependencies, with the usual simplifications. arg must have exactly n_dep() entries of matching shapes; a mismatch is a developer error. Symbolic primitives and constants return the node itself. Call nodes are re-called with the new arguments; OutputNodes cannot be evaluated this way. Mainly useful for writing graph transformations.

Parameters
argNew dependencies
uniqueInternal hints that the arguments are not referenced elsewhere; leave empty
Returns
List with n_out() results

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> (x * y).eval_mx([MX(2), MX(3)]), (x + y).eval_mx([y, x])
([MX(6)], [MX((y+x))])
>>> MX.sym("v", 3)[1].eval_mx([MX([4, 5, 6])])
[MX([4, 5, 6][1])]

>> x = MX.sym('x'); y = MX.sym('y');
>> e = x * y; r = e.eval_mx({MX(2), MX(3)});
>> numel(r), r{1}
1
6
>> e = x + y; r = e.eval_mx({y, x});
>> r{1}
(y+x)
>> v = MX.sym('v', 3); e = v(2); r = e.eval_mx({MX([4; 5; 6])});
>> r{1}
[4, 5, 6][1]

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> ca.eval_mx(x * y, [ca.MX(2), ca.MX(3)]), ca.eval_mx(x + y, [y, x])
(MX[6], MX[(y+x)])
julia> ca.eval_mx(ca.MX.sym("v", 3)[2], [ca.MX([4, 5, 6])])
1-element Vector{MX}:
 MX([4, 5, 6][1])

See also
dep, n_dep, graph_substitute

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

◆ eye()

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

>>> MX.eye(2), MX.eye(2).is_eye()
(MX(ones(2x2,2nz)), True)

>> MX.eye(2), MX.eye(2).is_eye()
ones(2x2,2nz)
1

julia> ca.MX.eye(2), ca.is_eye(ca.MX.eye(2))
(ones(2x2,2nz), true)

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

◆ get() [1/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

Definition at line 2962 of file mx.hpp.

◆ get() [2/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

Definition at line 2957 of file mx.hpp.

◆ get() [3/15]

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

Backs single-index subscripting. Elements are addressed in column-major order over all numel() entries, including structural zeros. Indices may be negative (counted from the end); out-of-range indices raise an error. The result is a new GetNonzeros node referencing the selected nonzeros of this expression; a structural zero is returned as an empty-sparsity constant. Accepted index types: integer, slice, list of integers, list of booleans (mask), integer DM, Sparsity pattern of the same shape (returns the projection on that pattern) and MX (a symbolic index into the nonzeros of a dense expression, evaluated at run time with truncation and NaN for out-of-range values).

Parameters
ind1True for 1-based indices (MATLAB), false for 0-based
rrElement indices

>>> X = MX.sym("X", 3, 3)
>>> X[4], X[-1], X[[0, 4]], X[1:3].shape
(MX(X[4]), MX(X[8]), MX(X[:8:4]), (2, 1))
>>> try:
...   X[9]
... except Exception as e:
...   print("Out of bounds" in str(e))
True
>>> S = MX.sym("S", Sparsity.lower(2))
>>> S[2], S[2].nnz()
(MX(00), 0)
>>> X[Sparsity.lower(3)]
MX(project(X))
>>> k = MX.sym("k")
>>> v = MX.sym("v", 4)
>>> v[k], Function("f", [v, k], [v[k]])(DM([10, 20, 30, 40]), 1.7)
(MX(v[k]), DM(20))

>> X = MX.sym('X', 3, 3);
>> a = X(5); b = X(end); c = X([1, 5]); d = X([2; 3]);
>> a, b, c, size(d)
X[4]
X[8]
X[:8:4]
2 1
>> try, X(10); catch e, disp('out of bounds'); end
out of bounds
>> S = MX.sym('S', Sparsity.lower(2));
>> e = S(3);
>> e, e.nnz()
00
0
>> X(Sparsity.lower(3))
project(X)
>> k = MX.sym('k'); v = MX.sym('v', 4);
>> f = Function('f', {v, k}, {v(k)});
>> v(k), f(DM([10; 20; 30; 40]), 2.7)
v[(k-1)]
20

julia> X = ca.MX.sym("X", 3, 3);
julia> X[5], X[end], X[[1, 5]], size(X[2:3])
(X[4], X[8], X[[0, 4]], (2, 1))
julia> try
           X[10]
       catch e
           println(e isa BoundsError)
       end
true
julia> S = ca.MX.sym("S", ca.Sparsity.lower(2));
julia> S[3], ca.nnz(S[3])
(00, 0)
julia> X[ca.Sparsity.lower(3)]
MX(project(X))
julia> k = ca.MX.sym("k"); v = ca.MX.sym("v", 4);
julia> v[k]
MX(v[(k-1)])
julia> ca.Function("f", [v, k], [v[k]])([10, 20, 30, 40], 2.7)
DM(20)

See also
set, get_nz, nz

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

Definition at line 2891 of file mx.hpp.

◆ get() [4/15]

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

Backs single-index subscripting. Elements are addressed in column-major order over all numel() entries, including structural zeros. Indices may be negative (counted from the end); out-of-range indices raise an error. The result is a new GetNonzeros node referencing the selected nonzeros of this expression; a structural zero is returned as an empty-sparsity constant. Accepted index types: integer, slice, list of integers, list of booleans (mask), integer DM, Sparsity pattern of the same shape (returns the projection on that pattern) and MX (a symbolic index into the nonzeros of a dense expression, evaluated at run time with truncation and NaN for out-of-range values).

Parameters
ind1True for 1-based indices (MATLAB), false for 0-based
rrElement indices

>>> X = MX.sym("X", 3, 3)
>>> X[4], X[-1], X[[0, 4]], X[1:3].shape
(MX(X[4]), MX(X[8]), MX(X[:8:4]), (2, 1))
>>> try:
...   X[9]
... except Exception as e:
...   print("Out of bounds" in str(e))
True
>>> S = MX.sym("S", Sparsity.lower(2))
>>> S[2], S[2].nnz()
(MX(00), 0)
>>> X[Sparsity.lower(3)]
MX(project(X))
>>> k = MX.sym("k")
>>> v = MX.sym("v", 4)
>>> v[k], Function("f", [v, k], [v[k]])(DM([10, 20, 30, 40]), 1.7)
(MX(v[k]), DM(20))

>> X = MX.sym('X', 3, 3);
>> a = X(5); b = X(end); c = X([1, 5]); d = X([2; 3]);
>> a, b, c, size(d)
X[4]
X[8]
X[:8:4]
2 1
>> try, X(10); catch e, disp('out of bounds'); end
out of bounds
>> S = MX.sym('S', Sparsity.lower(2));
>> e = S(3);
>> e, e.nnz()
00
0
>> X(Sparsity.lower(3))
project(X)
>> k = MX.sym('k'); v = MX.sym('v', 4);
>> f = Function('f', {v, k}, {v(k)});
>> v(k), f(DM([10; 20; 30; 40]), 2.7)
v[(k-1)]
20

julia> X = ca.MX.sym("X", 3, 3);
julia> X[5], X[end], X[[1, 5]], size(X[2:3])
(X[4], X[8], X[[0, 4]], (2, 1))
julia> try
           X[10]
       catch e
           println(e isa BoundsError)
       end
true
julia> S = ca.MX.sym("S", ca.Sparsity.lower(2));
julia> S[3], ca.nnz(S[3])
(00, 0)
julia> X[ca.Sparsity.lower(3)]
MX(project(X))
julia> k = ca.MX.sym("k"); v = ca.MX.sym("v", 4);
julia> v[k]
MX(v[(k-1)])
julia> ca.Function("f", [v, k], [v[k]])([10, 20, 30, 40], 2.7)
DM(20)

See also
set, get_nz, nz

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

◆ get() [5/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

◆ get() [6/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

◆ get() [7/15]

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

Backs single-index subscripting. Elements are addressed in column-major order over all numel() entries, including structural zeros. Indices may be negative (counted from the end); out-of-range indices raise an error. The result is a new GetNonzeros node referencing the selected nonzeros of this expression; a structural zero is returned as an empty-sparsity constant. Accepted index types: integer, slice, list of integers, list of booleans (mask), integer DM, Sparsity pattern of the same shape (returns the projection on that pattern) and MX (a symbolic index into the nonzeros of a dense expression, evaluated at run time with truncation and NaN for out-of-range values).

Parameters
ind1True for 1-based indices (MATLAB), false for 0-based
rrElement indices

>>> X = MX.sym("X", 3, 3)
>>> X[4], X[-1], X[[0, 4]], X[1:3].shape
(MX(X[4]), MX(X[8]), MX(X[:8:4]), (2, 1))
>>> try:
...   X[9]
... except Exception as e:
...   print("Out of bounds" in str(e))
True
>>> S = MX.sym("S", Sparsity.lower(2))
>>> S[2], S[2].nnz()
(MX(00), 0)
>>> X[Sparsity.lower(3)]
MX(project(X))
>>> k = MX.sym("k")
>>> v = MX.sym("v", 4)
>>> v[k], Function("f", [v, k], [v[k]])(DM([10, 20, 30, 40]), 1.7)
(MX(v[k]), DM(20))

>> X = MX.sym('X', 3, 3);
>> a = X(5); b = X(end); c = X([1, 5]); d = X([2; 3]);
>> a, b, c, size(d)
X[4]
X[8]
X[:8:4]
2 1
>> try, X(10); catch e, disp('out of bounds'); end
out of bounds
>> S = MX.sym('S', Sparsity.lower(2));
>> e = S(3);
>> e, e.nnz()
00
0
>> X(Sparsity.lower(3))
project(X)
>> k = MX.sym('k'); v = MX.sym('v', 4);
>> f = Function('f', {v, k}, {v(k)});
>> v(k), f(DM([10; 20; 30; 40]), 2.7)
v[(k-1)]
20

julia> X = ca.MX.sym("X", 3, 3);
julia> X[5], X[end], X[[1, 5]], size(X[2:3])
(X[4], X[8], X[[0, 4]], (2, 1))
julia> try
           X[10]
       catch e
           println(e isa BoundsError)
       end
true
julia> S = ca.MX.sym("S", ca.Sparsity.lower(2));
julia> S[3], ca.nnz(S[3])
(00, 0)
julia> X[ca.Sparsity.lower(3)]
MX(project(X))
julia> k = ca.MX.sym("k"); v = ca.MX.sym("v", 4);
julia> v[k]
MX(v[(k-1)])
julia> ca.Function("f", [v, k], [v[k]])([10, 20, 30, 40], 2.7)
DM(20)

See also
set, get_nz, nz

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

◆ get() [8/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

◆ get() [9/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

◆ get() [10/15]

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

Backs single-index subscripting. Elements are addressed in column-major order over all numel() entries, including structural zeros. Indices may be negative (counted from the end); out-of-range indices raise an error. The result is a new GetNonzeros node referencing the selected nonzeros of this expression; a structural zero is returned as an empty-sparsity constant. Accepted index types: integer, slice, list of integers, list of booleans (mask), integer DM, Sparsity pattern of the same shape (returns the projection on that pattern) and MX (a symbolic index into the nonzeros of a dense expression, evaluated at run time with truncation and NaN for out-of-range values).

Parameters
ind1True for 1-based indices (MATLAB), false for 0-based
rrElement indices

>>> X = MX.sym("X", 3, 3)
>>> X[4], X[-1], X[[0, 4]], X[1:3].shape
(MX(X[4]), MX(X[8]), MX(X[:8:4]), (2, 1))
>>> try:
...   X[9]
... except Exception as e:
...   print("Out of bounds" in str(e))
True
>>> S = MX.sym("S", Sparsity.lower(2))
>>> S[2], S[2].nnz()
(MX(00), 0)
>>> X[Sparsity.lower(3)]
MX(project(X))
>>> k = MX.sym("k")
>>> v = MX.sym("v", 4)
>>> v[k], Function("f", [v, k], [v[k]])(DM([10, 20, 30, 40]), 1.7)
(MX(v[k]), DM(20))

>> X = MX.sym('X', 3, 3);
>> a = X(5); b = X(end); c = X([1, 5]); d = X([2; 3]);
>> a, b, c, size(d)
X[4]
X[8]
X[:8:4]
2 1
>> try, X(10); catch e, disp('out of bounds'); end
out of bounds
>> S = MX.sym('S', Sparsity.lower(2));
>> e = S(3);
>> e, e.nnz()
00
0
>> X(Sparsity.lower(3))
project(X)
>> k = MX.sym('k'); v = MX.sym('v', 4);
>> f = Function('f', {v, k}, {v(k)});
>> v(k), f(DM([10; 20; 30; 40]), 2.7)
v[(k-1)]
20

julia> X = ca.MX.sym("X", 3, 3);
julia> X[5], X[end], X[[1, 5]], size(X[2:3])
(X[4], X[8], X[[0, 4]], (2, 1))
julia> try
           X[10]
       catch e
           println(e isa BoundsError)
       end
true
julia> S = ca.MX.sym("S", ca.Sparsity.lower(2));
julia> S[3], ca.nnz(S[3])
(00, 0)
julia> X[ca.Sparsity.lower(3)]
MX(project(X))
julia> k = ca.MX.sym("k"); v = ca.MX.sym("v", 4);
julia> v[k]
MX(v[(k-1)])
julia> ca.Function("f", [v, k], [v[k]])([10, 20, 30, 40], 2.7)
DM(20)

See also
set, get_nz, nz

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

◆ get() [11/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

Definition at line 2953 of file mx.hpp.

◆ get() [12/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

◆ get() [13/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

◆ get() [14/15]

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

Backs two-index subscripting. rr and cc select rows and columns (integer, slice, list, integer DM); the result has shape len(rr)-by-len(cc) and the corresponding part of the sparsity pattern. Out-of-range indices raise an error. An MX index is only allowed for dense expressions and selects at run time.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1], X[1, :].shape, X[[0, 2], [0, 2]].shape
(MX(X[3]), (1, 3), (2, 2))
>>> S = MX.sym("S", Sparsity.lower(3))
>>> S[:, 0].sparsity(), S[0, 2].nnz()
(Sparsity(3x1), 0)
>>> k = MX.sym("k")
>>> X[k, 1]
MX(X[(k;3:6:3)])

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(2, :); c = X([1 3], [1 3]);
>> a, size(b), size(c)
X[3]
1 3
2 2
>> S = MX.sym('S', Sparsity.lower(3));
>> a = S(:, 1); b = S(1, 3);
>> a.sparsity(), b.nnz()
3x1
0
>> k = MX.sym('k');
>> X(k, 2)
X[((k-1);3)]

julia> X = ca.MX.sym("X", 3, 3);
julia> X[1, 2], size(X[2, :]), size(X[[1, 3], [1, 3]])
(X[3], (1, 3), (2, 2))
julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> ca.sparsity(S[:, 1]), ca.nnz(S[1, 3])
(3x1, 0)

See also
set

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

◆ get() [15/15]

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

Backs single-index subscripting. Elements are addressed in column-major order over all numel() entries, including structural zeros. Indices may be negative (counted from the end); out-of-range indices raise an error. The result is a new GetNonzeros node referencing the selected nonzeros of this expression; a structural zero is returned as an empty-sparsity constant. Accepted index types: integer, slice, list of integers, list of booleans (mask), integer DM, Sparsity pattern of the same shape (returns the projection on that pattern) and MX (a symbolic index into the nonzeros of a dense expression, evaluated at run time with truncation and NaN for out-of-range values).

Parameters
ind1True for 1-based indices (MATLAB), false for 0-based
rrElement indices

>>> X = MX.sym("X", 3, 3)
>>> X[4], X[-1], X[[0, 4]], X[1:3].shape
(MX(X[4]), MX(X[8]), MX(X[:8:4]), (2, 1))
>>> try:
...   X[9]
... except Exception as e:
...   print("Out of bounds" in str(e))
True
>>> S = MX.sym("S", Sparsity.lower(2))
>>> S[2], S[2].nnz()
(MX(00), 0)
>>> X[Sparsity.lower(3)]
MX(project(X))
>>> k = MX.sym("k")
>>> v = MX.sym("v", 4)
>>> v[k], Function("f", [v, k], [v[k]])(DM([10, 20, 30, 40]), 1.7)
(MX(v[k]), DM(20))

>> X = MX.sym('X', 3, 3);
>> a = X(5); b = X(end); c = X([1, 5]); d = X([2; 3]);
>> a, b, c, size(d)
X[4]
X[8]
X[:8:4]
2 1
>> try, X(10); catch e, disp('out of bounds'); end
out of bounds
>> S = MX.sym('S', Sparsity.lower(2));
>> e = S(3);
>> e, e.nnz()
00
0
>> X(Sparsity.lower(3))
project(X)
>> k = MX.sym('k'); v = MX.sym('v', 4);
>> f = Function('f', {v, k}, {v(k)});
>> v(k), f(DM([10; 20; 30; 40]), 2.7)
v[(k-1)]
20

julia> X = ca.MX.sym("X", 3, 3);
julia> X[5], X[end], X[[1, 5]], size(X[2:3])
(X[4], X[8], X[[0, 4]], (2, 1))
julia> try
           X[10]
       catch e
           println(e isa BoundsError)
       end
true
julia> S = ca.MX.sym("S", ca.Sparsity.lower(2));
julia> S[3], ca.nnz(S[3])
(00, 0)
julia> X[ca.Sparsity.lower(3)]
MX(project(X))
julia> k = ca.MX.sym("k"); v = ca.MX.sym("v", 4);
julia> v[k]
MX(v[(k-1)])
julia> ca.Function("f", [v, k], [v[k]])([10, 20, 30, 40], 2.7)
DM(20)

See also
set, get_nz, nz

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

◆ get_colind()

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

The pattern is stored in compressed column format: colind has size2()+1 entries, the nonzeros of column j are the entries colind(j) up to colind(j+1) (exclusive), and row(el) is the row index of nonzero el. Without argument, the full vectors are returned. Out-of-range indices raise an error.

>>> x = DM(Sparsity.diag(2), [5, 6])
>>> x.row(), x.colind()
([0, 1], [0, 1, 2])
>>> x.row(1), x.colind(2)
(1, 2)

>> x = DM(Sparsity.diag(2), [5; 6]);
>> x.row(), x.colind()
0 1
0 1 2
>> fprintf('%d %d\n', x.row(1), x.colind(2))
1 2

julia> x = ca.DM(ca.Sparsity.diag(2), [5, 6]);
julia> ca.row(x), ca.colind(x)
([0, 1], [0, 1, 2])
julia> ca.row(x, 1), ca.colind(x, 2)
(1, 2)

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

Definition at line 603 of file generic_matrix.hpp.

◆ get_free()

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

Same as f.free_mx(): the symbolic primitives that occur in the outputs of an MX Function without being an input. Such a Function can only be created with the option "allow_free" and cannot be evaluated until the free variables are bound.

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> f = Function("f", [x], [x + y], {"allow_free": True})
>>> MX.get_free(f), MX.get_free(Function("g", [x], [x]))
([MX(y)], [])
>>> try:
...   Function("f", [x], [x + y])
... except Exception as e:
...   print("variables [y] are free" in str(e))
True

>> x = MX.sym('x'); y = MX.sym('y');
>> f = Function('f', {x}, {x + y}, struct('allow_free', true));
>> v = MX.get_free(f);
>> numel(v), v{1}
1
y
>> numel(MX.get_free(Function('g', {x}, {x})))
0
>> try, Function('f', {x}, {x + y}); catch e, disp('free variables'); end
free variables

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> f = ca.Function("f", [x], [x + y], Dict("allow_free" => true));
julia> ca.MX.get_free(f), ca.MX.get_free(ca.Function("g", [x], [x]))
(MX[y], MX[])
julia> try
           ca.Function("f", [x], [x + y])
       catch e
           println(occursin("variables [y] are free", sprint(showerror, e)))
       end
true

See also
Function.free_mx, symvar

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

◆ get_input()

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

Same as f.mx_in(). For an MX Function these are the original input expressions; for other Functions fresh symbols with the input names and sparsities.

>>> x = MX.sym("x")
>>> f = Function("f", [x], [2*x])
>>> MX.get_input(f), is_equal(MX.get_input(f)[0], x)
([MX(x)], True)

>> x = MX.sym('x');
>> f = Function('f', {x}, {2*x});
>> v = MX.get_input(f);
>> numel(v), v{1}, is_equal(v{1}, x)
1
x
1

julia> x = ca.MX.sym("x");
julia> f = ca.Function("f", [x], [2 * x]);
julia> ca.MX.get_input(f), ca.is_equal(ca.MX.get_input(f)[1], x)
(MX[x], true)

See also
Function.mx_in, get_free

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

◆ get_max_depth()

static casadi_int casadi::MX::get_max_depth ( )
static

◆ get_nonzeros()

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

Since an MX is not a container, each entry is an expression that selects one nonzero of this expression. When the expression satisfies is_valid_input, the entries are the scalar primitives themselves: for vertcat(x, y) with scalar symbols x and y the result is [x, y]. The list has nnz() entries, in column-major order.

See also
x.nz[:], primitives

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

◆ get_nz() [1/7]

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

Indexes the nnz() nonzeros directly, skipping structural zeros, in column-major order. Negative indices count from the end; out-of-range indices raise an error. An MX index (or an inner/outer pair of indices) creates a parametric lookup that is resolved at evaluation time; such a node has no derivative with respect to the index. For dense expressions x[k] and x.nz[k] coincide.

>>> S = MX.sym("S", Sparsity.lower(3))
>>> S.nz[1], S.nz[:].shape, len(S.nz)
(MX(S[1]), (6, 1), 6)
>>> k = MX.sym("k")
>>> S.nz[k]
MX(S[k])
>>> MX.sym("x", 2, 2).nz[[]].shape
(0, 1)

>> S = MX.sym('S', Sparsity.lower(3));
>> a = S.get_nz(true, 2); b = S.get_nz(true, Slice());
>> a, size(b), S.nnz()
S[1]
6 1
6
>> k = MX.sym('k');
>> S.get_nz(true, k)
S[(k-1)]
>> size(MX.sym('x', 2, 2).get_nz(true, []))
0 0

julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> S.nz[2], size(S.nz[:]), length(S.nz)
(S[1], (6, 1), 6)
julia> k = ca.MX.sym("k");
julia> S.nz[k]
MX(S[(k-1)])
julia> size(ca.MX.sym("x", 2, 2).nz[Int[]])
(0, 1)

See also
set_nz, get

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

Definition at line 3175 of file mx.hpp.

◆ get_nz() [2/7]

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

Indexes the nnz() nonzeros directly, skipping structural zeros, in column-major order. Negative indices count from the end; out-of-range indices raise an error. An MX index (or an inner/outer pair of indices) creates a parametric lookup that is resolved at evaluation time; such a node has no derivative with respect to the index. For dense expressions x[k] and x.nz[k] coincide.

>>> S = MX.sym("S", Sparsity.lower(3))
>>> S.nz[1], S.nz[:].shape, len(S.nz)
(MX(S[1]), (6, 1), 6)
>>> k = MX.sym("k")
>>> S.nz[k]
MX(S[k])
>>> MX.sym("x", 2, 2).nz[[]].shape
(0, 1)

>> S = MX.sym('S', Sparsity.lower(3));
>> a = S.get_nz(true, 2); b = S.get_nz(true, Slice());
>> a, size(b), S.nnz()
S[1]
6 1
6
>> k = MX.sym('k');
>> S.get_nz(true, k)
S[(k-1)]
>> size(MX.sym('x', 2, 2).get_nz(true, []))
0 0

julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> S.nz[2], size(S.nz[:]), length(S.nz)
(S[1], (6, 1), 6)
julia> k = ca.MX.sym("k");
julia> S.nz[k]
MX(S[(k-1)])
julia> size(ca.MX.sym("x", 2, 2).nz[Int[]])
(0, 1)

See also
set_nz, get

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

◆ get_nz() [3/7]

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

Indexes the nnz() nonzeros directly, skipping structural zeros, in column-major order. Negative indices count from the end; out-of-range indices raise an error. An MX index (or an inner/outer pair of indices) creates a parametric lookup that is resolved at evaluation time; such a node has no derivative with respect to the index. For dense expressions x[k] and x.nz[k] coincide.

>>> S = MX.sym("S", Sparsity.lower(3))
>>> S.nz[1], S.nz[:].shape, len(S.nz)
(MX(S[1]), (6, 1), 6)
>>> k = MX.sym("k")
>>> S.nz[k]
MX(S[k])
>>> MX.sym("x", 2, 2).nz[[]].shape
(0, 1)

>> S = MX.sym('S', Sparsity.lower(3));
>> a = S.get_nz(true, 2); b = S.get_nz(true, Slice());
>> a, size(b), S.nnz()
S[1]
6 1
6
>> k = MX.sym('k');
>> S.get_nz(true, k)
S[(k-1)]
>> size(MX.sym('x', 2, 2).get_nz(true, []))
0 0

julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> S.nz[2], size(S.nz[:]), length(S.nz)
(S[1], (6, 1), 6)
julia> k = ca.MX.sym("k");
julia> S.nz[k]
MX(S[(k-1)])
julia> size(ca.MX.sym("x", 2, 2).nz[Int[]])
(0, 1)

See also
set_nz, get

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

◆ get_nz() [4/7]

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

Indexes the nnz() nonzeros directly, skipping structural zeros, in column-major order. Negative indices count from the end; out-of-range indices raise an error. An MX index (or an inner/outer pair of indices) creates a parametric lookup that is resolved at evaluation time; such a node has no derivative with respect to the index. For dense expressions x[k] and x.nz[k] coincide.

>>> S = MX.sym("S", Sparsity.lower(3))
>>> S.nz[1], S.nz[:].shape, len(S.nz)
(MX(S[1]), (6, 1), 6)
>>> k = MX.sym("k")
>>> S.nz[k]
MX(S[k])
>>> MX.sym("x", 2, 2).nz[[]].shape
(0, 1)

>> S = MX.sym('S', Sparsity.lower(3));
>> a = S.get_nz(true, 2); b = S.get_nz(true, Slice());
>> a, size(b), S.nnz()
S[1]
6 1
6
>> k = MX.sym('k');
>> S.get_nz(true, k)
S[(k-1)]
>> size(MX.sym('x', 2, 2).get_nz(true, []))
0 0

julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> S.nz[2], size(S.nz[:]), length(S.nz)
(S[1], (6, 1), 6)
julia> k = ca.MX.sym("k");
julia> S.nz[k]
MX(S[(k-1)])
julia> size(ca.MX.sym("x", 2, 2).nz[Int[]])
(0, 1)

See also
set_nz, get

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

◆ get_nz() [5/7]

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

Indexes the nnz() nonzeros directly, skipping structural zeros, in column-major order. Negative indices count from the end; out-of-range indices raise an error. An MX index (or an inner/outer pair of indices) creates a parametric lookup that is resolved at evaluation time; such a node has no derivative with respect to the index. For dense expressions x[k] and x.nz[k] coincide.

>>> S = MX.sym("S", Sparsity.lower(3))
>>> S.nz[1], S.nz[:].shape, len(S.nz)
(MX(S[1]), (6, 1), 6)
>>> k = MX.sym("k")
>>> S.nz[k]
MX(S[k])
>>> MX.sym("x", 2, 2).nz[[]].shape
(0, 1)

>> S = MX.sym('S', Sparsity.lower(3));
>> a = S.get_nz(true, 2); b = S.get_nz(true, Slice());
>> a, size(b), S.nnz()
S[1]
6 1
6
>> k = MX.sym('k');
>> S.get_nz(true, k)
S[(k-1)]
>> size(MX.sym('x', 2, 2).get_nz(true, []))
0 0

julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> S.nz[2], size(S.nz[:]), length(S.nz)
(S[1], (6, 1), 6)
julia> k = ca.MX.sym("k");
julia> S.nz[k]
MX(S[(k-1)])
julia> size(ca.MX.sym("x", 2, 2).nz[Int[]])
(0, 1)

See also
set_nz, get

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

◆ get_nz() [6/7]

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

Indexes the nnz() nonzeros directly, skipping structural zeros, in column-major order. Negative indices count from the end; out-of-range indices raise an error. An MX index (or an inner/outer pair of indices) creates a parametric lookup that is resolved at evaluation time; such a node has no derivative with respect to the index. For dense expressions x[k] and x.nz[k] coincide.

>>> S = MX.sym("S", Sparsity.lower(3))
>>> S.nz[1], S.nz[:].shape, len(S.nz)
(MX(S[1]), (6, 1), 6)
>>> k = MX.sym("k")
>>> S.nz[k]
MX(S[k])
>>> MX.sym("x", 2, 2).nz[[]].shape
(0, 1)

>> S = MX.sym('S', Sparsity.lower(3));
>> a = S.get_nz(true, 2); b = S.get_nz(true, Slice());
>> a, size(b), S.nnz()
S[1]
6 1
6
>> k = MX.sym('k');
>> S.get_nz(true, k)
S[(k-1)]
>> size(MX.sym('x', 2, 2).get_nz(true, []))
0 0

julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> S.nz[2], size(S.nz[:]), length(S.nz)
(S[1], (6, 1), 6)
julia> k = ca.MX.sym("k");
julia> S.nz[k]
MX(S[(k-1)])
julia> size(ca.MX.sym("x", 2, 2).nz[Int[]])
(0, 1)

See also
set_nz, get

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

◆ get_nz() [7/7]

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

Indexes the nnz() nonzeros directly, skipping structural zeros, in column-major order. Negative indices count from the end; out-of-range indices raise an error. An MX index (or an inner/outer pair of indices) creates a parametric lookup that is resolved at evaluation time; such a node has no derivative with respect to the index. For dense expressions x[k] and x.nz[k] coincide.

>>> S = MX.sym("S", Sparsity.lower(3))
>>> S.nz[1], S.nz[:].shape, len(S.nz)
(MX(S[1]), (6, 1), 6)
>>> k = MX.sym("k")
>>> S.nz[k]
MX(S[k])
>>> MX.sym("x", 2, 2).nz[[]].shape
(0, 1)

>> S = MX.sym('S', Sparsity.lower(3));
>> a = S.get_nz(true, 2); b = S.get_nz(true, Slice());
>> a, size(b), S.nnz()
S[1]
6 1
6
>> k = MX.sym('k');
>> S.get_nz(true, k)
S[(k-1)]
>> size(MX.sym('x', 2, 2).get_nz(true, []))
0 0

julia> S = ca.MX.sym("S", ca.Sparsity.lower(3));
julia> S.nz[2], size(S.nz[:]), length(S.nz)
(S[1], (6, 1), 6)
julia> k = ca.MX.sym("k");
julia> S.nz[k]
MX(S[(k-1)])
julia> size(ca.MX.sym("x", 2, 2).nz[Int[]])
(0, 1)

See also
set_nz, get

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

◆ get_output()

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

For a multiple-output node (a Function call), returns the OutputNode selecting output oind. For any other node only oind == 0 is valid and the node itself is returned.

>>> a = SX.sym("a")
>>> f = Function("f", [a], [a + 1, 2*a])
>>> x = MX.sym("x")
>>> r0, r1 = f(x, )
>>> call = r0.dep(0)
>>> call.get_output(1), is_equal(call.get_output(0), r0)
(MX(f(x){1}), True)
>>> x.get_output(0)
MX(x)

>> a = SX.sym('a');
>> f = Function('f', {a}, {a + 1, 2*a});
>> x = MX.sym('x');
>> [r0, r1] = f(x);
>> call = r0.dep(0);
>> call.get_output(1), is_equal(call.get_output(0), r0)
f(x){1}
1
>> x.get_output(0)
x

julia> a = ca.SX.sym("a");
julia> f = ca.Function("f", [a], [a + 1, 2 * a]);
julia> x = ca.MX.sym("x");
julia> r0, r1 = f(x);
julia> call = ca.dep(r0, 0);
julia> ca.get_output(call, 1), ca.is_equal(ca.get_output(call, 0), r0)
(f(x){1}, true)
julia> ca.get_output(x, 0)
MX(x)

See also
n_out, which_output

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

◆ get_row()

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

The pattern is stored in compressed column format: colind has size2()+1 entries, the nonzeros of column j are the entries colind(j) up to colind(j+1) (exclusive), and row(el) is the row index of nonzero el. Without argument, the full vectors are returned. Out-of-range indices raise an error.

>>> x = DM(Sparsity.diag(2), [5, 6])
>>> x.row(), x.colind()
([0, 1], [0, 1, 2])
>>> x.row(1), x.colind(2)
(1, 2)

>> x = DM(Sparsity.diag(2), [5; 6]);
>> x.row(), x.colind()
0 1
0 1 2
>> fprintf('%d %d\n', x.row(1), x.colind(2))
1 2

julia> x = ca.DM(ca.Sparsity.diag(2), [5, 6]);
julia> ca.row(x), ca.colind(x)
([0, 1], [0, 1, 2])
julia> ca.row(x, 1), ca.colind(x, 2)
(1, 2)

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

Definition at line 602 of file generic_matrix.hpp.

◆ get_sparsity()

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

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

Definition at line 623 of file mx.hpp.

◆ get_str()

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

Definition at line 91 of file shared_object.hpp.

◆ 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

◆ has_output()

bool casadi::MX::has_output ( ) const

True for the call node of a Function with several outputs. Such a node cannot be used directly in expressions; select an output with get_output.

>>> a = SX.sym("a")
>>> f = Function("f", [a], [a + 1, 2*a])
>>> x = MX.sym("x")
>>> r0, r1 = f(x, )
>>> r0.dep(0).has_output(), r0.has_output(), x.has_output()
(True, False, False)

>> a = SX.sym('a');
>> f = Function('f', {a}, {a + 1, 2*a});
>> x = MX.sym('x');
>> [r0, r1] = f(x);
>> call = r0.dep(0);
>> call.has_output(), r0.has_output(), x.has_output()
1
0
0

julia> a = ca.SX.sym("a");
julia> f = ca.Function("f", [a], [a + 1, 2 * a]);
julia> x = ca.MX.sym("x");
julia> r0, r1 = f(x);
julia> ca.has_output(ca.dep(r0, 0)), ca.has_output(r0), ca.has_output(x)
(true, false, false)

See also
n_out, get_output, is_output

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

◆ inf() [1/3]

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

Dense nrow-by-ncol (default 1-by-1), or with the given sparsity pattern.

>>> MX.inf(), MX.inf(2, 1), MX.inf(Sparsity.lower(2))
(MX(inf), MX(inf(2x1)), MX(inf(2x2,3nz)))

>> MX.inf(), MX.inf(2, 1), MX.inf(Sparsity.lower(2))
inf
inf(2x1)
inf(2x2,3nz)

julia> ca.MX.inf(), ca.MX.inf(2, 1), ca.MX.inf(ca.Sparsity.lower(2))
(inf, inf(2x1), inf(2x2,3nz))

See also
is_inf, nan

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

◆ inf() [2/3]

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

Dense nrow-by-ncol (default 1-by-1), or with the given sparsity pattern.

>>> MX.inf(), MX.inf(2, 1), MX.inf(Sparsity.lower(2))
(MX(inf), MX(inf(2x1)), MX(inf(2x2,3nz)))

>> MX.inf(), MX.inf(2, 1), MX.inf(Sparsity.lower(2))
inf
inf(2x1)
inf(2x2,3nz)

julia> ca.MX.inf(), ca.MX.inf(2, 1), ca.MX.inf(ca.Sparsity.lower(2))
(inf, inf(2x1), inf(2x2,3nz))

See also
is_inf, nan

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

◆ inf() [3/3]

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

Dense nrow-by-ncol (default 1-by-1), or with the given sparsity pattern.

>>> MX.inf(), MX.inf(2, 1), MX.inf(Sparsity.lower(2))
(MX(inf), MX(inf(2x1)), MX(inf(2x2,3nz)))

>> MX.inf(), MX.inf(2, 1), MX.inf(Sparsity.lower(2))
inf
inf(2x1)
inf(2x2,3nz)

julia> ca.MX.inf(), ca.MX.inf(2, 1), ca.MX.inf(ca.Sparsity.lower(2))
(inf, inf(2x1), inf(2x2,3nz))

See also
is_inf, nan

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

◆ info()

Dict casadi::MX::info ( ) const

The contents depend on the node type; most nodes return an empty dictionary. For example, a slicing node reports the slice it takes.

>>> X = MX.sym("X", 2, 2)
>>> X[0, 1].info()
{'slice': {'start': 2, 'step': 1, 'stop': 3}}
>>> X.info()
{}

>> X = MX.sym('X', 2, 2);
>> e = X(1, 2); s = e.info();
>> s.slice
start: 2
step: 1
stop: 3
>> isempty(fieldnames(X.info()))
1

julia> X = ca.MX.sym("X", 3, 3);
julia> ca.info(X[1, 2])
Dict{String, Any} with 1 entry:
  "slice" => Dict{String, Any}("step"=>1, "stop"=>4, "start"=>3)
julia> ca.info(X)
Dict{String, Any}()

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

◆ interpn_linear()

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

Builds the interpolation as explicit MX operations (low, nonzero lookups and arithmetic) instead of an interpolant Function, so that the grid and values can be symbolic. Usually you want interpolant('name', 'linear', ...) instead. Extrapolates linearly outside the grid.

Parameters
xGrid points, one vector per dimension
vValues on the grid, flattened in column-major order over the grid; its length must be an integer multiple m of the number of grid points, giving m outputs per query point
xqQuery coordinates, one vector per dimension, all of the same length nq
opts"lookup_mode": list with one of "linear", "exact", "binary", "auto" (default) per dimension, see low
Returns
nq-by-m matrix

>>> g = MX.sym("g", 3)
>>> v = MX.sym("v", 3)
>>> xq = MX.sym("xq", 2)
>>> e = MX.interpn_linear([g], v, [xq])
>>> f = Function("f", [g, v, xq], [e])
>>> f(DM([0, 1, 2]), DM([0, 10, 40]), DM([0.5, 3]))
DM([5, 70])
>>> try:
...   MX.interpn_linear([g], MX.sym("w", 4), [xq])
... except Exception as e:
...   print("Dimension mismatch" in str(e))
True

>> g = MX.sym('g', 3); v = MX.sym('v', 3); xq = MX.sym('xq', 2);
>> e = MX.interpn_linear({g}, v, {xq});
>> f = Function('f', {g, v, xq}, {e});
>> f(DM([0; 1; 2]), DM([0; 10; 40]), DM([0.5; 3]))
[5, 70]
>> w = MX.sym('w', 4);
>> try, MX.interpn_linear({g}, w, {xq}); catch e, disp('dimension mismatch'); end
dimension mismatch

julia> g = ca.MX.sym("g", 3); v = ca.MX.sym("v", 3); xq = ca.MX.sym("xq", 2);
julia> ex = ca.MX.interpn_linear([g], v, [xq]);
julia> f = ca.Function("f", [g, v, xq], [ex]);
julia> f([0, 1, 2], [0, 10, 40], [0.5, 3])
DM([5, 70])
julia> try
           ca.MX.interpn_linear([g], ca.MX.sym("w", 4), [xq])
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

See also
interpolant, low

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

◆ inv()

MX casadi::MX::inv ( ) const

Not the matrix inverse: use inv or inv_node for that. Applied twice it returns the original node. Not available in MATLAB and Julia, where inv is the matrix inverse; use 1./x there.

>>> A = MX.sym("A", 2, 2)
>>> A.inv(), A.inv().inv()
(MX((1./A)), MX(A))
>>> evalf(MX(4).inv())
DM(0.25)

See also
inv, inv_node

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

◆ is_binary()

bool casadi::MX::is_binary ( ) const

Elementwise operations with two operands (OP_ADD, OP_MUL, OP_POW, comparisons, ...). Matrix products, concatenations and calls are not binary nodes. Note that x + x and x*x are stored as unary nodes (doubling, square).

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> A = MX.sym("A", 2, 2)
>>> (x + y).is_binary(), (x**y).is_binary(), (A @ A).is_binary(), (x + x).is_binary()
(True, True, False, False)

>> x = MX.sym('x'); y = MX.sym('y'); A = MX.sym('A', 2, 2);
>> a = x + y; b = x^y; c = A*A; d = x + x;
>> a.is_binary(), b.is_binary(), c.is_binary(), d.is_binary()
1
1
0
0

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y"); A = ca.MX.sym("A", 2, 2);
julia> ca.is_binary(x + y), ca.is_binary(x^y), ca.is_binary(A * A), ca.is_binary(x + x)
(true, true, false, false)

See also
is_unary, op

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

◆ is_call()

bool casadi::MX::is_call ( ) const

A call to a Function with several outputs is a single call node; the values you get back from calling the Function are OutputNodes that reference it (see is_output). MX Functions are inlined when called with MX arguments unless never_inline is set, so no call node appears in that case.

>>> a = SX.sym("a")
>>> f = Function("f", [a], [a + 1, 2*a])
>>> x = MX.sym("x")
>>> r0, r1 = f(x, )
>>> r0.is_call(), r0.is_output(), r0.dep(0).is_call()
(False, True, True)
>>> r0.dep(0).which_function().name()
'f'

>> a = SX.sym('a');
>> f = Function('f', {a}, {a + 1, 2*a});
>> x = MX.sym('x');
>> [r0, r1] = f(x);
>> call = r0.dep(0);
>> r0.is_call(), r0.is_output(), call.is_call()
0
1
1
>> call.which_function().name()
f

julia> a = ca.SX.sym("a");
julia> f = ca.Function("f", [a], [a + 1, 2 * a]);
julia> x = ca.MX.sym("x");
julia> r0, r1 = f(x);
julia> ca.is_call(r0), ca.is_output(r0), ca.is_call(ca.dep(r0, 0))
(false, true, true)
julia> ca.name(ca.which_function(ca.dep(r0, 0)))
"f"

See also
which_function, is_output, which_output, n_out

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

◆ is_column()

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

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

Definition at line 517 of file generic_matrix.hpp.

◆ is_commutative()

bool casadi::MX::is_commutative ( ) const

Unary operations count as commutative. Any other node raises an error.

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> (x + y).is_commutative(), (x - y).is_commutative()
(True, False)

>> x = MX.sym('x'); y = MX.sym('y');
>> a = x + y; b = x - y;
>> a.is_commutative(), b.is_commutative()
1
0

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> ca.is_commutative(x + y), ca.is_commutative(x - y)
(true, false)

See also
is_binary, is_unary

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

◆ is_constant()

bool casadi::MX::is_constant ( ) const

True only for constant nodes. Operations between constants are folded into a new constant where possible, but an expression without symbols is not constant in general (e.g. a Function call with constant arguments); use evalf to evaluate it.

>>> MX(1).is_constant(), MX(2, 2).is_constant(), MX.sym("x").is_constant()
(True, True, False)
>>> (MX(1) + MX(2)).is_constant()
True

>> MX(1).is_constant(), MX(2, 2).is_constant(), MX.sym('x').is_constant()
1
1
0
>> e = MX(1) + MX(2);
>> e.is_constant()
1

julia> ca.is_constant(ca.MX(1)), ca.is_constant(ca.MX(2, 2)), ca.is_constant(ca.MX.sym("x"))
(true, true, false)
julia> ca.is_constant(ca.MX(1) + ca.MX(2))
true

See also
is_symbolic, to_DM, evalf

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

◆ is_dense()

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

Based on the sparsity pattern only: stored numerical zeros still count as nonzeros. Empty matrices are dense.

>>> DM.zeros(2, 2).is_dense(), DM(2, 2).is_dense(), DM(0, 3).is_dense()
(True, False, True)

>> Z = DM.zeros(2, 2);
>> fprintf('%d %d %d\n', Z.is_dense(), DM(2, 2).is_dense(), DM(0, 3).is_dense())
1 0 1

julia> ca.is_dense(ca.DM.zeros(2, 2)), ca.is_dense(ca.DM(2, 2)), ca.is_dense(ca.DM(0, 3))
(true, false, true)

See also
nnz, numel, densify

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

Definition at line 434 of file generic_matrix.hpp.

◆ is_doubled()

bool casadi::MX::is_doubled ( ) const

True for x + x (which is stored as a doubling node) and for 2*x, and for a sum whose two operands compare equal up to get_max_depth.

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> (x + x).is_doubled(), (2*x).is_doubled(), (x + y).is_doubled()
(True, True, False)

>> x = MX.sym('x'); y = MX.sym('y');
>> a = x + x; b = 2*x; c = x + y;
>> a.is_doubled(), b.is_doubled(), c.is_doubled()
1
1
0

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> ca.is_doubled(x + x), ca.is_doubled(2 * x), ca.is_doubled(x + y)
(true, true, false)

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

◆ is_empty()

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

With both true, only a 0-by-0 matrix counts as empty.

>>> DM(0, 3).is_empty(), DM(0, 3).is_empty(True), DM(0, 0).is_empty(True)
(True, False, True)
>>> DM(1, 1).is_empty()
False

>> E = DM(0, 3);
>> fprintf('%d %d %d\n', E.is_empty(), E.is_empty(true), DM(0, 0).is_empty(true))
1 0 1
>> DM(1, 1).is_empty()
0

julia> E = ca.DM(0, 3);
julia> ca.is_empty(E), ca.is_empty(E, true), ca.is_empty(ca.DM(0, 0), true)
(true, false, true)
julia> ca.is_empty(ca.DM(1, 1))
false

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

Definition at line 402 of file generic_matrix.hpp.

◆ is_eye()

bool casadi::MX::is_eye ( ) const

Only recognizes constants created as an identity (MX.eye, DM.eye); a dense constant that happens to equal the identity is not detected. A 1-by-1 constant one counts.

>>> MX.eye(2).is_eye(), MX(DM([[1, 0], [0, 1]])).is_eye(), MX(1).is_eye()
(True, False, True)

>> MX.eye(2).is_eye(), MX(DM([1 0; 0 1])).is_eye(), MX(1).is_eye()
1
0
1

julia> ca.is_eye(ca.MX.eye(2)), ca.is_eye(ca.MX([1 0; 0 1])), ca.is_eye(ca.MX(1))
(true, false, true)

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

◆ is_half()

bool casadi::MX::is_half ( ) const

◆ is_inf()

bool casadi::MX::is_inf ( ) const

>>> MX.inf().is_inf(), MX.inf(2, 2).is_inf(), MX(1).is_inf()
(True, True, False)

>> MX.inf().is_inf(), MX.inf(2, 2).is_inf(), MX(1).is_inf()
1
1
0

julia> ca.is_inf(ca.MX.inf()), ca.is_inf(ca.MX.inf(2, 2)), ca.is_inf(ca.MX(1))
(true, true, false)

See also
is_minus_inf, MX.inf

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

◆ is_integer()

bool casadi::MX::is_integer ( ) const

False for non-constant nodes.

>>> MX(3).is_integer(), MX(3.5).is_integer(), MX.sym("x").is_integer()
(True, False, False)

>> MX(3).is_integer(), MX(3.5).is_integer(), MX.sym('x').is_integer()
1
0
0

julia> ca.is_integer(ca.MX(3)), ca.is_integer(ca.MX(3.5)), ca.is_integer(ca.MX.sym("x"))
(true, false, false)

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

◆ is_minus_inf()

bool casadi::MX::is_minus_inf ( ) const

>>> (-MX.inf()).is_minus_inf(), MX.inf().is_minus_inf()
(True, False)

>> m = -MX.inf();
>> m.is_minus_inf(), MX.inf().is_minus_inf()
1
0

julia> ca.is_minus_inf(-ca.MX.inf()), ca.is_minus_inf(ca.MX.inf())
(true, false)

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

◆ is_minus_one()

bool casadi::MX::is_minus_one ( ) const

>>> MX(-1).is_minus_one(), (-MX(1)).is_minus_one(), MX(1).is_minus_one()
(True, True, False)

>> m = -MX(1);
>> MX(-1).is_minus_one(), m.is_minus_one(), MX(1).is_minus_one()
1
1
0

julia> ca.is_minus_one(ca.MX(-1)), ca.is_minus_one(-ca.MX(1)), ca.is_minus_one(ca.MX(1))
(true, true, false)

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

◆ is_multiplication()

bool casadi::MX::is_multiplication ( ) const

Elementwise multiplication does not count.

>>> A = MX.sym("A", 2, 2)
>>> (A @ A).is_multiplication(), (A * A).is_multiplication()
(True, False)

>> A = MX.sym('A', 2, 2);
>> a = A*A; b = A.*A;
>> a.is_multiplication(), b.is_multiplication()
1
0

julia> A = ca.MX.sym("A", 2, 2);
julia> ca.is_multiplication(A * A), ca.is_multiplication(A .* A)
(true, false)

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

◆ is_nonnegative()

bool casadi::MX::is_nonnegative ( ) const

Recognizes nonnegative constants and operations with a nonnegative range such as squares, absolute values, exponentials and square roots.

>>> x = MX.sym("x")
>>> (x**2).is_nonnegative(), fabs(x).is_nonnegative(), exp(x).is_nonnegative()
(True, True, True)
>>> x.is_nonnegative(), MX(-3).is_nonnegative(), MX(0).is_nonnegative()
(False, False, True)

>> x = MX.sym('x');
>> a = x^2; b = abs(x); c = exp(x);
>> a.is_nonnegative(), b.is_nonnegative(), c.is_nonnegative()
1
1
1
>> x.is_nonnegative(), MX(-3).is_nonnegative(), MX(0).is_nonnegative()
0
0
1

julia> x = ca.MX.sym("x");
julia> ca.is_nonnegative(x^2), ca.is_nonnegative(abs(x)), ca.is_nonnegative(exp(x))
(true, true, true)
julia> ca.is_nonnegative(x), ca.is_nonnegative(ca.MX(-3)), ca.is_nonnegative(ca.MX(0))
(false, false, true)

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

◆ is_norm()

bool casadi::MX::is_norm ( ) const

>>> A = MX.sym("A", 2, 2)
>>> norm_2(A).is_norm(), norm_fro(A).is_norm(), sqrt(dot(A, A)).is_norm()
(True, True, False)

>> A = MX.sym('A', 2, 2);
>> a = norm_2(A); b = norm_fro(A); c = sqrt(dot(A, A));
>> a.is_norm(), b.is_norm(), c.is_norm()
1
1
0

julia> A = ca.MX.sym("A", 2, 2);
julia> ca.is_norm(ca.norm_2(A)), ca.is_norm(ca.norm_fro(A)), ca.is_norm(sqrt(ca.dot(A, A)))
(true, true, false)

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

◆ is_null()

bool casadi::GenericShared< SharedObject , SharedObjectInternal >::is_null
inherited

Definition at line 116 of file generic_shared_impl.hpp.

◆ is_one()

bool casadi::MX::is_one ( ) const

Structural zeros are ignored: MX(Sparsity.lower(2)) is not "one" because it has structural zeros that would be zero when densified.

>>> MX(1).is_one(), MX.ones(2, 2).is_one(), MX(Sparsity.lower(2)).is_one()
(True, True, False)

>> MX(1).is_one(), MX.ones(2, 2).is_one(), MX(Sparsity.lower(2)).is_one()
1
1
0

julia> ca.is_one(ca.MX(1)), ca.is_one(ca.MX.ones(2, 2)),
       ca.is_one(ca.MX(ca.Sparsity.lower(2)))
(true, true, false)

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

◆ is_op()

bool casadi::MX::is_op ( casadi_int  op) const
Parameters
opOne of the OP_* constants (OP_ADD, OP_SIN, OP_CALL, OP_GETNONZEROS, ...)

>>> x = MX.sym("x", 2)
>>> (x + 1).is_op(OP_ADD), sin(x).is_op(OP_SIN), x.is_op(OP_PARAMETER)
(True, True, True)
>>> x[0].is_op(OP_GETNONZEROS), MX(1).is_op(OP_CONST)
(True, True)

>> x = MX.sym('x', 2);
>> a = x + 1; b = sin(x);
>> a.is_op(OP_ADD), b.is_op(OP_SIN), x.is_op(OP_PARAMETER)
1
1
1
>> c = x(1);
>> c.is_op(OP_GETNONZEROS), MX(1).is_op(OP_CONST)
1
1

julia> x = ca.MX.sym("x", 2);
julia> ca.is_op(x + 1, ca.OP_ADD), ca.is_op(sin.(x), ca.OP_SIN),
       ca.is_op(x, ca.OP_PARAMETER)
(true, true, true)
julia> ca.is_op(x[1], ca.OP_GETNONZEROS), ca.is_op(ca.MX(1), ca.OP_CONST)
(true, true)

See also
op

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

◆ is_output()

bool casadi::MX::is_output ( ) const

The expressions obtained by calling a Function with several outputs are OutputNodes: they have one dependency, the call node, and which_output gives the index.

>>> a = SX.sym("a")
>>> f = Function("f", [a], [a + 1, 2*a])
>>> x = MX.sym("x")
>>> r0, r1 = f(x, )
>>> r1, r1.is_output(), r1.which_output(), r1.dep(0)
(MX(f(x){1}), True, 1, MX(f(x)))
>>> x.is_output(), (x + 1).is_output()
(False, False)

>> a = SX.sym('a');
>> f = Function('f', {a}, {a + 1, 2*a});
>> x = MX.sym('x');
>> [r0, r1] = f(x);
>> r1, r1.is_output(), r1.which_output(), r1.dep(0)
f(x){1}
1
1
f(x)
>> e = x + 1;
>> x.is_output(), e.is_output()
0
0

julia> a = ca.SX.sym("a");
julia> f = ca.Function("f", [a], [a + 1, 2 * a]);
julia> x = ca.MX.sym("x");
julia> r0, r1 = f(x);
julia> r1, ca.is_output(r1), ca.which_output(r1), ca.dep(r1, 0)
(f(x){1}, true, 1, f(x))
julia> ca.is_output(x), ca.is_output(x + 1)
(false, false)

See also
which_output, is_call, has_output

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

◆ is_regular()

bool casadi::MX::is_regular ( ) const

Only defined for constant nodes; a symbolic expression raises an error.

>>> MX(1).is_regular(), MX.inf().is_regular(), MX(DM([1, float("nan")])).is_regular()
(True, False, False)
>>> try:
...   MX.sym("x").is_regular()
... except Exception as e:
...   print("Cannot check regularity for symbolic MX" in str(e))
True

>> MX(1).is_regular(), MX.inf().is_regular()
1
0
>> MX(DM([1; nan])).is_regular()
0
>> try, MX.sym('x').is_regular(); catch e, disp('error'); end
error

julia> ca.is_regular(ca.MX(1)), ca.is_regular(ca.MX.inf()), ca.is_regular(ca.MX([1, NaN]))
(true, false, false)
julia> try
           ca.is_regular(ca.MX.sym("x"))
       catch e
           println(occursin("Cannot check regularity", sprint(showerror, e)))
       end
true

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

◆ is_row()

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

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

Definition at line 512 of file generic_matrix.hpp.

◆ is_scalar()

bool casadi::GenericMatrix< MX >::is_scalar ( bool  scalar_and_dense = false) const
inherited
Parameters
scalar_and_denseWhen true, a 1-by-1 matrix with a structural zero does not count

>>> x = SX.sym("x", Sparsity(1, 1))
>>> x.is_scalar(), x.is_scalar(True), SX.sym("y").is_scalar(True)
(True, False, True)

>> x = SX.sym('x', Sparsity(1, 1));
>> fprintf('%d %d %d\n', x.is_scalar(), x.is_scalar(true), SX.sym('y').is_scalar(true))
1 0 1

julia> x = ca.SX.sym("x", ca.Sparsity(1, 1));
julia> ca.is_scalar(x), ca.is_scalar(x, true), ca.is_scalar(ca.SX.sym("y"), true)
(true, false, true)

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

◆ is_square()

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

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

Definition at line 470 of file generic_matrix.hpp.

◆ is_symbolic()

bool casadi::MX::is_symbolic ( ) const

False for any expression built from primitives, including concatenations of primitives (use is_valid_input for those). A symbol without nonzeros, e.g. MX.sym("x", 2, 0), is stored as an empty constant and is not symbolic.

>>> x = MX.sym("x")
>>> x.is_symbolic(), (x + 1).is_symbolic(), vertcat(x, x).is_symbolic()
(True, False, False)
>>> MX.sym("e", 2, 0).is_symbolic()
False

>> x = MX.sym('x'); e = x + 1; v = vertcat(x, x);
>> x.is_symbolic(), e.is_symbolic(), v.is_symbolic()
1
0
0
>> MX.sym('e', 2, 0).is_symbolic()
0

julia> x = ca.MX.sym("x");
julia> ca.is_symbolic(x), ca.is_symbolic(x + 1), ca.is_symbolic([x; x])
(true, false, false)
julia> ca.is_symbolic(ca.MX.sym("e", 2, 0))
false

See also
is_constant, is_valid_input, name

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

◆ is_transpose()

bool casadi::MX::is_transpose ( ) const

The transpose of a scalar is the scalar itself and that of a vector is a reshape, so only transposes of genuine matrices create a transpose node.

>>> A = MX.sym("A", 2, 2)
>>> A.T.is_transpose(), A.is_transpose(), MX.sym("v", 2).T.is_transpose()
(True, False, False)

>> A = MX.sym('A', 2, 2); At = A'; vt = MX.sym('v', 2)';
>> At.is_transpose(), A.is_transpose(), vt.is_transpose()
1
0
0

julia> A = ca.MX.sym("A", 2, 2);
julia> ca.is_transpose(A'), ca.is_transpose(A), ca.is_transpose(ca.MX.sym("v", 2)')
(true, false, false)

See also
T

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

◆ is_tril()

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

Decided on the sparsity pattern alone, see is_triu.

See also
is_triu, tril, tril2symm

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

Definition at line 560 of file generic_matrix.hpp.

◆ is_triu()

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

Decided on the sparsity pattern alone: all structural nonzeros must lie on or above the diagonal. A dense matrix with numerical zeros below the diagonal is not upper triangular. Also defined for non-square matrices.

>>> DM(Sparsity.upper(3), 1).is_triu(), DM.zeros(2, 2).is_triu(), DM(2, 2).is_triu()
(True, False, True)

>> U = DM(Sparsity.upper(3), 1);
>> fprintf('%d %d %d\n', U.is_triu(), DM.zeros(2, 2).is_triu(), DM(2, 2).is_triu())
1 0 1

julia> U = ca.DM(ca.Sparsity.upper(3), 1);
julia> ca.is_triu(U), ca.is_triu(ca.DM.zeros(2, 2)), ca.is_triu(ca.DM(2, 2))
(true, false, true)

See also
is_tril, triu, triu2symm

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

Definition at line 551 of file generic_matrix.hpp.

◆ is_unary()

bool casadi::MX::is_unary ( ) const

OP_SIN, OP_NEG, OP_SQ, ... Transposes, reshapes and projections are not unary nodes even though they have one dependency.

>>> x = MX.sym("x")
>>> A = MX.sym("A", 2, 2)
>>> sin(x).is_unary(), (-x).is_unary(), x.is_unary(), A.T.is_unary()
(True, True, False, False)

>> x = MX.sym('x'); A = MX.sym('A', 2, 2);
>> a = sin(x); b = -x; c = A';
>> a.is_unary(), b.is_unary(), x.is_unary(), c.is_unary()
1
1
0
0

julia> x = ca.MX.sym("x"); A = ca.MX.sym("A", 2, 2);
julia> ca.is_unary(sin(x)), ca.is_unary(-x), ca.is_unary(x), ca.is_unary(A')
(true, true, false, false)

See also
is_binary, op

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

◆ is_valid_input()

bool casadi::MX::is_valid_input ( ) const

Valid inputs are symbolic primitives, reshapes and transposes of them, and (nested) concatenations of valid inputs. Constants without nonzeros are also accepted. An expression that is not a valid input raises an error when passed as a Function input.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> vertcat(x, y).is_valid_input(), vertcat(x, y).T.is_valid_input()
(True, True)
>>> (x + 1).is_valid_input(), MX(1).is_valid_input(), MX(2, 2).is_valid_input()
(False, False, True)

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> v = [x; y]; vt = v';
>> v.is_valid_input(), vt.is_valid_input()
1
1
>> e = x + 1;
>> e.is_valid_input(), MX(1).is_valid_input(), MX(2, 2).is_valid_input()
0
0
1

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> ca.is_valid_input([x; y]), ca.is_valid_input([x; y]')
(true, true)
julia> ca.is_valid_input(x + 1), ca.is_valid_input(ca.MX(1)), ca.is_valid_input(ca.MX(2, 2))
(false, false, true)

See also
primitives, n_primitives, is_symbolic

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

◆ is_value()

bool casadi::MX::is_value ( double  val) const

False negatives are possible: only constant nodes are inspected; symbolic expressions always give false.

>>> MX(3).is_value(3), MX(DM([3, 3])).is_value(3), MX(DM([3, 4])).is_value(3)
(True, True, False)
>>> MX.sym("x").is_value(3)
False

>> MX(3).is_value(3), MX(DM([3; 3])).is_value(3)
1
1
>> MX(DM([3; 4])).is_value(3)
0
>> MX.sym('x').is_value(3)
0

julia> ca.is_value(ca.MX(3), 3), ca.is_value(ca.MX([3, 3]), 3),
       ca.is_value(ca.MX([3, 4]), 3)
(true, true, false)
julia> ca.is_value(ca.MX.sym("x"), 3)
false

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

◆ is_vector()

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

True when size1() or size2() is one, so scalars and n-by-1 or 1-by-n empties count. A 0-by-0 matrix does not.

>>> DM(3, 1).is_vector(), DM(1, 3).is_vector(), DM(1, 1).is_vector()
(True, True, True)
>>> DM(3, 3).is_vector(), DM(0, 1).is_vector(), DM(0, 0).is_vector()
(False, True, False)

>> fprintf('%d %d %d\n', DM(3, 1).is_vector(), DM(1, 3).is_vector(), DM(1, 1).is_vector())
1 1 1
>> fprintf('%d %d %d\n', DM(3, 3).is_vector(), DM(0, 1).is_vector(), DM(0, 0).is_vector())
0 1 0

julia> ca.is_vector(ca.DM(3, 1)), ca.is_vector(ca.DM(1, 3)), ca.is_vector(ca.DM(1, 1))
(true, true, true)
julia> ca.is_vector(ca.DM(3, 3)), ca.is_vector(ca.DM(0, 1)), ca.is_vector(ca.DM(0, 0))
(false, true, false)

See also
is_row, is_column

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

Definition at line 507 of file generic_matrix.hpp.

◆ is_zero()

bool casadi::MX::is_zero ( ) const

True for constants with all nonzeros zero and for any matrix without nonzeros, whether symbolic or not. Expressions that simplify to zero (x - x, 0*x) are folded to a constant at construction and therefore report true.

>>> x = MX.sym("x")
>>> MX(0).is_zero(), MX(2, 2).is_zero(), (x - x).is_zero(), x.is_zero()
(True, True, True, False)
>>> MX.sym("e", 2, 0).is_zero()
True

>> x = MX.sym('x'); e = x - x;
>> MX(0).is_zero(), MX(2, 2).is_zero(), e.is_zero(), x.is_zero()
1
1
1
0
>> MX.sym('e', 2, 0).is_zero()
1

julia> x = ca.MX.sym("x");
julia> ca.is_zero(ca.MX(0)), ca.is_zero(ca.MX(2, 2)), ca.is_zero(x - x), ca.is_zero(x)
(true, true, true, false)
julia> ca.is_zero(ca.MX.sym("e", 2, 0))
true

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

◆ join_primitives() [1/3]

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

Inverse of split_primitives: v holds one value per primitive, with the shape of that primitive, and the result has the shape of this expression. A wrong number of pieces raises an error.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> X = vertcat(x, y)
>>> X.join_primitives([DM([1, 2]), DM([3, 4, 5])])
DM([1, 2, 3, 4, 5])
>>> try:
...   X.join_primitives([DM([1, 2])])
... except Exception as e:
...   print("Wrong number of primitives" in str(e))
True

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> X = [x; y];
>> X.join_primitives({DM([1; 2]), DM([3; 4; 5])})
[1, 2, 3, 4, 5]
>> try, X.join_primitives({DM([1; 2])}); catch e, disp('wrong number of primitives'); end
wrong number of primitives

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> X = [x; y];
julia> ca.join_primitives(X, [ca.DM([1, 2]), ca.DM([3, 4, 5])])
DM([1, 2, 3, 4, 5])
julia> try
           ca.join_primitives(X, [ca.DM([1, 2])])
       catch e
           println(occursin("Wrong number of primitives", sprint(showerror, e)))
       end
true

See also
split_primitives, primitives

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

◆ join_primitives() [2/3]

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

Inverse of split_primitives: v holds one value per primitive, with the shape of that primitive, and the result has the shape of this expression. A wrong number of pieces raises an error.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> X = vertcat(x, y)
>>> X.join_primitives([DM([1, 2]), DM([3, 4, 5])])
DM([1, 2, 3, 4, 5])
>>> try:
...   X.join_primitives([DM([1, 2])])
... except Exception as e:
...   print("Wrong number of primitives" in str(e))
True

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> X = [x; y];
>> X.join_primitives({DM([1; 2]), DM([3; 4; 5])})
[1, 2, 3, 4, 5]
>> try, X.join_primitives({DM([1; 2])}); catch e, disp('wrong number of primitives'); end
wrong number of primitives

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> X = [x; y];
julia> ca.join_primitives(X, [ca.DM([1, 2]), ca.DM([3, 4, 5])])
DM([1, 2, 3, 4, 5])
julia> try
           ca.join_primitives(X, [ca.DM([1, 2])])
       catch e
           println(occursin("Wrong number of primitives", sprint(showerror, e)))
       end
true

See also
split_primitives, primitives

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

◆ join_primitives() [3/3]

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

Inverse of split_primitives: v holds one value per primitive, with the shape of that primitive, and the result has the shape of this expression. A wrong number of pieces raises an error.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> X = vertcat(x, y)
>>> X.join_primitives([DM([1, 2]), DM([3, 4, 5])])
DM([1, 2, 3, 4, 5])
>>> try:
...   X.join_primitives([DM([1, 2])])
... except Exception as e:
...   print("Wrong number of primitives" in str(e))
True

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> X = [x; y];
>> X.join_primitives({DM([1; 2]), DM([3; 4; 5])})
[1, 2, 3, 4, 5]
>> try, X.join_primitives({DM([1; 2])}); catch e, disp('wrong number of primitives'); end
wrong number of primitives

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> X = [x; y];
julia> ca.join_primitives(X, [ca.DM([1, 2]), ca.DM([3, 4, 5])])
DM([1, 2, 3, 4, 5])
julia> try
           ca.join_primitives(X, [ca.DM([1, 2])])
       catch e
           println(occursin("Wrong number of primitives", sprint(showerror, e)))
       end
true

See also
split_primitives, primitives

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

◆ 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

◆ 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

◆ logsumexp()

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

◆ mapping()

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

For a slicing node (x[...], x.nz[...]) returns the indices of the nonzeros of the dependency that make up the result; for an assignment node, the target indices. Any other node raises an error.

>>> X = MX.sym("X", 3, 3)
>>> X[0, 1].mapping(), X[:, 1].mapping()
(DM(3), DM([3, 4, 5]))
>>> try:
...   X.mapping()
... except Exception as e:
...   print("'mapping' not defined" in str(e))
True

>> X = MX.sym('X', 3, 3);
>> a = X(1, 2); b = X(:, 2);
>> a.mapping(), b.mapping()
3
[3, 4, 5]
>> try, X.mapping(); catch e, disp('error'); end
error

julia> X = ca.MX.sym("X", 3, 3);
julia> ca.mapping(X[1, 2]), ca.mapping(X[:, 2])
([3;;], [3; 4; 5;;])
julia> try
           ca.mapping(X)
       catch e
           println(occursin("'mapping' not defined", sprint(showerror, e)))
       end
true

See also
info

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

◆ monitor()

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

When the returned expression is evaluated, comment followed by the nonzeros is printed to standard output. The value is unchanged. A matrix without nonzeros is returned as is.

>>> x = MX.sym("x")
>>> e = x.monitor("value of x")
>>> e, e.is_op(OP_MONITOR)
(MX(monitor(x, value of x)), True)

>> x = MX.sym('x');
>> e = x.monitor('value of x');
>> e, e.is_op(OP_MONITOR)
monitor(x, value of x)
1

julia> x = ca.MX.sym("x");
julia> e = ca.monitor(x, "value of x");
julia> e, ca.is_op(e, ca.OP_MONITOR)
(monitor(x, value of x), true)

See also
printme, dump, attachAssert

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

◆ mpower()

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

Functions called by friend functions defined here

◆ n_dep()

casadi_int casadi::MX::n_dep ( ) const

Zero for symbolic primitives and constants, one for unary operations and projections, two for binary operations, the number of arguments for concatenations and Function calls.

>>> x = MX.sym("x")
>>> A = MX.sym("A", 2, 2)
>>> x.n_dep(), sin(x).n_dep(), (x + 1).n_dep(), vertcat(x, x, x).n_dep()
(0, 1, 2, 3)
>>> (A @ A).n_dep(), A[0, 0].n_dep(), MX(1).n_dep()
(3, 1, 0)

>> x = MX.sym('x'); A = MX.sym('A', 2, 2);
>> e = x + 1; v = vertcat(x, x, x);
>> x.n_dep(), sin(x).n_dep(), e.n_dep(), v.n_dep()
0
1
2
3
>> P = A*A; a = A(1, 1);
>> P.n_dep(), a.n_dep(), MX(1).n_dep()
3
1
0

julia> x = ca.MX.sym("x"); A = ca.MX.sym("A", 2, 2);
julia> ca.n_dep(x), ca.n_dep(sin(x)), ca.n_dep(x + 1), ca.n_dep([x; x; x])
(0, 1, 2, 3)
julia> ca.n_dep(A * A), ca.n_dep(A[1, 1]), ca.n_dep(ca.MX(1))
(3, 1, 0)

See also
dep

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

◆ n_out()

casadi_int casadi::MX::n_out ( ) const

Greater than one only for multiple-output nodes, i.e. a call to a Function with several outputs. Every other node, including the OutputNode that selects one output of such a call, reports 1.

>>> a = SX.sym("a")
>>> f = Function("f", [a], [a + 1, 2*a])
>>> x = MX.sym("x")
>>> r0, r1 = f(x, )
>>> call = r0.dep(0)
>>> call, call.n_out(), r0.n_out()
(MX(f(x)), 2, 1)

>> a = SX.sym('a');
>> f = Function('f', {a}, {a + 1, 2*a});
>> x = MX.sym('x');
>> [r0, r1] = f(x);
>> call = r0.dep(0);
>> call, call.n_out(), r0.n_out()
f(x)
2
1

julia> a = ca.SX.sym("a");
julia> f = ca.Function("f", [a], [a + 1, 2 * a]);
julia> x = ca.MX.sym("x");
julia> r0, r1 = f(x);
julia> call = ca.dep(r0, 0);
julia> call, ca.n_out(call), ca.n_out(r0)
(f(x), 2, 1)

See also
get_output, has_output, is_output

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

◆ n_primitives()

casadi_int casadi::MX::n_primitives ( ) const

For an expression that is not a valid input the count is 1 (the expression itself); check is_valid_input first.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> vertcat(x, y).n_primitives(), x.n_primitives()
(2, 1)

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> v = [x; y];
>> v.n_primitives(), x.n_primitives()
2
1

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> ca.n_primitives([x; y]), ca.n_primitives(x)
(2, 1)

See also
primitives, is_valid_input

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

◆ name()

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

Only defined for nodes created by MX.sym; any other node raises an error. Check with is_symbolic first.

>>> x = MX.sym("x", 2)
>>> x.name()
'x'
>>> try:
...   (x + 1).name()
... except Exception as e:
...   print("'name' not defined" in str(e))
True

>> x = MX.sym('x', 2);
>> x.name()
x
>> e = x + 1;
>> try, e.name(); catch err, disp('error'); end
error

julia> x = ca.MX.sym("x", 2);
julia> ca.name(x)
"x"
julia> try
           ca.name(x + 1)
       catch e
           println(occursin("'name' not defined", sprint(showerror, e)))
       end
true

See also
is_symbolic, symvar

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

◆ nan() [1/3]

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

Dense nrow-by-ncol (default 1-by-1), or with the given sparsity pattern.

>>> MX.nan(), MX.nan(Sparsity.lower(2))
(MX(nan), MX(nan(2x2,3nz)))

>> MX.nan(), MX.nan(Sparsity.lower(2))
nan
nan(2x2,3nz)

julia> ca.MX.nan(), ca.MX.nan(ca.Sparsity.lower(2))
(nan, nan(2x2,3nz))

See also
inf

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

◆ nan() [2/3]

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

Dense nrow-by-ncol (default 1-by-1), or with the given sparsity pattern.

>>> MX.nan(), MX.nan(Sparsity.lower(2))
(MX(nan), MX(nan(2x2,3nz)))

>> MX.nan(), MX.nan(Sparsity.lower(2))
nan
nan(2x2,3nz)

julia> ca.MX.nan(), ca.MX.nan(ca.Sparsity.lower(2))
(nan, nan(2x2,3nz))

See also
inf

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

◆ nan() [3/3]

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

Dense nrow-by-ncol (default 1-by-1), or with the given sparsity pattern.

>>> MX.nan(), MX.nan(Sparsity.lower(2))
(MX(nan), MX(nan(2x2,3nz)))

>> MX.nan(), MX.nan(Sparsity.lower(2))
nan
nan(2x2,3nz)

julia> ca.MX.nan(), ca.MX.nan(ca.Sparsity.lower(2))
(nan, nan(2x2,3nz))

See also
inf

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

◆ nnz()

casadi_int casadi::GenericMatrix< MX >::nnz ( ) const
inherited

Counts the entries present in the sparsity pattern. Entries that are stored but numerically zero still count; structural zeros (not stored) do not. For a dense matrix this equals numel().

>>> DM.eye(3).nnz(), DM.ones(3, 3).nnz(), DM(3, 3).nnz()
(3, 9, 0)
>>> DM([[1, 0], [0, 2]]).nnz()
4
>>> MX.sym("x", 2, 0).nnz()
0

>> fprintf('%d %d %d\n', DM.eye(3).nnz(), DM.ones(3, 3).nnz(), DM(3, 3).nnz())
3 9 0
>> DM([1 0; 0 2]).nnz()
4
>> MX.sym('x', 2, 0).nnz()
0

julia> ca.nnz(ca.DM.eye(3)), ca.nnz(ca.DM.ones(3, 3)), ca.nnz(ca.DM(3, 3))
(3, 9, 0)
julia> ca.nnz(ca.DM([1 0; 0 2]))
4
julia> ca.nnz(ca.MX.sym("x", 2, 0))
0

See also
numel, nnz_lower, nnz_upper, nnz_diag, sparsity

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

◆ nnz_diag()

casadi_int casadi::GenericMatrix< MX >::nnz_diag ( ) const
inherited

Also defined for non-square matrices.

>>> DM.ones(3, 3).nnz_diag(), DM.eye(3).nnz_diag(), DM.ones(2, 3).nnz_diag()
(3, 3, 2)

>> A = DM.ones(3, 3); I = DM.eye(3); B = DM.ones(2, 3);
>> fprintf('%d %d %d\n', A.nnz_diag(), I.nnz_diag(), B.nnz_diag())
3 3 2

julia> A = ca.DM.ones(3, 3); I = ca.DM.eye(3); B = ca.DM.ones(2, 3);
julia> ca.nnz_diag(A), ca.nnz_diag(I), ca.nnz_diag(B)
(3, 3, 2)

See also
nnz, nnz_lower, nnz_upper

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

◆ nnz_lower()

casadi_int casadi::GenericMatrix< MX >::nnz_lower ( ) const
inherited

>>> DM.ones(3, 3).nnz_lower(), DM.eye(3).nnz_lower(), DM.ones(2, 3).nnz_lower()
(6, 3, 3)

>> A = DM.ones(3, 3); I = DM.eye(3); B = DM.ones(2, 3);
>> fprintf('%d %d %d\n', A.nnz_lower(), I.nnz_lower(), B.nnz_lower())
6 3 3

julia> A = ca.DM.ones(3, 3); I = ca.DM.eye(3); B = ca.DM.ones(2, 3);
julia> ca.nnz_lower(A), ca.nnz_lower(I), ca.nnz_lower(B)
(6, 3, 3)

See also
nnz, nnz_upper, nnz_diag

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

◆ nnz_upper()

casadi_int casadi::GenericMatrix< MX >::nnz_upper ( ) const
inherited

>>> DM.ones(3, 3).nnz_upper(), DM.eye(3).nnz_upper(), DM.ones(2, 3).nnz_upper()
(6, 3, 5)

>> A = DM.ones(3, 3); I = DM.eye(3); B = DM.ones(2, 3);
>> fprintf('%d %d %d\n', A.nnz_upper(), I.nnz_upper(), B.nnz_upper())
6 3 5

julia> A = ca.DM.ones(3, 3); I = ca.DM.eye(3); B = ca.DM.ones(2, 3);
julia> ca.nnz_upper(A), ca.nnz_upper(I), ca.nnz_upper(B)
(6, 3, 5)

See also
nnz, nnz_lower, nnz_diag

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

◆ numel()

casadi_int casadi::GenericMatrix< MX >::numel ( ) const
inherited

Counts all entries, structural zeros included.

>>> DM.eye(3).numel(), MX.sym("x", 2, 0).numel(), SX.sym("x").numel()
(9, 0, 1)

>> fprintf('%d %d %d\n', DM.eye(3).numel(), MX.sym('x', 2, 0).numel(), SX.sym('x').numel())
9 0 1

julia> ca.numel(ca.DM.eye(3)), ca.numel(ca.MX.sym("x", 2, 0)), ca.numel(ca.SX.sym("x"))
(9, 0, 1)
julia> length(ca.DM.eye(3))
9

See also
nnz, size

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

◆ ones() [1/3]

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

ones(sp) sets every structural nonzero of sp to one; other entries stay structurally zero.

>>> DM.ones(2, 3)
DM(
[[1, 1, 1],
 [1, 1, 1]])
>>> DM.ones(Sparsity.diag(2))
DM(
[[1, 00],
 [00, 1]])

>> DM.ones(2, 3)
[[1, 1, 1],
 [1, 1, 1]]
>> DM.ones(Sparsity.diag(2))
[[1, 00],
 [00, 1]]

julia> ca.DM.ones(2, 3)
DM(
[[1, 1, 1],
 [1, 1, 1]])
julia> ca.DM.ones(ca.Sparsity.diag(2))
DM(
[[1, 00],
 [00, 1]])

See also
zeros, Sparsity

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

Definition at line 5206 of file generic_matrix.hpp.

◆ ones() [2/3]

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

ones(sp) sets every structural nonzero of sp to one; other entries stay structurally zero.

>>> DM.ones(2, 3)
DM(
[[1, 1, 1],
 [1, 1, 1]])
>>> DM.ones(Sparsity.diag(2))
DM(
[[1, 00],
 [00, 1]])

>> DM.ones(2, 3)
[[1, 1, 1],
 [1, 1, 1]]
>> DM.ones(Sparsity.diag(2))
[[1, 00],
 [00, 1]]

julia> ca.DM.ones(2, 3)
DM(
[[1, 1, 1],
 [1, 1, 1]])
julia> ca.DM.ones(ca.Sparsity.diag(2))
DM(
[[1, 00],
 [00, 1]])

See also
zeros, Sparsity

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

Definition at line 5209 of file generic_matrix.hpp.

◆ ones() [3/3]

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

ones(sp) sets every structural nonzero of sp to one; other entries stay structurally zero.

>>> DM.ones(2, 3)
DM(
[[1, 1, 1],
 [1, 1, 1]])
>>> DM.ones(Sparsity.diag(2))
DM(
[[1, 00],
 [00, 1]])

>> DM.ones(2, 3)
[[1, 1, 1],
 [1, 1, 1]]
>> DM.ones(Sparsity.diag(2))
[[1, 00],
 [00, 1]]

julia> ca.DM.ones(2, 3)
DM(
[[1, 1, 1],
 [1, 1, 1]])
julia> ca.DM.ones(ca.Sparsity.diag(2))
DM(
[[1, 00],
 [00, 1]])

See also
zeros, Sparsity

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

Definition at line 5210 of file generic_matrix.hpp.

◆ op()

casadi_int casadi::MX::op ( ) const

Returns one of the OP_* constants. Every node, not only elementwise operations, has an operation code (OP_PARAMETER, OP_CONST, OP_CALL, OP_MTIMES, OP_GETNONZEROS, ...). Function print_operator shows the symbol of an operation.

>>> x = MX.sym("x")
>>> x.op() == OP_PARAMETER, (x + 1).op() == OP_ADD, MX(1).op() == OP_CONST
(True, True, True)
>>> print_operator(x + 1, ["a", "b"])
'(a+b)'

>> x = MX.sym('x'); e = x + 1;
>> x.op() == OP_PARAMETER, e.op() == OP_ADD, MX(1).op() == OP_CONST
1
1
1
>> print_operator(x + 1, {'a', 'b'})
(a+b)

julia> x = ca.MX.sym("x");
julia> ca.op(x) == ca.OP_PARAMETER, ca.op(x + 1) == ca.OP_ADD,
       ca.op(ca.MX(1)) == ca.OP_CONST
(true, true, true)
julia> ca.print_operator(x + 1, ["a", "b"])
"(a+b)"

See also
is_op, print_operator

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

◆ operator double()

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

Raises an error for symbolic nodes and for constants that are not 1-by-1.

>>> float(MX(2))
2.0
>>> try:
...   float(MX.sym("x"))
... except Exception as e:
...   print("'to_double' not defined" in str(e))
True

>> MX(2).to_double()
2
>> try, MX.sym('x').to_double(); catch e, disp('error'); end
error

julia> Float64(ca.MX(2))
2.0
julia> try
           Float64(ca.MX.sym("x"))
       catch e
           println(occursin("requires a constant scalar", sprint(showerror, e)))
       end
true

See also
to_DM, evalf

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

◆ operator Matrix< double >()

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

Only defined for constant nodes (is_constant). For a general expression without free symbols use evalf instead. Not defined for constants created from a file or with a pool name.

>>> MX(DM([[1, 2], [3, 4]])).to_DM()
DM(
[[1, 2],
 [3, 4]])
>>> try:
...   (MX.sym("x") + 1).to_DM()
... except Exception as e:
...   print("'get_DM' not defined" in str(e))
True

>> MX(DM([1 2; 3 4])).to_DM()
[[1, 2],
 [3, 4]]
>> e = MX.sym('x') + 1;
>> try, e.to_DM(); catch err, disp('error'); end
error

julia> ca.to_DM(ca.MX(ca.DM([1 2; 3 4])))
DM(
[[1, 2],
 [3, 4]])
julia> try
           ca.to_DM(ca.MX.sym("x") + 1)
       catch e
           println(occursin("'get_DM' not defined", sprint(showerror, e)))
       end
true

See also
evalf, is_constant

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

◆ operator+()

MX casadi::MX::operator+ ( ) const
inline

Definition at line 798 of file mx.hpp.

◆ operator-()

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

Negating a negation returns the original node.

>>> x = MX.sym("x")
>>> -x, -(-x)
(MX((-x)), MX(x))

>> x = MX.sym('x');
>> -x, -(-x)
(-x)
x

julia> x = ca.MX.sym("x");
julia> -x, -(-x)
((-x), x)

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

◆ primitives()

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

Returns the symbolic primitives in the order in which they appear in the concatenation. For an expression that is not a valid input, the expression itself is returned.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> vertcat(x, y).primitives()
[MX(x), MX(y)]
>>> reshape(x, 1, 2).primitives()
[MX(x)]

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> v = [x; y];
>> p = v.primitives();
>> numel(p), p{1}, p{2}
2
x
y
>> p = reshape(x, 1, 2).primitives();
>> numel(p), p{1}
1
x

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> ca.primitives([x; y])
2-element Vector{MX}:
 MX(x)
 MX(y)
julia> ca.primitives(reshape(x, 1, 2))
1-element Vector{MX}:
 MX(x)

See also
n_primitives, split_primitives, join_primitives, is_valid_input

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

◆ printme()

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

Returns an expression equal to this one; when it is evaluated, every nonzero is printed to standard output as "|> b : value", where b is a scalar expression evaluated alongside it (an identifier for the print). Useful to trace values inside a Function. The printing is not part of the returned value and not seen by the doctest runner's output capture.

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> e = x.printme(y)
>>> e, e.is_op(OP_PRINTME)
(MX(printme(x,y)), True)

>> x = MX.sym('x'); y = MX.sym('y');
>> e = x.printme(y);
>> e, e.is_op(OP_PRINTME)
printme(x,y)
1

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> e = ca.printme(x, y);
julia> e, ca.is_op(e, ca.OP_PRINTME)
(printme(x,y), true)

See also
monitor, attachAssert

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

◆ rank1()

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

Calculates A + alpha * x*y'

Parameters
alphaScalar; if it is a structural zero, A is returned unchanged
xVector with x.numel() == A.size1() (row vectors are transposed)
yVector with y.numel() == A.size2()

>>> A = DM([[1, 2], [3, 4]])
>>> rank1(A, 2, DM([1, 0]), DM([0, 1]))
DM(
[[1, 4],
 [3, 4]])
>>> rank1(MX.sym("A", 2, 2), MX.sym("a"), MX.sym("x", 2), MX.sym("y", 2))
MX(rank1(A, a, x, y))

>> A = DM([1 2; 3 4]);
>> rank1(A, 2, DM([1; 0]), DM([0; 1]))
[[1, 4],
 [3, 4]]
>> rank1(MX.sym('A', 2, 2), MX.sym('a'), MX.sym('x', 2), MX.sym('y', 2))
rank1(A, a, x, y)

julia> A = ca.DM([1 2; 3 4]);
julia> ca.rank1(A, 2, ca.DM([1, 0]), ca.DM([0, 1]))
DM(
[[1, 4],
 [3, 4]])
julia> ca.rank1(ca.MX.sym("A", 2, 2), ca.MX.sym("a"), ca.MX.sym("x", 2), ca.MX.sym("y", 2))
MX(rank1(A, a, x, y))

See also
bilin, mtimes

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

◆ reset_dump_count()

void casadi::MX::reset_dump_count ( )

◆ row()

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

The pattern is stored in compressed column format: colind has size2()+1 entries, the nonzeros of column j are the entries colind(j) up to colind(j+1) (exclusive), and row(el) is the row index of nonzero el. Without argument, the full vectors are returned. Out-of-range indices raise an error.

>>> x = DM(Sparsity.diag(2), [5, 6])
>>> x.row(), x.colind()
([0, 1], [0, 1, 2])
>>> x.row(1), x.colind(2)
(1, 2)

>> x = DM(Sparsity.diag(2), [5; 6]);
>> x.row(), x.colind()
0 1
0 1 2
>> fprintf('%d %d\n', x.row(1), x.colind(2))
1 2

julia> x = ca.DM(ca.Sparsity.diag(2), [5, 6]);
julia> ca.row(x), ca.colind(x)
([0, 1], [0, 1, 2])
julia> ca.row(x, 1), ca.colind(x, 2)
(1, 2)

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

Definition at line 608 of file generic_matrix.hpp.

◆ rows()

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

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

Definition at line 283 of file generic_matrix.hpp.

◆ serialize()

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

From Python, serialize through StringSerializer, FileSerializer or pickle within a pickle context; the stream object is not constructed directly.

>>> x = MX.sym("x")
>>> s = StringSerializer()
>>> s.pack(x + 1)
>>> d = StringDeserializer(s.encode())
>>> d.unpack()
MX((1+x))

>> x = MX.sym('x');
>> s = StringSerializer();
>> s.pack(x + 1);
>> d = StringDeserializer(s.encode());
>> d.unpack()
(1+x)

julia> x = ca.MX.sym("x");
julia> s = ca.StringSerializer();
julia> ca.pack(s, x + 1)
julia> d = ca.StringDeserializer(ca.encode(s));
julia> ca.unpack(d)
MX((1+x))

See also
deserialize, StringSerializer

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

◆ set() [1/7]

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

Expressions are immutable: assignment rebinds this object to a new SetNonzeros node whose dependencies are the old expression and m. Expressions built earlier from the old value are unaffected, but a second name bound to the same object (Y0 = Y) sees the new node; copy the handle with MX(Y) to keep the old value. The sparsity pattern grows to include the assigned elements; assigning a sparse m first erases the targeted elements. m must have the shape of the index set, be a scalar (broadcast), or be its transpose when the index set is a vector; anything else raises a dimension mismatch error. A Sparsity index assigns to the nonzero positions of that pattern. An MX index is only supported for the nonzero (x.nz[k]) form.

>>> Y = MX.sym("Y", 3, 3)
>>> alias, copy, twice = Y, MX(Y), 2*Y
>>> Y[[4]] = 5
>>> Y, Y.n_dep(), Y.dep(0), Y.dep(1)
(MX((Y[4] = 5)), 2, MX(Y), MX(5))
>>> alias, copy, twice
(MX((Y[4] = 5)), MX(Y), MX((2.*Y)))
>>> Z = MX(3, 3)
>>> Z[[0, 8]] = MX.sym("a", 2)
>>> Z.sparsity()
Sparsity(3x3,2nz)
>>> Y = MX.sym("Y", 3, 3)
>>> Y[Sparsity.lower(3)] = 1
>>> Y
MX((Y[0, 1, 2, 4, 5, 8] = ones(6x1)))

>> Y = MX.sym('Y', 3, 3);
>> alias = Y; copy = MX(Y); twice = 2*Y;
>> Y(5) = 5;
>> Y, Y.n_dep(), Y.dep(0), Y.dep(1)
(Y[4] = 5)
2
Y
5
>> alias, copy, twice
(Y[4] = 5)
Y
(2.*Y)
>> Z = MX(3, 3);
>> Z([1, 9]) = MX.sym('a', 2);
>> Z.sparsity()
3x3,2nz
>> Y = MX.sym('Y', 3, 3);
>> Y(Sparsity.lower(3)) = 1;
>> Y
(Y[0, 1, 2, 4, 5, 8] = ones(6x1))

julia> Y = ca.MX.sym("Y", 3, 3);
julia> alias, copy, twice = Y, ca.MX(Y), 2 * Y;
julia> Y[[5]] = 5;
julia> Y, ca.n_dep(Y), ca.dep(Y, 0), ca.dep(Y, 1)
((Y[4] = 5), 2, Y, 5)
julia> alias, copy, twice
((Y[4] = 5), Y, (2.*Y))
julia> Z = ca.MX(3, 3);
julia> Z[[1, 9]] = ca.MX.sym("a", 2);
julia> ca.sparsity(Z)
Sparsity(3x3,2nz)

See also
get, set_nz

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

◆ set() [2/7]

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

Same semantics as the single-index form. m must be len(rr)-by-len(cc), a scalar, or the transpose of a vector block.

>>> Y = MX.sym("Y", 3, 3)
>>> Y[:, 0] = MX.sym("c", 1, 3)
>>> Y
MX((Y[:3] = c'))
>>> Y[0:2, 0:2] = 0
>>> Y.sparsity()
Sparsity(3x3)
>>> try:
...   Y[:, 0] = MX.sym("d", 2)
... except Exception as e:
...   print("Dimension mismatch" in str(e))
True

>> Y = MX.sym('Y', 3, 3);
>> Y(:, 1) = MX.sym('c', 1, 3);
>> Y
(Y[:3] = c')
>> Y(1:2, 1:2) = 0;
>> Y.sparsity()
3x3
>> try, Y(:, 1) = MX.sym('d', 2); catch e, disp('dimension mismatch'); end
dimension mismatch

julia> Y = ca.MX.sym("Y", 3, 3);
julia> Y[:, 1] = ca.MX.sym("c", 1, 3);
julia> Y
MX((Y[:3] = c'))
julia> Y[1:2, 1:2] = 0;
julia> ca.sparsity(Y)
Sparsity(3x3)
julia> try
           Y[:, 1] = ca.MX.sym("d", 2)
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

See also
get

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

◆ set() [3/7]

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

Same semantics as the single-index form. m must be len(rr)-by-len(cc), a scalar, or the transpose of a vector block.

>>> Y = MX.sym("Y", 3, 3)
>>> Y[:, 0] = MX.sym("c", 1, 3)
>>> Y
MX((Y[:3] = c'))
>>> Y[0:2, 0:2] = 0
>>> Y.sparsity()
Sparsity(3x3)
>>> try:
...   Y[:, 0] = MX.sym("d", 2)
... except Exception as e:
...   print("Dimension mismatch" in str(e))
True

>> Y = MX.sym('Y', 3, 3);
>> Y(:, 1) = MX.sym('c', 1, 3);
>> Y
(Y[:3] = c')
>> Y(1:2, 1:2) = 0;
>> Y.sparsity()
3x3
>> try, Y(:, 1) = MX.sym('d', 2); catch e, disp('dimension mismatch'); end
dimension mismatch

julia> Y = ca.MX.sym("Y", 3, 3);
julia> Y[:, 1] = ca.MX.sym("c", 1, 3);
julia> Y
MX((Y[:3] = c'))
julia> Y[1:2, 1:2] = 0;
julia> ca.sparsity(Y)
Sparsity(3x3)
julia> try
           Y[:, 1] = ca.MX.sym("d", 2)
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

See also
get

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

◆ set() [4/7]

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

Expressions are immutable: assignment rebinds this object to a new SetNonzeros node whose dependencies are the old expression and m. Expressions built earlier from the old value are unaffected, but a second name bound to the same object (Y0 = Y) sees the new node; copy the handle with MX(Y) to keep the old value. The sparsity pattern grows to include the assigned elements; assigning a sparse m first erases the targeted elements. m must have the shape of the index set, be a scalar (broadcast), or be its transpose when the index set is a vector; anything else raises a dimension mismatch error. A Sparsity index assigns to the nonzero positions of that pattern. An MX index is only supported for the nonzero (x.nz[k]) form.

>>> Y = MX.sym("Y", 3, 3)
>>> alias, copy, twice = Y, MX(Y), 2*Y
>>> Y[[4]] = 5
>>> Y, Y.n_dep(), Y.dep(0), Y.dep(1)
(MX((Y[4] = 5)), 2, MX(Y), MX(5))
>>> alias, copy, twice
(MX((Y[4] = 5)), MX(Y), MX((2.*Y)))
>>> Z = MX(3, 3)
>>> Z[[0, 8]] = MX.sym("a", 2)
>>> Z.sparsity()
Sparsity(3x3,2nz)
>>> Y = MX.sym("Y", 3, 3)
>>> Y[Sparsity.lower(3)] = 1
>>> Y
MX((Y[0, 1, 2, 4, 5, 8] = ones(6x1)))

>> Y = MX.sym('Y', 3, 3);
>> alias = Y; copy = MX(Y); twice = 2*Y;
>> Y(5) = 5;
>> Y, Y.n_dep(), Y.dep(0), Y.dep(1)
(Y[4] = 5)
2
Y
5
>> alias, copy, twice
(Y[4] = 5)
Y
(2.*Y)
>> Z = MX(3, 3);
>> Z([1, 9]) = MX.sym('a', 2);
>> Z.sparsity()
3x3,2nz
>> Y = MX.sym('Y', 3, 3);
>> Y(Sparsity.lower(3)) = 1;
>> Y
(Y[0, 1, 2, 4, 5, 8] = ones(6x1))

julia> Y = ca.MX.sym("Y", 3, 3);
julia> alias, copy, twice = Y, ca.MX(Y), 2 * Y;
julia> Y[[5]] = 5;
julia> Y, ca.n_dep(Y), ca.dep(Y, 0), ca.dep(Y, 1)
((Y[4] = 5), 2, Y, 5)
julia> alias, copy, twice
((Y[4] = 5), Y, (2.*Y))
julia> Z = ca.MX(3, 3);
julia> Z[[1, 9]] = ca.MX.sym("a", 2);
julia> ca.sparsity(Z)
Sparsity(3x3,2nz)

See also
get, set_nz

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

◆ set() [5/7]

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

Same semantics as the single-index form. m must be len(rr)-by-len(cc), a scalar, or the transpose of a vector block.

>>> Y = MX.sym("Y", 3, 3)
>>> Y[:, 0] = MX.sym("c", 1, 3)
>>> Y
MX((Y[:3] = c'))
>>> Y[0:2, 0:2] = 0
>>> Y.sparsity()
Sparsity(3x3)
>>> try:
...   Y[:, 0] = MX.sym("d", 2)
... except Exception as e:
...   print("Dimension mismatch" in str(e))
True

>> Y = MX.sym('Y', 3, 3);
>> Y(:, 1) = MX.sym('c', 1, 3);
>> Y
(Y[:3] = c')
>> Y(1:2, 1:2) = 0;
>> Y.sparsity()
3x3
>> try, Y(:, 1) = MX.sym('d', 2); catch e, disp('dimension mismatch'); end
dimension mismatch

julia> Y = ca.MX.sym("Y", 3, 3);
julia> Y[:, 1] = ca.MX.sym("c", 1, 3);
julia> Y
MX((Y[:3] = c'))
julia> Y[1:2, 1:2] = 0;
julia> ca.sparsity(Y)
Sparsity(3x3)
julia> try
           Y[:, 1] = ca.MX.sym("d", 2)
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

See also
get

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

◆ set() [6/7]

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

Same semantics as the single-index form. m must be len(rr)-by-len(cc), a scalar, or the transpose of a vector block.

>>> Y = MX.sym("Y", 3, 3)
>>> Y[:, 0] = MX.sym("c", 1, 3)
>>> Y
MX((Y[:3] = c'))
>>> Y[0:2, 0:2] = 0
>>> Y.sparsity()
Sparsity(3x3)
>>> try:
...   Y[:, 0] = MX.sym("d", 2)
... except Exception as e:
...   print("Dimension mismatch" in str(e))
True

>> Y = MX.sym('Y', 3, 3);
>> Y(:, 1) = MX.sym('c', 1, 3);
>> Y
(Y[:3] = c')
>> Y(1:2, 1:2) = 0;
>> Y.sparsity()
3x3
>> try, Y(:, 1) = MX.sym('d', 2); catch e, disp('dimension mismatch'); end
dimension mismatch

julia> Y = ca.MX.sym("Y", 3, 3);
julia> Y[:, 1] = ca.MX.sym("c", 1, 3);
julia> Y
MX((Y[:3] = c'))
julia> Y[1:2, 1:2] = 0;
julia> ca.sparsity(Y)
Sparsity(3x3)
julia> try
           Y[:, 1] = ca.MX.sym("d", 2)
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

See also
get

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

◆ set() [7/7]

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

Expressions are immutable: assignment rebinds this object to a new SetNonzeros node whose dependencies are the old expression and m. Expressions built earlier from the old value are unaffected, but a second name bound to the same object (Y0 = Y) sees the new node; copy the handle with MX(Y) to keep the old value. The sparsity pattern grows to include the assigned elements; assigning a sparse m first erases the targeted elements. m must have the shape of the index set, be a scalar (broadcast), or be its transpose when the index set is a vector; anything else raises a dimension mismatch error. A Sparsity index assigns to the nonzero positions of that pattern. An MX index is only supported for the nonzero (x.nz[k]) form.

>>> Y = MX.sym("Y", 3, 3)
>>> alias, copy, twice = Y, MX(Y), 2*Y
>>> Y[[4]] = 5
>>> Y, Y.n_dep(), Y.dep(0), Y.dep(1)
(MX((Y[4] = 5)), 2, MX(Y), MX(5))
>>> alias, copy, twice
(MX((Y[4] = 5)), MX(Y), MX((2.*Y)))
>>> Z = MX(3, 3)
>>> Z[[0, 8]] = MX.sym("a", 2)
>>> Z.sparsity()
Sparsity(3x3,2nz)
>>> Y = MX.sym("Y", 3, 3)
>>> Y[Sparsity.lower(3)] = 1
>>> Y
MX((Y[0, 1, 2, 4, 5, 8] = ones(6x1)))

>> Y = MX.sym('Y', 3, 3);
>> alias = Y; copy = MX(Y); twice = 2*Y;
>> Y(5) = 5;
>> Y, Y.n_dep(), Y.dep(0), Y.dep(1)
(Y[4] = 5)
2
Y
5
>> alias, copy, twice
(Y[4] = 5)
Y
(2.*Y)
>> Z = MX(3, 3);
>> Z([1, 9]) = MX.sym('a', 2);
>> Z.sparsity()
3x3,2nz
>> Y = MX.sym('Y', 3, 3);
>> Y(Sparsity.lower(3)) = 1;
>> Y
(Y[0, 1, 2, 4, 5, 8] = ones(6x1))

julia> Y = ca.MX.sym("Y", 3, 3);
julia> alias, copy, twice = Y, ca.MX(Y), 2 * Y;
julia> Y[[5]] = 5;
julia> Y, ca.n_dep(Y), ca.dep(Y, 0), ca.dep(Y, 1)
((Y[4] = 5), 2, Y, 5)
julia> alias, copy, twice
((Y[4] = 5), Y, (2.*Y))
julia> Z = ca.MX(3, 3);
julia> Z[[1, 9]] = ca.MX.sym("a", 2);
julia> ca.sparsity(Z)
Sparsity(3x3,2nz)

See also
get, set_nz

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

◆ set_max_depth()

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

Global setting (default 1) used when building expressions: operations such as x - y or x/y test whether their operands are equal up to this depth with is_equal and simplify to 0 or 1 when they are. Depth 0 compares node identity only; each extra level compares the operation and recurses into the dependencies. Larger depths find more simplifications at a cost per created node. Calling without an argument restores the default.

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> MX.get_max_depth(), (x + y) - (x + y)
(1, MX(0))
>>> MX.set_max_depth(0)
>>> (x + y) - (x + y)
MX(((x+y)-(x+y)))
>>> MX.set_max_depth()
>>> MX.get_max_depth()
1

>> x = MX.sym('x'); y = MX.sym('y');
>> MX.get_max_depth(), (x + y) - (x + y)
1
0
>> MX.set_max_depth(0);
>> (x + y) - (x + y)
((x+y)-(x+y))
>> MX.set_max_depth();
>> MX.get_max_depth()
1

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> ca.MX.get_max_depth(), (x + y) - (x + y)
(1, 0)
julia> ca.MX.set_max_depth(0)
julia> (x + y) - (x + y)
MX(((x+y)-(x+y)))
julia> ca.MX.set_max_depth()
julia> ca.MX.get_max_depth()
1

See also
get_max_depth, is_equal

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

◆ set_nz() [1/4]

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

Rebinds this object to a SetNonzeros node; the sparsity pattern is unchanged. m must have as many nonzeros as the index set or be a scalar. With an MX index the assignment is parametric and resolved at evaluation time.

>>> Y = MX.sym("Y", 3, 3)
>>> Y.nz[0] = 7
>>> Y
MX((Y[0] = 7))
>>> k = MX.sym("k")
>>> Y.nz[k] = 8
>>> Y, Y.is_op(OP_SETNONZEROS_PARAM)
(MX(((Y[0] = 7)[k] = 8)), True)

>> Y = MX.sym('Y', 3, 3);
>> Y.set_nz(7, true, 1);
>> Y
(Y[0] = 7)
>> k = MX.sym('k');
>> Y.set_nz(8, true, k);
>> Y, Y.is_op(OP_SETNONZEROS_PARAM)
((Y[0] = 7)[(k-1)] = 8)
1

julia> Y = ca.MX.sym("Y", 3, 3);
julia> Y.nz[1] = 7;
julia> Y
MX((Y[0] = 7))
julia> k = ca.MX.sym("k");
julia> Y.nz[k] = 8;
julia> Y
MX(((Y[0] = 7)[(k-1)] = 8))
julia> ca.is_op(Y, ca.OP_SETNONZEROS_PARAM)
true

See also
get_nz, set

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

Definition at line 3238 of file mx.hpp.

◆ set_nz() [2/4]

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

Rebinds this object to a SetNonzeros node; the sparsity pattern is unchanged. m must have as many nonzeros as the index set or be a scalar. With an MX index the assignment is parametric and resolved at evaluation time.

>>> Y = MX.sym("Y", 3, 3)
>>> Y.nz[0] = 7
>>> Y
MX((Y[0] = 7))
>>> k = MX.sym("k")
>>> Y.nz[k] = 8
>>> Y, Y.is_op(OP_SETNONZEROS_PARAM)
(MX(((Y[0] = 7)[k] = 8)), True)

>> Y = MX.sym('Y', 3, 3);
>> Y.set_nz(7, true, 1);
>> Y
(Y[0] = 7)
>> k = MX.sym('k');
>> Y.set_nz(8, true, k);
>> Y, Y.is_op(OP_SETNONZEROS_PARAM)
((Y[0] = 7)[(k-1)] = 8)
1

julia> Y = ca.MX.sym("Y", 3, 3);
julia> Y.nz[1] = 7;
julia> Y
MX((Y[0] = 7))
julia> k = ca.MX.sym("k");
julia> Y.nz[k] = 8;
julia> Y
MX(((Y[0] = 7)[(k-1)] = 8))
julia> ca.is_op(Y, ca.OP_SETNONZEROS_PARAM)
true

See also
get_nz, set

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

◆ set_nz() [3/4]

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

Rebinds this object to a SetNonzeros node; the sparsity pattern is unchanged. m must have as many nonzeros as the index set or be a scalar. With an MX index the assignment is parametric and resolved at evaluation time.

>>> Y = MX.sym("Y", 3, 3)
>>> Y.nz[0] = 7
>>> Y
MX((Y[0] = 7))
>>> k = MX.sym("k")
>>> Y.nz[k] = 8
>>> Y, Y.is_op(OP_SETNONZEROS_PARAM)
(MX(((Y[0] = 7)[k] = 8)), True)

>> Y = MX.sym('Y', 3, 3);
>> Y.set_nz(7, true, 1);
>> Y
(Y[0] = 7)
>> k = MX.sym('k');
>> Y.set_nz(8, true, k);
>> Y, Y.is_op(OP_SETNONZEROS_PARAM)
((Y[0] = 7)[(k-1)] = 8)
1

julia> Y = ca.MX.sym("Y", 3, 3);
julia> Y.nz[1] = 7;
julia> Y
MX((Y[0] = 7))
julia> k = ca.MX.sym("k");
julia> Y.nz[k] = 8;
julia> Y
MX(((Y[0] = 7)[(k-1)] = 8))
julia> ca.is_op(Y, ca.OP_SETNONZEROS_PARAM)
true

See also
get_nz, set

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

◆ set_nz() [4/4]

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

Rebinds this object to a SetNonzeros node; the sparsity pattern is unchanged. m must have as many nonzeros as the index set or be a scalar. With an MX index the assignment is parametric and resolved at evaluation time.

>>> Y = MX.sym("Y", 3, 3)
>>> Y.nz[0] = 7
>>> Y
MX((Y[0] = 7))
>>> k = MX.sym("k")
>>> Y.nz[k] = 8
>>> Y, Y.is_op(OP_SETNONZEROS_PARAM)
(MX(((Y[0] = 7)[k] = 8)), True)

>> Y = MX.sym('Y', 3, 3);
>> Y.set_nz(7, true, 1);
>> Y
(Y[0] = 7)
>> k = MX.sym('k');
>> Y.set_nz(8, true, k);
>> Y, Y.is_op(OP_SETNONZEROS_PARAM)
((Y[0] = 7)[(k-1)] = 8)
1

julia> Y = ca.MX.sym("Y", 3, 3);
julia> Y.nz[1] = 7;
julia> Y
MX((Y[0] = 7))
julia> k = ca.MX.sym("k");
julia> Y.nz[k] = 8;
julia> Y
MX(((Y[0] = 7)[(k-1)] = 8))
julia> ca.is_op(Y, ca.OP_SETNONZEROS_PARAM)
true

See also
get_nz, set

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

◆ simplify_combine_terms()

static bool casadi::MX::simplify_combine_terms ( std::vector< MX > &  arg,
std::vector< MX > &  res,
const Dict &  opts = Dict() 
)
static

◆ simplify_const_folding()

static bool casadi::MX::simplify_const_folding ( std::vector< MX > &  arg,
std::vector< MX > &  res,
const Dict &  opts = Dict() 
)
static

◆ simplify_ref_count()

static bool casadi::MX::simplify_ref_count ( std::vector< MX > &  arg,
std::vector< MX > &  res,
const Dict &  opts = Dict() 
)
static

◆ size() [1/2]

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

>>> x = MX.sym("x", 2, 3)
>>> x.size(), x.size(1), x.size(2)
((2, 3), 2, 3)

>> x = MX.sym('x', 2, 3);
>> size(x)
2 3
>> fprintf('%d %d\n', size(x, 1), size(x, 2))
2 3

julia> x = ca.MX.sym("x", 2, 3);
julia> size(x), size(x, 1), size(x, 2)
((2, 3), 2, 3)

See also
size1, size2, numel, dim

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

◆ size() [2/2]

casadi_int casadi::GenericMatrix< MX >::size ( casadi_int  axis) const
inherited
Parameters
axis1 for the number of rows, 2 for the number of columns; any other value raises an error

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

◆ size1()

casadi_int casadi::GenericMatrix< MX >::size1 ( ) const
inherited

>>> x = MX.sym("x", 2, 3)
>>> x.size1(), x.size2()
(2, 3)

>> x = MX.sym('x', 2, 3);
>> fprintf('%d %d\n', x.size1(), x.size2())
2 3

julia> x = ca.MX.sym("x", 2, 3);
julia> ca.size1(x), ca.size2(x)
(2, 3)

See also
size2, size, rows

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

◆ size2()

casadi_int casadi::GenericMatrix< MX >::size2 ( ) const
inherited

◆ soc()

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

Functions called by friend functions defined here

◆ sparsity()

Sparsity casadi::GenericMatrix< MX >::sparsity ( ) const
inherited

>>> DM.eye(2).sparsity()
Sparsity(2x2,2nz)
>>> DM.eye(2).sparsity() == Sparsity.diag(2)
True

>> DM.eye(2).sparsity()
2x2,2nz
>> DM.eye(2).sparsity() == Sparsity.diag(2)
1

julia> ca.sparsity(ca.DM.eye(2))
Sparsity(2x2,2nz)
julia> ca.is_equal(ca.sparsity(ca.DM.eye(2)), ca.Sparsity.diag(2))
true

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

◆ split_primitives() [1/3]

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

Uses this expression (a valid Function input, see is_valid_input) as a template: x must have the same shape and is cut into one piece per primitive, each with the shape of that primitive. The typical use is to split a solver result back into the variables it was concatenated from. Works with numeric (DM), SX and MX values. A shape mismatch raises an error.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> X = vertcat(x, y)
>>> X.split_primitives(DM([1, 2, 3, 4, 5]))
[DM([1, 2]), DM([3, 4, 5])]
>>> X.split_primitives(SX.sym("z", 5))
[SX([z_0, z_1]), SX([z_2, z_3, z_4])]

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> X = [x; y];
>> parts = X.split_primitives(DM([1; 2; 3; 4; 5]));
>> parts{1}, parts{2}
[1, 2]
[3, 4, 5]
>> parts = X.split_primitives(SX.sym('z', 5));
>> parts{1}, parts{2}
[z_0, z_1]
[z_2, z_3, z_4]

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> X = [x; y];
julia> ca.split_primitives(X, ca.DM([1, 2, 3, 4, 5]))
2-element Vector{DM}:
 DM([1, 2])
 DM([3, 4, 5])
julia> ca.split_primitives(X, ca.SX.sym("z", 5))
2-element Vector{SX}:
 SX([z_0, z_1])
 SX([z_2, z_3, z_4])

See also
join_primitives, primitives

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

◆ split_primitives() [2/3]

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

Uses this expression (a valid Function input, see is_valid_input) as a template: x must have the same shape and is cut into one piece per primitive, each with the shape of that primitive. The typical use is to split a solver result back into the variables it was concatenated from. Works with numeric (DM), SX and MX values. A shape mismatch raises an error.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> X = vertcat(x, y)
>>> X.split_primitives(DM([1, 2, 3, 4, 5]))
[DM([1, 2]), DM([3, 4, 5])]
>>> X.split_primitives(SX.sym("z", 5))
[SX([z_0, z_1]), SX([z_2, z_3, z_4])]

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> X = [x; y];
>> parts = X.split_primitives(DM([1; 2; 3; 4; 5]));
>> parts{1}, parts{2}
[1, 2]
[3, 4, 5]
>> parts = X.split_primitives(SX.sym('z', 5));
>> parts{1}, parts{2}
[z_0, z_1]
[z_2, z_3, z_4]

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> X = [x; y];
julia> ca.split_primitives(X, ca.DM([1, 2, 3, 4, 5]))
2-element Vector{DM}:
 DM([1, 2])
 DM([3, 4, 5])
julia> ca.split_primitives(X, ca.SX.sym("z", 5))
2-element Vector{SX}:
 SX([z_0, z_1])
 SX([z_2, z_3, z_4])

See also
join_primitives, primitives

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

◆ split_primitives() [3/3]

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

Uses this expression (a valid Function input, see is_valid_input) as a template: x must have the same shape and is cut into one piece per primitive, each with the shape of that primitive. The typical use is to split a solver result back into the variables it was concatenated from. Works with numeric (DM), SX and MX values. A shape mismatch raises an error.

>>> x = MX.sym("x", 2)
>>> y = MX.sym("y", 3)
>>> X = vertcat(x, y)
>>> X.split_primitives(DM([1, 2, 3, 4, 5]))
[DM([1, 2]), DM([3, 4, 5])]
>>> X.split_primitives(SX.sym("z", 5))
[SX([z_0, z_1]), SX([z_2, z_3, z_4])]

>> x = MX.sym('x', 2); y = MX.sym('y', 3);
>> X = [x; y];
>> parts = X.split_primitives(DM([1; 2; 3; 4; 5]));
>> parts{1}, parts{2}
[1, 2]
[3, 4, 5]
>> parts = X.split_primitives(SX.sym('z', 5));
>> parts{1}, parts{2}
[z_0, z_1]
[z_2, z_3, z_4]

julia> x = ca.MX.sym("x", 2); y = ca.MX.sym("y", 3);
julia> X = [x; y];
julia> ca.split_primitives(X, ca.DM([1, 2, 3, 4, 5]))
2-element Vector{DM}:
 DM([1, 2])
 DM([3, 4, 5])
julia> ca.split_primitives(X, ca.SX.sym("z", 5))
2-element Vector{SX}:
 SX([z_0, z_1])
 SX([z_2, z_3, z_4])

See also
join_primitives, primitives

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

◆ 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

>>> SX.sym("x", 1, 1, 2)
[SX(x0), SX(x1)]

>> v = SX.sym('x', 1, 1, 2);
>> v{1}, v{2}
x0
x1

julia> ca.SX.sym("x", 1, 1, 2)
2-element Vector{SX}:
 SX(x0)
 SX(x1)

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

Definition at line 5063 of file generic_matrix.hpp.

◆ 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

>>> MX.sym("x", 1, 1, 2, 2)
[[MX(x_00), MX(x_01)], [MX(x_10), MX(x_11)]]

>> v = MX.sym('x', 1, 1, 2, 2);
>> numel(v), v{1}{2}, v{2}{1}
2
x_01
x_10

julia> ca.MX.sym("x", 1, 1, 2, 2)
2-element Vector{Vector{MX}}:
 MX[x_00, x_01]
 MX[x_10, x_11]

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

Definition at line 5107 of file generic_matrix.hpp.

◆ sym() [3/7]

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

Creates a new, dense symbol; two symbols with the same name are still different symbols. For SX each entry is a separate scalar symbol named name_k (k the column-major index), while an MX symbol is a single node with the given name. Only SX and MX have symbols; DM.sym raises an error.

Parameters
nameName used for printing. For SX, a name of the form "[a,b,c]" assigns individual names to the numel() entries.
nrowNumber of rows, default 1
ncolNumber of columns, default 1

>>> SX.sym("x"), MX.sym("x", 2, 3).dim()
(SX(x), '2x3')
>>> SX.sym("x", 2, 2)
SX(
[[x_0, x_2],
 [x_1, x_3]])
>>> SX.sym("[a,b,c]", 3)
SX([a, b, c])
>>> is_equal(SX.sym("x"), SX.sym("x"))
False

>> SX.sym('x'), MX.sym('x', 2, 3).dim()
x
2x3
>> SX.sym('x', 2, 2)
[[x_0, x_2],
 [x_1, x_3]]
>> SX.sym('[a,b,c]', 3)
[a, b, c]
>> is_equal(SX.sym('x'), SX.sym('x'))
0

julia> ca.SX.sym("x"), ca.dim(ca.MX.sym("x", 2, 3))
(x, "2x3")
julia> ca.SX.sym("x", 2, 2)
SX(
[[x_0, x_2],
 [x_1, x_3]])
julia> ca.SX.sym("[a,b,c]", 3)
SX([a, b, c])
julia> isequal(ca.SX.sym("x"), ca.SX.sym("x"))
false

See also
symvar, zeros, ones

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

Definition at line 4948 of file generic_matrix.hpp.

◆ sym() [4/7]

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

Only the structural nonzeros of sp become symbolic entries; for SX they are numbered by nonzero index.

>>> SX.sym("p", Sparsity.diag(2))
SX(
[[p_0, 00],
 [00, p_1]])
>>> MX.sym("p", Sparsity.diag(2)).dim(True)
'2x2,2nz'

>> SX.sym('p', Sparsity.diag(2))
[[p_0, 00],
 [00, p_1]]
>> MX.sym('p', Sparsity.diag(2)).dim(true)
2x2,2nz

julia> ca.SX.sym("p", ca.Sparsity.diag(2))
SX(
[[p_0, 00],
 [00, p_1]])
julia> ca.dim(ca.MX.sym("p", ca.Sparsity.diag(2)), true)
"2x2,2nz"

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

Definition at line 4997 of file generic_matrix.hpp.

◆ sym() [5/7]

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

The k-th symbol is named name followed by k (no separator).

>>> MX.sym("x", Sparsity.dense(2, 1), 3)
[MX(x0), MX(x1), MX(x2)]

>> v = MX.sym('x', Sparsity.dense(2, 1), 3);
>> numel(v), v{1}, v{3}
3
x0
x2

julia> ca.MX.sym("x", ca.Sparsity.dense(2, 1), 3)
3-element Vector{MX}:
 MX(x0)
 MX(x1)
 MX(x2)

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

Definition at line 5033 of file generic_matrix.hpp.

◆ 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

with given sparsity. The symbol at outer index i and inner index k is named name_ik.

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

Definition at line 5075 of file generic_matrix.hpp.

◆ sym() [7/7]

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

◆ T()

MX casadi::MX::T ( ) const

In Python use the property x.T. Scalars are returned as is, vectors become a reshape node and matrices a transpose node. Transposing twice returns the original node.

>>> A = MX.sym("A", 2, 3)
>>> A.T, A.T.shape, A.T.T
(MX(A'), (3, 2), MX(A))

>> A = MX.sym('A', 2, 3);
>> At = A';
>> At, size(At), At'
A'
3 2
A

julia> A = ca.MX.sym("A", 2, 3);
julia> A', size(A'), (A')'
(A', (3, 2), A)

See also
is_transpose, transpose

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

◆ 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()

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

◆ type_name()

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

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

Definition at line 150 of file mx.hpp.

◆ unary()

static MX casadi::MX::unary ( casadi_int  op,
const MX &  x,
bool  unique = false 
)
static

Low-level constructors behind the operators: MX.binary(OP_ADD, x, y) is x + y. Operands must have the same shape, or one of them is a scalar, or one is a horizontal repetition of the other (which is repmat-ed). The usual simplifications are applied. An invalid operation code is not checked.

Parameters
opOne of the OP_* constants
unique_x,unique_yInternal hints for in-place simplification; leave false

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> MX.binary(OP_ADD, x, y), MX.unary(OP_SIN, x)
(MX((x+y)), MX(sin(x)))
>>> MX.binary(OP_ADD, MX.sym("V", 2, 4), MX.sym("W", 2, 2))
MX((V+repmat(W, 2)))
>>> try:
...   MX.binary(OP_ADD, MX.sym("v", 2), MX.sym("w", 3))
... except Exception as e:
...   print("Dimension mismatch" in str(e))
True

>> x = MX.sym('x'); y = MX.sym('y');
>> MX.binary(OP_ADD, x, y), MX.unary(OP_SIN, x)
(x+y)
sin(x)
>> MX.binary(OP_ADD, MX.sym('V', 2, 4), MX.sym('W', 2, 2))
(V+repmat(W, 2))
>> v = MX.sym('v', 2); w = MX.sym('w', 3);
>> try, MX.binary(OP_ADD, v, w); catch e, disp('dimension mismatch'); end
dimension mismatch

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> ca.MX.binary(ca.OP_ADD, x, y), ca.MX.unary(ca.OP_SIN, x)
((x+y), sin(x))
julia> ca.MX.binary(ca.OP_ADD, ca.MX.sym("V", 2, 4), ca.MX.sym("W", 2, 2))
MX((V+repmat(W, 2)))
julia> try
           ca.MX.binary(ca.OP_ADD, ca.MX.sym("v", 2), ca.MX.sym("w", 3))
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

See also
op

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

◆ which_function()

Function casadi::MX::which_function ( ) const

Only valid when is_call() is true; raises an error otherwise, also for the OutputNode of a call (use dep(0) to reach the call node).

>>> a = SX.sym("a")
>>> f = Function("f", [a], [2*a])
>>> x = MX.sym("x")
>>> r = f(x)
>>> r.dep(0).which_function().name()
'f'
>>> try:
...   r.which_function()
... except Exception as e:
...   print("'which_function' not defined" in str(e))
True

>> a = SX.sym('a');
>> f = Function('f', {a}, {2*a});
>> x = MX.sym('x');
>> r = f(x);
>> call = r.dep(0);
>> call.which_function().name()
f
>> try, r.which_function(); catch e, disp('error'); end
error

julia> a = ca.SX.sym("a");
julia> f = ca.Function("f", [a], [2 * a]);
julia> x = ca.MX.sym("x");
julia> r = f(x);
julia> ca.name(ca.which_function(ca.dep(r, 0)))
"f"
julia> try
           ca.which_function(r)
       catch e
           println(occursin("'which_function' not defined", sprint(showerror, e)))
       end
true

See also
is_call

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

◆ which_output()

casadi_int casadi::MX::which_output ( ) const

Only valid when is_output() is true; raises an error otherwise.

>>> a = SX.sym("a")
>>> f = Function("f", [a], [a + 1, 2*a])
>>> x = MX.sym("x")
>>> r0, r1 = f(x, )
>>> r0.which_output(), r1.which_output()
(0, 1)

>> a = SX.sym('a');
>> f = Function('f', {a}, {a + 1, 2*a});
>> x = MX.sym('x');
>> [r0, r1] = f(x);
>> r0.which_output(), r1.which_output()
0
1

julia> a = ca.SX.sym("a");
julia> f = ca.Function("f", [a], [a + 1, 2 * a]);
julia> x = ca.MX.sym("x");
julia> r0, r1 = f(x);
julia> ca.which_output(r0), ca.which_output(r1)
(0, 1)

See also
is_output

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

◆ zeros() [1/3]

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

zeros(nrow, ncol) stores nrow*ncol numerical zeros; zeros(sp) stores a zero at every structural nonzero of sp. To get structural zeros instead, construct from a shape, e.g. DM(2, 2).

>>> DM.zeros(2, 2).nnz(), DM.zeros(Sparsity.diag(2)).nnz(), DM(2, 2).nnz()
(4, 2, 0)
>>> MX.zeros(2, 3)
MX(zeros(2x3))

>> Z = DM.zeros(Sparsity.diag(2));
>> fprintf('%d %d %d\n', DM.zeros(2, 2).nnz(), Z.nnz(), DM(2, 2).nnz())
4 2 0
>> MX.zeros(2, 3)
zeros(2x3)

julia> Z = ca.DM.zeros(ca.Sparsity.diag(2));
julia> ca.nnz(ca.DM.zeros(2, 2)), ca.nnz(Z), ca.nnz(ca.DM(2, 2))
(4, 2, 0)
julia> ca.MX.zeros(2, 3)
MX(zeros(2x3))

See also
ones, Sparsity

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

Definition at line 5151 of file generic_matrix.hpp.

◆ zeros() [2/3]

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

zeros(nrow, ncol) stores nrow*ncol numerical zeros; zeros(sp) stores a zero at every structural nonzero of sp. To get structural zeros instead, construct from a shape, e.g. DM(2, 2).

>>> DM.zeros(2, 2).nnz(), DM.zeros(Sparsity.diag(2)).nnz(), DM(2, 2).nnz()
(4, 2, 0)
>>> MX.zeros(2, 3)
MX(zeros(2x3))

>> Z = DM.zeros(Sparsity.diag(2));
>> fprintf('%d %d %d\n', DM.zeros(2, 2).nnz(), Z.nnz(), DM(2, 2).nnz())
4 2 0
>> MX.zeros(2, 3)
zeros(2x3)

julia> Z = ca.DM.zeros(ca.Sparsity.diag(2));
julia> ca.nnz(ca.DM.zeros(2, 2)), ca.nnz(Z), ca.nnz(ca.DM(2, 2))
(4, 2, 0)
julia> ca.MX.zeros(2, 3)
MX(zeros(2x3))

See also
ones, Sparsity

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

Definition at line 5154 of file generic_matrix.hpp.

◆ zeros() [3/3]

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

zeros(nrow, ncol) stores nrow*ncol numerical zeros; zeros(sp) stores a zero at every structural nonzero of sp. To get structural zeros instead, construct from a shape, e.g. DM(2, 2).

>>> DM.zeros(2, 2).nnz(), DM.zeros(Sparsity.diag(2)).nnz(), DM(2, 2).nnz()
(4, 2, 0)
>>> MX.zeros(2, 3)
MX(zeros(2x3))

>> Z = DM.zeros(Sparsity.diag(2));
>> fprintf('%d %d %d\n', DM.zeros(2, 2).nnz(), Z.nnz(), DM(2, 2).nnz())
4 2 0
>> MX.zeros(2, 3)
zeros(2x3)

julia> Z = ca.DM.zeros(ca.Sparsity.diag(2));
julia> ca.nnz(ca.DM.zeros(2, 2)), ca.nnz(Z), ca.nnz(ca.DM(2, 2))
(4, 2, 0)
julia> ca.MX.zeros(2, 3)
MX(zeros(2x3))

See also
ones, Sparsity

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

Definition at line 5155 of file generic_matrix.hpp.


The documentation for this class was generated from the following file: