List of all members | Public Types | Public Member Functions | Static Public Member Functions | Friends
casadi::Matrix< Scalar > Class Template Reference

Sparse matrix class. SX and DM are specializations. More...

#include <matrix_decl.hpp>

Detailed Description

template<typename Scalar>
class casadi::Matrix< Scalar >

General sparse matrix class that is designed with the idea that "everything is a matrix", that is, also scalars and vectors.
This philosophy makes it easy to use and to interface in particularly with Python and Matlab/Octave.
Index starts with 0.
Index vec happens as follows: (rr, cc) -> k = rr+cc*size1()
Vectors are column vectors.
The storage format is Compressed Column Storage (CCS), similar to that used for sparse matrices in Matlab,
but unlike this format, we do allow for elements to be structurally non-zero but numerically zero.
Matrix<Scalar> is polymorphic with a std::vector<Scalar> that contain all non-identical-zero elements.
The sparsity can be accessed with Sparsity& sparsity()
SX holds symbolic scalar entries (SXElem), DM holds doubles. A structural zero prints as 00, a numerical zero as 0. The structural nonzeros are available as a flat column-major vector with nonzeros().

>>> A = DM([[1, 2], [3, 4]])
>>> A.sparsity().dim(), A.nonzeros()
('2x2', [1.0, 3.0, 2.0, 4.0])
>>> S = DM(Sparsity.diag(2))
>>> S
DM(
[[1, 00],
 [00, 1]])
>>> S.nnz(), S.numel()
(2, 4)

>> A = DM([1 2; 3 4]);
>> A.sparsity().dim(), A.nonzeros()
2x2
1 3 2 4
>> S = DM(Sparsity.diag(2));
>> S
[[1, 00],
 [00, 1]]
>> fprintf('%d %d\n', S.nnz(), S.numel())
2 4

julia> A = ca.DM([1 2; 3 4]);
julia> ca.dim(ca.sparsity(A)), ca.nonzeros(A)
("2x2", [1.0, 3.0, 2.0, 4.0])
julia> S = ca.DM(ca.Sparsity.diag(2))
DM(
[[1, 00],
 [00, 1]])
julia> ca.nnz(S), ca.numel(S)
(2, 4)

Author
Joel Andersson
Date
2010-2014

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

Definition at line 143 of file matrix_decl.hpp.

Inheritance diagram for casadi::Matrix< Scalar >:
Inheritance graph
[legend]
Collaboration diagram for casadi::Matrix< Scalar >:
Collaboration graph
[legend]

Public Types

typedef Scalar ScalarType
 Scalar type. More...
 
typedef GenericMatrix< Matrix< Scalar > > B
 Base class. More...
 

Public Member Functions

 Matrix ()
 Empty 0-by-0 matrix. More...
 
 Matrix (const Matrix< Scalar > &m)
 Copy constructor: the copy has its own sparsity pattern and nonzeros. More...
 
Matrix< Scalar > & operator= (const Matrix< Scalar > &m)
 Assignment (normal) More...
 
 Matrix (casadi_int nrow, casadi_int ncol)
 Create a matrix of the given shape with all structural zeros (no nonzeros) More...
 
 Matrix (const std::pair< casadi_int, casadi_int > &rc)
 Create a sparse matrix with all structural zeros. More...
 
std::vector< Scalar > * operator-> ()
 Access functions of the node. More...
 
const std::vector< Scalar > * operator-> () const
 Const access functions of the node. More...
 
 Matrix (const Sparsity &sp)
 Create a matrix with the given sparsity pattern and all nonzeros equal to one. More...
 
 Matrix (const Sparsity &sp, const Matrix< Scalar > &d)
 Create a matrix with a given sparsity pattern and given nonzeros. More...
 
 Matrix (double val)
 Dense 1-by-1 matrix holding a number; enables implicit conversion from a numeric type. More...
 
 Matrix (const std::vector< std::vector< double > > &m)
 Dense matrix from a list of rows. More...
 
template<typename A >
 Matrix (const std::vector< A > &x)
 Dense column vector from a flat list of numbers. More...
 
template<typename A >
 Matrix (const Matrix< A > &x)
 Create a matrix from another matrix with a different entry type. More...
 
 Matrix (const std::vector< Scalar > &x)
 Construct from a vector. More...
 
 Matrix (std::initializer_list< Scalar > x)
 Construct from initializer list. More...
 
const Scalar scalar () const
 Convert to scalar type. More...
 
bool has_nz (casadi_int rr, casadi_int cc) const
 Check whether element (rr, cc) is a structural nonzero. More...
 
bool __nonzero__ () const
 Truth value of a 1-by-1 matrix (Python: bool(x)) More...
 
Matrix< Scalar > operator+ () const
 
Matrix< Scalar > operator- () const
 
Matrix< Scalar > printme (const Matrix< Scalar > &y) const
 
Matrix< Scalar > T () const
 Transpose the matrix. More...
 
void print_split (std::vector< std::string > &nz, std::vector< std::string > &inter) const
 String representation of each nonzero and of the shared subexpressions. More...
 
void disp (std::ostream &stream, bool more=false) const
 Print a representation of the object to a stream. More...
 
std::string get_str (bool more=false) const
 Get the string representation that disp prints. More...
 
void print_scalar (std::ostream &stream) const
 Print a 1-by-1 matrix as a bare number or expression. More...
 
void print_vector (std::ostream &stream, bool truncate=true) const
 Print a column vector as [a, b, ...], structural zeros as 00. More...
 
void print_dense (std::ostream &stream, bool truncate=true) const
 Print as a dense matrix, one row per line, structural zeros as 00. More...
 
void print_sparse (std::ostream &stream, bool truncate=true) const
 Print as a list of (row, col) -> value triplets, column-major order. More...
 
void clear ()
 Reset to an empty 0-by-0 matrix, in place. More...
 
void resize (casadi_int nrow, casadi_int ncol)
 Change the shape in place, keeping nonzeros that remain inside the new bounds. More...
 
void reserve (casadi_int nnz)
 Reserve storage for the given number of nonzeros (performance hint, no visible effect) More...
 
void reserve (casadi_int nnz, casadi_int ncol)
 
void erase (const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
 Erase the block at the given rows and columns, leaving structural zeros. More...
 
void erase (const std::vector< casadi_int > &rr, bool ind1=false)
 Erase the elements at the given linear indices, leaving structural zeros. More...
 
void remove (const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc)
 Delete rows and columns, shrinking the matrix in 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 the matrix in place, moving the existing rows and columns to new positions. More...
 
const Sparsity & sparsity () const
 Const access the sparsity - reference to data member. More...
 
Sparsity get_sparsity () const
 Get the sparsity pattern (Python: sparsity()) More...
 
Matrix< Scalar > get_output (casadi_int oind) const
 Get an output. More...
 
casadi_int element_hash () const
 Hash of the node of a 1-by-1 SX expression. More...
 
bool is_regular () const
 Check that no entry is nan, inf or -inf. More...
 
bool is_smooth () const
 Check whether an SX expression contains only smooth operations. More...
 
bool is_leaf () const
 Check if SX is a leaf of the SX graph. More...
 
bool is_commutative () const
 Check whether a binary SX is commutative. More...
 
bool is_symbolic () const
 Check if symbolic (Dense) More...
 
bool is_valid_input () const
 Check if matrix can be used to define function inputs. More...
 
bool has_duplicates () const
 Detect symbolic primitives that appear more than once in an SX matrix. More...
 
void reset_input () const
 Clear the markers set by has_duplicates. More...
 
bool is_constant () const
 Check if every entry is a numerical constant (no symbols) More...
 
bool is_call () const
 Check if function call. More...
 
bool is_output () const
 Check if evaluation output. More...
 
bool has_output () const
 Check if a multiple output node. More...
 
casadi_int which_output () const
 Get the index of evaluation output - only valid when is_output() is true. More...
 
Function which_function () const
 Get function - only valid when is_call() is true. More...
 
bool is_integer () const
 Check if the matrix is integer-valued. More...
 
bool is_zero () const
 check if the matrix is 0 (note that false negative answers are possible) More...
 
bool is_one () const
 check if the matrix is 1 (note that false negative answers are possible) More...
 
bool is_minus_one () const
 check if the matrix is -1 (note that false negative answers are possible) More...
 
bool is_half () const
 check if the matrix is 0.5 (note that false negative answers are possible) More...
 
bool is_value (double val) const
 check if the matrix is value More...
 
bool is_inf () const
 check if the matrix is inf (note that false negative answers are possible) More...
 
bool is_minus_inf () const
 check if the matrix is -inf (note that false negative answers are possible) More...
 
bool is_nonnegative () const
 check if the matrix is not negative More...
 
bool is_eye () const
 check if the matrix is an identity matrix (note that false negative answers More...
 
casadi_int op () const
 Get operation type. More...
 
bool is_op (casadi_int op) const
 Is it a certain operation. More...
 
bool has_zeros () const
 Check if any structural nonzero is known to be numerically zero. More...
 
std::vector< Scalar > get_nonzeros () const
 Get all structural nonzeros as a flat list in column-major order (Python: nonzeros()) More...
 
std::vector< Scalar > get_elements () const
 Get all elements as a flat list in column-major order (Python: elements()) More...
 
template<typename A >
std::vector< A > get_nonzeros () const
 Get all nonzeros. More...
 
 operator double () const
 Conversion of a 1-by-1 matrix to a double (Python: float(x)) More...
 
 operator casadi_int () const
 Conversion of a 1-by-1 matrix to an integer (Python: int(x)), truncating. More...
 
template<typename A >
 operator std::vector< A > () const
 Type conversion to a vector. More...
 
std::string name () const
 Name of a 1-by-1 symbolic SX primitive. More...
 
Matrix< Scalar > dep (casadi_int ch=0) const
 Get operand ch of a 1-by-1 SX expression. More...
 
casadi_int n_dep () const
 Number of operands of a 1-by-1 SX expression (0 for symbols and constants) More...
 
void export_code (const std::string &lang, std::ostream &stream=casadi::uout(), const Dict &options=Dict()) const
 Export matrix in specific language. More...
 
Dict info () const
 Dictionary with the sparsity pattern and the nonzeros (DM only) More...
 
void serialize (std::ostream &stream) const
 Serialize an object. More...
 
std::string serialize () const
 Serialize to a string. More...
 
void serialize (SerializingStream &s) const
 Serialize an object. More...
 
void to_file (const std::string &filename, const std::string &format="") const
 Write a numerical matrix to a text file (DM only) More...
 
 Matrix (const Sparsity &sp, const Scalar &val, bool dummy)
 Sparse matrix with a given sparsity with all values same. More...
 
 Matrix (const Sparsity &sp, const std::vector< Scalar > &d, bool dummy)
 Sparse matrix with a given sparsity and non-zero elements. 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...
 
const Matrix< Scalar > nz (const K &k) const
 Get vector nonzero or slice of nonzeros. More...
 
NonZeros< Matrix< Scalar >, K > nz (const K &k)
 Access vector nonzero or slice of nonzeros. More...
 
const Matrix< Scalar > operator() (const RR &rr) const
 Get vector element or slice. More...
 
const Matrix< Scalar > operator() (const RR &rr, const CC &cc) const
 Get Matrix element or slice. More...
 
SubIndex< Matrix< Scalar >, RR > operator() (const RR &rr)
 Access Matrix elements (one argument) More...
 
SubMatrix< Matrix< Scalar >, RR, CC > operator() (const RR &rr, const CC &cc)
 Access Matrix elements (two arguments) More...
 
void get (Matrix< Scalar > &m, bool ind1, const Slice &rr) const
 Get elements by a single (linear) index. More...
 
void get (Matrix< Scalar > &m, bool ind1, const Matrix< casadi_int > &rr) const
 Get elements by a single (linear) index. More...
 
void get (Matrix< Scalar > &m, bool ind1, const Sparsity &sp) const
 Get elements by a single (linear) index. More...
 
void get (Matrix< Scalar > &m, bool ind1, const Slice &rr, const Slice &cc) const
 Get a submatrix by row and column indices. More...
 
void get (Matrix< Scalar > &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc) const
 Get a submatrix by row and column indices. More...
 
void get (Matrix< Scalar > &m, bool ind1, const Matrix< casadi_int > &rr, const Slice &cc) const
 Get a submatrix by row and column indices. More...
 
void get (Matrix< Scalar > &m, bool ind1, const Matrix< casadi_int > &rr, const Matrix< casadi_int > &cc) const
 Get a submatrix by row and column indices. More...
 
void set (const Matrix< Scalar > &m, bool ind1, const Slice &rr)
 Assign elements by a single (linear) index. More...
 
void set (const Matrix< Scalar > &m, bool ind1, const Matrix< casadi_int > &rr)
 Assign elements by a single (linear) index. More...
 
void set (const Matrix< Scalar > &m, bool ind1, const Sparsity &sp)
 Assign elements by a single (linear) index. More...
 
void set (const Matrix< Scalar > &m, bool ind1, const Slice &rr, const Slice &cc)
 Assign a submatrix by row and column indices. More...
 
void set (const Matrix< Scalar > &m, bool ind1, const Slice &rr, const Matrix< casadi_int > &cc)
 Assign a submatrix by row and column indices. More...
 
void set (const Matrix< Scalar > &m, bool ind1, const Matrix< casadi_int > &rr, const Slice &cc)
 Assign a submatrix by row and column indices. More...
 
void set (const Matrix< Scalar > &m, bool ind1, const Matrix< casadi_int > &rr, const Matrix< casadi_int > &cc)
 Assign a submatrix by row and column indices. More...
 
void get_nz (Matrix< Scalar > &m, bool ind1, const Slice &k) const
 Get structural nonzeros by their position in the nonzero storage. More...
 
void get_nz (Matrix< Scalar > &m, bool ind1, const Matrix< casadi_int > &k) const
 Get structural nonzeros by their position in the nonzero storage. More...
 
void set_nz (const Matrix< Scalar > &m, bool ind1, const Slice &k)
 Assign structural nonzeros by their position in the nonzero storage. More...
 
void set_nz (const Matrix< Scalar > &m, bool ind1, const Matrix< casadi_int > &k)
 Assign structural nonzeros by their position in the nonzero storage. More...
 
std::vector< Scalar > & nonzeros ()
 
const std::vector< Scalar > & nonzeros () const
 
std::vector< casadi_int > get_row () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
std::vector< casadi_int > get_colind () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
const casadi_int * row () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
casadi_int row (casadi_int el) const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
const casadi_int * colind () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
casadi_int colind (casadi_int col) const
 Get the sparsity pattern. See the Sparsity class for details. More...
 

Static Public Member Functions

static bool simplify_ref_count (std::vector< Matrix< Scalar > > &arg, std::vector< Matrix< Scalar > > &res, const Dict &opts=Dict())
 
static bool simplify_const_folding (std::vector< Matrix< Scalar > > &arg, std::vector< Matrix< Scalar > > &res, const Dict &opts=Dict())
 
static bool simplify_combine_terms (std::vector< Matrix< Scalar > > &arg, std::vector< Matrix< Scalar > > &res, const Dict &opts=Dict())
 
static void set_max_depth (casadi_int eq_depth=1)
 Set the depth to which SX subexpressions are compared during simplification. More...
 
static casadi_int get_max_depth ()
 Get the depth to which SX subexpressions are compared during simplification. More...
 
static std::vector< Matrix< Scalar > > get_input (const Function &f)
 Get the symbolic input expressions of an SX Function. More...
 
static std::vector< Matrix< Scalar > > get_free (const Function &f)
 Get the free symbols of an SX Function. More...
 
static std::string type_name ()
 Name of the class: "DM" or "SX". More...
 
static void print_default (std::ostream &stream, const Sparsity &sp, const Scalar *nonzeros, bool truncate=true)
 Print default style. More...
 
static void print_scalar (std::ostream &stream, const Scalar &e)
 Print scalar. More...
 
static void print_vector (std::ostream &stream, const Sparsity &sp, const Scalar *nonzeros, bool truncate=true)
 Print vector-style. More...
 
static void print_canonical (std::ostream &stream, const Sparsity &sp, const Scalar *nonzeros, bool truncate=true)
 Print canonical style. More...
 
static void print_sparse (std::ostream &stream, const Sparsity &sp, const Scalar *nonzeros, bool truncate=true)
 Print scalar. More...
 
static void print_split (casadi_int nnz, const Scalar *nonzeros, std::vector< std::string > &nz, std::vector< std::string > &inter)
 Get strings corresponding to the nonzeros and the interdependencies. More...
 
static void print_dense (std::ostream &stream, const Sparsity &sp, const Scalar *nonzeros, bool truncate=true)
 Print dense matrix-stype. More...
 
static Matrix< Scalar > eye (casadi_int n)
 Create an n-by-n identity matrix. More...
 
static void set_precision (casadi_int precision)
 Set the precision, field width and scientific flag used when printing nonzeros. More...
 
static void set_width (casadi_int width)
 
static void set_scientific (bool scientific)
 
static casadi_int get_precision ()
 Get the precision, field width and scientific flag used when printing nonzeros. More...
 
static casadi_int get_width ()
 
static bool get_scientific ()
 
static void rng (casadi_int seed)
 Seed the random number generator used by rand. More...
 
static Matrix< Scalar > deserialize (std::istream &stream)
 Build a matrix from a serialization stream. More...
 
static Matrix< Scalar > deserialize (const std::string &s)
 Build a matrix from a string produced by serialize. More...
 
static Matrix< Scalar > deserialize (DeserializingStream &s)
 
static void to_file (const std::string &filename, const Sparsity &sp, const Scalar *nonzeros, const std::string &format="")
 
static Matrix< double > from_file (const std::string &filename, const std::string &format_hint="")
 Read a numerical matrix from a text file written by to_file (DM only) More...
 
static Matrix< Scalar > _sym (const std::string &name, const Sparsity &sp)
 
static Matrix< Scalar > logsumexp (const Matrix< Scalar > &x)
 
static Matrix< Scalar > binary (casadi_int op, const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 Create nodes by their ID. More...
 
static Matrix< Scalar > unary (casadi_int op, const Matrix< Scalar > &x)
 Create nodes by their ID. More...
 
static Matrix< Scalar > scalar_matrix (casadi_int op, const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 Create nodes by their ID. More...
 
static Matrix< Scalar > matrix_scalar (casadi_int op, const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 Create nodes by their ID. More...
 
static Matrix< Scalar > matrix_matrix (casadi_int op, const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 Create nodes by their ID. More...
 
static std::vector< Matrix< Scalar > > call (const Function &f, const std::vector< Matrix< Scalar > > &x)
 Create nodes by their ID. More...
 
static std::vector< Scalar > call (const Function &f, const std::vector< Scalar > &x)
 Create nodes by their ID. More...
 
static bool is_equal (const Matrix< Scalar > &x, const Matrix< Scalar > &y, casadi_int depth=0)
 
static Matrix< Scalar > mmin (const Matrix< Scalar > &x)
 
static Matrix< Scalar > mmax (const Matrix< Scalar > &x)
 
static Matrix< Scalar > simplify (const Matrix< Scalar > &x)
 
static Matrix< Scalar > transform (const Matrix< Scalar > &x, const Dict &opts=Dict())
 
static Matrix< Scalar > transform (const Matrix< Scalar > &x, const std::vector< std::vector< GenericType > > &passes, const Dict &opts=Dict())
 
static std::vector< Matrix< Scalar > > transform (const std::vector< Matrix< Scalar > > &x, const Dict &opts=Dict())
 
static std::vector< Matrix< Scalar > > transform (const std::vector< Matrix< Scalar > > &x, const std::vector< std::vector< GenericType > > &passes, const Dict &opts=Dict())
 
static Matrix< Scalar > jacobian (const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Dict &opts=Dict())
 
static Sparsity jacobian_sparsity (const Matrix< Scalar > &f, const Matrix< Scalar > &x)
 
static Matrix< Scalar > hessian (const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Dict &opts=Dict())
 
static Matrix< Scalar > hessian (const Matrix< Scalar > &f, const Matrix< Scalar > &x, Matrix< Scalar > &g, const Dict &opts=Dict())
 
static Matrix< Scalar > substitute (const Matrix< Scalar > &ex, const Matrix< Scalar > &v, const Matrix< Scalar > &vdef)
 
static std::vector< Matrix< Scalar > > substitute (const std::vector< Matrix< Scalar > > &ex, const std::vector< Matrix< Scalar > > &v, const std::vector< Matrix< Scalar > > &vdef)
 
static void substitute_inplace (const std::vector< Matrix< Scalar > > &v, std::vector< Matrix< Scalar > > &vdef, std::vector< Matrix< Scalar > > &ex, bool revers)
 
static Matrix< Scalar > pinv (const Matrix< Scalar > &x)
 
static Matrix< Scalar > pinv (const Matrix< Scalar > &A, const std::string &lsolver, const Dict &opts)
 
static Matrix< Scalar > expm_const (const Matrix< Scalar > &A, const Matrix< Scalar > &t)
 
static Matrix< Scalar > expm (const Matrix< Scalar > &A)
 
static Matrix< Scalar > solve (const Matrix< Scalar > &A, const Matrix< Scalar > &b)
 
static Matrix< Scalar > solve (const Matrix< Scalar > &A, const Matrix< Scalar > &b, const std::string &lsolver, const Dict &opts)
 
static Matrix< Scalar > inv (const Matrix< Scalar > &A)
 
static Matrix< Scalar > inv (const Matrix< Scalar > &A, const std::string &lsolver, const Dict &opts)
 
static casadi_int n_nodes (const Matrix< Scalar > &x)
 
static std::string print_operator (const Matrix< Scalar > &x, const std::vector< std::string > &args)
 
static void extract (std::vector< Matrix< Scalar >> &ex, std::vector< Matrix< Scalar >> &v, std::vector< Matrix< Scalar >> &vdef, const Dict &opts=Dict())
 
static void shared (std::vector< Matrix< Scalar > > &ex, std::vector< Matrix< Scalar > > &v, std::vector< Matrix< Scalar > > &vdef, const std::string &v_prefix, const std::string &v_suffix)
 
static Matrix< Scalar > _bilin (const Matrix< Scalar > &A, const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static Matrix< Scalar > _rank1 (const Matrix< Scalar > &A, const Matrix< Scalar > &alpha, const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static Matrix< Scalar > if_else (const Matrix< Scalar > &x, const Matrix< Scalar > &if_true, const Matrix< Scalar > &if_false, bool short_circuit=false)
 
static Matrix< Scalar > conditional (const Matrix< Scalar > &ind, const std::vector< Matrix< Scalar > > &x, const Matrix< Scalar > &x_default, bool short_circuit=false)
 
static bool depends_on (const Matrix< Scalar > &x, const Matrix< Scalar > &arg)
 
static bool contains (const std::vector< Matrix< Scalar > > &v, const Matrix< Scalar > &n)
 
static bool contains_all (const std::vector< Matrix< Scalar > > &v, const std::vector< Matrix< Scalar > > &n)
 
static bool contains_any (const std::vector< Matrix< Scalar > > &v, const std::vector< Matrix< Scalar > > &n)
 
static Matrix< Scalar > mrdivide (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static Matrix< Scalar > mldivide (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static std::vector< Matrix< Scalar > > symvar (const Matrix< Scalar > &x)
 
static Matrix< Scalar > det (const Matrix< Scalar > &x)
 
static Matrix< Scalar > det (const Matrix< Scalar > &x, const std::string &lsolver, const Dict &dict=Dict())
 
static Matrix< Scalar > inv_minor (const Matrix< Scalar > &x)
 
static Matrix< Scalar > trace (const Matrix< Scalar > &x)
 
static Matrix< Scalar > norm_1 (const Matrix< Scalar > &x)
 
static Matrix< Scalar > norm_2 (const Matrix< Scalar > &x)
 
static Matrix< Scalar > norm_fro (const Matrix< Scalar > &x)
 
static Matrix< Scalar > norm_inf (const Matrix< Scalar > &x)
 
static Matrix< Scalar > sum2 (const Matrix< Scalar > &x)
 
static Matrix< Scalar > sum1 (const Matrix< Scalar > &x)
 
static Matrix< Scalar > dot (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static Matrix< Scalar > nullspace (const Matrix< Scalar > &x)
 
static Matrix< Scalar > diag (const Matrix< Scalar > &x)
 
static Matrix< Scalar > unite (const Matrix< Scalar > &A, const Matrix< Scalar > &B)
 
static Matrix< Scalar > project (const Matrix< Scalar > &x, const Sparsity &sp, bool intersect=false)
 
static Matrix< Scalar > polyval (const Matrix< Scalar > &p, const Matrix< Scalar > &x)
 
static Matrix< Scalar > densify (const Matrix< Scalar > &x, const Matrix< Scalar > &val)
 
static Matrix< Scalar > densify (const Matrix< Scalar > &x)
 
static Matrix< Scalar > einstein (const Matrix< Scalar > &A, const Matrix< Scalar > &B, const Matrix< Scalar > &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 Matrix< Scalar > einstein (const Matrix< Scalar > &A, const Matrix< Scalar > &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 Matrix< Scalar > cumsum (const Matrix< Scalar > &x, casadi_int axis=-1)
 
static Matrix< Scalar > _logsumexp (const Matrix< Scalar > &x)
 
static std::vector< Matrix< Scalar > > cse (const std::vector< Matrix< Scalar > > &e)
 
static Matrix< Scalar > blockcat (const std::vector< std::vector< Matrix< Scalar > > > &v)
 
static Matrix< Scalar > horzcat (const std::vector< Matrix< Scalar > > &v)
 
static std::vector< Matrix< Scalar > > horzsplit (const Matrix< Scalar > &x, const std::vector< casadi_int > &offset)
 
static Matrix< Scalar > vertcat (const std::vector< Matrix< Scalar > > &v)
 
static std::vector< Matrix< Scalar > > vertsplit (const Matrix< Scalar > &x, const std::vector< casadi_int > &offset)
 
static std::vector< Matrix< Scalar > > diagsplit (const Matrix< Scalar > &x, const std::vector< casadi_int > &offset1, const std::vector< casadi_int > &offset2)
 
static Matrix< Scalar > reshape (const Matrix< Scalar > &x, casadi_int nrow, casadi_int ncol)
 
static Matrix< Scalar > reshape (const Matrix< Scalar > &x, const Sparsity &sp)
 
static Matrix< Scalar > sparsity_cast (const Matrix< Scalar > &x, const Sparsity &sp)
 
static Matrix< Scalar > kron (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static Matrix< Scalar > mtimes (const Matrix< Scalar > &x, const Matrix< Scalar > &y, const std::string &blas="reference")
 
static Matrix< Scalar > mac (const Matrix< Scalar > &x, const Matrix< Scalar > &y, const Matrix< Scalar > &z, const std::string &blas="reference")
 
static void extract_parametric (const Matrix< Scalar > &expr, const Matrix< Scalar > &par, Matrix< Scalar > &expr_ret, std::vector< Matrix< Scalar > > &symbols, std::vector< Matrix< Scalar >> &parametric, const Dict &opts)
 
static void separate_linear (const Matrix< Scalar > &expr, const Matrix< Scalar > &sym_lin, const Matrix< Scalar > &sym_const, Matrix< Scalar > &expr_const, Matrix< Scalar > &expr_lin, Matrix< Scalar > &expr_nonlin)
 
static Matrix< Scalar > sparsify (const Matrix< Scalar > &x, double tol=0)
 
static void expand (const Matrix< Scalar > &x, Matrix< Scalar > &weights, Matrix< Scalar > &terms)
 
static Matrix< Scalar > pw_const (const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)
 
static Matrix< Scalar > pw_lin (const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)
 
static Matrix< Scalar > heaviside (const Matrix< Scalar > &x)
 
static Matrix< Scalar > rectangle (const Matrix< Scalar > &x)
 
static Matrix< Scalar > triangle (const Matrix< Scalar > &x)
 
static Matrix< Scalar > ramp (const Matrix< Scalar > &x)
 
static Matrix< Scalar > gauss_quadrature (const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int order=5)
 
static Matrix< Scalar > gauss_quadrature (const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int order, const Matrix< Scalar > &w)
 
static std::vector< std::vector< Matrix< Scalar > > > forward (const std::vector< Matrix< Scalar > > &ex, const std::vector< Matrix< Scalar > > &arg, const std::vector< std::vector< Matrix< Scalar > > > &v, const Dict &opts=Dict())
 
static std::vector< std::vector< Matrix< Scalar > > > reverse (const std::vector< Matrix< Scalar > > &ex, const std::vector< Matrix< Scalar > > &arg, const std::vector< std::vector< Matrix< Scalar > > > &v, const Dict &opts=Dict())
 
static std::vector< bool > which_depends (const Matrix< Scalar > &expr, const Matrix< Scalar > &var, casadi_int order=1, bool tr=false)
 
static Matrix< Scalar > taylor (const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order)
 
static Matrix< Scalar > mtaylor (const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order)
 
static Matrix< Scalar > mtaylor (const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order, const std::vector< casadi_int > &order_contributions)
 
static Matrix< Scalar > poly_coeff (const Matrix< Scalar > &ex, const Matrix< Scalar > &x)
 
static Matrix< Scalar > poly_roots (const Matrix< Scalar > &p)
 
static Matrix< Scalar > eig_symbolic (const Matrix< Scalar > &m)
 
static Matrix< double > evalf (const Matrix< Scalar > &m)
 
static void qr_sparse (const Matrix< Scalar > &A, Matrix< Scalar > &V, Matrix< Scalar > &R, Matrix< Scalar > &beta, std::vector< casadi_int > &prinv, std::vector< casadi_int > &pc, bool amd=true)
 
static Matrix< Scalar > qr_solve (const Matrix< Scalar > &b, const Matrix< Scalar > &v, const Matrix< Scalar > &r, const Matrix< Scalar > &beta, const std::vector< casadi_int > &prinv, const std::vector< casadi_int > &pc, bool tr=false)
 
static void qr (const Matrix< Scalar > &A, Matrix< Scalar > &Q, Matrix< Scalar > &R)
 
static void ldl (const Matrix< Scalar > &A, Matrix< Scalar > &D, Matrix< Scalar > &LT, std::vector< casadi_int > &p, bool amd=true)
 
static Matrix< Scalar > ldl_solve (const Matrix< Scalar > &b, const Matrix< Scalar > &D, const Matrix< Scalar > &LT, const std::vector< casadi_int > &p)
 
static Matrix< Scalar > all (const Matrix< Scalar > &x)
 
static Matrix< Scalar > any (const Matrix< Scalar > &x)
 
static Matrix< Scalar > adj (const Matrix< Scalar > &x)
 
static Matrix< Scalar > minor (const Matrix< Scalar > &x, casadi_int i, casadi_int j)
 
static Matrix< Scalar > cofactor (const Matrix< Scalar > &A, casadi_int i, casadi_int j)
 
static Matrix< Scalar > chol (const Matrix< Scalar > &A)
 
static Matrix< Scalar > norm_inf_mul (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static Matrix< Scalar > diagcat (const std::vector< Matrix< Scalar > > &A)
 
static Matrix< Scalar > triplet (const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d)
 Construct a sparse matrix from triplet form (row indices, column indices, values) More...
 
static Matrix< Scalar > triplet (const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d, casadi_int nrow, casadi_int ncol)
 Construct a sparse matrix from triplet form (row indices, column indices, values) More...
 
static Matrix< Scalar > triplet (const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, const Matrix< Scalar > &d, const std::pair< casadi_int, casadi_int > &rc)
 Construct a sparse matrix from triplet form (row indices, column indices, values) More...
 
static Matrix< Scalar > inf (const Sparsity &sp)
 Create a matrix with every nonzero equal to +inf. More...
 
static Matrix< Scalar > inf (casadi_int nrow=1, casadi_int ncol=1)
 Create a matrix with every nonzero equal to +inf. More...
 
static Matrix< Scalar > inf (const std::pair< casadi_int, casadi_int > &rc)
 Create a matrix with every nonzero equal to +inf. More...
 
static Matrix< Scalar > nan (const Sparsity &sp)
 Create a matrix with every nonzero equal to nan. More...
 
static Matrix< Scalar > nan (casadi_int nrow=1, casadi_int ncol=1)
 Create a matrix with every nonzero equal to nan. More...
 
static Matrix< Scalar > nan (const std::pair< casadi_int, casadi_int > &rc)
 Create a matrix with every nonzero equal to nan. More...
 
static Matrix< Scalar > rand (casadi_int nrow=1, casadi_int ncol=1)
 Create a matrix with uniformly distributed random numbers in [0, 1) More...
 
static Matrix< Scalar > rand (const Sparsity &sp)
 Create a matrix with uniformly distributed random numbers in [0, 1) More...
 
static Matrix< Scalar > rand (const std::pair< casadi_int, casadi_int > &rc)
 Create a matrix with uniformly distributed random numbers in [0, 1) More...
 
static Matrix< Scalar > interp1d (const std::vector< double > &x, const Matrix< Scalar > &v, const std::vector< double > &xq, const std::string &mode, bool equidistant)
 Functions called by friend functions defined here. More...
 
static casadi_int sprank (const Matrix< Scalar > &x)
 Functions called by friend functions defined here. More...
 
static casadi_int norm_0_mul (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > tril (const Matrix< Scalar > &x, bool includeDiagonal=true)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > triu (const Matrix< Scalar > &x, bool includeDiagonal=true)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > sumsqr (const Matrix< Scalar > &x)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > linspace (const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int nsteps)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > cross (const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int dim=-1)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > skew (const Matrix< Scalar > &a)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > inv_skew (const Matrix< Scalar > &a)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > tril2symm (const Matrix< Scalar > &x)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > triu2symm (const Matrix< Scalar > &x)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > repsum (const Matrix< Scalar > &x, casadi_int n, casadi_int m=1)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > diff (const Matrix< Scalar > &x, casadi_int n=1, casadi_int axis=-1)
 Functions called by friend functions defined here. More...
 
static bool is_linear (const Matrix< Scalar > &expr, const Matrix< Scalar > &var)
 Functions called by friend functions defined here. More...
 
static bool is_quadratic (const Matrix< Scalar > &expr, const Matrix< Scalar > &var)
 Functions called by friend functions defined here. More...
 
static void quadratic_coeff (const Matrix< Scalar > &expr, const Matrix< Scalar > &var, Matrix< Scalar > &A, Matrix< Scalar > &b, Matrix< Scalar > &c, bool check)
 Functions called by friend functions defined here. More...
 
static void linear_coeff (const Matrix< Scalar > &expr, const Matrix< Scalar > &var, Matrix< Scalar > &A, Matrix< Scalar > &b, bool check)
 Functions called by friend functions defined here. More...
 
static Matrix< Scalar > mpower (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static Matrix< Scalar > soc (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 
static Matrix< Scalar > linearize (const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Matrix< Scalar > &x0, const Dict &opts=Dict())
 
static Matrix< Scalar > gradient (const Matrix< Scalar > &ex, const Matrix< Scalar > &arg, const Dict &opts=Dict())
 
static Matrix< Scalar > tangent (const Matrix< Scalar > &ex, const Matrix< Scalar > &arg, const Dict &opts=Dict())
 
static Matrix< Scalar > jtimes (const Matrix< Scalar > &ex, const Matrix< Scalar > &arg, const Matrix< Scalar > &v, bool tr=false, const Dict &opts=Dict())
 
static Matrix< Scalar > bilin (const Matrix< Scalar > &A, const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 Calculate bilinear/quadratic form x^T A y. More...
 
static Matrix< Scalar > rank1 (const Matrix< Scalar > &A, const Matrix< Scalar > &alpha, const Matrix< Scalar > &x, const Matrix< Scalar > &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 Matrix< Scalar > sym (const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
 Create an nrow-by-ncol symbolic primitive. More...
 
static Matrix< Scalar > 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 Matrix< Scalar > sym (const std::string &name, const Sparsity &sp)
 Create symbolic primitive with a given sparsity pattern. More...
 
static std::vector< Matrix< Scalar > > 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< Matrix< Scalar > > 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< Matrix< Scalar > > > 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< Matrix< Scalar > > > 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 Matrix< Scalar > 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 Matrix< Scalar > zeros (const Sparsity &sp)
 Create a dense matrix or a matrix with specified sparsity with all entries zero. More...
 
static Matrix< Scalar > 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 Matrix< Scalar > 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 Matrix< Scalar > ones (const Sparsity &sp)
 Create a dense matrix or a matrix with specified sparsity with all entries one. More...
 
static Matrix< Scalar > ones (const std::pair< casadi_int, casadi_int > &rc)
 Create a dense matrix or a matrix with specified sparsity with all entries one. More...
 

Static Private Member Functions

static Matrix< Scalar > printme (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 Debug printing: (x,y) -> x, printing x when evaluated. More...
 

Friends

Matrix< Scalar > adj (const Matrix< Scalar > &A)
 Adjugate (classical adjoint) of a square matrix. More...
 
Matrix< Scalar > minor (const Matrix< Scalar > &x, casadi_int i, casadi_int j)
 Minor of a square matrix: determinant with one column and one row removed. More...
 
Matrix< Scalar > cofactor (const Matrix< Scalar > &x, casadi_int i, casadi_int j)
 Cofactor of a square matrix: (-1)^(i+j) times minor(x, i, j) More...
 
void qr (const Matrix< Scalar > &A, Matrix< Scalar > &Q, Matrix< Scalar > &R)
 Dense QR factorization using the modified Gram-Schmidt algorithm. More...
 
void qr_sparse (const Matrix< Scalar > &A, Matrix< Scalar > &V, Matrix< Scalar > &R, Matrix< Scalar > &beta, std::vector< casadi_int > &prinv, std::vector< casadi_int > &pc, bool amd=true)
 Sparse direct QR factorization with Householder reflections. More...
 
Matrix< Scalar > qr_solve (const Matrix< Scalar > &b, const Matrix< Scalar > &v, const Matrix< Scalar > &r, const Matrix< Scalar > &beta, const std::vector< casadi_int > &prinv, const std::vector< casadi_int > &pc, bool tr=false)
 Solve A*x = b (or A^T*x = b) with the factors returned by qr_sparse. More...
 
Matrix< Scalar > chol (const Matrix< Scalar > &A)
 Cholesky factorization of a symmetric positive definite matrix. More...
 
void ldl (const Matrix< Scalar > &A, Matrix< Scalar > &D, Matrix< Scalar > &LT, std::vector< casadi_int > &p, bool amd=true)
 Sparse LDL^T factorization of a symmetric matrix. More...
 
Matrix< Scalar > ldl_solve (const Matrix< Scalar > &b, const Matrix< Scalar > &D, const Matrix< Scalar > &LT, const std::vector< casadi_int > &p)
 Solve A*x = b with the factors returned by ldl. More...
 
Matrix< Scalar > any (const Matrix< Scalar > &x)
 True if any entry equals one. More...
 
Matrix< Scalar > all (const Matrix< Scalar > &x)
 True if every entry equals one. More...
 
Matrix< Scalar > norm_inf_mul (const Matrix< Scalar > &x, const Matrix< Scalar > &y)
 Infinity norm of a matrix product, without forming the product. More...
 
Matrix< Scalar > sparsify (const Matrix< Scalar > &A, double tol=0)
 Make a matrix sparse by turning numerical zeros into structural zeros. More...
 
void expand (const Matrix< Scalar > &ex, Matrix< Scalar > &weights, Matrix< Scalar > &terms)
 Expand a scalar SX expression as a weighted sum of terms with constant weights. More...
 
Matrix< Scalar > pw_const (const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)
 Piecewise constant function of a scalar symbolic variable. More...
 
Matrix< Scalar > pw_lin (const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)
 Piecewise linear function of a scalar symbolic variable. More...
 
Matrix< Scalar > heaviside (const Matrix< Scalar > &x)
 Heaviside step function, elementwise. More...
 
Matrix< Scalar > rectangle (const Matrix< Scalar > &x)
 Rectangle (gate, pulse, window) function, elementwise. More...
 
Matrix< Scalar > triangle (const Matrix< Scalar > &x)
 Triangle (hat) function, elementwise. More...
 
Matrix< Scalar > ramp (const Matrix< Scalar > &x)
 Ramp function, elementwise. More...
 
Matrix< Scalar > mtaylor (const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order=1)
 Multivariate Taylor series expansion. More...
 
Matrix< Scalar > mtaylor (const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order, const std::vector< casadi_int > &order_contributions)
 Multivariate Taylor series expansion with weighted degrees. More...
 
Matrix< Scalar > poly_coeff (const Matrix< Scalar > &f, const Matrix< Scalar > &x)
 Extract the polynomial coefficients of a scalar expression in one symbol. More...
 
Matrix< Scalar > poly_roots (const Matrix< Scalar > &p)
 Roots of a polynomial given by its coefficients, in closed form. More...
 
Matrix< Scalar > eig_symbolic (const Matrix< Scalar > &m)
 Eigenvalues of a square SX matrix in closed form. More...
 
Matrix< double > evalf (const Matrix< Scalar > &expr)
 Evaluate a constant expression numerically, returning a DM. More...
 
Matrix< Scalar > gauss_quadrature (const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int order=5)
 Integrate a scalar SX expression over [a, b] with 5-point Gauss-Legendre quadrature. More...
 
Matrix< Scalar > gauss_quadrature (const Matrix< Scalar > &f, const Matrix< Scalar > &x, const Matrix< Scalar > &a, const Matrix< Scalar > &b, casadi_int order, const Matrix< Scalar > &w)
 Integrate a scalar SX expression over [a, b] with 5-point Gauss-Legendre quadrature. More...
 
Matrix< Scalar > taylor (const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order=1)
 Univariate Taylor series expansion. More...
 
Matrix< Scalar > taylor (const Matrix< Scalar > &ex, const Matrix< Scalar > &x)
 Univariate Taylor series expansion. More...
 
Scalar * ptr ()
 
const Scalar * ptr () const
 
Scalar * get_ptr (Matrix< Scalar > &v)
 
const Scalar * get_ptr (const Matrix< Scalar > &v)
 

Member Typedef Documentation

◆ B

template<typename Scalar >
typedef GenericMatrix<Matrix<Scalar> > casadi::Matrix< Scalar >::B

Definition at line 478 of file matrix_decl.hpp.

◆ ScalarType

template<typename Scalar >
typedef Scalar casadi::Matrix< Scalar >::ScalarType

Definition at line 475 of file matrix_decl.hpp.

Constructor & Destructor Documentation

◆ Matrix() [1/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix

>>> DM().dim(), SX().is_empty()
('0x0', True)

>> fprintf('%s %d\n', DM().dim(), SX().is_empty())
0x0 1

julia> ca.dim(ca.DM()), ca.is_empty(ca.SX())
("0x0", true)

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

Definition at line 586 of file matrix_impl.hpp.

586  : sparsity_(Sparsity(0, 0)) {
587  }

◆ Matrix() [2/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( const Matrix< Scalar > &  m)

Definition at line 590 of file matrix_impl.hpp.

590  : sparsity_(m.sparsity_), nonzeros_(m.nonzeros_) {
591  }

◆ Matrix() [3/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( casadi_int  nrow,
casadi_int  ncol 
)

The result has nnz()==0. Use zeros(nrow, ncol) for a dense matrix of numerical zeros. Structural zeros print as 00.

Parameters
nrowNumber of rows, nonnegative
ncolNumber of columns, nonnegative

>>> DM(2, 3)
DM(
[[00, 00, 00],
 [00, 00, 00]])
>>> DM(2, 3).nnz(), DM.zeros(2, 3).nnz()
(0, 6)
>>> SX(2, 0).dim()
'2x0'

>> DM(2, 3)
[[00, 00, 00],
 [00, 00, 00]]
>> fprintf('%d %d\n', DM(2, 3).nnz(), DM.zeros(2, 3).nnz())
0 6
>> SX(2, 0).dim()
2x0

julia> ca.DM(2, 3)
DM(
[[00, 00, 00],
 [00, 00, 00]])
julia> ca.nnz(ca.DM(2, 3)), ca.nnz(ca.DM.zeros(2, 3))
(0, 6)
julia> ca.dim(ca.SX(2, 0))
"2x0"

See also
zeros, ones

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

◆ Matrix() [4/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( const std::pair< casadi_int, casadi_int > &  rc)
explicit

◆ Matrix() [5/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( const Sparsity &  sp)
explicit

Same as ones(sp).

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

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

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

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

◆ Matrix() [6/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( const Sparsity &  sp,
const Matrix< Scalar > &  d 
)
Parameters
spSparsity pattern of the result
dEither a scalar, which is used for every structural nonzero, or a dense vector (row or column) with exactly sp.nnz() entries, in column-major order of the pattern. Anything else raises an error.

>>> DM(Sparsity.diag(2), [5, 6])
DM(
[[5, 00],
 [00, 6]])
>>> DM(Sparsity.diag(2), 5)
DM(
[[5, 00],
 [00, 5]])
>>> SX(Sparsity.lower(2), SX.sym("v", 3))
SX(
[[v_0, 00],
 [v_1, v_2]])
>>> try: DM(Sparsity.diag(3), [1, 2])
... except Exception as e: print("error")
error

>> DM(Sparsity.diag(2), [5; 6])
[[5, 00],
 [00, 6]]
>> DM(Sparsity.diag(2), 5)
[[5, 00],
 [00, 5]]
>> SX(Sparsity.lower(2), SX.sym('v', 3))
[[v_0, 00],
 [v_1, v_2]]
>> try, DM(Sparsity.diag(3), [1; 2]); catch e, disp('error'); end
error

julia> ca.DM(ca.Sparsity.diag(2), [5, 6])
DM(
[[5, 00],
 [00, 6]])
julia> ca.DM(ca.Sparsity.diag(2), 5)
DM(
[[5, 00],
 [00, 5]])
julia> ca.SX(ca.Sparsity.lower(2), ca.SX.sym("v", 3))
SX(
[[v_0, 00],
 [v_1, v_2]])
julia> try ca.DM(ca.Sparsity.diag(3), [1, 2]); catch; println("error") end
error

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

◆ Matrix() [7/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( double  val)

◆ Matrix() [8/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( const std::vector< std::vector< double > > &  m)
explicit

Every row must have the same length; a ragged input raises an error. An empty outer list gives a 0-by-1 matrix. At the Python level, 2-D NumPy arrays and SciPy sparse matrices are accepted as well; a sparse input keeps its sparsity pattern.

>>> DM([[1, 2], [3, 4]])
DM(
[[1, 2],
 [3, 4]])
>>> DM(np.array([[1, 2], [3, 4]])).dim()
'2x2'
>>> try: DM([[1, 2], [3]])
... except Exception as e: print("error")
error

julia> ca.DM([1 2; 3 4])
DM(
[[1, 2],
 [3, 4]])
julia> try ca.DM([[1, 2], [3]]); catch; println("error") end
error

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

◆ Matrix() [9/14]

template<typename Scalar >
template<typename A >
casadi::Matrix< Scalar >::Matrix ( const std::vector< A > &  x)
inline

A 1-D NumPy array also gives a column vector.

>>> DM([1, 2, 3]).dim(), DM(np.array([1, 2, 3])).dim(), DM([]).dim()
('3x1', '3x1', '0x1')

julia> ca.dim(ca.DM([1, 2, 3])), ca.dim(ca.DM(Float64[]))
("3x1", "0x1")

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

Definition at line 409 of file matrix_decl.hpp.

409  : sparsity_(Sparsity::dense(x.size(), 1)),
410  nonzeros_(std::vector<Scalar>(x.size())) {
411  auto x_it = x.begin();
412  for (auto&& d : nonzeros_) d = static_cast<Scalar>(*x_it++);
413  }
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern.
Definition: sparsity.cpp:1041

◆ Matrix() [10/14]

template<typename Scalar >
template<typename A >
casadi::Matrix< Scalar >::Matrix ( const Matrix< A > &  x)
inline

The sparsity pattern is kept and every nonzero is converted. SX(DM) always works. DM(SX) requires constant entries: a symbolic entry is converted to nan without an error. Use evalf to get an error instead.

>>> SX(DM(Sparsity.diag(2))).dim()
'2x2'
>>> DM(SX([1, 2]))
DM([1, 2])
>>> DM(SX.sym("x"))
DM(nan)

>> SX(DM(Sparsity.diag(2))).dim()
2x2
>> DM(SX([1; 2]))
[1, 2]
>> DM(SX.sym('x'))
nan

julia> ca.dim(ca.SX(ca.DM(ca.Sparsity.diag(2))))
"2x2"
julia> ca.DM(ca.SX([1, 2]))
DM([1, 2])
julia> ca.DM(ca.SX.sym("x"))
DM(nan)

See also
evalf

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

Definition at line 459 of file matrix_decl.hpp.

459  : sparsity_(x.sparsity()), nonzeros_(std::vector<Scalar>(x.nnz())) {
460  auto x_it = x->begin();
461  for (auto&& d : nonzeros_) d = static_cast<Scalar>(*x_it++);
462  }

◆ Matrix() [11/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( const std::vector< Scalar > &  x)

Definition at line 594 of file matrix_impl.hpp.

594  :
595  sparsity_(Sparsity::dense(x.size(), 1)), nonzeros_(x) {
596  }

◆ Matrix() [12/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( std::initializer_list< Scalar >  x)
inline

Definition at line 469 of file matrix_decl.hpp.

469 : Matrix<Scalar>(std::vector<Scalar>(x)) {}

◆ Matrix() [13/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( const Sparsity &  sp,
const Scalar &  val,
bool  dummy 
)

◆ Matrix() [14/14]

template<typename Scalar >
casadi::Matrix< Scalar >::Matrix ( const Sparsity &  sp,
const std::vector< Scalar > &  d,
bool  dummy 
)

Member Function Documentation

◆ __nonzero__()

bool CASADI_EXPORT casadi::SX::__nonzero__

Only defined for 1-by-1 matrices; other shapes raise an error. A structurally empty 1-by-1 matrix is false. A symbolic SX entry raises an error unless the on-the-fly simplifications already decided its value (x==x is true).

>>> bool(DM(2)), bool(DM(0)), bool(DM(1, 1))
(True, False, False)
>>> x = SX.sym("x")
>>> bool(x == x)
True
>>> try: bool(x > 0)
... except Exception as e: print("error")
error

julia> Bool(ca.DM(1)), Bool(ca.DM(0)), Bool(ca.DM(1, 1))
(true, false, false)
julia> x = ca.SX.sym("x");
julia> Bool(x == x)
true
julia> try Bool(x > 0); catch; println("error") end
error

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

Definition at line 80 of file matrix_impl.hpp.

80  {
81  if (numel()!=1) {
82  casadi_error("Only scalar Matrix could have a truth value, but you "
83  "provided a shape" + dim());
84  }
85  return nonzeros().at(0)!=0;
86  }
casadi_int numel() const
Get the number of elements, size1()*size2()
std::string dim(bool with_nz=false) const
Get string representation of dimensions.
std::vector< Scalar > & nonzeros()

◆ _bilin()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::_bilin ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

Functions called by friend functions defined for GenericMatrix

◆ _logsumexp()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::_logsumexp ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ _rank1()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::_rank1 ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  alpha,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

Functions called by friend functions defined for GenericMatrix

◆ _sym()

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

Definition at line 54 of file sx_instantiator.cpp.

54  {
55  // Create a dense n-by-m matrix
56  std::vector<SXElem> retv;
57 
58  // Check if individial names have been provided
59  if (name[0]=='[') {
60 
61  // Make a copy of the string and modify it as to remove the special characters
62  std::string modname = name;
63  for (std::string::iterator it=modname.begin(); it!=modname.end(); ++it) {
64  switch (*it) {
65  case '(': case ')': case '[': case ']': case '{': case '}': case ',': case ';': *it = ' ';
66  }
67  }
68 
69  std::istringstream iss(modname);
70  std::string varname;
71 
72  // Loop over elements
73  while (!iss.fail()) {
74  // Read the name
75  iss >> varname;
76 
77  // Append to the return vector
78  if (!iss.fail())
79  retv.push_back(SXElem::sym(varname));
80  }
81  } else if (sp.is_scalar(true)) {
82  retv.push_back(SXElem::sym(name));
83  } else {
84  // Scalar
85  std::stringstream ss;
86  for (casadi_int k=0; k<sp.nnz(); ++k) {
87  ss.str("");
88  ss << name << "_" << k;
89  retv.push_back(SXElem::sym(ss.str()));
90  }
91  }
92 
93  // Determine dimensions automatically if empty
94  if (sp.is_scalar(true)) {
95  return SX(retv);
96  } else {
97  return SX(sp, retv, false);
98  }
99  }
std::string name() const
Name of a 1-by-1 symbolic SX primitive.
static SXElem sym(const std::string &name)
Create a symbolic primitive.
Definition: sx_elem.cpp:95
Matrix< SXElem > SX
Definition: sx_fwd.hpp:32

References casadi::Sparsity::is_scalar(), casadi::Matrix< Scalar >::name(), casadi::Sparsity::nnz(), and casadi::SXElem::sym().

◆ adj()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::adj ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined here

◆ all()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::all ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined here

◆ any()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::any ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined here

◆ bilin()

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::bilin ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  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()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::binary ( casadi_int  op,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

◆ blockcat()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::blockcat ( const std::vector< std::vector< Matrix< Scalar > > > &  v)
static

Functions called by friend functions defined for SparsityInterface

◆ call() [1/2]

template<typename Scalar >
static std::vector< Matrix<Scalar> > casadi::Matrix< Scalar >::call ( const Function &  f,
const std::vector< Matrix< Scalar > > &  x 
)
static

◆ call() [2/2]

template<typename Scalar >
static std::vector< Scalar > casadi::Matrix< Scalar >::call ( const Function &  f,
const std::vector< Scalar > &  x 
)
static

◆ chol()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::chol ( const Matrix< Scalar > &  A)
static

Functions called by friend functions defined here

◆ clear()

template<typename Scalar >
void casadi::Matrix< Scalar >::clear ( )

◆ cofactor()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::cofactor ( const Matrix< Scalar > &  A,
casadi_int  i,
casadi_int  j 
)
static

Functions called by friend functions defined here

◆ colind() [1/2]

const casadi_int* casadi::GenericMatrix< Matrix< Scalar > >::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 606 of file generic_matrix.hpp.

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

◆ colind() [2/2]

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::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.

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

◆ columns()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::columns ( ) const
inlineinherited

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

Definition at line 295 of file generic_matrix.hpp.

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

◆ conditional()

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

Functions called by friend functions defined for GenericMatrix

◆ contains()

template<typename Scalar >
static bool casadi::Matrix< Scalar >::contains ( const std::vector< Matrix< Scalar > > &  v,
const Matrix< Scalar > &  n 
)
static

Functions called by friend functions defined for GenericMatrix

◆ contains_all()

template<typename Scalar >
static bool casadi::Matrix< Scalar >::contains_all ( const std::vector< Matrix< Scalar > > &  v,
const std::vector< Matrix< Scalar > > &  n 
)
static

Functions called by friend functions defined for GenericMatrix

◆ contains_any()

template<typename Scalar >
static bool casadi::Matrix< Scalar >::contains_any ( const std::vector< Matrix< Scalar > > &  v,
const std::vector< Matrix< Scalar > > &  n 
)
static

Functions called by friend functions defined for GenericMatrix

◆ cross()

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

Definition at line 662 of file generic_matrix.hpp.

5326  {
5327  casadi_assert(a.size1()==b.size1() && a.size2()==b.size2(),
5328  "cross(a, b): Inconsistent dimensions. Dimension of a ("
5329  + a.dim() + " ) must equal that of b (" + b.dim() + ").");
5330 
5331  casadi_assert(a.size1()==3 || a.size2()==3,
5332  "cross(a, b): One of the dimensions of a should have length 3, but got "
5333  + a.dim() + ".");
5334  casadi_assert(dim==-1 || dim==1 || dim==2,
5335  "cross(a, b, dim): Dim must be 1, 2 or -1 (automatic).");
5336 
5337  std::vector<MatType> ret(3);
5338 
5339  bool t = a.size1()==3;
5340 
5341  if (dim==1) t = true;
5342  if (dim==2) t = false;
5343 
5344  MatType a1 = t ? a(0, Slice()) : a(Slice(), 0);
5345  MatType a2 = t ? a(1, Slice()) : a(Slice(), 1);
5346  MatType a3 = t ? a(2, Slice()) : a(Slice(), 2);
5347 
5348  MatType b1 = t ? b(0, Slice()) : b(Slice(), 0);
5349  MatType b2 = t ? b(1, Slice()) : b(Slice(), 1);
5350  MatType b3 = t ? b(2, Slice()) : b(Slice(), 2);
5351 
5352  ret[0] = a2*b3-a3*b2;
5353  ret[1] = a3*b1-a1*b3;
5354  ret[2] = a1*b2-a2*b1;
5355 
5356  return t ? vertcat(ret) : horzcat(ret);
5357  }
friend MatType vertcat(const std::vector< MatType > &v)
Concatenate a list of matrices vertically.
friend MatType horzcat(const std::vector< MatType > &v)
Concatenate a list of matrices horizontally.

◆ cse()

template<typename Scalar >
static std::vector< Matrix<Scalar> > casadi::Matrix< Scalar >::cse ( const std::vector< Matrix< Scalar > > &  e)
static

Functions called by friend functions defined for GenericMatrix

◆ cumsum()

template<typename Scalar >
Matrix< Scalar > casadi::Matrix< Scalar >::cumsum ( const Matrix< Scalar > &  x,
casadi_int  axis = -1 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 537 of file matrix_impl.hpp.

537  {
538  if (axis==-1) axis = x.is_row();
539  Matrix<Scalar> ret = x;
540  if (axis==0) {
541  for (casadi_int i=1;i<x.size1();++i)
542  ret(i, Slice()) += ret(i-1, Slice());
543  } else {
544  for (casadi_int i=1;i<x.size2();++i)
545  ret(Slice(), i) += ret(Slice(), i-1);
546  }
547  return ret;
548  }

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

◆ densify() [1/2]

template<typename Scalar >
Matrix< Scalar > casadi::Matrix< Scalar >::densify ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 502 of file matrix_impl.hpp.

502  {
503  return densify(x, 0);
504  }
static Matrix< Scalar > densify(const Matrix< Scalar > &x, const Matrix< Scalar > &val)

◆ densify() [2/2]

template<typename Scalar >
Matrix< Scalar > casadi::Matrix< Scalar >::densify ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  val 
)
static

Functions called by friend functions defined for GenericMatrix

Definition at line 507 of file matrix_impl.hpp.

508  {
509  // Check argument
510  casadi_assert_dev(val.is_scalar());
511 
512  // Quick return if possible
513  if (x.is_dense()) return x;
514 
515  // Get sparsity pattern
516  casadi_int nrow = x.size1();
517  casadi_int ncol = x.size2();
518  const casadi_int* colind = x.colind();
519  const casadi_int* row = x.row();
520  auto it = x.nonzeros().cbegin();
521 
522  // New data vector
523  std::vector<Scalar> d(nrow*ncol, val.scalar());
524 
525  // Copy nonzeros
526  for (casadi_int cc=0; cc<ncol; ++cc) {
527  for (casadi_int el=colind[cc]; el<colind[cc+1]; ++el) {
528  d[cc*nrow + row[el]] = *it++;
529  }
530  }
531 
532  // Construct return matrix
533  return Matrix<Scalar>(Sparsity::dense(x.size()), d);
534  }

References casadi::GenericMatrix< MatType >::colind(), casadi::Sparsity::dense(), casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::Matrix< Scalar >::nonzeros(), casadi::GenericMatrix< MatType >::row(), casadi::Matrix< Scalar >::scalar(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MatType >::size1(), and casadi::GenericMatrix< MatType >::size2().

◆ dep()

SX CASADI_EXPORT casadi::SX::dep ( casadi_int  ch = 0) const

Only defined for a scalar SX that is the result of an operation; a symbol, a constant or a DM raises an error.

Parameters
chOperand index, 0 or 1

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

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

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

See also
n_dep, op

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

Definition at line 225 of file sx_instantiator.cpp.

225  {
226  return scalar().dep(ch);
227  }
const Scalar scalar() const
Convert to scalar type.

References casadi::Matrix< Scalar >::scalar().

◆ depends_on()

template<typename Scalar >
static bool casadi::Matrix< Scalar >::depends_on ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  arg 
)
static

Functions called by friend functions defined for GenericMatrix

◆ deserialize() [1/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::deserialize ( const std::string &  s)
static

A string that is not a CasADi serialization raises an error.

See also
serialize

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

◆ deserialize() [2/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::deserialize ( DeserializingStream &  s)
static

◆ deserialize() [3/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::deserialize ( std::istream &  stream)
static

◆ det() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::det ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ det() [2/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::det ( const Matrix< Scalar > &  x,
const std::string &  lsolver,
const Dict &  dict = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

◆ diag()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::diag ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ diagcat()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::diagcat ( const std::vector< Matrix< Scalar > > &  A)
static

Functions called by friend functions defined here

◆ diagsplit()

template<typename Scalar >
static std::vector< Matrix<Scalar> > casadi::Matrix< Scalar >::diagsplit ( const Matrix< Scalar > &  x,
const std::vector< casadi_int > &  offset1,
const std::vector< casadi_int > &  offset2 
)
static

Functions called by friend functions defined for SparsityInterface

◆ diff()

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

Definition at line 668 of file generic_matrix.hpp.

5709  {
5710  casadi_assert(axis==-1 || axis==0 || axis==1, "Axis argument invalid");
5711  casadi_assert(n>=1, "n argument invalid");
5712 
5713  MatType ret = x;
5714  for (casadi_int i=0;i<n;++i) {
5715  // Matlab's special case
5716  if (axis==-1 && ret.is_scalar()) return MatType();
5717 
5718  casadi_int local_axis = (axis==-1) ? ret.is_row() : axis;
5719  if (local_axis==0) {
5720  if (ret.size1()<=1) {
5721  ret = MatType::zeros(0, ret.size2());
5722  } else {
5723  ret = ret(Slice(1, ret.size1()), Slice())-ret(Slice(0, ret.size1()-1), Slice());
5724  }
5725  } else {
5726  if (ret.size2()<=1) {
5727  ret = MatType::zeros(ret.size1(), 0);
5728  } else {
5729  ret = ret(Slice(), Slice(1, ret.size2()))-ret(Slice(), Slice(0, ret.size2()-1));
5730  }
5731  }
5732  }
5733  return ret;
5734  }

◆ dim()

std::string casadi::GenericMatrix< Matrix< Scalar > >::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

Definition at line 325 of file generic_matrix.hpp.

5274  {
5275  return sparsity().dim(with_nz);
5276  }
std::string dim(bool with_nz=false) const
Dimensions as a string "nrow x ncol", optionally with the number of nonzeros.
Definition: sparsity.cpp:595

◆ disp()

template<typename Scalar >
void casadi::Matrix< Scalar >::disp ( std::ostream &  stream,
bool  more = false 
) const

Picks the layout automatically: scalar, vector, dense matrix (small or at least half full) or sparse coordinate list. This is what str() and print() use. more has no effect for matrices.

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

◆ dot()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::dot ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

Functions called by friend functions defined for GenericMatrix

◆ eig_symbolic()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::eig_symbolic ( const Matrix< Scalar > &  m)
static

Functions called by friend functions defined here

◆ einstein() [1/2]

template<typename Scalar >
Matrix< Scalar > casadi::Matrix< Scalar >::einstein ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  B,
const Matrix< Scalar > &  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

Functions called by friend functions defined for GenericMatrix

Definition at line 551 of file matrix_impl.hpp.

556  {
557  std::vector<casadi_int> iter_dims;
558  std::vector<casadi_int> strides_a;
559  std::vector<casadi_int> strides_b;
560  std::vector<casadi_int> strides_c;
561  casadi_int n_iter = einstein_process(A, B, C, dim_a, dim_b, dim_c, a, b, c,
562  iter_dims, strides_a, strides_b, strides_c);
563 
564  const std::vector<Scalar>& Av = A.nonzeros();
565  const std::vector<Scalar>& Bv = B.nonzeros();
566 
567  Matrix<Scalar> ret = C;
568  std::vector<Scalar>& Cv = ret.nonzeros();
569 
570  einstein_eval(n_iter, iter_dims, strides_a, strides_b, strides_c,
571  get_ptr(Av), get_ptr(Bv), get_ptr(Cv));
572  return ret;
573  }
friend Scalar * get_ptr(Matrix< Scalar > &v)
GenericMatrix< Matrix< Scalar > > B
Base class.
void einstein_eval(casadi_int n_iter, const std::vector< casadi_int > &iter_dims, const std::vector< casadi_int > &strides_a, const std::vector< casadi_int > &strides_b, const std::vector< casadi_int > &strides_c, const T *a_in, const T *b_in, T *c_in)
Definition: shared.hpp:161
casadi_int einstein_process(const T &A, const T &B, const T &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, std::vector< casadi_int > &iter_dims, std::vector< casadi_int > &strides_a, std::vector< casadi_int > &strides_b, std::vector< casadi_int > &strides_c)
Definition: shared.hpp:35

References casadi::C, casadi::einstein_eval(), casadi::einstein_process(), casadi::get_ptr(), and casadi::Matrix< Scalar >::nonzeros().

Referenced by casadi::Matrix< double >::__nonzero__().

◆ einstein() [2/2]

template<typename Scalar >
Matrix< Scalar > casadi::Matrix< Scalar >::einstein ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  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

Functions called by friend functions defined for GenericMatrix

Definition at line 576 of file matrix_impl.hpp.

580  {
582  dim_a, dim_b, dim_c, a, b, c);
583  }
static Matrix< Scalar > zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
static Matrix< Scalar > einstein(const Matrix< Scalar > &A, const Matrix< Scalar > &B, const Matrix< Scalar > &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)
T product(const std::vector< T > &values)
product

◆ element_hash()

casadi_int CASADI_EXPORT casadi::SX::element_hash

Equal expressions (same node) have the same hash. Only defined for a scalar SX; a vector or a DM raises an error.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> x.element_hash() == x.element_hash(), x.element_hash() == y.element_hash()
(True, False)

>> x = SX.sym('x'); y = SX.sym('y');
>> h = x.element_hash();
>> fprintf('%d %d\n', h == x.element_hash(), h == y.element_hash())
1 0

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

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

Definition at line 128 of file sx_instantiator.cpp.

128  {
129  return scalar().__hash__();
130  }

References casadi::Matrix< Scalar >::scalar().

◆ enlarge()

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

The new shape is nrow-by-ncol. rr lists, for every existing row, its index in the enlarged matrix; cc does the same for the columns. Both must have exactly size1() and size2() entries. All other rows and columns are structurally zero.

Parameters
nrowNew number of rows
ncolNew number of columns
rrNew position of each existing row, length size1()
ccNew position of each existing column, length size2()
ind1True for one-based indices

>>> A = DM([[1, 2], [3, 4]])
>>> A.enlarge(3, 4, [0, 2], [0, 1])
>>> A
DM(
[[1, 2, 00, 00],
 [00, 00, 00, 00],
 [3, 4, 00, 00]])
>>> try: A.enlarge(4, 4, [0, 1], [0, 1])
... except Exception as e: print("error")
error

>> A = DM([1 2; 3 4]);
>> A.enlarge(3, 4, [1 3], [1 2], true);
>> A
[[1, 2, 00, 00],
 [00, 00, 00, 00],
 [3, 4, 00, 00]]
>> try, A.enlarge(4, 4, [1 2], [1 2], true); catch e, disp('error'); end
error

julia> A = ca.DM([1 2; 3 4]);
julia> ca.enlarge(A, 3, 4, [0, 2], [0, 1])
julia> A
DM(
[[1, 2, 00, 00],
 [00, 00, 00, 00],
 [3, 4, 00, 00]])
julia> try ca.enlarge(A, 4, 4, [0, 1], [0, 1]); catch; println("error") end
error

See also
resize, Sparsity::enlarge

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

◆ erase() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::erase ( const std::vector< casadi_int > &  rr,
bool  ind1 = false 
)

Column-major linear indices, as in get. The shape is unchanged.

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A.erase([0, -1])
>>> A
DM(
[[00, 2, 3],
 [4, 5, 00]])

>> A = DM([1 2 3; 4 5 6]);
>> A.erase([1 6], true);
>> A
[[00, 2, 3],
 [4, 5, 00]]

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> ca.erase(A, [0, 5])
julia> A
DM(
[[00, 2, 3],
 [4, 5, 00]])

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

◆ erase() [2/2]

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

The shape is unchanged; the entries of the submatrix rr x cc are removed from the sparsity pattern. Negative indices count from the end; out-of-range indices raise an error.

Parameters
rrRow indices
ccColumn indices
ind1True for one-based indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A.erase([0], [1])
>>> A
DM(
[[1, 00, 3],
 [4, 5, 6]])
>>> A.dim(), A.nnz()
('2x3', 5)

>> A = DM([1 2 3; 4 5 6]);
>> A.erase(1, 2, true);
>> A
[[1, 00, 3],
 [4, 5, 6]]
>> fprintf('%s %d\n', A.dim(), A.nnz())
2x3 5

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> ca.erase(A, [0], [1])
julia> A
DM(
[[1, 00, 3],
 [4, 5, 6]])
julia> ca.dim(A), ca.nnz(A)
("2x3", 5)

See also
remove, sparsify

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

◆ evalf()

template<typename Scalar >
static Matrix<double> casadi::Matrix< Scalar >::evalf ( const Matrix< Scalar > &  m)
static

Functions called by friend functions defined here

◆ expand()

template<typename Scalar >
static void casadi::Matrix< Scalar >::expand ( const Matrix< Scalar > &  x,
Matrix< Scalar > &  weights,
Matrix< Scalar > &  terms 
)
static

Functions called by friend functions defined here

◆ expm()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::expm ( const Matrix< Scalar > &  A)
static

Functions called by friend functions defined for GenericMatrix

◆ expm_const()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::expm_const ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  t 
)
static

Functions called by friend functions defined for GenericMatrix

◆ export_code()

template<typename Scalar >
void casadi::Matrix< Scalar >::export_code ( const std::string &  lang,
std::ostream &  stream = casadi::uout(),
const Dict &  options = Dict() 
) const

lang: only 'matlab' supported for now

* options:
*   inline: Indicates if you want everything on a single line (default: False)
*   name: Name of exported variable (default: 'm')
*   indent_level: Level of indentation (default: 0)
*   spoof_zero: Replace numerical zero by a 1e-200 (default: false)
*               might be needed for matlab sparse construct,
*               which doesn't allow numerical zero
* 

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

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

◆ extract()

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

Functions called by friend functions defined for GenericMatrix

◆ extract_parametric()

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

Functions called by friend functions defined for SparsityInterface

◆ eye()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::eye ( casadi_int  n)
static

The result is sparse: only the n diagonal entries are structural nonzeros.

>>> DM.eye(2)
DM(
[[1, 00],
 [00, 1]])
>>> DM.eye(3).nnz(), DM.eye(0).dim()
(3, '0x0')

>> DM.eye(2)
[[1, 00],
 [00, 1]]
>> fprintf('%d %s\n', DM.eye(3).nnz(), DM.eye(0).dim())
3 0x0

julia> ca.DM.eye(2)
DM(
[[1, 00],
 [00, 1]])
julia> ca.nnz(ca.DM.eye(3)), ca.dim(ca.DM.eye(0))
(3, "0x0")

See also
is_eye, Sparsity::diag

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

◆ forward()

template<typename Scalar >
static std::vector<std::vector<Matrix<Scalar> > > casadi::Matrix< Scalar >::forward ( const std::vector< Matrix< Scalar > > &  ex,
const std::vector< Matrix< Scalar > > &  arg,
const std::vector< std::vector< Matrix< Scalar > > > &  v,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined here

◆ from_file()

template<typename Scalar >
static Matrix<double> casadi::Matrix< Scalar >::from_file ( const std::string &  filename,
const std::string &  format_hint = "" 
)
static

Same formats as to_file. In txt format, lines starting with #, % or / and empty lines are skipped, 00 marks a structural zero and a plain 0 a stored zero; every row must have the same number of columns, otherwise an error is raised. An empty file gives a 0-by-0 matrix. A missing file raises an error.

Parameters
filenameInput path
format_hint"mtx" or "txt"; empty means deduce from the extension

>>> import os, tempfile
>>> fn = os.path.join(tempfile.mkdtemp(), "a.txt")
>>> with open(fn, "w") as f: n = f.write("# comment\n1 00\n0 2\n")
>>> A = DM.from_file(fn)
>>> A
DM(
[[1, 00],
 [0, 2]])
>>> A.nnz()
3
>>> os.remove(fn); os.rmdir(os.path.dirname(fn))

>> fn = [tempname '.txt'];
>> fid = fopen(fn, 'w'); fprintf(fid, '# comment\n1 00\n0 2\n'); fclose(fid);
>> A = DM.from_file(fn);
>> A
[[1, 00],
 [0, 2]]
>> A.nnz()
3
>> delete(fn);

julia> fn = joinpath(mktempdir(), "a.txt");
julia> write(fn, "# comment\n1 00\n0 2\n");
julia> A = ca.DM.from_file(fn)
DM(
[[1, 00],
 [0, 2]])
julia> ca.nnz(A)
3
julia> rm(dirname(fn), recursive=true)

See also
to_file

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

◆ gauss_quadrature() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::gauss_quadrature ( const Matrix< Scalar > &  f,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  a,
const Matrix< Scalar > &  b,
casadi_int  order,
const Matrix< Scalar > &  w 
)
static

Functions called by friend functions defined here

◆ gauss_quadrature() [2/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::gauss_quadrature ( const Matrix< Scalar > &  f,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  a,
const Matrix< Scalar > &  b,
casadi_int  order = 5 
)
static

Functions called by friend functions defined here

◆ get() [1/7]

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

Backs the Python expressions x[k] and x[sp]. The index addresses elements, not nonzeros, in column-major order: element (i, j) has linear index i + j*size1(). Negative indices count from the end; out-of-range indices raise an error.

  • A scalar index returns a 1-by-1 matrix. If that element is a structural zero, the result is structurally empty (prints 00).
  • A Slice (Python slice) or a list of indices returns a column vector, except that indexing a row vector gives a row vector.
  • A matrix of indices (IM) returns a matrix of the same shape.
  • A Sparsity pattern must have the same shape as the matrix; the result is the projection of the matrix onto that pattern (entries outside it are dropped).

Note that the result of a comparison such as A > 2 is a DM of zeros and ones, not a boolean mask: indexing with it uses those values as linear indices. Use a Sparsity pattern or the nz accessor for masking.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
rrLinear index, Slice or list/matrix of indices
spSparsity pattern of the same shape as the matrix

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[1], A[2], A[-1]
(DM(4), DM(2), DM(6))
>>> A[[0, 5]]
DM([1, 6])
>>> A[Sparsity.diag(2, 3)]
DM(
[[1, 00, 00],
 [00, 5, 00]])
>>> S = DM(Sparsity.diag(2))
>>> S[1]
DM(00)
>>> A.get(False, Slice(0, 2))
DM([1, 4])
>>> try: A[6]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(2), A(3), A(end)
4
2
6
>> A([1 6])
[[1, 6]]
>> A(Sparsity.diag(2, 3))
[[1, 00, 00],
 [00, 5, 00]]
>> S = DM(Sparsity.diag(2));
>> S(2)
00
>> A.get(true, [1; 2])
[1, 4]
>> try, A(7); catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[2], A[3], A[end]
(4, 2, 6)
julia> A[[1, 6]]
DM([1, 6])
julia> S = ca.DM(ca.Sparsity.diag(2));
julia> S[2]
DM(00)
julia> try A[7]; catch; println("error") end
error

See also
set, get_nz, Sparsity::sub

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

Definition at line 162 of file matrix_impl.hpp.

162  {
163  // Scalar
164  if (rr.is_scalar(true)) {
165  return get(m, ind1, to_slice(rr, ind1));
166  }
167 
168  // If the indexed matrix is dense, use nonzero indexing
169  if (is_dense()) {
170  return get_nz(m, ind1, rr);
171  }
172 
173  // Get the sparsity pattern - does bounds checking
174  std::vector<casadi_int> mapping;
175  Sparsity sp = sparsity().sub(rr.nonzeros(), rr.sparsity(), mapping, ind1);
176 
177  // If indexed matrix was a row/column vector, make sure that the result is too
178  bool tr = (is_column() && rr.is_row()) || (is_row() && rr.is_column());
179 
180  // Copy nonzeros
181  m = Matrix<Scalar>::zeros(tr ? sp.T() : sp);
182  for (casadi_int k=0; k<mapping.size(); ++k) m->at(k) = nonzeros().at(mapping[k]);
183  }
bool is_dense() const
Check if the matrix is dense, i.e. nnz() equals numel()
bool is_column() const
Check if the matrix is a column vector (i.e. size2()==1)
bool is_row() const
Check if the matrix is a row vector (i.e. size1()==1)
bool is_scalar(bool scalar_and_dense=false) const
Check if the matrix is scalar, i.e. 1-by-1.
void get(Matrix< Scalar > &m, bool ind1, const Slice &rr) const
Get elements by a single (linear) index.
const Sparsity & sparsity() const
Const access the sparsity - reference to data member.
void get_nz(Matrix< Scalar > &m, bool ind1, const Slice &k) const
Get structural nonzeros by their position in the nonzero storage.
Sparsity sub(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, std::vector< casadi_int > &mapping, bool ind1=false) const
Pattern of the submatrix with rows rr and columns cc.
Definition: sparsity.cpp:336
Slice CASADI_EXPORT to_slice(const IM &x, bool ind1=false)
Convert IM to Slice.

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

◆ get() [2/7]

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

Backs the Python expression x[i, j]. Each of rr and cc is a scalar, a Slice or a list/vector of indices (any order, repetitions allowed, negative counts from the end). The result has len(rr) rows and len(cc) columns and keeps the sparsity of the selected entries. Two scalar indices give a 1-by-1 matrix (structurally empty when the entry is a structural zero). Out-of-range indices raise an error.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
rrRow index, Slice or list/matrix of row indices
ccColumn index, Slice or list/matrix of column indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[0, 1], A[1, -1]
(DM(2), DM(6))
>>> A[:, 1]
DM([2, 5])
>>> A[[1, 0], [0, 2]]
DM(
[[4, 6],
 [1, 3]])
>>> A[1:1, :].dim()
'0x3'
>>> x = SX.sym("x", 2, 2)
>>> x[0, :]
SX([[x_0, x_2]])
>>> DM(Sparsity.diag(2))[1, 0]
DM(00)
>>> try: A[2, 0]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(1, 2), A(2, end)
2
6
>> A(:, 2)
[2, 5]
>> A([2 1], [1 3])
[[4, 6],
 [1, 3]]
>> B = A(2:1, :); disp(B.dim())
0x3
>> x = SX.sym('x', 2, 2);
>> x(1, :)
[[x_0, x_2]]
>> S = DM(Sparsity.diag(2));
>> S(2, 1)
00
>> try, A(3, 1); catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[1, 2], A[2, end]
(2, 6)
julia> A[:, 2]
DM([2, 5])
julia> A[[2, 1], [1, 3]]
DM(
[[4, 6],
 [1, 3]])
julia> ca.dim(A[2:1, :])
"0x3"
julia> x = ca.SX.sym("x", 2, 2);
julia> x[1, :]
SX([[x_0, x_2]])
julia> ca.DM(ca.Sparsity.diag(2))[2, 1]
DM(00)
julia> try A[3, 1]; catch; println("error") end
error

See also
set

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

Definition at line 121 of file matrix_impl.hpp.

122  {
123  // Scalar
124  if (rr.is_scalar(true) && cc.is_scalar(true)) {
125  return get(m, ind1, to_slice(rr, ind1), to_slice(cc, ind1));
126  }
127 
128  // Make sure dense vectors
129  casadi_assert(rr.is_dense() && rr.is_vector(),
130  "Marix::get: First index must be a dense vector");
131  casadi_assert(cc.is_dense() && cc.is_vector(),
132  "Marix::get: Second index must be a dense vector");
133 
134  // Get the sparsity pattern - does bounds checking
135  std::vector<casadi_int> mapping;
136  Sparsity sp = sparsity().sub(rr.nonzeros(), cc.nonzeros(), mapping, ind1);
137 
138  // Copy nonzeros
139  m = Matrix<Scalar>::zeros(sp);
140  for (casadi_int k=0; k<mapping.size(); ++k) m->at(k) = nonzeros().at(mapping[k]);
141  }
bool is_vector() const
Check if the matrix is a row or column vector.

References casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::GenericMatrix< MatType >::is_vector(), casadi::Matrix< Scalar >::nonzeros(), casadi::Sparsity::sub(), casadi::to_slice(), and casadi::GenericMatrix< Matrix< Scalar > >::zeros().

◆ get() [3/7]

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

Backs the Python expression x[i, j]. Each of rr and cc is a scalar, a Slice or a list/vector of indices (any order, repetitions allowed, negative counts from the end). The result has len(rr) rows and len(cc) columns and keeps the sparsity of the selected entries. Two scalar indices give a 1-by-1 matrix (structurally empty when the entry is a structural zero). Out-of-range indices raise an error.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
rrRow index, Slice or list/matrix of row indices
ccColumn index, Slice or list/matrix of column indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[0, 1], A[1, -1]
(DM(2), DM(6))
>>> A[:, 1]
DM([2, 5])
>>> A[[1, 0], [0, 2]]
DM(
[[4, 6],
 [1, 3]])
>>> A[1:1, :].dim()
'0x3'
>>> x = SX.sym("x", 2, 2)
>>> x[0, :]
SX([[x_0, x_2]])
>>> DM(Sparsity.diag(2))[1, 0]
DM(00)
>>> try: A[2, 0]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(1, 2), A(2, end)
2
6
>> A(:, 2)
[2, 5]
>> A([2 1], [1 3])
[[4, 6],
 [1, 3]]
>> B = A(2:1, :); disp(B.dim())
0x3
>> x = SX.sym('x', 2, 2);
>> x(1, :)
[[x_0, x_2]]
>> S = DM(Sparsity.diag(2));
>> S(2, 1)
00
>> try, A(3, 1); catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[1, 2], A[2, end]
(2, 6)
julia> A[:, 2]
DM([2, 5])
julia> A[[2, 1], [1, 3]]
DM(
[[4, 6],
 [1, 3]])
julia> ca.dim(A[2:1, :])
"0x3"
julia> x = ca.SX.sym("x", 2, 2);
julia> x[1, :]
SX([[x_0, x_2]])
julia> ca.DM(ca.Sparsity.diag(2))[2, 1]
DM(00)
julia> try A[3, 1]; catch; println("error") end
error

See also
set

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

Definition at line 114 of file matrix_impl.hpp.

115  {
116  // Fall back on IM-IM
117  get(m, ind1, rr, cc.all(size2(), ind1));
118  }

References casadi::Slice::all().

◆ get() [4/7]

template<typename Scalar >
void casadi::Matrix< Scalar >::get ( Matrix< Scalar > &  m,
bool  ind1,
const Slice &  rr 
) const

Backs the Python expressions x[k] and x[sp]. The index addresses elements, not nonzeros, in column-major order: element (i, j) has linear index i + j*size1(). Negative indices count from the end; out-of-range indices raise an error.

  • A scalar index returns a 1-by-1 matrix. If that element is a structural zero, the result is structurally empty (prints 00).
  • A Slice (Python slice) or a list of indices returns a column vector, except that indexing a row vector gives a row vector.
  • A matrix of indices (IM) returns a matrix of the same shape.
  • A Sparsity pattern must have the same shape as the matrix; the result is the projection of the matrix onto that pattern (entries outside it are dropped).

Note that the result of a comparison such as A > 2 is a DM of zeros and ones, not a boolean mask: indexing with it uses those values as linear indices. Use a Sparsity pattern or the nz accessor for masking.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
rrLinear index, Slice or list/matrix of indices
spSparsity pattern of the same shape as the matrix

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[1], A[2], A[-1]
(DM(4), DM(2), DM(6))
>>> A[[0, 5]]
DM([1, 6])
>>> A[Sparsity.diag(2, 3)]
DM(
[[1, 00, 00],
 [00, 5, 00]])
>>> S = DM(Sparsity.diag(2))
>>> S[1]
DM(00)
>>> A.get(False, Slice(0, 2))
DM([1, 4])
>>> try: A[6]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(2), A(3), A(end)
4
2
6
>> A([1 6])
[[1, 6]]
>> A(Sparsity.diag(2, 3))
[[1, 00, 00],
 [00, 5, 00]]
>> S = DM(Sparsity.diag(2));
>> S(2)
00
>> A.get(true, [1; 2])
[1, 4]
>> try, A(7); catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[2], A[3], A[end]
(4, 2, 6)
julia> A[[1, 6]]
DM([1, 6])
julia> S = ca.DM(ca.Sparsity.diag(2));
julia> S[2]
DM(00)
julia> try A[7]; catch; println("error") end
error

See also
set, get_nz, Sparsity::sub

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

Definition at line 144 of file matrix_impl.hpp.

144  {
145  // Scalar
146  if (rr.is_scalar(numel())) {
147  casadi_int r = rr.scalar(numel());
148  casadi_int k = sparsity().get_nz(r % size1(), r / size1());
149  if (k>=0) {
150  m = nonzeros().at(k);
151  } else {
152  m = Matrix<Scalar>(1, 1);
153  }
154  return;
155  }
156 
157  // Fall back on IM
158  get(m, ind1, rr.all(numel(), ind1));
159  }
casadi_int size1() const
Get the first dimension (i.e. number of rows)
casadi_int get_nz(casadi_int rr, casadi_int cc) const
Nonzero index of entry (rr, cc), or -1 if it is a structural zero.
Definition: sparsity.cpp:248

References casadi::Slice::all(), casadi::Slice::is_scalar(), and casadi::Slice::scalar().

◆ get() [5/7]

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

Backs the Python expression x[i, j]. Each of rr and cc is a scalar, a Slice or a list/vector of indices (any order, repetitions allowed, negative counts from the end). The result has len(rr) rows and len(cc) columns and keeps the sparsity of the selected entries. Two scalar indices give a 1-by-1 matrix (structurally empty when the entry is a structural zero). Out-of-range indices raise an error.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
rrRow index, Slice or list/matrix of row indices
ccColumn index, Slice or list/matrix of column indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[0, 1], A[1, -1]
(DM(2), DM(6))
>>> A[:, 1]
DM([2, 5])
>>> A[[1, 0], [0, 2]]
DM(
[[4, 6],
 [1, 3]])
>>> A[1:1, :].dim()
'0x3'
>>> x = SX.sym("x", 2, 2)
>>> x[0, :]
SX([[x_0, x_2]])
>>> DM(Sparsity.diag(2))[1, 0]
DM(00)
>>> try: A[2, 0]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(1, 2), A(2, end)
2
6
>> A(:, 2)
[2, 5]
>> A([2 1], [1 3])
[[4, 6],
 [1, 3]]
>> B = A(2:1, :); disp(B.dim())
0x3
>> x = SX.sym('x', 2, 2);
>> x(1, :)
[[x_0, x_2]]
>> S = DM(Sparsity.diag(2));
>> S(2, 1)
00
>> try, A(3, 1); catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[1, 2], A[2, end]
(2, 6)
julia> A[:, 2]
DM([2, 5])
julia> A[[2, 1], [1, 3]]
DM(
[[4, 6],
 [1, 3]])
julia> ca.dim(A[2:1, :])
"0x3"
julia> x = ca.SX.sym("x", 2, 2);
julia> x[1, :]
SX([[x_0, x_2]])
julia> ca.DM(ca.Sparsity.diag(2))[2, 1]
DM(00)
julia> try A[3, 1]; catch; println("error") end
error

See also
set

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

Definition at line 107 of file matrix_impl.hpp.

108  {
109  // Fall back on IM-IM
110  get(m, ind1, rr.all(size1(), ind1), cc);
111  }

References casadi::Slice::all().

◆ get() [6/7]

template<typename Scalar >
void casadi::Matrix< Scalar >::get ( Matrix< Scalar > &  m,
bool  ind1,
const Slice &  rr,
const Slice &  cc 
) const

Backs the Python expression x[i, j]. Each of rr and cc is a scalar, a Slice or a list/vector of indices (any order, repetitions allowed, negative counts from the end). The result has len(rr) rows and len(cc) columns and keeps the sparsity of the selected entries. Two scalar indices give a 1-by-1 matrix (structurally empty when the entry is a structural zero). Out-of-range indices raise an error.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
rrRow index, Slice or list/matrix of row indices
ccColumn index, Slice or list/matrix of column indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[0, 1], A[1, -1]
(DM(2), DM(6))
>>> A[:, 1]
DM([2, 5])
>>> A[[1, 0], [0, 2]]
DM(
[[4, 6],
 [1, 3]])
>>> A[1:1, :].dim()
'0x3'
>>> x = SX.sym("x", 2, 2)
>>> x[0, :]
SX([[x_0, x_2]])
>>> DM(Sparsity.diag(2))[1, 0]
DM(00)
>>> try: A[2, 0]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(1, 2), A(2, end)
2
6
>> A(:, 2)
[2, 5]
>> A([2 1], [1 3])
[[4, 6],
 [1, 3]]
>> B = A(2:1, :); disp(B.dim())
0x3
>> x = SX.sym('x', 2, 2);
>> x(1, :)
[[x_0, x_2]]
>> S = DM(Sparsity.diag(2));
>> S(2, 1)
00
>> try, A(3, 1); catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[1, 2], A[2, end]
(2, 6)
julia> A[:, 2]
DM([2, 5])
julia> A[[2, 1], [1, 3]]
DM(
[[4, 6],
 [1, 3]])
julia> ca.dim(A[2:1, :])
"0x3"
julia> x = ca.SX.sym("x", 2, 2);
julia> x[1, :]
SX([[x_0, x_2]])
julia> ca.DM(ca.Sparsity.diag(2))[2, 1]
DM(00)
julia> try A[3, 1]; catch; println("error") end
error

See also
set

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

Definition at line 89 of file matrix_impl.hpp.

90  {
91  // Both are scalar
92  if (rr.is_scalar(size1()) && cc.is_scalar(size2())) {
93  casadi_int k = sparsity().get_nz(rr.scalar(size1()), cc.scalar(size2()));
94  if (k>=0) {
95  m = nonzeros().at(k);
96  } else {
97  m = Matrix<Scalar>(1, 1);
98  }
99  return;
100  }
101 
102  // Fall back on IM-IM
103  get(m, ind1, rr.all(size1(), ind1), cc.all(size2(), ind1));
104  }

References casadi::Slice::all(), casadi::Slice::is_scalar(), and casadi::Slice::scalar().

◆ get() [7/7]

template<typename Scalar >
void casadi::Matrix< Scalar >::get ( Matrix< Scalar > &  m,
bool  ind1,
const Sparsity &  sp 
) const

Backs the Python expressions x[k] and x[sp]. The index addresses elements, not nonzeros, in column-major order: element (i, j) has linear index i + j*size1(). Negative indices count from the end; out-of-range indices raise an error.

  • A scalar index returns a 1-by-1 matrix. If that element is a structural zero, the result is structurally empty (prints 00).
  • A Slice (Python slice) or a list of indices returns a column vector, except that indexing a row vector gives a row vector.
  • A matrix of indices (IM) returns a matrix of the same shape.
  • A Sparsity pattern must have the same shape as the matrix; the result is the projection of the matrix onto that pattern (entries outside it are dropped).

Note that the result of a comparison such as A > 2 is a DM of zeros and ones, not a boolean mask: indexing with it uses those values as linear indices. Use a Sparsity pattern or the nz accessor for masking.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
rrLinear index, Slice or list/matrix of indices
spSparsity pattern of the same shape as the matrix

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[1], A[2], A[-1]
(DM(4), DM(2), DM(6))
>>> A[[0, 5]]
DM([1, 6])
>>> A[Sparsity.diag(2, 3)]
DM(
[[1, 00, 00],
 [00, 5, 00]])
>>> S = DM(Sparsity.diag(2))
>>> S[1]
DM(00)
>>> A.get(False, Slice(0, 2))
DM([1, 4])
>>> try: A[6]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(2), A(3), A(end)
4
2
6
>> A([1 6])
[[1, 6]]
>> A(Sparsity.diag(2, 3))
[[1, 00, 00],
 [00, 5, 00]]
>> S = DM(Sparsity.diag(2));
>> S(2)
00
>> A.get(true, [1; 2])
[1, 4]
>> try, A(7); catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[2], A[3], A[end]
(4, 2, 6)
julia> A[[1, 6]]
DM([1, 6])
julia> S = ca.DM(ca.Sparsity.diag(2));
julia> S[2]
DM(00)
julia> try A[7]; catch; println("error") end
error

See also
set, get_nz, Sparsity::sub

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

Definition at line 186 of file matrix_impl.hpp.

186  {
187  casadi_assert(size()==sp.size(),
188  "Shape mismatch. This matrix has shape "
189  + str(size()) + ", but supplied sparsity index has shape "
190  + str(sp.size()) + ".");
191  m = project(*this, sp);
192  }
std::pair< casadi_int, casadi_int > size() const
Get the shape as a pair (number of rows, number of columns)
static Matrix< Scalar > project(const Matrix< Scalar > &x, const Sparsity &sp, bool intersect=false)
std::string str(const T &v)
String representation, any type.

References casadi::Sparsity::size(), and casadi::str().

◆ get_colind()

std::vector<casadi_int> casadi::GenericMatrix< Matrix< Scalar > >::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.

603 { return sparsity().get_colind(); }
std::vector< casadi_int > get_colind() const
Column offsets of the compressed column storage.
Definition: sparsity.cpp:366

◆ get_elements()

template<typename Scalar >
std::vector<Scalar> casadi::Matrix< Scalar >::get_elements ( ) const

Like nonzeros, but structural zeros are included as 0, so the list has numel() entries.

>>> DM(Sparsity.diag(2)).elements()
[1.0, 0.0, 0.0, 1.0]

>> DM(Sparsity.diag(2)).elements()
1 0 0 1

julia> ca.elements(ca.DM(ca.Sparsity.diag(2)))
4-element Vector{Float64}:
 1.0
 0.0
 0.0
 1.0

See also
nonzeros, densify

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

Referenced by casadi::OptiNode::show_infeasibilities().

◆ get_free()

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

Same as Function::free_sx. Only defined for SX.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> SX.get_free(Function("f", [x], [x + y], {"allow_free": True}))
[SX(y)]

>> x = SX.sym('x'); y = SX.sym('y');
>> v = SX.get_free(Function('f', {x}, {x + y}, struct('allow_free', true)));
>> v{1}
y

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.SX.get_free(ca.Function("f", [x], [x + y], Dict("allow_free" => true)))
1-element Vector{SX}:
 SX(y)

See also
Function::free_sx, get_input

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

Definition at line 1644 of file sx_instantiator.cpp.

1644  {
1645  return f.free_sx();
1646  }

References casadi::Function::free_sx().

◆ get_input()

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

Same as Function::sx_in. Only defined for SX.

>>> x = SX.sym("x")
>>> SX.get_input(Function("f", [x], [x + 1]))
[SX(x)]

>> x = SX.sym('x');
>> v = SX.get_input(Function('f', {x}, {x + 1}));
>> numel(v), v{1}
1
x

julia> x = ca.SX.sym("x");
julia> ca.SX.get_input(ca.Function("f", [x], [x + 1]))
1-element Vector{SX}:
 SX(x)

See also
Function::sx_in, get_free

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

Definition at line 1640 of file sx_instantiator.cpp.

1640  {
1641  return f.sx_in();
1642  }

References casadi::Function::sx_in().

◆ get_max_depth()

casadi_int CASADI_EXPORT casadi::SX::get_max_depth
static
See also
set_max_depth

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

Definition at line 49 of file sx_instantiator.cpp.

49  {
50  return SXNode::eq_depth_;
51  }
static casadi_int eq_depth_
Definition: sx_node.hpp:184

References casadi::SXNode::eq_depth_.

◆ get_nonzeros() [1/2]

template<typename Scalar >
std::vector< A > casadi::Matrix< Scalar >::get_nonzeros ( ) const

Implementation of Matrix::get_nonzeros (in public API)

Returns floats for DM and 1-by-1 SX objects for SX. Structural zeros are skipped.

>>> DM([[1, 2], [3, 4]]).nonzeros()
[1.0, 3.0, 2.0, 4.0]
>>> DM(Sparsity.diag(2)).nonzeros(), DM(2, 0).nonzeros()
([1.0, 1.0], [])
>>> SX.sym("x", 2).nonzeros()
[SX(x_0), SX(x_1)]

>> DM([1 2; 3 4]).nonzeros()
1 3 2 4
>> DM(Sparsity.diag(2)).nonzeros(), size(DM(2, 0).nonzeros())
1 1
1 0
>> v = SX.sym('x', 2).nonzeros();
>> class(v), v{2}
cell
x_1

julia> ca.nonzeros(ca.DM([1 2; 3 4]))
4-element Vector{Float64}:
 1.0
 3.0
 2.0
 4.0
julia> ca.nonzeros(ca.DM(ca.Sparsity.diag(2))), ca.nonzeros(ca.DM(2, 0))
([1.0, 1.0], Float64[])

See also
elements, get_nz

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

Definition at line 4699 of file matrix_decl.hpp.

4699  {
4700  std::vector<A> ret(nnz());
4701  auto r = ret.begin();
4702  for (auto&& e : nonzeros()) *r++ = static_cast<A>(e);
4703  return ret;
4704  }
casadi_int nnz() const
Get the number of structural nonzeros.

◆ get_nonzeros() [2/2]

template<typename Scalar >
template<typename A >
std::vector<A> casadi::Matrix< Scalar >::get_nonzeros ( ) const

◆ get_nz() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::get_nz ( Matrix< Scalar > &  m,
bool  ind1,
const Matrix< casadi_int > &  k 
) const

Backs the Python expression x.nz[k]. Index k runs over the nonzeros in column-major order, 0 <= k < nnz(); negative values count from the end and out-of-range values raise an error. Structural zeros are skipped, so for a dense matrix this coincides with linear indexing. The result is a column vector shaped like the index (a row vector when indexing a row vector; a matrix for a matrix index). Iterating over x.nz in Python yields the nonzeros one by one.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
kNonzero index, Slice or list/matrix of indices

>>> S = DM(Sparsity.diag(3)); S[0, 2] = 7
>>> S.nz[:]
DM([1, 1, 7, 1])
>>> S.nz[2], S.nz[-1], len(S.nz)
(DM(7), DM(1), 4)
>>> x = SX.sym("x", Sparsity.lower(2))
>>> x.nz[[0, 2]]
SX([x_0, x_2])
>>> SX.sym("e", 2, 0).nz[:].dim()
'0x1'
>>> try: S.nz[4]
... except Exception as e: print("error")
error

>> S = DM(Sparsity.diag(3)); S(1, 3) = 7;
>> S.get_nz(true, Slice())
[1, 1, 7, 1]
>> S.get_nz(true, 3), S.get_nz(true, [1; 3])
7
[1, 7]
>> x = SX.sym('x', Sparsity.lower(2));
>> x.get_nz(true, [1; 3])
[x_0, x_2]
>> SX.sym('e', 2, 0).get_nz(true, Slice()).dim()
0x1
>> try, S.get_nz(true, 5); catch e, disp('error'); end
error

julia> S = ca.DM(ca.Sparsity.diag(3)); S[1, 3] = 7;
julia> S.nz[:]
DM([1, 1, 7, 1])
julia> S.nz[3], S.nz[end], length(S.nz)
(7, 1, 4)
julia> x = ca.SX.sym("x", ca.Sparsity.lower(2));
julia> x.nz[[1, 3]]
SX([x_0, x_2])
julia> ca.dim(ca.SX.sym("e", 2, 0).nz[:])
"0x1"
julia> try S.nz[5]; catch e; println(typeof(e)) end
BoundsError

See also
set_nz, get, nonzeros

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

Definition at line 416 of file matrix_impl.hpp.

416  {
417  // Scalar
418  if (kk.is_scalar(true)) {
419  return get_nz(m, ind1, to_slice(kk, ind1));
420  }
421 
422  // Get nonzeros of kk
423  const std::vector<casadi_int>& k = kk.nonzeros();
424  casadi_int sz = nnz();
425 
426  // Check bounds
427  casadi_assert_in_range(k, -sz+ind1, sz+ind1);
428 
429  // If indexed matrix was a row/column vector, make sure that the result is too
430  bool tr = (is_column() && kk.is_row()) || (is_row() && kk.is_column());
431 
432  // Copy nonzeros
433  m = zeros(tr ? kk.sparsity().T() : kk.sparsity());
434  for (casadi_int el=0; el<k.size(); ++el) {
435  casadi_assert(!(ind1 && k[el]<=0), "Matlab is 1-based, but requested index "
436  + str(k[el]) + ". Note that negative slices are"
437  " disabled in the Matlab interface. "
438  "Possibly you may want to use 'end'.");
439  casadi_int k_el = k[el]-ind1;
440  m->at(el) = nonzeros().at(k_el>=0 ? k_el : k_el+sz);
441  }
442  }

References casadi::GenericMatrix< MatType >::is_column(), casadi::GenericMatrix< MatType >::is_row(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::Matrix< Scalar >::nonzeros(), casadi::Matrix< Scalar >::sparsity(), casadi::str(), casadi::Sparsity::T(), and casadi::to_slice().

◆ get_nz() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::get_nz ( Matrix< Scalar > &  m,
bool  ind1,
const Slice &  k 
) const

Backs the Python expression x.nz[k]. Index k runs over the nonzeros in column-major order, 0 <= k < nnz(); negative values count from the end and out-of-range values raise an error. Structural zeros are skipped, so for a dense matrix this coincides with linear indexing. The result is a column vector shaped like the index (a row vector when indexing a row vector; a matrix for a matrix index). Iterating over x.nz in Python yields the nonzeros one by one.

Parameters
ind1True for one-based indices (MATLAB); Python always passes false
kNonzero index, Slice or list/matrix of indices

>>> S = DM(Sparsity.diag(3)); S[0, 2] = 7
>>> S.nz[:]
DM([1, 1, 7, 1])
>>> S.nz[2], S.nz[-1], len(S.nz)
(DM(7), DM(1), 4)
>>> x = SX.sym("x", Sparsity.lower(2))
>>> x.nz[[0, 2]]
SX([x_0, x_2])
>>> SX.sym("e", 2, 0).nz[:].dim()
'0x1'
>>> try: S.nz[4]
... except Exception as e: print("error")
error

>> S = DM(Sparsity.diag(3)); S(1, 3) = 7;
>> S.get_nz(true, Slice())
[1, 1, 7, 1]
>> S.get_nz(true, 3), S.get_nz(true, [1; 3])
7
[1, 7]
>> x = SX.sym('x', Sparsity.lower(2));
>> x.get_nz(true, [1; 3])
[x_0, x_2]
>> SX.sym('e', 2, 0).get_nz(true, Slice()).dim()
0x1
>> try, S.get_nz(true, 5); catch e, disp('error'); end
error

julia> S = ca.DM(ca.Sparsity.diag(3)); S[1, 3] = 7;
julia> S.nz[:]
DM([1, 1, 7, 1])
julia> S.nz[3], S.nz[end], length(S.nz)
(7, 1, 4)
julia> x = ca.SX.sym("x", ca.Sparsity.lower(2));
julia> x.nz[[1, 3]]
SX([x_0, x_2])
julia> ca.dim(ca.SX.sym("e", 2, 0).nz[:])
"0x1"
julia> try S.nz[5]; catch e; println(typeof(e)) end
BoundsError

See also
set_nz, get, nonzeros

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

Definition at line 404 of file matrix_impl.hpp.

404  {
405  // Scalar
406  if (kk.is_scalar(nnz())) {
407  m = nonzeros().at(kk.scalar(nnz()));
408  return;
409  }
410 
411  // Fall back on IM
412  get_nz(m, ind1, kk.all(nnz(), ind1));
413  }

References casadi::Slice::all(), casadi::Slice::is_scalar(), and casadi::Slice::scalar().

◆ get_output()

SX CASADI_EXPORT casadi::SX::get_output ( casadi_int  oind) const

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

Definition at line 167 of file sx_instantiator.cpp.

167  {
168  return scalar().get_output(oind);
169  }

References casadi::Matrix< Scalar >::scalar().

◆ get_precision()

template<typename Scalar >
casadi_int casadi::Matrix< Scalar >::get_precision
static

Definition at line 49 of file matrix_impl.hpp.

49 { return stream_precision_; }

◆ get_row()

std::vector<casadi_int> casadi::GenericMatrix< Matrix< Scalar > >::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.

602 { return sparsity().get_row(); }
std::vector< casadi_int > get_row() const
Row index of every nonzero, in nonzero (column-major) order.
Definition: sparsity.cpp:374

◆ get_scientific()

template<typename Scalar >
bool casadi::Matrix< Scalar >::get_scientific
static

Definition at line 55 of file matrix_impl.hpp.

55 { return stream_scientific_; }

◆ get_sparsity()

template<typename Scalar >
Sparsity casadi::Matrix< Scalar >::get_sparsity ( ) const

Returns a copy of the Sparsity object; patterns are reference counted, so this is cheap.

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

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

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

See also
nonzeros

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

◆ get_str()

template<typename Scalar >
std::string casadi::Matrix< Scalar >::get_str ( bool  more = false) const

◆ get_width()

template<typename Scalar >
casadi_int casadi::Matrix< Scalar >::get_width
static

Definition at line 52 of file matrix_impl.hpp.

52 { return stream_width_; }

◆ gradient()

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

Functions called by friend functions defined here

◆ has_duplicates()

bool CASADI_EXPORT casadi::SX::has_duplicates

Returns true and emits a warning naming each duplicate. Used when checking Function inputs ("input expressions are not independent"). Marks the visited nodes; a second call on the same symbols reports them as duplicates until reset_input is called. Only defined for SX.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> v = vertcat(x, y)
>>> v.has_duplicates()
False
>>> v.reset_input()
>>> vertcat(x, x).has_duplicates()
True

>> x = SX.sym('x'); y = SX.sym('y');
>> v = vertcat(x, y);
>> v.has_duplicates()
0
>> v.reset_input();
>> vertcat(x, x).has_duplicates()
...
1

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> v = [x; y];
julia> ca.has_duplicates(v)
false
julia> ca.reset_input(v)
julia> ca.has_duplicates([x; x])
true

See also
reset_input, is_valid_input

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

Definition at line 200 of file sx_instantiator.cpp.

200  {
201  bool has_duplicates = false;
202  for (auto&& i : nonzeros_) {
203  bool is_duplicate = i.get_temp()!=0;
204  if (is_duplicate) {
205  casadi_warning("Duplicate expression: " + str(i));
206  }
207  has_duplicates = has_duplicates || is_duplicate;
208  i.set_temp(1);
209  }
210  return has_duplicates;
211  }
bool has_duplicates() const
Detect symbolic primitives that appear more than once in an SX matrix.

References casadi::str().

◆ has_nz()

template<typename Scalar >
bool casadi::Matrix< Scalar >::has_nz ( casadi_int  rr,
casadi_int  cc 
) const

Looks only at the sparsity pattern; a stored numerical zero still counts as a nonzero. Negative indices count from the end. An index past the end raises an error.

>>> S = DM(Sparsity.diag(2))
>>> S.has_nz(0, 0), S.has_nz(0, 1), DM([0, 1]).has_nz(0, 0)
(True, False, True)

>> S = DM(Sparsity.diag(2));
>> fprintf('%d %d %d\n', S.has_nz(0, 0), S.has_nz(0, 1), DM([0; 1]).has_nz(0, 0))
1 0 1

julia> S = ca.DM(ca.Sparsity.diag(2));
julia> ca.has_nz(S, 0, 0), ca.has_nz(S, 0, 1), ca.has_nz(ca.DM([0, 1]), 0, 0)
(true, false, true)

See also
has_zeros, Sparsity::has_nz

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

Definition at line 75 of file matrix_impl.hpp.

75  {
76  return sparsity().has_nz(rr, cc);
77  }
bool has_nz(casadi_int rr, casadi_int cc) const
Check whether entry (rr, cc) is a structural nonzero.
Definition: sparsity.cpp:243

◆ has_output()

bool CASADI_EXPORT casadi::SX::has_output

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

Definition at line 162 of file sx_instantiator.cpp.

162  {
163  return scalar().has_output();
164  }

References casadi::Matrix< Scalar >::scalar().

◆ has_zeros()

template<typename Scalar >
bool casadi::Matrix< Scalar >::has_zeros ( ) const

For SX only entries that are the constant 0 count. Use sparsify to remove them.

>>> DM([1, 0]).has_zeros(), DM([1, 2]).has_zeros(), DM(2, 2).has_zeros()
(True, False, False)
>>> x = SX.sym("x")
>>> vertcat(x, 0).has_zeros(), vertcat(x, x * 0).has_zeros()
(True, True)

>> fprintf('%d %d\n', DM([1; 0]).has_zeros(), DM([1; 2]).has_zeros())
1 0
>> DM(2, 2).has_zeros()
0
>> x = SX.sym('x');
>> fprintf('%d %d\n', vertcat(x, 0).has_zeros(), vertcat(x, x * 0).has_zeros())
1 1

julia> ca.has_zeros(ca.DM([1, 0])), ca.has_zeros(ca.DM([1, 2])), ca.has_zeros(ca.DM(2, 2))
(true, false, false)
julia> x = ca.SX.sym("x");
julia> ca.has_zeros([x; 0]), ca.has_zeros([x; x * 0])
(true, true)

See also
sparsify, has_nz

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

◆ heaviside()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::heaviside ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined here

◆ hessian() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::hessian ( const Matrix< Scalar > &  f,
const Matrix< Scalar > &  x,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

◆ hessian() [2/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::hessian ( const Matrix< Scalar > &  f,
const Matrix< Scalar > &  x,
Matrix< Scalar > &  g,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

◆ horzcat()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::horzcat ( const std::vector< Matrix< Scalar > > &  v)
static

Functions called by friend functions defined for SparsityInterface

◆ horzsplit()

template<typename Scalar >
static std::vector<Matrix<Scalar> > casadi::Matrix< Scalar >::horzsplit ( const Matrix< Scalar > &  x,
const std::vector< casadi_int > &  offset 
)
static

Functions called by friend functions defined for SparsityInterface

◆ if_else()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::if_else ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  if_true,
const Matrix< Scalar > &  if_false,
bool  short_circuit = false 
)
static

Functions called by friend functions defined for GenericMatrix

◆ inf() [1/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::inf ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern.

>>> DM.inf(), DM.inf(2, 1).is_inf()
(DM(inf), True)
>>> DM.inf(Sparsity.diag(2))
DM(
[[inf, 00],
 [00, inf]])

>> DM.inf(), DM.inf(2, 1).is_inf()
inf
1
>> DM.inf(Sparsity.diag(2))
[[inf, 00],
 [00, inf]]

julia> ca.DM.inf(), ca.is_inf(ca.DM.inf(2, 1))
(inf, true)
julia> ca.DM.inf(ca.Sparsity.diag(2))
DM(
[[inf, 00],
 [00, inf]])

See also
nan, is_inf

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

◆ inf() [2/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::inf ( const Sparsity &  sp)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern.

>>> DM.inf(), DM.inf(2, 1).is_inf()
(DM(inf), True)
>>> DM.inf(Sparsity.diag(2))
DM(
[[inf, 00],
 [00, inf]])

>> DM.inf(), DM.inf(2, 1).is_inf()
inf
1
>> DM.inf(Sparsity.diag(2))
[[inf, 00],
 [00, inf]]

julia> ca.DM.inf(), ca.is_inf(ca.DM.inf(2, 1))
(inf, true)
julia> ca.DM.inf(ca.Sparsity.diag(2))
DM(
[[inf, 00],
 [00, inf]])

See also
nan, is_inf

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

◆ inf() [3/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::inf ( const std::pair< casadi_int, casadi_int > &  rc)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern.

>>> DM.inf(), DM.inf(2, 1).is_inf()
(DM(inf), True)
>>> DM.inf(Sparsity.diag(2))
DM(
[[inf, 00],
 [00, inf]])

>> DM.inf(), DM.inf(2, 1).is_inf()
inf
1
>> DM.inf(Sparsity.diag(2))
[[inf, 00],
 [00, inf]]

julia> ca.DM.inf(), ca.is_inf(ca.DM.inf(2, 1))
(inf, true)
julia> ca.DM.inf(ca.Sparsity.diag(2))
DM(
[[inf, 00],
 [00, inf]])

See also
nan, is_inf

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

◆ info()

Dict CASADI_EXPORT casadi::SX::info

>>> sorted(DM([1, 2]).info().keys())
['data', 'sparsity']

>> s = DM([1; 2]).info();
>> strjoin(sort(fieldnames(s))', ' ')
data sparsity

julia> sort(collect(keys(ca.info(ca.DM([1, 2])))))
2-element Vector{String}:
 "data"
 "sparsity"

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

Definition at line 1649 of file sx_instantiator.cpp.

1649  {
1650  return {{"function", Function("f", std::vector<SX>{}, std::vector<SX>{*this})}};
1651  }

◆ interp1d()

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

Definition at line 648 of file generic_matrix.hpp.

5364  {
5365 
5366  bool mode_floor = false;
5367  bool mode_ceil = false;
5368  if (mode=="floor") {
5369  mode_floor = true;
5370  } else if (mode=="ceil") {
5371  mode_ceil = true;
5372  } else if (mode=="linear") {
5373  //
5374  } else {
5375  casadi_error("interp1d(x, v, xq, mode): "
5376  "Mode must be 'floor', 'ceil' or 'linear'. Got '" + mode + "' instead.");
5377  }
5378 
5379  casadi_assert(is_increasing(x), "interp1d(x, v, xq): x must be increasing.");
5380 
5381  casadi_assert(x.size()==v.size1(),
5382  "interp1d(x, v, xq): dimensions mismatch. v expected to have " + str(x.size()) + " rows,"
5383  " but got " + str(v.size1()) + " instead.");
5384 
5385  // Need at least two elements
5386  casadi_assert(x.size()>=2, "interp1d(x, v, xq): x must be at least length 2.");
5387 
5388  // Vectors to compose a sparse matrix
5389  std::vector<double> val;
5390  std::vector<casadi_int> colind(1, 0);
5391  std::vector<casadi_int> row;
5392 
5393  // Number of nonzeros in to-be composed matrix
5394  casadi_int nnz = 0;
5395  for (casadi_int i=0;i<xq.size();++i) {
5396  // Obtain index corresponding to xq[i]
5397  double ind = index_interp1d(x, xq[i], equidistant);
5398 
5399  if (mode_floor) ind = floor(ind);
5400  if (mode_ceil) ind = ceil(ind);
5401 
5402  // Split into integer and fractional part
5403  double int_partd;
5404  double frac_part = modf(ind, &int_partd);
5405  casadi_int int_part = static_cast<casadi_int>(int_partd);
5406 
5407  if (frac_part==0) {
5408  // Create a single entry
5409  val.push_back(1);
5410  row.push_back(int_part);
5411  nnz+=1;
5412  colind.push_back(nnz);
5413  } else {
5414  // Create a double entry
5415  val.push_back(1-frac_part);
5416  val.push_back(frac_part);
5417  row.push_back(int_part);
5418  row.push_back(int_part+1);
5419  nnz+=2;
5420  colind.push_back(nnz);
5421  }
5422  }
5423 
5424  // Construct sparsity for composed matrix
5425  Sparsity sp(x.size(), xq.size() , colind, row);
5426 
5427  return MatType::mtimes(MatType(sp, val).T(), v);
5428 
5429  }
double index_interp1d(const std::vector< double > &x, double xq, bool equidistant)
bool is_increasing(const std::vector< T > &v)
Check if the vector is strictly increasing.

◆ inv() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::inv ( const Matrix< Scalar > &  A)
static

Functions called by friend functions defined for GenericMatrix

◆ inv() [2/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::inv ( const Matrix< Scalar > &  A,
const std::string &  lsolver,
const Dict &  opts 
)
static

Functions called by friend functions defined for GenericMatrix

◆ inv_minor()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::inv_minor ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ inv_skew()

Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::inv_skew ( const Matrix< Scalar > &  a)
staticinherited

Definition at line 664 of file generic_matrix.hpp.

5443  {
5444  casadi_assert(a.size1()==3 && a.size2()==3,
5445  "inv_skew(a): Expecting 3-by-3 matrix, got " + a.dim() + ".");
5446 
5447  return 0.5*vertcat(std::vector<MatType>({a(2, 1)-a(1, 2), a(0, 2)-a(2, 0), a(1, 0)-a(0, 1)}));
5448  }

◆ is_call()

bool CASADI_EXPORT casadi::SX::is_call

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

Definition at line 152 of file sx_instantiator.cpp.

152  {
153  return scalar().is_call();
154  }

References casadi::Matrix< Scalar >::scalar().

◆ is_column()

bool casadi::GenericMatrix< Matrix< Scalar > >::is_column ( ) const
inlineinherited

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

Definition at line 517 of file generic_matrix.hpp.

517 { return sparsity().is_column();}
bool is_column() const
Check whether the pattern is a column vector, i.e. size2() == 1.
Definition: sparsity.cpp:287

◆ is_commutative()

bool CASADI_EXPORT casadi::SX::is_commutative

Only defined if symbolic scalar.

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

Definition at line 138 of file sx_instantiator.cpp.

138  {
139  return scalar().is_commutative();
140  }

References casadi::Matrix< Scalar >::scalar().

◆ is_constant()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_constant ( ) const

Always true for DM. For SX, true when no entry depends on a symbol; the simplifications make x*0 and x-x constant. A false negative is possible for expressions that are constant only in value, e.g. x - x after other operations.

>>> x = SX.sym("x")
>>> SX([1, 2]).is_constant(), x.is_constant(), (x * 0).is_constant()
(True, False, True)
>>> vertcat(x, 1).is_constant(), SX.sym("e", 2, 0).is_constant()
(False, True)

>> x = SX.sym('x'); z = x * 0;
>> fprintf('%d %d %d\n', SX([1; 2]).is_constant(), x.is_constant(), z.is_constant())
1 0 1
>> fprintf('%d %d\n', vertcat(x, 1).is_constant(), SX.sym('e', 2, 0).is_constant())
0 1

julia> x = ca.SX.sym("x");
julia> ca.is_constant(ca.SX([1, 2])), ca.is_constant(x), ca.is_constant(x * 0)
(true, false, true)
julia> ca.is_constant([x; 1]), ca.is_constant(ca.SX.sym("e", 2, 0))
(false, true)

See also
is_symbolic, evalf

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

Referenced by casadi::OptiNode::value().

◆ is_dense()

bool casadi::GenericMatrix< Matrix< Scalar > >::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.

434 { return sparsity().is_dense();}
bool is_dense() const
Check whether every entry is a structural nonzero, i.e. nnz() == numel()
Definition: sparsity.cpp:275

◆ is_empty()

bool casadi::GenericMatrix< Matrix< Scalar > >::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.

402 { return sparsity().is_empty(both);}
bool is_empty(bool both=false) const
Check whether the pattern has a zero dimension.
Definition: sparsity.cpp:146

◆ is_equal()

template<typename Scalar >
static bool casadi::Matrix< Scalar >::is_equal ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y,
casadi_int  depth = 0 
)
static

Functions called by friend functions defined for GenericExpression

◆ is_eye()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_eye ( ) const

◆ is_half()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_half ( ) const

◆ is_inf()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_inf ( ) const

◆ is_integer()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_integer ( ) const

(note that false negative answers are possible)

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

Referenced by casadi::ConstantDM::is_integer().

◆ is_leaf()

bool CASADI_EXPORT casadi::SX::is_leaf

Only defined if symbolic scalar.

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

Definition at line 133 of file sx_instantiator.cpp.

133  {
134  return scalar().is_leaf();
135  }

References casadi::Matrix< Scalar >::scalar().

◆ is_linear()

bool casadi::GenericMatrix< Matrix< Scalar > >::is_linear ( const Matrix< Scalar > &  expr,
const Matrix< Scalar > &  var 
)
staticinherited

Definition at line 670 of file generic_matrix.hpp.

5678  {
5679  return !any(MatType::which_depends(expr, var, 2, true));
5680  }
bool any(const std::vector< bool > &v)
Check if any arguments are true.
Definition: casadi_misc.cpp:88

◆ is_minus_inf()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_minus_inf ( ) const

◆ is_minus_one()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_minus_one ( ) const

◆ is_nonnegative()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_nonnegative ( ) const

(note that false negative answers are possible)

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

Referenced by casadi::ConstantDM::is_nonnegative().

◆ is_one()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_one ( ) const

◆ is_op()

bool CASADI_EXPORT casadi::SX::is_op ( casadi_int  op) const

Definition at line 196 of file sx_instantiator.cpp.

196  {
197  return scalar().is_op(op);
198  }
casadi_int op() const
Get operation type.

References casadi::Matrix< Scalar >::op(), and casadi::Matrix< Scalar >::scalar().

◆ is_output()

bool CASADI_EXPORT casadi::SX::is_output

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

Definition at line 157 of file sx_instantiator.cpp.

157  {
158  return scalar().is_output();
159  }

References casadi::Matrix< Scalar >::scalar().

◆ is_quadratic()

bool casadi::GenericMatrix< Matrix< Scalar > >::is_quadratic ( const Matrix< Scalar > &  expr,
const Matrix< Scalar > &  var 
)
staticinherited

Definition at line 671 of file generic_matrix.hpp.

5683  {
5684  return is_linear(gradient(expr, var), var);
5685  }
static bool is_linear(const Matrix< Scalar > &expr, const Matrix< Scalar > &var)
friend Matrix< Scalar > gradient(const Matrix< Scalar > &ex, const Matrix< Scalar > &arg, const Dict &opts=Dict())
Calculate the gradient of an expression.

◆ is_regular()

bool CASADI_EXPORT casadi::SX::is_regular

Structural zeros are ignored. For SX, every entry must be a constant: the check raises an error when it reaches a symbolic entry (an infinite constant found before it still gives false).

>>> DM([1, 2]).is_regular(), DM([1, inf]).is_regular(), DM.nan(2, 2).is_regular()
(True, False, False)
>>> DM(2, 2).is_regular(), SX([1, 2]).is_regular()
(True, True)
>>> try: SX.sym("x").is_regular()
... except Exception as e: print("error")
error

>> fprintf('%d %d\n', DM([1; 2]).is_regular(), DM([1; inf]).is_regular())
1 0
>> fprintf('%d %d\n', DM.nan(2, 2).is_regular(), DM(2, 2).is_regular())
0 1
>> SX([1; 2]).is_regular()
1
>> try, SX.sym('x').is_regular(); catch e, disp('error'); end
error

julia> ca.is_regular(ca.DM([1, 2])), ca.is_regular(ca.DM([1, Inf]))
(true, false)
julia> ca.is_regular(ca.DM.nan(2, 2))
false
julia> ca.is_regular(ca.DM(2, 2)), ca.is_regular(ca.SX([1, 2]))
(true, true)
julia> try ca.is_regular(ca.SX.sym("x")); catch; println("error") end
error

See also
nan, inf

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

Definition at line 102 of file sx_instantiator.cpp.

102  {
103  // First pass: ignore symbolics
104  for (casadi_int i=0; i<nnz(); ++i) {
105  const SXElem& x = nonzeros().at(i);
106  if (x.is_constant()) {
107  if (x.is_nan() || x.is_inf() || x.is_minus_inf()) return false;
108  }
109  }
110  // Second pass: don't ignore symbolics
111  for (casadi_int i=0; i<nnz(); ++i) {
112  if (!nonzeros().at(i).is_regular()) return false;
113  }
114  return true;
115  }

References casadi::SXElem::is_constant(), casadi::SXElem::is_inf(), casadi::SXElem::is_minus_inf(), casadi::SXElem::is_nan(), casadi::GenericMatrix< Matrix< Scalar > >::nnz(), and casadi::Matrix< Scalar >::nonzeros().

Referenced by casadi::MX::is_regular().

◆ is_row()

bool casadi::GenericMatrix< Matrix< Scalar > >::is_row ( ) const
inlineinherited

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

Definition at line 512 of file generic_matrix.hpp.

512 { return sparsity().is_row();}
bool is_row() const
Check whether the pattern is a row vector, i.e. size1() == 1.
Definition: sparsity.cpp:283

◆ is_scalar()

bool casadi::GenericMatrix< Matrix< Scalar > >::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

Definition at line 465 of file generic_matrix.hpp.

5279  {
5280  return sparsity().is_scalar(scalar_and_dense);
5281  }
bool is_scalar(bool scalar_and_dense=false) const
Check whether the pattern is 1-by-1.
Definition: sparsity.cpp:271

◆ is_smooth()

bool CASADI_EXPORT casadi::SX::is_smooth

False when any node is a non-smooth operation such as floor, ceil, sign, a comparison or if_else. Operations such as fabs, fmin and fmax count as smooth. Always true for DM.

>>> x = SX.sym("x")
>>> sin(x).is_smooth(), fabs(x).is_smooth(), floor(x).is_smooth()
(True, True, False)
>>> if_else(x > 0, x, 0).is_smooth(), (x > 0).is_smooth(), DM(1).is_smooth()
(False, False, True)

>> x = SX.sym('x');
>> fprintf('%d %d %d\n', sin(x).is_smooth(), abs(x).is_smooth(), floor(x).is_smooth())
1 1 0
>> c = x > 0; b = if_else(c, x, 0);
>> fprintf('%d %d %d\n', b.is_smooth(), c.is_smooth(), DM(1).is_smooth())
0 0 1

julia> x = ca.SX.sym("x");
julia> ca.is_smooth(sin(x)), ca.is_smooth(abs(x)), ca.is_smooth(floor(x))
(true, true, false)
julia> ca.is_smooth(ifelse(x > 0, x, 0)), ca.is_smooth(x > 0), ca.is_smooth(ca.DM(1))
(false, false, true)

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

Definition at line 118 of file sx_instantiator.cpp.

118  {
119  // Make a function
120  Function temp("tmp_is_smooth", {SX()}, {*this}, Dict{{"max_io", 0}, {"allow_free", true}});
121 
122  // Run the function on the temporary variable
123  SXFunction* t = temp.get<SXFunction>();
124  return t->is_smooth();
125  }
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.

References casadi::SXFunction::is_smooth().

◆ is_square()

bool casadi::GenericMatrix< Matrix< Scalar > >::is_square ( ) const
inlineinherited

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

Definition at line 470 of file generic_matrix.hpp.

470 { return sparsity().is_square();}
bool is_square() const
Check whether the number of rows equals the number of columns.
Definition: sparsity.cpp:295

◆ is_symbolic()

bool CASADI_EXPORT casadi::SX::is_symbolic

Check if an expression is a pure symbol. Pure means that no operations should happen on the symbol (not even vec, transpose, index, concatenation, ...) By consequence, a slice of a vector-shaped MX symbol is not a symbol. However, the SX type is really a container format with scalar entries. Therefore, a slice of a vector-shaped SX symbol is still a symbol.

Sparse matrices invariable return false

\seealso is_valid_input

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

Definition at line 182 of file sx_instantiator.cpp.

182  {
183  if (is_dense()) {
184  return is_valid_input();
185  } else {
186  return false;
187  }
188  }
bool is_valid_input() const
Check if matrix can be used to define function inputs.

References casadi::GenericMatrix< Matrix< Scalar > >::is_dense(), and casadi::Matrix< Scalar >::is_valid_input().

◆ is_tril()

bool casadi::GenericMatrix< Matrix< Scalar > >::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.

560 { return sparsity().is_tril();}
bool is_tril(bool strictly=false) const
Check whether all nonzeros are on or below the diagonal (row >= col)
Definition: sparsity.cpp:323

◆ is_triu()

bool casadi::GenericMatrix< Matrix< Scalar > >::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.

551 { return sparsity().is_triu();}
bool is_triu(bool strictly=false) const
Check whether all nonzeros are on or above the diagonal (row <= col)
Definition: sparsity.cpp:327

◆ is_valid_input()

bool CASADI_EXPORT casadi::SX::is_valid_input

is_valid_input is more forgiving than is_symbolic. Some compositions are allowed: vec, vertcat.

Sparse matrices can return true if all non-zero elements are symbolic

\seealso is_symbolic

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

Definition at line 143 of file sx_instantiator.cpp.

143  {
144  for (casadi_int k=0; k<nnz(); ++k) // loop over non-zero elements
145  if (!nonzeros().at(k)->is_symbolic()) // if an element is not symbolic
146  return false;
147 
148  return true;
149  }

References casadi::GenericMatrix< Matrix< Scalar > >::nnz(), and casadi::Matrix< Scalar >::nonzeros().

Referenced by casadi::Matrix< Scalar >::is_symbolic().

◆ is_value()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_value ( double  val) const

◆ is_vector()

bool casadi::GenericMatrix< Matrix< Scalar > >::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.

507 { return sparsity().is_vector();}
bool is_vector() const
Check whether the pattern is a row or a column vector.
Definition: sparsity.cpp:291

◆ is_zero()

template<typename Scalar >
bool casadi::Matrix< Scalar >::is_zero ( ) const

◆ jacobian()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::jacobian ( const Matrix< Scalar > &  f,
const Matrix< Scalar > &  x,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

Referenced by casadi::AmplInterface::init().

◆ jacobian_sparsity()

template<typename Scalar >
static Sparsity casadi::Matrix< Scalar >::jacobian_sparsity ( const Matrix< Scalar > &  f,
const Matrix< Scalar > &  x 
)
static

Functions called by friend functions defined for GenericMatrix

◆ jtimes()

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

Functions called by friend functions defined here

◆ kron()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::kron ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

Functions called by friend functions defined for SparsityInterface

◆ ldl()

template<typename Scalar >
static void casadi::Matrix< Scalar >::ldl ( const Matrix< Scalar > &  A,
Matrix< Scalar > &  D,
Matrix< Scalar > &  LT,
std::vector< casadi_int > &  p,
bool  amd = true 
)
static

Functions called by friend functions defined here

◆ ldl_solve()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::ldl_solve ( const Matrix< Scalar > &  b,
const Matrix< Scalar > &  D,
const Matrix< Scalar > &  LT,
const std::vector< casadi_int > &  p 
)
static

Functions called by friend functions defined here

◆ linear_coeff()

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

Definition at line 674 of file generic_matrix.hpp.

5700  {
5701  casadi_assert(expr.is_vector(), "'linear_coeff' only defined for vector expressions.");
5702  if (check)
5703  casadi_assert(is_linear(expr, var), "'linear_coeff' called on non-linear expression.");
5704  A = substitute(jacobian(expr, var), var, 0);
5705  b = vec(substitute(expr, var, 0));
5706  }
static MatType vec(const MatType &x)
friend Matrix< Scalar > substitute(const Matrix< Scalar > &ex, const Matrix< Scalar > &v, const Matrix< Scalar > &vdef)
Substitute variable v with expression vdef in an expression ex.
friend Matrix< Scalar > jacobian(const Matrix< Scalar > &ex, const Matrix< Scalar > &arg, const Dict &opts=Dict())
Calculate Jacobian.

◆ linearize()

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

Functions called by friend functions defined here

◆ linspace()

Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::linspace ( const Matrix< Scalar > &  a,
const Matrix< Scalar > &  b,
casadi_int  nsteps 
)
staticinherited

Definition at line 661 of file generic_matrix.hpp.

5312  {
5313  casadi_assert(nsteps>=1, "linspace: nsteps (" + str(nsteps) + ") must be at least 1");
5314  std::vector<MatType> ret(nsteps);
5315  ret[0] = a;
5316  MatType step = (b-a)/static_cast<MatType>(nsteps-1);
5317 
5318  for (casadi_int i=1; i<nsteps-1; ++i)
5319  ret[i] = a + i * step;
5320 
5321  ret[nsteps-1] = b;
5322  return vertcat(ret);
5323  }

◆ logsumexp()

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

◆ mac()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::mac ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y,
const Matrix< Scalar > &  z,
const std::string &  blas = "reference" 
)
static

Functions called by friend functions defined for SparsityInterface

◆ matrix_matrix()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::matrix_matrix ( casadi_int  op,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

◆ matrix_scalar()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::matrix_scalar ( casadi_int  op,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

◆ minor()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::minor ( const Matrix< Scalar > &  x,
casadi_int  i,
casadi_int  j 
)
static

Functions called by friend functions defined here

◆ mldivide()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::mldivide ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

Functions called by friend functions defined for GenericMatrix

◆ mmax()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::mmax ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericExpression

◆ mmin()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::mmin ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericExpression

◆ mpower()

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

Functions called by friend functions defined here

◆ mrdivide()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::mrdivide ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

Functions called by friend functions defined for GenericMatrix

◆ mtaylor() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::mtaylor ( const Matrix< Scalar > &  ex,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  a,
casadi_int  order 
)
static

Functions called by friend functions defined here

◆ mtaylor() [2/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::mtaylor ( const Matrix< Scalar > &  ex,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  a,
casadi_int  order,
const std::vector< casadi_int > &  order_contributions 
)
static

Functions called by friend functions defined here

◆ mtimes()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::mtimes ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y,
const std::string &  blas = "reference" 
)
static

Functions called by friend functions defined for SparsityInterface

◆ n_dep()

casadi_int CASADI_EXPORT casadi::SX::n_dep
See also
dep

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

Definition at line 230 of file sx_instantiator.cpp.

230  {
231  return scalar().n_dep();
232  }

References casadi::Matrix< Scalar >::scalar().

◆ n_nodes()

template<typename Scalar >
static casadi_int casadi::Matrix< Scalar >::n_nodes ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ name()

std::string CASADI_EXPORT casadi::SX::name

Raises an error for a non-scalar, for an expression that is not a pure symbol, and for DM.

>>> SX.sym("x").name(), SX.sym("v", 2)[1].name()
('x', 'v_1')

>> v = SX.sym('v', 2); v2 = v(2);
>> fprintf('%s %s\n', SX.sym('x').name(), v2.name())
x v_1

julia> ca.name(ca.SX.sym("x")), ca.name(ca.SX.sym("v", 2)[2])
("x", "v_1")

See also
is_symbolic

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

Definition at line 220 of file sx_instantiator.cpp.

220  {
221  return scalar().name();
222  }

References casadi::Matrix< Scalar >::scalar().

Referenced by casadi::Matrix< Scalar >::_sym().

◆ nan() [1/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::nan ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern. Useful as a placeholder that is easy to detect with is_regular.

>>> DM.nan(), DM.nan((2, 1)).dim()
(DM(nan), '2x1')
>>> DM.nan(2, 2).is_regular()
False

>> DM.nan(), DM.nan([2 1]).dim()
nan
2x1
>> DM.nan(2, 2).is_regular()
0

julia> ca.DM.nan(), ca.dim(ca.DM.nan(2, 1))
(nan, "2x1")
julia> ca.is_regular(ca.DM.nan(2, 2))
false

See also
inf, is_regular

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

◆ nan() [2/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::nan ( const Sparsity &  sp)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern. Useful as a placeholder that is easy to detect with is_regular.

>>> DM.nan(), DM.nan((2, 1)).dim()
(DM(nan), '2x1')
>>> DM.nan(2, 2).is_regular()
False

>> DM.nan(), DM.nan([2 1]).dim()
nan
2x1
>> DM.nan(2, 2).is_regular()
0

julia> ca.DM.nan(), ca.dim(ca.DM.nan(2, 1))
(nan, "2x1")
julia> ca.is_regular(ca.DM.nan(2, 2))
false

See also
inf, is_regular

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

◆ nan() [3/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::nan ( const std::pair< casadi_int, casadi_int > &  rc)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern. Useful as a placeholder that is easy to detect with is_regular.

>>> DM.nan(), DM.nan((2, 1)).dim()
(DM(nan), '2x1')
>>> DM.nan(2, 2).is_regular()
False

>> DM.nan(), DM.nan([2 1]).dim()
nan
2x1
>> DM.nan(2, 2).is_regular()
0

julia> ca.DM.nan(), ca.dim(ca.DM.nan(2, 1))
(nan, "2x1")
julia> ca.is_regular(ca.DM.nan(2, 2))
false

See also
inf, is_regular

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

◆ nnz()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::nnz
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

Definition at line 123 of file generic_matrix.hpp.

5229  {
5230  return sparsity().nnz();
5231  }
casadi_int nnz() const
Number of structural nonzeros.
Definition: sparsity.cpp:150

◆ nnz_diag()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::nnz_diag
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

Definition at line 215 of file generic_matrix.hpp.

5244  {
5245  return sparsity().nnz_diag();
5246  }
casadi_int nnz_diag() const
Number of nonzeros on the main diagonal, i.e. entries (i, i)
Definition: sparsity.cpp:362

◆ nnz_lower()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::nnz_lower
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

Definition at line 153 of file generic_matrix.hpp.

5234  {
5235  return sparsity().nnz_lower();
5236  }
casadi_int nnz_lower(bool strictly=false) const
Number of nonzeros in the lower triangular part, i.e. entries (i, j) with i >= j.
Definition: sparsity.cpp:354

◆ nnz_upper()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::nnz_upper
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

Definition at line 183 of file generic_matrix.hpp.

5239  {
5240  return sparsity().nnz_upper();
5241  }
casadi_int nnz_upper(bool strictly=false) const
Number of nonzeros in the upper triangular part, i.e. entries (i, j) with i <= j.
Definition: sparsity.cpp:358

◆ nonzeros() [1/2]

template<typename Scalar >
std::vector<Scalar>& casadi::Matrix< Scalar >::nonzeros ( )

◆ nonzeros() [2/2]

template<typename Scalar >
const std::vector<Scalar>& casadi::Matrix< Scalar >::nonzeros ( ) const

Access the non-zero elements

◆ norm_0_mul()

static casadi_int casadi::GenericMatrix< Matrix< Scalar > >::norm_0_mul ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
inlinestaticinherited

Definition at line 651 of file generic_matrix.hpp.

651  {
652  return Sparsity::norm_0_mul(x.sparsity(), y.sparsity());
653  }

◆ norm_1()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::norm_1 ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ norm_2()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::norm_2 ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ norm_fro()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::norm_fro ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ norm_inf()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::norm_inf ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ norm_inf_mul()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::norm_inf_mul ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

Functions called by friend functions defined here

◆ nullspace()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::nullspace ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

Referenced by casadi::MX::nullspace().

◆ numel()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::numel
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

Definition at line 247 of file generic_matrix.hpp.

5249  {
5250  return sparsity().numel();
5251  }
casadi_int numel() const
Total number of entries, structural zeros included, i.e. size1()*size2()
Definition: sparsity.cpp:132

◆ nz() [1/2]

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

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

Definition at line 693 of file generic_matrix.hpp.

693  {
694  return NonZeros<MatType, K>(self(), k);
695  }

◆ nz() [2/2]

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

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

Definition at line 683 of file generic_matrix.hpp.

683  {
684  MatType ret;
685  self().get_nz(ret, false, k);
686  return ret;
687  }

◆ ones() [1/3]

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::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.

5206  {
5207  return ones(Sparsity::dense(nrow, ncol));
5208  }
static Matrix< Scalar > ones(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries one.

◆ ones() [2/3]

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::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.

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

◆ ones() [3/3]

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::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.

5210  {
5211  return ones(rc.first, rc.second);
5212  }

◆ op()

casadi_int CASADI_EXPORT casadi::SX::op

Definition at line 191 of file sx_instantiator.cpp.

191  {
192  return scalar().op();
193  }

References casadi::Matrix< Scalar >::scalar().

Referenced by casadi::Matrix< Scalar >::is_op().

◆ operator casadi_int()

template<typename Scalar >
casadi::Matrix< Scalar >::operator casadi_int ( ) const
explicit

>>> int(DM(3.7))
3

>> DM(3.7).to_int()
3

julia> ca.to_int(ca.DM(3.7))
3

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

◆ operator double()

template<typename Scalar >
casadi::Matrix< Scalar >::operator double ( ) const
explicit

Other shapes raise an error. A symbolic SX entry converts to nan without an error.

>>> float(DM(2.5)), float(SX(3))
(2.5, 3.0)
>>> try: float(DM([1, 2]))
... except Exception as e: print("error")
error

>> fprintf('%g %g\n', full(DM(2.5)), SX(3).to_double())
2.5 3
>> try, DM([1; 2]).to_double(); catch e, disp('error'); end
error

julia> Float64(ca.DM(2.5)), Float64(ca.SX(3))
(2.5, 3.0)
julia> try Float64(ca.DM([1, 2])); catch; println("error") end
error

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

◆ operator std::vector< A >()

template<typename Scalar >
template<typename A >
casadi::Matrix< Scalar >::operator std::vector< A >
explicit

Implementation of std::vector(Matrix) (in public API)

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

Definition at line 4709 of file matrix_decl.hpp.

4709  {
4710  // Get sparsity pattern
4711  casadi_int size1 = this->size1(), size2 = this->size2();
4712  const casadi_int *colind = this->colind(), *row = this->row();
4713  // Copy the nonzeros
4714  auto it = nonzeros().begin();
4715  std::vector<A> ret(numel(), 0);
4716  for (casadi_int cc=0; cc<size2; ++cc) {
4717  for (casadi_int el=colind[cc]; el<colind[cc+1]; ++el) {
4718  ret[row[el] + cc*size1] = static_cast<A>(*it++);
4719  }
4720  }
4721  return ret;
4722  }

◆ operator()() [1/4]

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

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

Definition at line 721 of file generic_matrix.hpp.

721  {
722  return SubIndex<MatType, RR>(self(), rr);
723  }

◆ operator()() [2/4]

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

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

Definition at line 701 of file generic_matrix.hpp.

701  {
702  MatType ret;
703  self().get(ret, false, rr);
704  return ret;
705  }

◆ operator()() [3/4]

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

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

Definition at line 729 of file generic_matrix.hpp.

729  {
730  return SubMatrix<MatType, RR, CC>(self(), rr, cc);
731  }

◆ operator()() [4/4]

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

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

Definition at line 711 of file generic_matrix.hpp.

711  {
712  MatType ret;
713  self().get(ret, false, rr, cc);
714  return ret;
715  }

◆ operator+()

template<typename Scalar >
Matrix<Scalar> casadi::Matrix< Scalar >::operator+ ( ) const
Examples
SX/SXaddition.cc.

◆ operator-()

template<typename Scalar >
Matrix<Scalar> casadi::Matrix< Scalar >::operator- ( ) const

◆ operator->() [1/2]

template<typename Scalar >
std::vector< Scalar > * casadi::Matrix< Scalar >::operator->

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

Definition at line 606 of file matrix_impl.hpp.

606  {
607  return &nonzeros_;
608  }

◆ operator->() [2/2]

template<typename Scalar >
const std::vector< Scalar > * casadi::Matrix< Scalar >::operator->

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

Definition at line 611 of file matrix_impl.hpp.

611  {
612  return &nonzeros_;
613  }

◆ operator=()

template<typename Scalar >
Matrix< Scalar > & casadi::Matrix< Scalar >::operator= ( const Matrix< Scalar > &  m)

Definition at line 599 of file matrix_impl.hpp.

599  {
600  sparsity_ = m.sparsity_;
601  nonzeros_ = m.nonzeros_;
602  return *this;
603  }

◆ pinv() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::pinv ( const Matrix< Scalar > &  A,
const std::string &  lsolver,
const Dict &  opts 
)
static

Functions called by friend functions defined for GenericMatrix

◆ pinv() [2/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::pinv ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ poly_coeff()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::poly_coeff ( const Matrix< Scalar > &  ex,
const Matrix< Scalar > &  x 
)
static

Functions called by friend functions defined here

◆ poly_roots()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::poly_roots ( const Matrix< Scalar > &  p)
static

Functions called by friend functions defined here

◆ polyval()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::polyval ( const Matrix< Scalar > &  p,
const Matrix< Scalar > &  x 
)
static

Functions called by friend functions defined for GenericMatrix

◆ print_canonical()

template<typename Scalar >
void casadi::Matrix< Scalar >::print_canonical ( std::ostream &  stream,
const Sparsity &  sp,
const Scalar *  nonzeros,
bool  truncate = true 
)
static

Definition at line 730 of file matrix_impl.hpp.

731  {
732  casadi_error("'print_canonical' not defined for " + type_name());
733  }
static std::string type_name()
Name of the class: "DM" or "SX".

◆ print_default()

template<typename Scalar >
void casadi::Matrix< Scalar >::print_default ( std::ostream &  stream,
const Sparsity &  sp,
const Scalar *  nonzeros,
bool  truncate = true 
)
static

Definition at line 708 of file matrix_impl.hpp.

709  {
710  if (sp.is_empty()) {
711  stream << sp.size1() << "x" << sp.size2();
712  } else if (sp.numel()==1) {
713  if (sp.nnz()==0) {
714  stream << "00";
715  } else {
716  print_scalar(stream, *nonzeros);
717  }
718  } else if (sp.is_column()) {
719  print_vector(stream, sp, nonzeros, truncate);
720  } else if (std::max(sp.size1(), sp.size2())<=10 ||
721  static_cast<double>(sp.nnz())/static_cast<double>(sp.numel())>=0.5) {
722  // if "small" or "dense"
723  print_dense(stream, sp, nonzeros, truncate);
724  } else {
725  print_sparse(stream, sp, nonzeros, truncate);
726  }
727  }
void print_sparse(std::ostream &stream, bool truncate=true) const
Print as a list of (row, col) -> value triplets, column-major order.
void print_dense(std::ostream &stream, bool truncate=true) const
Print as a dense matrix, one row per line, structural zeros as 00.
void print_vector(std::ostream &stream, bool truncate=true) const
Print a column vector as [a, b, ...], structural zeros as 00.
void print_scalar(std::ostream &stream) const
Print a 1-by-1 matrix as a bare number or expression.

References casadi::Sparsity::is_column(), casadi::Sparsity::is_empty(), casadi::Sparsity::nnz(), casadi::Sparsity::numel(), casadi::Sparsity::size1(), and casadi::Sparsity::size2().

◆ print_dense() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_dense ( std::ostream &  stream,
bool  truncate = true 
) const

Shared SX subexpressions are printed first as @1=..., @2=....

Parameters
truncatePrint only 3 leading and trailing rows and columns when the matrix has more than 1000 elements and at least 7 rows or columns

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

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

See also
print_sparse

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

Definition at line 691 of file matrix_impl.hpp.

691  {
692  print_dense(stream, sparsity(), ptr(), truncate);
693  }
Scalar * ptr()

◆ print_dense() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_dense ( std::ostream &  stream,
const Sparsity &  sp,
const Scalar *  nonzeros,
bool  truncate = true 
)
static

◆ print_operator()

template<typename Scalar >
static std::string casadi::Matrix< Scalar >::print_operator ( const Matrix< Scalar > &  x,
const std::vector< std::string > &  args 
)
static

Functions called by friend functions defined for GenericMatrix

◆ print_scalar() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_scalar ( std::ostream &  stream) const

A structurally empty 1-by-1 matrix prints as 00; other shapes raise an error.

>>> DM(3).print_scalar()
3
>>> DM(1, 1).print_scalar()
00

julia> ca.print_scalar(ca.DM(3))
3
julia> ca.print_scalar(ca.DM(1, 1))
00

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

Definition at line 619 of file matrix_impl.hpp.

619  {
620  casadi_assert(numel()==1, "Not a scalar");
621 
622  StreamStateGuard backup(stream);
623 
624  stream.precision(stream_precision_);
625  stream.width(stream_width_);
626  if (stream_scientific_) {
627  stream.setf(std::ios::scientific);
628  } else {
629  stream.unsetf(std::ios::scientific);
630  }
631 
632  if (nnz()==0) {
633  stream << "00";
634  } else {
635  stream << scalar();
636  }
637  stream << std::flush;
638  }

◆ print_scalar() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_scalar ( std::ostream &  stream,
const Scalar &  e 
)
static

Definition at line 736 of file matrix_impl.hpp.

736  {
737  std::streamsize precision = stream.precision();
738  std::streamsize width = stream.width();
739  std::ios_base::fmtflags flags = stream.flags();
740 
741  stream.precision(stream_precision_);
742  stream.width(stream_width_);
743  if (stream_scientific_) {
744  stream.setf(std::ios::scientific);
745  } else {
746  stream.unsetf(std::ios::scientific);
747  }
748  stream << e;
749  stream << std::flush;
750 
751  stream.precision(precision);
752  stream.width(width);
753  stream.flags(flags);
754  }

◆ print_sparse() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_sparse ( std::ostream &  stream,
bool  truncate = true 
) const
Parameters
truncatePrint only the first and last 3 nonzeros when there are more than 1000

>>> DM(Sparsity.diag(2)).print_sparse()
sparse: 2-by-2, 2 nnz
 (0, 0) -> 1
 (1, 1) -> 1
>>> DM(2, 2).print_sparse()
all zero sparse: 2-by-2

julia> ca.print_sparse(ca.DM(ca.Sparsity.diag(2)))
sparse: 2-by-2, 2 nnz
 (0, 0) -> 1
 (1, 1) -> 1
julia> ca.print_sparse(ca.DM(2, 2))
all zero sparse: 2-by-2

See also
print_dense

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

Definition at line 696 of file matrix_impl.hpp.

696  {
697  print_sparse(stream, sparsity(), ptr(), truncate);
698  }

Referenced by casadi::CsparseInterface::nfact().

◆ print_sparse() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_sparse ( std::ostream &  stream,
const Sparsity &  sp,
const Scalar *  nonzeros,
bool  truncate = true 
)
static

Definition at line 782 of file matrix_impl.hpp.

783  {
784  // Access data structures
785  casadi_int size1 = sp.size1();
786  casadi_int size2 = sp.size2();
787  const casadi_int* colind = sp.colind();
788  const casadi_int* row = sp.row();
789  casadi_int nnz = sp.nnz();
790 
791  // Quick return if all zero sparse
792  if (nnz==0) {
793  stream << "all zero sparse: " << size1 << "-by-" << size2 << std::flush;
794  return;
795  }
796 
797  // Print header
798  stream << "sparse: " << size1 << "-by-" << size2 << ", " << nnz << " nnz";
799 
800  // Get components
801  std::vector<std::string> nz, inter;
802  print_split(nnz, nonzeros, nz, inter);
803 
804  // Print intermediate expressions
805  for (casadi_int i=0; i<inter.size(); ++i)
806  stream << std::endl << " @" << (i+1) << "=" << inter[i] << ",";
807  inter.clear();
808 
809  // No need to truncate if less than 1000 nonzeros
810  const casadi_int max_nnz = 1000;
811  if (truncate && nnz<=max_nnz) truncate=false;
812 
813  // Print nonzeros
814  for (casadi_int cc=0; cc<size2; ++cc) {
815  for (casadi_int el=colind[cc]; el<colind[cc+1]; ++el) {
816  if (truncate && el>=3 && el<nnz-3) {
817  if (el==3) stream << std::endl << " ...";
818  } else {
819  stream << std::endl << " (" << row[el] << ", " << cc << ") -> " << nz.at(el);
821  }
822  }
823  }
824  stream << std::flush;
825  }
const Matrix< Scalar > nz(const K &k) const
Get vector nonzero or slice of nonzeros.
static void check()
Raises an error if an interrupt was captured.
void print_split(std::vector< std::string > &nz, std::vector< std::string > &inter) const
String representation of each nonzero and of the shared subexpressions.

References casadi::InterruptHandler::check(), casadi::Sparsity::colind(), casadi::Sparsity::nnz(), casadi::Sparsity::row(), casadi::Sparsity::size1(), and casadi::Sparsity::size2().

◆ print_split() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_split ( casadi_int  nnz,
const Scalar *  nonzeros,
std::vector< std::string > &  nz,
std::vector< std::string > &  inter 
)
static

Definition at line 757 of file matrix_impl.hpp.

759  {
760  nz.resize(nnz);
761  inter.resize(0);
762 
763  // Temporary
764  std::stringstream ss;
765  ss.precision(stream_precision_);
766  ss.width(stream_width_);
767  if (stream_scientific_) {
768  ss.setf(std::ios::scientific);
769  } else {
770  ss.unsetf(std::ios::scientific);
771  }
772 
773  // Print nonzeros
774  for (casadi_int i=0; i<nz.size(); ++i) {
775  ss.str(std::string());
776  ss << nonzeros[i];
777  nz[i] = ss.str();
778  }
779  }

◆ print_split() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_split ( std::vector< std::string > &  nz,
std::vector< std::string > &  inter 
) const

Returns two lists: one string per nonzero (in storage order) and the definitions of the intermediate expressions "@1", "@2", ... that the nonzero strings refer to (always empty for DM).

>>> DM([[1, 2], [3, 4]]).print_split()
(['1', '3', '2', '4'], [])
>>> x = SX.sym("x"); y = SX.sym("y")
>>> e = x * y
>>> vertcat(e + 1, e - 1).print_split()
(['(@1+@2)', '(@1-@2)'], ['(x*y)', '1'])

>> [nz, inter] = DM([1 2; 3 4]).print_split();
>> disp(strjoin(cellstr(nz)', ' ')), disp(isempty(inter))
1 3 2 4
1
>> x = SX.sym('x'); y = SX.sym('y');
>> e = x * y;
>> [nz, inter] = vertcat(e + 1, e - 1).print_split();
>> disp(strjoin(cellstr(nz)', ' ')), disp(strjoin(cellstr(inter)', ' '))
(@1+@2) (@1-@2)
(x*y) 1

julia> ca.print_split(ca.DM([1 2; 3 4]))
(["1", "3", "2", "4"], String[])
julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> e = x * y;
julia> ca.print_split([e + 1; e - 1])
(["(@1+@2)", "(@1-@2)"], ["(x*y)", "1"])

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

Definition at line 701 of file matrix_impl.hpp.

702  {
703 
704  print_split(nnz(), ptr(), nz, inter);
705  }

◆ print_vector() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_vector ( std::ostream &  stream,
bool  truncate = true 
) const

Only column vectors are accepted; a row vector raises an error.

Parameters
truncatePrint only the first and last 3 entries of vectors longer than 1000

>>> DM(Sparsity.unit(3, 1)).print_vector()
[00, 1, 00]

julia> ca.print_vector(ca.DM(ca.Sparsity.unit(3, 1)))
[00, 1, 00]

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

Definition at line 641 of file matrix_impl.hpp.

641  {
642  print_vector(stream, sparsity(), ptr(), truncate);
643  }

◆ print_vector() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::print_vector ( std::ostream &  stream,
const Sparsity &  sp,
const Scalar *  nonzeros,
bool  truncate = true 
)
static

Definition at line 646 of file matrix_impl.hpp.

647  {
648  casadi_assert(sp.is_column(), "Not a vector");
649 
650  // Get components
651  std::vector<std::string> nz, inter;
652  print_split(sp.nnz(), nonzeros, nz, inter);
653 
654  // Print intermediate expressions
655  for (casadi_int i=0; i<inter.size(); ++i)
656  stream << "@" << (i+1) << "=" << inter[i] << ", ";
657  inter.clear();
658 
659  // Access data structures
660  const casadi_int* row = sp.row();
661  casadi_int nnz = sp.nnz();
662  casadi_int size1 = sp.size1();
663 
664  // No need to truncate if less than 1000 entries
665  const casadi_int max_numel = 1000;
666  if (truncate && size1<=max_numel) truncate=false;
667 
668  // Nonzero
669  casadi_int el=0;
670 
671  // Loop over rows
672  stream << "[";
673  for (casadi_int rr=0; rr<size1; ++rr) {
674  // String representation
675  std::string s = el<nnz && rr==row[el] ? nz.at(el++) : "00";
676 
677  // Truncate?
678  if (truncate && rr>=3 && rr<size1-3) {
679  // Do not print
680  if (rr==3) stream << ", ...";
681  } else {
682  // Print
683  if (rr!=0) stream << ", ";
684  stream << s;
685  }
686  }
687  stream << "]" << std::flush;
688  }

References casadi::Sparsity::is_column(), casadi::Sparsity::nnz(), casadi::Sparsity::row(), and casadi::Sparsity::size1().

◆ printme()

template<typename Scalar >
Matrix<Scalar> casadi::Matrix< Scalar >::printme ( const Matrix< Scalar > &  y) const

◆ project()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::project ( const Matrix< Scalar > &  x,
const Sparsity &  sp,
bool  intersect = false 
)
static

Functions called by friend functions defined for GenericMatrix

◆ ptr() [1/2]

template<typename Scalar >
Scalar* casadi::Matrix< Scalar >::ptr ( )

◆ ptr() [2/2]

template<typename Scalar >
const Scalar* casadi::Matrix< Scalar >::ptr ( ) const

Get a pointer to the data

◆ pw_const()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::pw_const ( const Matrix< Scalar > &  t,
const Matrix< Scalar > &  tval,
const Matrix< Scalar > &  val 
)
static

Functions called by friend functions defined here

◆ pw_lin()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::pw_lin ( const Matrix< Scalar > &  t,
const Matrix< Scalar > &  tval,
const Matrix< Scalar > &  val 
)
static

Functions called by friend functions defined here

◆ qr()

template<typename Scalar >
static void casadi::Matrix< Scalar >::qr ( const Matrix< Scalar > &  A,
Matrix< Scalar > &  Q,
Matrix< Scalar > &  R 
)
static

Functions called by friend functions defined here

◆ qr_solve()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::qr_solve ( const Matrix< Scalar > &  b,
const Matrix< Scalar > &  v,
const Matrix< Scalar > &  r,
const Matrix< Scalar > &  beta,
const std::vector< casadi_int > &  prinv,
const std::vector< casadi_int > &  pc,
bool  tr = false 
)
static

Functions called by friend functions defined here

◆ qr_sparse()

template<typename Scalar >
static void casadi::Matrix< Scalar >::qr_sparse ( const Matrix< Scalar > &  A,
Matrix< Scalar > &  V,
Matrix< Scalar > &  R,
Matrix< Scalar > &  beta,
std::vector< casadi_int > &  prinv,
std::vector< casadi_int > &  pc,
bool  amd = true 
)
static

Functions called by friend functions defined here

Referenced by casadi::qr_block_det().

◆ quadratic_coeff()

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

Definition at line 672 of file generic_matrix.hpp.

5689  {
5690  casadi_assert(expr.is_scalar(), "'quadratic_coeff' only defined for scalar expressions.");
5691  A = hessian(expr, var);
5692  b = substitute(jacobian(expr, var), var, 0).T();
5693  if (check)
5694  casadi_assert(!depends_on(A, var), "'quadratic_coeff' called on non-quadratic expression.");
5695  c = substitute(expr, var, 0);
5696  }
friend bool depends_on(const Matrix< Scalar > &f, const Matrix< Scalar > &arg)
Check if expression depends on the argument.
friend Matrix< Scalar > hessian(const Matrix< Scalar > &ex, const Matrix< Scalar > &arg, const Dict &opts=Dict())
Hessian and (optionally) gradient.

◆ ramp()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::ramp ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined here

◆ rand() [1/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::rand ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern, in which case only the structural nonzeros get a random value. Only available for DM.

>>> DM.rand(2, 3).dim(), DM.rand().dim(), DM.rand(Sparsity.diag(3)).nnz()
('2x3', '1x1', 3)
>>> try: SX.rand(2)
... except Exception as e: print("error")
error

>> fprintf('%s %s\n', DM.rand(2, 3).dim(), DM.rand().dim())
2x3 1x1
>> DM.rand(Sparsity.diag(3)).nnz()
3
>> try, SX.rand(2); catch e, disp('error'); end
error

julia> ca.dim(ca.DM.rand(2, 3)), ca.dim(ca.DM.rand())
("2x3", "1x1")
julia> ca.nnz(ca.DM.rand(ca.Sparsity.diag(3)))
3
julia> try ca.SX.rand(2); catch; println("error") end
error

See also
rng

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

◆ rand() [2/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::rand ( const Sparsity &  sp)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern, in which case only the structural nonzeros get a random value. Only available for DM.

>>> DM.rand(2, 3).dim(), DM.rand().dim(), DM.rand(Sparsity.diag(3)).nnz()
('2x3', '1x1', 3)
>>> try: SX.rand(2)
... except Exception as e: print("error")
error

>> fprintf('%s %s\n', DM.rand(2, 3).dim(), DM.rand().dim())
2x3 1x1
>> DM.rand(Sparsity.diag(3)).nnz()
3
>> try, SX.rand(2); catch e, disp('error'); end
error

julia> ca.dim(ca.DM.rand(2, 3)), ca.dim(ca.DM.rand())
("2x3", "1x1")
julia> ca.nnz(ca.DM.rand(ca.Sparsity.diag(3)))
3
julia> try ca.SX.rand(2); catch; println("error") end
error

See also
rng

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

◆ rand() [3/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::rand ( const std::pair< casadi_int, casadi_int > &  rc)
static

Takes a shape (dense result, default 1-by-1) or a sparsity pattern, in which case only the structural nonzeros get a random value. Only available for DM.

>>> DM.rand(2, 3).dim(), DM.rand().dim(), DM.rand(Sparsity.diag(3)).nnz()
('2x3', '1x1', 3)
>>> try: SX.rand(2)
... except Exception as e: print("error")
error

>> fprintf('%s %s\n', DM.rand(2, 3).dim(), DM.rand().dim())
2x3 1x1
>> DM.rand(Sparsity.diag(3)).nnz()
3
>> try, SX.rand(2); catch e, disp('error'); end
error

julia> ca.dim(ca.DM.rand(2, 3)), ca.dim(ca.DM.rand())
("2x3", "1x1")
julia> ca.nnz(ca.DM.rand(ca.Sparsity.diag(3)))
3
julia> try ca.SX.rand(2); catch; println("error") end
error

See also
rng

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

◆ rank1()

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::rank1 ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  alpha,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  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

◆ rectangle()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::rectangle ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined here

◆ remove()

template<typename Scalar >
void casadi::Matrix< Scalar >::remove ( const std::vector< casadi_int > &  rr,
const std::vector< casadi_int > &  cc 
)

Both lists may be empty; duplicates are ignored, out-of-range indices raise an error.

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A.remove([0], [1])
>>> A
DM([[4, 6]])

>> A = DM([1 2 3; 4 5 6]);
>> A.remove(0, 1);
>> A
[[4, 6]]

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> ca.remove(A, [0], [1])
julia> A
DM([[4, 6]])

See also
erase

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

◆ repsum()

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

Definition at line 667 of file generic_matrix.hpp.

5461  {
5462  casadi_assert(n>0 && x.size1() % n==0,
5463  "repsum: x.size1() (" + str(x.size1()) + ") must be a multiple of n (" + str(n) + ")");
5464  casadi_assert(m>0 && x.size2() % m==0,
5465  "repsum: x.size2() (" + str(x.size2()) + ") must be a multiple of m (" + str(m) + ")");
5466  std::vector< std::vector< MatType> > s =
5467  blocksplit(x, x.size1()/n, x.size2()/m);
5468  MatType sum = 0;
5469  for (casadi_int i=0;i<s.size();++i) {
5470  for (casadi_int j=0;j<s[i].size();++j) {
5471  sum = sum + s[i][j];
5472  }
5473  }
5474  return sum;
5475  }
static std::vector< std::vector< MatType > > blocksplit(const MatType &x, const std::vector< casadi_int > &vert_offset, const std::vector< casadi_int > &horz_offset)
friend MatType sum(const MatType &x)
Sum all elements.

◆ reserve() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::reserve ( casadi_int  nnz)

◆ reserve() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::reserve ( casadi_int  nnz,
casadi_int  ncol 
)

◆ reset_input()

void CASADI_EXPORT casadi::SX::reset_input

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

Definition at line 213 of file sx_instantiator.cpp.

213  {
214  for (auto&& i : nonzeros_) {
215  i.set_temp(0);
216  }
217  }

◆ reshape() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::reshape ( const Matrix< Scalar > &  x,
casadi_int  nrow,
casadi_int  ncol 
)
static

Functions called by friend functions defined for SparsityInterface

◆ reshape() [2/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::reshape ( const Matrix< Scalar > &  x,
const Sparsity &  sp 
)
static

Functions called by friend functions defined for SparsityInterface

◆ resize()

template<typename Scalar >
void casadi::Matrix< Scalar >::resize ( casadi_int  nrow,
casadi_int  ncol 
)

Growing appends structural zeros; shrinking drops the entries outside the new shape.

>>> A = DM([[1, 2], [3, 4]])
>>> A.resize(3, 3)
>>> A
DM(
[[1, 2, 00],
 [3, 4, 00],
 [00, 00, 00]])
>>> A.resize(1, 1)
>>> A
DM(1)

>> A = DM([1 2; 3 4]);
>> A.resize(3, 3);
>> A
[[1, 2, 00],
 [3, 4, 00],
 [00, 00, 00]]
>> A.resize(1, 1);
>> A
1

julia> A = ca.DM([1 2; 3 4]);
julia> ca.resize(A, 3, 3)
julia> A
DM(
[[1, 2, 00],
 [3, 4, 00],
 [00, 00, 00]])
julia> ca.resize(A, 1, 1)
julia> A
DM(1)

See also
enlarge, reshape

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

◆ reverse()

template<typename Scalar >
static std::vector<std::vector<Matrix<Scalar> > > casadi::Matrix< Scalar >::reverse ( const std::vector< Matrix< Scalar > > &  ex,
const std::vector< Matrix< Scalar > > &  arg,
const std::vector< std::vector< Matrix< Scalar > > > &  v,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined here

◆ rng()

template<typename Scalar >
void casadi::Matrix< Scalar >::rng ( casadi_int  seed)
static

The generator is global to the class and is seeded from the clock at startup; seeding it makes subsequent rand calls reproducible.

>>> DM.rng(1); a = DM.rand(2, 2)
>>> DM.rng(1); b = DM.rand(2, 2)
>>> norm_inf(a - b)
DM(0)

>> DM.rng(1); a = DM.rand(2, 2);
>> DM.rng(1); b = DM.rand(2, 2);
>> norm_inf(a - b)
0

julia> ca.DM.rng(1); a = ca.DM.rand(2, 2);
julia> ca.DM.rng(1); b = ca.DM.rand(2, 2);
julia> ca.norm_inf(a - b)
DM(0)

See also
rand

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

Definition at line 70 of file matrix_impl.hpp.

70  {
71  rng_.seed(seed);
72  }

◆ row() [1/2]

const casadi_int* casadi::GenericMatrix< Matrix< Scalar > >::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 605 of file generic_matrix.hpp.

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

◆ row() [2/2]

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::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.

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

◆ rows()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::rows ( ) const
inlineinherited

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

Definition at line 283 of file generic_matrix.hpp.

283 {return size1();}

◆ scalar()

template<typename Scalar >
const Scalar casadi::Matrix< Scalar >::scalar ( ) const

◆ scalar_matrix()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::scalar_matrix ( casadi_int  op,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
static

◆ separate_linear()

template<typename Scalar >
static void casadi::Matrix< Scalar >::separate_linear ( const Matrix< Scalar > &  expr,
const Matrix< Scalar > &  sym_lin,
const Matrix< Scalar > &  sym_const,
Matrix< Scalar > &  expr_const,
Matrix< Scalar > &  expr_lin,
Matrix< Scalar > &  expr_nonlin 
)
static

Functions called by friend functions defined for SparsityInterface

◆ serialize() [1/3]

template<typename Scalar >
std::string casadi::Matrix< Scalar >::serialize ( ) const

The string can be restored with deserialize of the same class (DM or SX). For SX the whole expression graph is stored, symbols included.

>>> s = DM([[1, 2], [3, 4]]).serialize()
>>> DM.deserialize(s)
DM(
[[1, 2],
 [3, 4]])
>>> x = SX.sym("x")
>>> SX.deserialize((sin(x) + x).serialize())
SX((sin(x)+x))

>> s = DM([1 2; 3 4]).serialize();
>> DM.deserialize(s)
[[1, 2],
 [3, 4]]
>> x = SX.sym('x'); e = sin(x) + x;
>> SX.deserialize(e.serialize())
(sin(x)+x)

julia> s = ca.serialize(ca.DM([1 2; 3 4]));
julia> ca.DM.deserialize(s)
DM(
[[1, 2],
 [3, 4]])
julia> x = ca.SX.sym("x");
julia> ca.SX.deserialize(ca.serialize(sin(x) + x))
SX((sin(x)+x))

See also
deserialize, StringSerializer

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

◆ serialize() [2/3]

template<typename Scalar >
void casadi::Matrix< Scalar >::serialize ( SerializingStream &  s) const

◆ serialize() [3/3]

template<typename Scalar >
void casadi::Matrix< Scalar >::serialize ( std::ostream &  stream) const

◆ set() [1/7]

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

Backs the Python statements x[k] = v and x[sp] = v. Indices are linear, column-major and zero-based (see get). The matrix is modified in place.

  • m must be a scalar (assigned to every selected element) or have as many entries as there are indices (a vector of the other orientation is transposed). Any other shape raises an error.
  • Assigning to a structural zero inserts a new nonzero; the sparsity pattern grows. Assigning a structurally empty value (e.g. DM(1, 1)) removes the entry instead.
  • With a Sparsity index of the same shape, the entries at the pattern's positions receive m (a scalar) or the corresponding entries of m (a matrix).
Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
rrLinear index, Slice or list/matrix of indices
spSparsity pattern of the same shape as the matrix

>>> A = DM([[1, 2], [3, 4]])
>>> A[1] = 10; A[[0, 3]] = [7, 8]
>>> A
DM(
[[7, 2],
 [10, 8]])
>>> A[Sparsity.diag(2)] = 0
>>> A
DM(
[[0, 2],
 [10, 0]])
>>> S = DM(Sparsity.diag(2))
>>> S[1] = 5
>>> S.nnz(), S[0, 1]
(3, DM(00))
>>> try: A[[0, 1]] = [1, 2, 3]
... except Exception as e: print("error")
error

>> A = DM([1 2; 3 4]);
>> A(2) = 10; A([1 4]) = [7 8];
>> A
[[7, 2],
 [10, 8]]
>> A(Sparsity.diag(2)) = 0;
>> A
[[0, 2],
 [10, 0]]
>> S = DM(Sparsity.diag(2));
>> S(2) = 5;
>> S.nnz(), S(1, 2)
3
00
>> try, A([1 2]) = [1 2 3]; catch e, disp('error'); end
error

julia> A = ca.DM([1 2; 3 4]);
julia> A[2] = 10; A[[1, 4]] = [7, 8];
julia> A
DM(
[[7, 2],
 [10, 8]])
julia> S = ca.DM(ca.Sparsity.diag(2));
julia> S[2] = 5;
julia> ca.nnz(S), S[1, 2]
(3, 00)
julia> try A[[1, 2]] = [1, 2, 3]; catch; println("error") end
error

See also
get, set_nz, erase

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

Definition at line 314 of file matrix_impl.hpp.

314  {
315  // Scalar
316  if (rr.is_scalar(true) && m.is_dense()) {
317  return set(m, ind1, to_slice(rr, ind1));
318  }
319 
320  // Assert dimensions of assigning matrix
321  if (rr.sparsity() != m.sparsity()) {
322  if (rr.size() == m.size()) {
323  // Remove submatrix to be replaced
324  erase(rr.nonzeros(), ind1);
325 
326  // Find the intersection between rr's and m's sparsity patterns
327  Sparsity sp = rr.sparsity() * m.sparsity();
328 
329  // Project both matrices to this sparsity
330  return set(project(m, sp), ind1, Matrix<casadi_int>::project(rr, sp));
331  } else if (m.is_scalar()) {
332  // m scalar means "set all"
333  if (m.is_dense()) {
334  return set(Matrix<Scalar>(rr.sparsity(), m), ind1, rr);
335  } else {
336  return set(Matrix<Scalar>(rr.size()), ind1, rr);
337  }
338  } else if (rr.size1() == m.size2() && rr.size2() == m.size1()
339  && std::min(m.size1(), m.size2()) == 1) {
340  // m is transposed if necessary
341  return set(m.T(), ind1, rr);
342  } else {
343  // Error otherwise
344  casadi_error("Dimension mismatch. lhs is " + str(rr.size())
345  + ", while rhs is " + str(m.size()));
346  }
347  }
348 
349  // Dimensions of this
350  casadi_int sz1 = size1(), sz2 = size2(), sz = nnz(), nel = numel(), rrsz = rr.nnz();
351 
352  // Quick return if nothing to set
353  if (rrsz==0) return;
354 
355  // Check bounds
356  casadi_assert_in_range(rr.nonzeros(), -nel+ind1, nel+ind1);
357 
358  // Dense mode
359  if (is_dense() && m.is_dense()) {
360  return set_nz(m, ind1, rr);
361  }
362 
363  // Construct new sparsity pattern
364  std::vector<casadi_int> new_row =
365  sparsity().get_row(), new_col=sparsity().get_col(), nz(rr.nonzeros());
366  new_row.reserve(sz+rrsz);
367  new_col.reserve(sz+rrsz);
368  nz.reserve(rrsz);
369  for (std::vector<casadi_int>::iterator i=nz.begin(); i!=nz.end(); ++i) {
370  if (ind1) (*i)--;
371  if (*i<0) *i += nel;
372  new_row.push_back(*i % sz1);
373  new_col.push_back(*i / sz1);
374  }
375  Sparsity sp = Sparsity::triplet(sz1, sz2, new_row, new_col);
376 
377  // If needed, update pattern
378  if (sp != sparsity()) *this = project(*this, sp);
379 
380  // Find the nonzeros corresponding to rr
381  sparsity().get_nz(nz);
382 
383  // Carry out the assignments
384  for (casadi_int i=0; i<nz.size(); ++i) {
385  nonzeros().at(nz[i]) = m->at(i);
386  }
387  }
void set(const Matrix< Scalar > &m, bool ind1, const Slice &rr)
Assign elements by a single (linear) index.
void erase(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
Erase the block at the given rows and columns, leaving structural zeros.
void set_nz(const Matrix< Scalar > &m, bool ind1, const Slice &k)
Assign structural nonzeros by their position in the nonzero storage.
std::vector< casadi_int > get_col() const
Column index of every nonzero, in nonzero (column-major) order.
Definition: sparsity.cpp:370
static Sparsity triplet(casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, std::vector< casadi_int > &mapping, bool invert_mapping)
Create a sparsity pattern from the (row, col) index of every nonzero.
Definition: sparsity.cpp:1157

References casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::GenericMatrix< MatType >::nnz(), casadi::Matrix< Scalar >::nonzeros(), casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), casadi::Matrix< Scalar >::sparsity(), casadi::str(), casadi::Matrix< Scalar >::T(), casadi::to_slice(), and casadi::Sparsity::triplet().

◆ set() [2/7]

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

Backs the Python statement x[i, j] = v. The matrix is modified in place. m must be len(rr)-by-len(cc), a scalar (broadcast to all selected entries) or a vector of the transposed orientation (transposed automatically); other shapes raise an error. Assigning into structural zeros inserts nonzeros; assigning a sparse m first erases the selected block, so its structural zeros become structural zeros.

Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
rrRow index, Slice or list/matrix of row indices
ccColumn index, Slice or list/matrix of column indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[0, 1] = 9; A[1, :] = 0
>>> A
DM(
[[1, 9, 3],
 [0, 0, 0]])
>>> A[0, :] = DM([7, 8, 9])
>>> A[0, :]
DM([[7, 8, 9]])
>>> A[:, 0] = DM(2, 1)
>>> A.nnz()
4
>>> x = SX.sym("x", 2, 2)
>>> x[0, 0] = 0
>>> x
SX(
[[0, x_2],
 [x_1, x_3]])
>>> try: A[0, :] = [1, 2]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(1, 2) = 9; A(2, :) = 0;
>> A
[[1, 9, 3],
 [0, 0, 0]]
>> A(1, :) = DM([7; 8; 9]);
>> A(1, :)
[[7, 8, 9]]
>> A(:, 1) = DM(2, 1);
>> A.nnz()
4
>> x = SX.sym('x', 2, 2);
>> x(1, 1) = 0;
>> x
[[0, x_2],
 [x_1, x_3]]
>> try, A(1, :) = [1 2]; catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[1, 2] = 9; A[2, :] = 0;
julia> A
DM(
[[1, 9, 3],
 [0, 0, 0]])
julia> A[1, :] = ca.DM([7, 8, 9]);
julia> A[1, :]
DM([[7, 8, 9]])
julia> A[:, 1] = ca.DM(2, 1);
julia> ca.nnz(A)
4
julia> x = ca.SX.sym("x", 2, 2);
julia> x[1, 1] = 0;
julia> x
SX(
[[0, x_2],
 [x_1, x_3]])
julia> try A[1, :] = [1, 2]; catch; println("error") end
error

See also
get

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

Definition at line 228 of file matrix_impl.hpp.

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

References casadi::GenericMatrix< MatType >::is_dense(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), casadi::Matrix< Scalar >::T(), and casadi::to_slice().

◆ set() [3/7]

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

Backs the Python statement x[i, j] = v. The matrix is modified in place. m must be len(rr)-by-len(cc), a scalar (broadcast to all selected entries) or a vector of the transposed orientation (transposed automatically); other shapes raise an error. Assigning into structural zeros inserts nonzeros; assigning a sparse m first erases the selected block, so its structural zeros become structural zeros.

Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
rrRow index, Slice or list/matrix of row indices
ccColumn index, Slice or list/matrix of column indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[0, 1] = 9; A[1, :] = 0
>>> A
DM(
[[1, 9, 3],
 [0, 0, 0]])
>>> A[0, :] = DM([7, 8, 9])
>>> A[0, :]
DM([[7, 8, 9]])
>>> A[:, 0] = DM(2, 1)
>>> A.nnz()
4
>>> x = SX.sym("x", 2, 2)
>>> x[0, 0] = 0
>>> x
SX(
[[0, x_2],
 [x_1, x_3]])
>>> try: A[0, :] = [1, 2]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(1, 2) = 9; A(2, :) = 0;
>> A
[[1, 9, 3],
 [0, 0, 0]]
>> A(1, :) = DM([7; 8; 9]);
>> A(1, :)
[[7, 8, 9]]
>> A(:, 1) = DM(2, 1);
>> A.nnz()
4
>> x = SX.sym('x', 2, 2);
>> x(1, 1) = 0;
>> x
[[0, x_2],
 [x_1, x_3]]
>> try, A(1, :) = [1 2]; catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[1, 2] = 9; A[2, :] = 0;
julia> A
DM(
[[1, 9, 3],
 [0, 0, 0]])
julia> A[1, :] = ca.DM([7, 8, 9]);
julia> A[1, :]
DM([[7, 8, 9]])
julia> A[:, 1] = ca.DM(2, 1);
julia> ca.nnz(A)
4
julia> x = ca.SX.sym("x", 2, 2);
julia> x[1, 1] = 0;
julia> x
SX(
[[0, x_2],
 [x_1, x_3]])
julia> try A[1, :] = [1, 2]; catch; println("error") end
error

See also
get

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

Definition at line 221 of file matrix_impl.hpp.

222  {
223  // Fall back on (IM, IM)
224  set(m, ind1, rr, cc.all(size2(), ind1));
225  }

References casadi::Slice::all().

◆ set() [4/7]

template<typename Scalar >
void casadi::Matrix< Scalar >::set ( const Matrix< Scalar > &  m,
bool  ind1,
const Slice &  rr 
)

Backs the Python statements x[k] = v and x[sp] = v. Indices are linear, column-major and zero-based (see get). The matrix is modified in place.

  • m must be a scalar (assigned to every selected element) or have as many entries as there are indices (a vector of the other orientation is transposed). Any other shape raises an error.
  • Assigning to a structural zero inserts a new nonzero; the sparsity pattern grows. Assigning a structurally empty value (e.g. DM(1, 1)) removes the entry instead.
  • With a Sparsity index of the same shape, the entries at the pattern's positions receive m (a scalar) or the corresponding entries of m (a matrix).
Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
rrLinear index, Slice or list/matrix of indices
spSparsity pattern of the same shape as the matrix

>>> A = DM([[1, 2], [3, 4]])
>>> A[1] = 10; A[[0, 3]] = [7, 8]
>>> A
DM(
[[7, 2],
 [10, 8]])
>>> A[Sparsity.diag(2)] = 0
>>> A
DM(
[[0, 2],
 [10, 0]])
>>> S = DM(Sparsity.diag(2))
>>> S[1] = 5
>>> S.nnz(), S[0, 1]
(3, DM(00))
>>> try: A[[0, 1]] = [1, 2, 3]
... except Exception as e: print("error")
error

>> A = DM([1 2; 3 4]);
>> A(2) = 10; A([1 4]) = [7 8];
>> A
[[7, 2],
 [10, 8]]
>> A(Sparsity.diag(2)) = 0;
>> A
[[0, 2],
 [10, 0]]
>> S = DM(Sparsity.diag(2));
>> S(2) = 5;
>> S.nnz(), S(1, 2)
3
00
>> try, A([1 2]) = [1 2 3]; catch e, disp('error'); end
error

julia> A = ca.DM([1 2; 3 4]);
julia> A[2] = 10; A[[1, 4]] = [7, 8];
julia> A
DM(
[[7, 2],
 [10, 8]])
julia> S = ca.DM(ca.Sparsity.diag(2));
julia> S[2] = 5;
julia> ca.nnz(S), S[1, 2]
(3, 00)
julia> try A[[1, 2]] = [1, 2, 3]; catch; println("error") end
error

See also
get, set_nz, erase

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

Definition at line 295 of file matrix_impl.hpp.

295  {
296  // Scalar
297  if (rr.is_scalar(numel()) && m.is_dense()) {
298  casadi_int r = rr.scalar(numel());
299  casadi_int oldsize = sparsity_.nnz();
300  casadi_int ind = sparsity_.add_nz(r % size1(), r / size1());
301  if (oldsize == sparsity_.nnz()) {
302  nonzeros_.at(ind) = m.scalar();
303  } else {
304  nonzeros_.insert(nonzeros_.begin()+ind, m.scalar());
305  }
306  return;
307  }
308 
309  // Fall back on IM
310  set(m, ind1, rr.all(numel(), ind1));
311  }
casadi_int add_nz(casadi_int rr, casadi_int cc)
Nonzero index of entry (rr, cc), inserting the entry into the pattern if needed.
Definition: sparsity.cpp:196

References casadi::Slice::all(), casadi::GenericMatrix< MatType >::is_dense(), casadi::Slice::is_scalar(), casadi::Matrix< Scalar >::scalar(), and casadi::Slice::scalar().

Referenced by casadi::override_num().

◆ set() [5/7]

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

Backs the Python statement x[i, j] = v. The matrix is modified in place. m must be len(rr)-by-len(cc), a scalar (broadcast to all selected entries) or a vector of the transposed orientation (transposed automatically); other shapes raise an error. Assigning into structural zeros inserts nonzeros; assigning a sparse m first erases the selected block, so its structural zeros become structural zeros.

Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
rrRow index, Slice or list/matrix of row indices
ccColumn index, Slice or list/matrix of column indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[0, 1] = 9; A[1, :] = 0
>>> A
DM(
[[1, 9, 3],
 [0, 0, 0]])
>>> A[0, :] = DM([7, 8, 9])
>>> A[0, :]
DM([[7, 8, 9]])
>>> A[:, 0] = DM(2, 1)
>>> A.nnz()
4
>>> x = SX.sym("x", 2, 2)
>>> x[0, 0] = 0
>>> x
SX(
[[0, x_2],
 [x_1, x_3]])
>>> try: A[0, :] = [1, 2]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(1, 2) = 9; A(2, :) = 0;
>> A
[[1, 9, 3],
 [0, 0, 0]]
>> A(1, :) = DM([7; 8; 9]);
>> A(1, :)
[[7, 8, 9]]
>> A(:, 1) = DM(2, 1);
>> A.nnz()
4
>> x = SX.sym('x', 2, 2);
>> x(1, 1) = 0;
>> x
[[0, x_2],
 [x_1, x_3]]
>> try, A(1, :) = [1 2]; catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[1, 2] = 9; A[2, :] = 0;
julia> A
DM(
[[1, 9, 3],
 [0, 0, 0]])
julia> A[1, :] = ca.DM([7, 8, 9]);
julia> A[1, :]
DM([[7, 8, 9]])
julia> A[:, 1] = ca.DM(2, 1);
julia> ca.nnz(A)
4
julia> x = ca.SX.sym("x", 2, 2);
julia> x[1, 1] = 0;
julia> x
SX(
[[0, x_2],
 [x_1, x_3]])
julia> try A[1, :] = [1, 2]; catch; println("error") end
error

See also
get

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

Definition at line 214 of file matrix_impl.hpp.

215  {
216  // Fall back on (IM, IM)
217  set(m, ind1, rr.all(size1(), ind1), cc);
218  }

References casadi::Slice::all().

◆ set() [6/7]

template<typename Scalar >
void casadi::Matrix< Scalar >::set ( const Matrix< Scalar > &  m,
bool  ind1,
const Slice &  rr,
const Slice &  cc 
)

Backs the Python statement x[i, j] = v. The matrix is modified in place. m must be len(rr)-by-len(cc), a scalar (broadcast to all selected entries) or a vector of the transposed orientation (transposed automatically); other shapes raise an error. Assigning into structural zeros inserts nonzeros; assigning a sparse m first erases the selected block, so its structural zeros become structural zeros.

Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
rrRow index, Slice or list/matrix of row indices
ccColumn index, Slice or list/matrix of column indices

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> A[0, 1] = 9; A[1, :] = 0
>>> A
DM(
[[1, 9, 3],
 [0, 0, 0]])
>>> A[0, :] = DM([7, 8, 9])
>>> A[0, :]
DM([[7, 8, 9]])
>>> A[:, 0] = DM(2, 1)
>>> A.nnz()
4
>>> x = SX.sym("x", 2, 2)
>>> x[0, 0] = 0
>>> x
SX(
[[0, x_2],
 [x_1, x_3]])
>>> try: A[0, :] = [1, 2]
... except Exception as e: print("error")
error

>> A = DM([1 2 3; 4 5 6]);
>> A(1, 2) = 9; A(2, :) = 0;
>> A
[[1, 9, 3],
 [0, 0, 0]]
>> A(1, :) = DM([7; 8; 9]);
>> A(1, :)
[[7, 8, 9]]
>> A(:, 1) = DM(2, 1);
>> A.nnz()
4
>> x = SX.sym('x', 2, 2);
>> x(1, 1) = 0;
>> x
[[0, x_2],
 [x_1, x_3]]
>> try, A(1, :) = [1 2]; catch e, disp('error'); end
error

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> A[1, 2] = 9; A[2, :] = 0;
julia> A
DM(
[[1, 9, 3],
 [0, 0, 0]])
julia> A[1, :] = ca.DM([7, 8, 9]);
julia> A[1, :]
DM([[7, 8, 9]])
julia> A[:, 1] = ca.DM(2, 1);
julia> ca.nnz(A)
4
julia> x = ca.SX.sym("x", 2, 2);
julia> x[1, 1] = 0;
julia> x
SX(
[[0, x_2],
 [x_1, x_3]])
julia> try A[1, :] = [1, 2]; catch; println("error") end
error

See also
get

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

Definition at line 195 of file matrix_impl.hpp.

196  {
197  // Both are scalar
198  if (rr.is_scalar(size1()) && cc.is_scalar(size2()) && m.is_dense()) {
199  casadi_int oldsize = sparsity_.nnz();
200  casadi_int ind = sparsity_.add_nz(rr.scalar(size1()), cc.scalar(size2()));
201  if (oldsize == sparsity_.nnz()) {
202  nonzeros_.at(ind) = m.scalar();
203  } else {
204  nonzeros_.insert(nonzeros_.begin()+ind, m.scalar());
205  }
206  return;
207  }
208 
209  // Fall back on (IM, IM)
210  set(m, ind1, rr.all(size1(), ind1), cc.all(size2(), ind1));
211  }

References casadi::Slice::all(), casadi::GenericMatrix< MatType >::is_dense(), casadi::Slice::is_scalar(), casadi::Matrix< Scalar >::scalar(), and casadi::Slice::scalar().

◆ set() [7/7]

template<typename Scalar >
void casadi::Matrix< Scalar >::set ( const Matrix< Scalar > &  m,
bool  ind1,
const Sparsity &  sp 
)

Backs the Python statements x[k] = v and x[sp] = v. Indices are linear, column-major and zero-based (see get). The matrix is modified in place.

  • m must be a scalar (assigned to every selected element) or have as many entries as there are indices (a vector of the other orientation is transposed). Any other shape raises an error.
  • Assigning to a structural zero inserts a new nonzero; the sparsity pattern grows. Assigning a structurally empty value (e.g. DM(1, 1)) removes the entry instead.
  • With a Sparsity index of the same shape, the entries at the pattern's positions receive m (a scalar) or the corresponding entries of m (a matrix).
Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
rrLinear index, Slice or list/matrix of indices
spSparsity pattern of the same shape as the matrix

>>> A = DM([[1, 2], [3, 4]])
>>> A[1] = 10; A[[0, 3]] = [7, 8]
>>> A
DM(
[[7, 2],
 [10, 8]])
>>> A[Sparsity.diag(2)] = 0
>>> A
DM(
[[0, 2],
 [10, 0]])
>>> S = DM(Sparsity.diag(2))
>>> S[1] = 5
>>> S.nnz(), S[0, 1]
(3, DM(00))
>>> try: A[[0, 1]] = [1, 2, 3]
... except Exception as e: print("error")
error

>> A = DM([1 2; 3 4]);
>> A(2) = 10; A([1 4]) = [7 8];
>> A
[[7, 2],
 [10, 8]]
>> A(Sparsity.diag(2)) = 0;
>> A
[[0, 2],
 [10, 0]]
>> S = DM(Sparsity.diag(2));
>> S(2) = 5;
>> S.nnz(), S(1, 2)
3
00
>> try, A([1 2]) = [1 2 3]; catch e, disp('error'); end
error

julia> A = ca.DM([1 2; 3 4]);
julia> A[2] = 10; A[[1, 4]] = [7, 8];
julia> A
DM(
[[7, 2],
 [10, 8]])
julia> S = ca.DM(ca.Sparsity.diag(2));
julia> S[2] = 5;
julia> ca.nnz(S), S[1, 2]
(3, 00)
julia> try A[[1, 2]] = [1, 2, 3]; catch; println("error") end
error

See also
get, set_nz, erase

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

Definition at line 390 of file matrix_impl.hpp.

390  {
391  casadi_assert(size()==sp.size(),
392  "set(Sparsity sp): shape mismatch. This matrix has shape "
393  + str(size()) + ", but supplied sparsity index has shape "
394  + str(sp.size()) + ".");
395  std::vector<casadi_int> ii = sp.find();
396  if (m.is_scalar()) {
397  (*this)(ii) = densify(m);
398  } else {
399  (*this)(ii) = densify(m(ii));
400  }
401  }

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

◆ set_max_depth()

void CASADI_EXPORT casadi::SX::set_max_depth ( casadi_int  eq_depth = 1)
static

The on-the-fly simplifications (x - x -> 0, x/x -> 1, x == x -> 1, ...) recognise two operands as equal when they are the same node, or, up to this depth, when they are the same operation applied to equal operands. Depth 1 (the default) recognises (x+y) - (x+y); depth 2 also sin(x+y) - sin(x+y). Depth 0 compares nodes only. Global setting, SX only; calling without an argument restores the default.

Parameters
eq_depthComparison depth, default 1

>>> x = SX.sym("x"); y = SX.sym("y")
>>> sin(x + y) - sin(x + y)
SX((sin((x+y))-sin((x+y))))
>>> SX.set_max_depth(2)
>>> sin(x + y) - sin(x + y)
SX(0)
>>> SX.set_max_depth()
>>> SX.get_max_depth()
1

>> x = SX.sym('x'); y = SX.sym('y');
>> sin(x + y) - sin(x + y)
(sin((x+y))-sin((x+y)))
>> SX.set_max_depth(2);
>> sin(x + y) - sin(x + y)
0
>> SX.set_max_depth();
>> SX.get_max_depth()
1

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> sin(x + y) - sin(x + y)
SX((sin((x+y))-sin((x+y))))
julia> ca.SX.set_max_depth(2)
julia> sin(x + y) - sin(x + y)
SX(0)
julia> ca.SX.set_max_depth()
julia> ca.SX.get_max_depth()
1

See also
get_max_depth, is_equal

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

Definition at line 44 of file sx_instantiator.cpp.

44  {
45  SXNode::eq_depth_ = eq_depth;
46  }

References casadi::SXNode::eq_depth_.

◆ set_nz() [1/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::set_nz ( const Matrix< Scalar > &  m,
bool  ind1,
const Matrix< casadi_int > &  k 
)

Backs the Python statement x.nz[k] = v. The sparsity pattern never changes: only stored values are overwritten (writing 0 keeps the entry as a structural nonzero). m must be a scalar (assigned to every selected nonzero) or have as many entries as there are indices; a mismatch raises an error.

Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
kNonzero index, Slice or list/matrix of indices

>>> S = DM(Sparsity.diag(3))
>>> S.nz[:] = [4, 5, 6]; S.nz[0] = 0
>>> S
DM(
[[0, 00, 00],
 [00, 5, 00],
 [00, 00, 6]])
>>> S.nnz()
3
>>> try: S.nz[[0, 1]] = [1, 2, 3]
... except Exception as e: print("error")
error

>> S = DM(Sparsity.diag(3));
>> S.set_nz([4 5 6], true, 1:3); S.set_nz(0, true, 1);
>> S
[[0, 00, 00],
 [00, 5, 00],
 [00, 00, 6]]
>> S.nnz()
3
>> try, S.set_nz([1 2 3], true, [1 2]); catch e, disp('error'); end
error

julia> S = ca.DM(ca.Sparsity.diag(3));
julia> S.nz[:] = [4, 5, 6]; S.nz[1] = 0;
julia> S
DM(
[[0, 00, 00],
 [00, 5, 00],
 [00, 00, 6]])
julia> ca.nnz(S)
3
julia> try S.nz[[1, 2]] = [1, 2, 3]; catch; println("error") end
error

See also
get_nz, set

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

Definition at line 457 of file matrix_impl.hpp.

457  {
458  // Scalar
459  if (kk.is_scalar(true)) {
460  return set_nz(m, ind1, to_slice(kk, ind1));
461  }
462 
463  // Assert dimensions of assigning matrix
464  if (kk.sparsity() != m.sparsity()) {
465  if (m.is_scalar()) {
466  // m scalar means "set all"
467  if (!m.is_dense()) return; // Nothing to set
468  return set_nz(Matrix<Scalar>(kk.sparsity(), m), ind1, kk);
469  } else if (kk.size() == m.size()) {
470  // Project sparsity if needed
471  return set_nz(project(m, kk.sparsity()), ind1, kk);
472  } else if (kk.size1() == m.size2() && kk.size2() == m.size1()
473  && std::min(m.size1(), m.size2()) == 1) {
474  // m is transposed if necessary
475  return set_nz(m.T(), ind1, kk);
476  } else {
477  // Error otherwise
478  casadi_error("Dimension mismatch. lhs is " + str(kk.size())
479  + ", while rhs is " + str(m.size()));
480  }
481  }
482 
483  // Get nonzeros
484  const std::vector<casadi_int>& k = kk.nonzeros();
485  casadi_int sz = nnz();
486 
487  // Check bounds
488  casadi_assert_in_range(k, -sz+ind1, sz+ind1);
489 
490  // Set nonzeros, ignoring negative indices
491  for (casadi_int el=0; el<k.size(); ++el) {
492  casadi_assert(!(ind1 && k[el]<=0),
493  "Matlab is 1-based, but requested index " + str(k[el])
494  + ". Note that negative slices are disabled in the Matlab interface. "
495  "Possibly you may want to use 'end'.");
496  casadi_int k_el = k[el]-ind1;
497  nonzeros().at(k_el>=0 ? k_el : k_el+sz) = m->at(el);
498  }
499  }

References casadi::GenericMatrix< MatType >::is_scalar(), casadi::Matrix< Scalar >::sparsity(), and casadi::to_slice().

◆ set_nz() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::set_nz ( const Matrix< Scalar > &  m,
bool  ind1,
const Slice &  k 
)

Backs the Python statement x.nz[k] = v. The sparsity pattern never changes: only stored values are overwritten (writing 0 keeps the entry as a structural nonzero). m must be a scalar (assigned to every selected nonzero) or have as many entries as there are indices; a mismatch raises an error.

Parameters
mValue(s) to assign
ind1True for one-based indices (MATLAB); Python always passes false
kNonzero index, Slice or list/matrix of indices

>>> S = DM(Sparsity.diag(3))
>>> S.nz[:] = [4, 5, 6]; S.nz[0] = 0
>>> S
DM(
[[0, 00, 00],
 [00, 5, 00],
 [00, 00, 6]])
>>> S.nnz()
3
>>> try: S.nz[[0, 1]] = [1, 2, 3]
... except Exception as e: print("error")
error

>> S = DM(Sparsity.diag(3));
>> S.set_nz([4 5 6], true, 1:3); S.set_nz(0, true, 1);
>> S
[[0, 00, 00],
 [00, 5, 00],
 [00, 00, 6]]
>> S.nnz()
3
>> try, S.set_nz([1 2 3], true, [1 2]); catch e, disp('error'); end
error

julia> S = ca.DM(ca.Sparsity.diag(3));
julia> S.nz[:] = [4, 5, 6]; S.nz[1] = 0;
julia> S
DM(
[[0, 00, 00],
 [00, 5, 00],
 [00, 00, 6]])
julia> ca.nnz(S)
3
julia> try S.nz[[1, 2]] = [1, 2, 3]; catch; println("error") end
error

See also
get_nz, set

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

Definition at line 445 of file matrix_impl.hpp.

445  {
446  // Scalar
447  if (kk.is_scalar(nnz())) {
448  nonzeros().at(kk.scalar(nnz())) = m.scalar();
449  return;
450  }
451 
452  // Fallback on IM
453  set_nz(m, ind1, kk.all(nnz(), ind1));
454  }

References casadi::Slice::all(), casadi::Slice::is_scalar(), casadi::Matrix< Scalar >::scalar(), and casadi::Slice::scalar().

◆ set_precision()

template<typename Scalar >
void casadi::Matrix< Scalar >::set_precision ( casadi_int  precision)
static

Global settings per class (DM and SX each keep their own), applied by str(), print() and the print_* methods. Defaults: precision 6, width 0, scientific false.

>>> DM.set_precision(3)
>>> DM(1/3)
DM(0.333)
>>> DM.set_precision(6)
>>> DM(1/3)
DM(0.333333)

>> DM.set_precision(3);
>> DM(1/3)
0.333
>> DM.set_precision(6);
>> DM(1/3)
0.333333

julia> ca.DM.set_precision(3)
julia> ca.DM(1/3)
DM(0.333)
julia> ca.DM.set_precision(6)
julia> ca.DM(1/3)
DM(0.333333)

See also
get_precision

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

Definition at line 40 of file matrix_impl.hpp.

40 { stream_precision_ = precision; }

◆ set_scientific()

template<typename Scalar >
void casadi::Matrix< Scalar >::set_scientific ( bool  scientific)
static

Definition at line 46 of file matrix_impl.hpp.

46 { stream_scientific_ = scientific; }

◆ set_width()

template<typename Scalar >
void casadi::Matrix< Scalar >::set_width ( casadi_int  width)
static

Definition at line 43 of file matrix_impl.hpp.

43 { stream_width_ = width; }

◆ shared()

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

Functions called by friend functions defined for GenericMatrix

◆ simplify()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::simplify ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ simplify_combine_terms()

template<typename Scalar >
static bool casadi::Matrix< Scalar >::simplify_combine_terms ( std::vector< Matrix< Scalar > > &  arg,
std::vector< Matrix< Scalar > > &  res,
const Dict &  opts = Dict() 
)
static

◆ simplify_const_folding()

template<typename Scalar >
static bool casadi::Matrix< Scalar >::simplify_const_folding ( std::vector< Matrix< Scalar > > &  arg,
std::vector< Matrix< Scalar > > &  res,
const Dict &  opts = Dict() 
)
static

◆ simplify_ref_count()

template<typename Scalar >
static bool casadi::Matrix< Scalar >::simplify_ref_count ( std::vector< Matrix< Scalar > > &  arg,
std::vector< Matrix< Scalar > > &  res,
const Dict &  opts = Dict() 
)
static

◆ size() [1/2]

std::pair< casadi_int, casadi_int > casadi::GenericMatrix< Matrix< Scalar > >::size
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

Definition at line 358 of file generic_matrix.hpp.

5264  {
5265  return sparsity().size();
5266  }
std::pair< casadi_int, casadi_int > size() const
Shape as a pair (number of rows, number of columns)
Definition: sparsity.cpp:154

◆ size() [2/2]

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::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

Definition at line 366 of file generic_matrix.hpp.

5269  {
5270  return sparsity().size(axis);
5271  }

◆ size1()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::size1
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

Definition at line 278 of file generic_matrix.hpp.

5254  {
5255  return sparsity().size1();
5256  }
casadi_int size1() const
Number of rows.
Definition: sparsity.cpp:124

◆ size2()

casadi_int casadi::GenericMatrix< Matrix< Scalar > >::size2
inherited
See also
size1, size, columns

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

Definition at line 290 of file generic_matrix.hpp.

5259  {
5260  return sparsity().size2();
5261  }
casadi_int size2() const
Number of columns.
Definition: sparsity.cpp:128

◆ skew()

Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::skew ( const Matrix< Scalar > &  a)
staticinherited

Definition at line 663 of file generic_matrix.hpp.

5432  {
5433  casadi_assert(a.is_vector() && (a.size1()==3 || a.size2()==3),
5434  "skew(a): Expecting 3-vector, got " + a.dim() + ".");
5435 
5436  MatType x = a(0);
5437  MatType y = a(1);
5438  MatType z = a(2);
5439  return blockcat(std::vector< std::vector<MatType> >({{0, -z, y}, {z, 0, -x}, {-y, x, 0}}));
5440  }
friend MatType blockcat(const std::vector< std::vector< MatType > > &v)
Construct a matrix from a list of lists of blocks.

◆ soc()

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

Functions called by friend functions defined here

◆ solve() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::solve ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  b 
)
static

Functions called by friend functions defined for GenericMatrix

Referenced by casadi::SymbolicQr::init().

◆ solve() [2/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::solve ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  b,
const std::string &  lsolver,
const Dict &  opts 
)
static

Functions called by friend functions defined for GenericMatrix

◆ sparsify()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::sparsify ( const Matrix< Scalar > &  x,
double  tol = 0 
)
static

Functions called by friend functions defined here

◆ sparsity()

template<typename Scalar >
const Sparsity& casadi::Matrix< Scalar >::sparsity ( ) const

◆ sparsity_cast()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::sparsity_cast ( const Matrix< Scalar > &  x,
const Sparsity &  sp 
)
static

Functions called by friend functions defined for SparsityInterface

◆ sprank()

static casadi_int casadi::GenericMatrix< Matrix< Scalar > >::sprank ( const Matrix< Scalar > &  x)
inlinestaticinherited

Definition at line 650 of file generic_matrix.hpp.

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

◆ substitute() [1/2]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::substitute ( const Matrix< Scalar > &  ex,
const Matrix< Scalar > &  v,
const Matrix< Scalar > &  vdef 
)
static

Functions called by friend functions defined for GenericMatrix

◆ substitute() [2/2]

template<typename Scalar >
static std::vector<Matrix<Scalar> > casadi::Matrix< Scalar >::substitute ( const std::vector< Matrix< Scalar > > &  ex,
const std::vector< Matrix< Scalar > > &  v,
const std::vector< Matrix< Scalar > > &  vdef 
)
static

Functions called by friend functions defined for GenericMatrix

◆ substitute_inplace()

template<typename Scalar >
static void casadi::Matrix< Scalar >::substitute_inplace ( const std::vector< Matrix< Scalar > > &  v,
std::vector< Matrix< Scalar > > &  vdef,
std::vector< Matrix< Scalar > > &  ex,
bool  revers 
)
static

Functions called by friend functions defined for GenericMatrix

◆ sum1()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::sum1 ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ sum2()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::sum2 ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ sumsqr()

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::sumsqr ( const Matrix< Scalar > &  x)
inlinestaticinherited

Definition at line 660 of file generic_matrix.hpp.

660 { return dot(x, x);}
friend Matrix< Scalar > dot(const Matrix< Scalar > &x, const Matrix< Scalar > &y)
Inner product of two matrices.

◆ sym() [1/7]

static std::vector<Matrix< Scalar > > casadi::GenericMatrix< Matrix< Scalar > >::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.

5064  {
5065  return sym(name, Sparsity::dense(nrow, ncol), p);
5066  }
static Matrix< Scalar > sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.

◆ sym() [2/7]

static std::vector<std::vector<Matrix< Scalar > > > casadi::GenericMatrix< Matrix< Scalar > >::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.

5107  {
5108  return sym(name, Sparsity::dense(nrow, ncol), p, r);
5109  }

◆ sym() [3/7]

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::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

Examples
SX/SXaddition.cc, and SX/ssym.py.

Definition at line 4948 of file generic_matrix.hpp.

4948  {
4949  return sym(name, Sparsity::dense(nrow, ncol));
4950  }

◆ sym() [4/7]

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::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.

4997  {
4998  return MatType::_sym(name, sp);
4999  }

◆ sym() [5/7]

std::vector< Matrix< Scalar > > casadi::GenericMatrix< Matrix< Scalar > >::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.

5287  {
5288  std::vector<MatType> ret(p);
5289  std::stringstream ss;
5290  for (casadi_int k=0; k<p; ++k) {
5291  ss.str("");
5292  ss << name << k;
5293  ret[k] = sym(ss.str(), sp);
5294  }
5295  return ret;
5296  }

◆ sym() [6/7]

std::vector< std::vector< Matrix< Scalar > > > casadi::GenericMatrix< Matrix< Scalar > >::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.

5301  {
5302  std::vector<std::vector<MatType> > ret(r);
5303  for (casadi_int k=0; k<r; ++k) {
5304  std::stringstream ss;
5305  ss << name << "_" << k;
5306  ret[k] = sym(ss.str(), sp, p);
5307  }
5308  return ret;
5309  }

◆ sym() [7/7]

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

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

Definition at line 4955 of file generic_matrix.hpp.

4955  {
4956  return sym(name, rc.first, rc.second);
4957  }

◆ symvar()

template<typename Scalar >
static std::vector<Matrix<Scalar> > casadi::Matrix< Scalar >::symvar ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ T()

template<typename Scalar >
Matrix<Scalar> casadi::Matrix< Scalar >::T ( ) const

◆ tangent()

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

Functions called by friend functions defined here

◆ taylor()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::taylor ( const Matrix< Scalar > &  ex,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  a,
casadi_int  order 
)
static

Functions called by friend functions defined here

◆ to_file() [1/2]

template<typename Scalar >
static void casadi::Matrix< Scalar >::to_file ( const std::string &  filename,
const Sparsity &  sp,
const Scalar *  nonzeros,
const std::string &  format = "" 
)
static

◆ to_file() [2/2]

template<typename Scalar >
void casadi::Matrix< Scalar >::to_file ( const std::string &  filename,
const std::string &  format = "" 
) const

Supported formats, selected by the file extension unless format is given:

  • mtx: Matrix Market coordinate format (sparse, one-based row/col/value triplets)
  • txt: one line per row, whitespace separated, aligned columns, full precision, structural zeros written as 00. Does not scale to large sparse matrices. Values are written with the C locale; nan and inf round-trip. An unknown extension or format raises an error. SX is not supported.
Parameters
filenameOutput path
format"mtx" or "txt"; empty means deduce from the extension

>>> import os, tempfile
>>> fn = os.path.join(tempfile.mkdtemp(), "a.txt")
>>> A = DM(Sparsity.diag(2)) * 2.5
>>> A.to_file(fn)
>>> DM.from_file(fn)
DM(
[[2.5, 00],
 [00, 2.5]])
>>> os.remove(fn); os.rmdir(os.path.dirname(fn))

>> fn = [tempname '.txt'];
>> A = DM(Sparsity.diag(2)) * 2.5;
>> A.to_file(fn);
>> DM.from_file(fn)
[[2.5, 00],
 [00, 2.5]]
>> delete(fn);

julia> fn = joinpath(mktempdir(), "a.txt");
julia> A = ca.DM(ca.Sparsity.diag(2)) * 2.5;
julia> ca.to_file(A, fn)
julia> ca.DM.from_file(fn)
DM(
[[2.5, 00],
 [00, 2.5]])
julia> rm(dirname(fn), recursive=true)

See also
from_file, Sparsity::to_file

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

◆ trace()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::trace ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined for GenericMatrix

◆ transform() [1/4]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::transform ( const Matrix< Scalar > &  x,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

◆ transform() [2/4]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::transform ( const Matrix< Scalar > &  x,
const std::vector< std::vector< GenericType > > &  passes,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

◆ transform() [3/4]

template<typename Scalar >
static std::vector<Matrix<Scalar> > casadi::Matrix< Scalar >::transform ( const std::vector< Matrix< Scalar > > &  x,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

◆ transform() [4/4]

template<typename Scalar >
static std::vector<Matrix<Scalar> > casadi::Matrix< Scalar >::transform ( const std::vector< Matrix< Scalar > > &  x,
const std::vector< std::vector< GenericType > > &  passes,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined for GenericMatrix

◆ triangle()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::triangle ( const Matrix< Scalar > &  x)
static

Functions called by friend functions defined here

◆ tril()

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

Definition at line 654 of file generic_matrix.hpp.

654  {
655  return project(x, Sparsity::tril(x.sparsity(), includeDiagonal));
656  }
friend Matrix< Scalar > project(const Matrix< Scalar > &A, const Sparsity &sp, bool intersect=false)
Create a new matrix with a given sparsity pattern but with the.

◆ tril2symm()

Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::tril2symm ( const Matrix< Scalar > &  x)
staticinherited

Definition at line 665 of file generic_matrix.hpp.

5452  {
5453  casadi_assert(x.is_square(),
5454  "Shape error in tril2symm. Expecting square shape but got " + x.dim());
5455  casadi_assert(x.nnz_upper()-x.nnz_diag()==0,
5456  "Sparsity error in tril2symm. Found above-diagonal entries in argument: " + x.dim());
5457  return x + x.T() - diag(diag(x));
5458  }
friend Matrix< Scalar > diag(const Matrix< Scalar > &A)
Get the diagonal of a matrix or construct a diagonal.

◆ triplet() [1/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::triplet ( const std::vector< casadi_int > &  row,
const std::vector< casadi_int > &  col,
const Matrix< Scalar > &  d 
)
static

Entry k of the result is at (row[k], col[k]) with value d[k]. The three lists must have the same length; indices are zero-based and must be within the matrix bounds. The nonzeros are reordered into column-major order. When the same (row, col) pair appears more than once, only the first value is kept; duplicates are not summed.

Parameters
rowRow index of each entry
colColumn index of each entry
dDense vector of values, one per entry
nrowNumber of rows of the result [default: largest row index plus one]
ncolNumber of columns of the result [default: largest column index plus one]

>>> DM.triplet([0, 1], [1, 0], [5, 6], 3, 3)
DM(
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]])
>>> DM.triplet([0, 1], [1, 0], [5, 6], (3, 3)).nonzeros()
[6.0, 5.0]
>>> SX.triplet([0, 1], [0, 1], SX.sym("v", 2), 2, 2)
SX(
[[v_0, 00],
 [00, v_1]])
>>> DM.triplet([], [], [], 2, 2).nnz()
0
>>> try: DM.triplet([0, 1], [1, 0], [5], 3, 3)
... except Exception as e: print("error")
error

>> DM.triplet([0 1], [1 0], [5 6], 3, 3)
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]]
>> DM.triplet([0 1], [1 0], [5 6], [3 3]).nonzeros()
6 5
>> SX.triplet([0 1], [0 1], SX.sym('v', 2), 2, 2)
[[v_0, 00],
 [00, v_1]]
>> DM.triplet([], [], [], 2, 2).nnz()
0
>> try, DM.triplet([0 1], [1 0], 5, 3, 3); catch e, disp('error'); end
error

julia> ca.DM.triplet([0, 1], [1, 0], [5, 6], 3, 3)
DM(
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]])
julia> ca.SX.triplet([0, 1], [0, 1], ca.SX.sym("v", 2), 2, 2)
SX(
[[v_0, 00],
 [00, v_1]])
julia> ca.nnz(ca.DM.triplet([], [], [], 2, 2))
0
julia> try ca.DM.triplet([0, 1], [1, 0], [5], 3, 3); catch; println("error") end
error

See also
Sparsity::triplet

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

◆ triplet() [2/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::triplet ( const std::vector< casadi_int > &  row,
const std::vector< casadi_int > &  col,
const Matrix< Scalar > &  d,
casadi_int  nrow,
casadi_int  ncol 
)
static

Entry k of the result is at (row[k], col[k]) with value d[k]. The three lists must have the same length; indices are zero-based and must be within the matrix bounds. The nonzeros are reordered into column-major order. When the same (row, col) pair appears more than once, only the first value is kept; duplicates are not summed.

Parameters
rowRow index of each entry
colColumn index of each entry
dDense vector of values, one per entry
nrowNumber of rows of the result [default: largest row index plus one]
ncolNumber of columns of the result [default: largest column index plus one]

>>> DM.triplet([0, 1], [1, 0], [5, 6], 3, 3)
DM(
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]])
>>> DM.triplet([0, 1], [1, 0], [5, 6], (3, 3)).nonzeros()
[6.0, 5.0]
>>> SX.triplet([0, 1], [0, 1], SX.sym("v", 2), 2, 2)
SX(
[[v_0, 00],
 [00, v_1]])
>>> DM.triplet([], [], [], 2, 2).nnz()
0
>>> try: DM.triplet([0, 1], [1, 0], [5], 3, 3)
... except Exception as e: print("error")
error

>> DM.triplet([0 1], [1 0], [5 6], 3, 3)
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]]
>> DM.triplet([0 1], [1 0], [5 6], [3 3]).nonzeros()
6 5
>> SX.triplet([0 1], [0 1], SX.sym('v', 2), 2, 2)
[[v_0, 00],
 [00, v_1]]
>> DM.triplet([], [], [], 2, 2).nnz()
0
>> try, DM.triplet([0 1], [1 0], 5, 3, 3); catch e, disp('error'); end
error

julia> ca.DM.triplet([0, 1], [1, 0], [5, 6], 3, 3)
DM(
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]])
julia> ca.SX.triplet([0, 1], [0, 1], ca.SX.sym("v", 2), 2, 2)
SX(
[[v_0, 00],
 [00, v_1]])
julia> ca.nnz(ca.DM.triplet([], [], [], 2, 2))
0
julia> try ca.DM.triplet([0, 1], [1, 0], [5], 3, 3); catch; println("error") end
error

See also
Sparsity::triplet

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

◆ triplet() [3/3]

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::triplet ( const std::vector< casadi_int > &  row,
const std::vector< casadi_int > &  col,
const Matrix< Scalar > &  d,
const std::pair< casadi_int, casadi_int > &  rc 
)
static

Entry k of the result is at (row[k], col[k]) with value d[k]. The three lists must have the same length; indices are zero-based and must be within the matrix bounds. The nonzeros are reordered into column-major order. When the same (row, col) pair appears more than once, only the first value is kept; duplicates are not summed.

Parameters
rowRow index of each entry
colColumn index of each entry
dDense vector of values, one per entry
nrowNumber of rows of the result [default: largest row index plus one]
ncolNumber of columns of the result [default: largest column index plus one]

>>> DM.triplet([0, 1], [1, 0], [5, 6], 3, 3)
DM(
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]])
>>> DM.triplet([0, 1], [1, 0], [5, 6], (3, 3)).nonzeros()
[6.0, 5.0]
>>> SX.triplet([0, 1], [0, 1], SX.sym("v", 2), 2, 2)
SX(
[[v_0, 00],
 [00, v_1]])
>>> DM.triplet([], [], [], 2, 2).nnz()
0
>>> try: DM.triplet([0, 1], [1, 0], [5], 3, 3)
... except Exception as e: print("error")
error

>> DM.triplet([0 1], [1 0], [5 6], 3, 3)
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]]
>> DM.triplet([0 1], [1 0], [5 6], [3 3]).nonzeros()
6 5
>> SX.triplet([0 1], [0 1], SX.sym('v', 2), 2, 2)
[[v_0, 00],
 [00, v_1]]
>> DM.triplet([], [], [], 2, 2).nnz()
0
>> try, DM.triplet([0 1], [1 0], 5, 3, 3); catch e, disp('error'); end
error

julia> ca.DM.triplet([0, 1], [1, 0], [5, 6], 3, 3)
DM(
[[00, 5, 00],
 [6, 00, 00],
 [00, 00, 00]])
julia> ca.SX.triplet([0, 1], [0, 1], ca.SX.sym("v", 2), 2, 2)
SX(
[[v_0, 00],
 [00, v_1]])
julia> ca.nnz(ca.DM.triplet([], [], [], 2, 2))
0
julia> try ca.DM.triplet([0, 1], [1, 0], [5], 3, 3); catch; println("error") end
error

See also
Sparsity::triplet

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

◆ triu()

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

Definition at line 657 of file generic_matrix.hpp.

657  {
658  return project(x, Sparsity::triu(x.sparsity(), includeDiagonal));
659  }

◆ triu2symm()

Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::triu2symm ( const Matrix< Scalar > &  x)
staticinherited

Definition at line 666 of file generic_matrix.hpp.

5478  {
5479  casadi_assert(x.is_square(),
5480  "Shape error in triu2symm. Expecting square shape but got " + x.dim());
5481  casadi_assert(x.nnz_lower()-x.nnz_diag()==0,
5482  "Sparsity error in triu2symm. Found below-diagonal entries in argument: " + x.dim());
5483  return x + x.T() - diag(diag(x));
5484  }

◆ type_name()

template<typename Scalar >
std::string casadi::Matrix< Scalar >::type_name
static

>>> DM.type_name(), SX.type_name()
('DM', 'SX')

>> fprintf('%s %s\n', DM.type_name(), SX.type_name())
DM SX

julia> ca.DM.type_name(), ca.SX.type_name()
("DM", "SX")

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

Definition at line 616 of file matrix_impl.hpp.

616 { return matrixName<Scalar>(); }

◆ unary()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::unary ( casadi_int  op,
const Matrix< Scalar > &  x 
)
static

◆ unite()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::unite ( const Matrix< Scalar > &  A,
const Matrix< Scalar > &  B 
)
static

Functions called by friend functions defined for GenericMatrix

◆ vertcat()

template<typename Scalar >
static Matrix<Scalar> casadi::Matrix< Scalar >::vertcat ( const std::vector< Matrix< Scalar > > &  v)
static

Functions called by friend functions defined for SparsityInterface

◆ vertsplit()

template<typename Scalar >
static std::vector< Matrix<Scalar> > casadi::Matrix< Scalar >::vertsplit ( const Matrix< Scalar > &  x,
const std::vector< casadi_int > &  offset 
)
static

Functions called by friend functions defined for SparsityInterface

◆ which_depends()

template<typename Scalar >
static std::vector<bool> casadi::Matrix< Scalar >::which_depends ( const Matrix< Scalar > &  expr,
const Matrix< Scalar > &  var,
casadi_int  order = 1,
bool  tr = false 
)
static

Functions called by friend functions defined here

◆ which_function()

Function CASADI_EXPORT casadi::SX::which_function

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

Definition at line 172 of file sx_instantiator.cpp.

172  {
173  return scalar().which_function();
174  }

References casadi::Matrix< Scalar >::scalar().

◆ which_output()

casadi_int CASADI_EXPORT casadi::SX::which_output

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

Definition at line 177 of file sx_instantiator.cpp.

177  {
178  return scalar().which_output();
179  }

References casadi::Matrix< Scalar >::scalar().

◆ zeros() [1/3]

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::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.

5151  {
5152  return zeros(Sparsity::dense(nrow, ncol));
5153  }

◆ zeros() [2/3]

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::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.

5154 { return MatType(sp, 0, false);}

◆ zeros() [3/3]

static Matrix< Scalar > casadi::GenericMatrix< Matrix< Scalar > >::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.

5155  {
5156  return zeros(rc.first, rc.second);
5157  }

Friends And Related Function Documentation

◆ get_ptr [1/2]

template<typename Scalar >
const Scalar* get_ptr ( const Matrix< Scalar > &  v)
friend

Get a pointer to the data

Definition at line 3397 of file matrix_decl.hpp.

3397 { return v.ptr(); }

◆ get_ptr [2/2]

template<typename Scalar >
Scalar* get_ptr ( Matrix< Scalar > &  v)
friend

Get a pointer to the data

Definition at line 3396 of file matrix_decl.hpp.

3396 { return v.ptr(); }

The documentation for this class was generated from the following files: