List of all members | Public Member Functions | Static Public Member Functions | Friends
casadi::GenericMatrix< MatType > Class Template Reference

Matrix base class. More...

#include <generic_matrix.hpp>

Detailed Description

template<typename MatType>
class casadi::GenericMatrix< MatType >

This is a common base class for MX and Matrix<>, introducing a uniform syntax and implementing common functionality using the curiously recurring template pattern (CRTP) idiom.
The class 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.
The syntax tries to stay as close as possible to the ublas syntax when it comes to vector/matrix operations.
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.
The sparsity pattern, which is reference counted and cached, can be accessed with Sparsity& sparsity()

Author
Joel Andersson
Date
2012

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

Definition at line 75 of file generic_matrix.hpp.

Inheritance diagram for casadi::GenericMatrix< MatType >:
Inheritance graph
[legend]

Public Member Functions

casadi_int nnz () const
 Get the number of (structural) non-zero elements. More...
 
casadi_int nnz_lower () const
 Get the number of non-zeros in the lower triangular half. More...
 
casadi_int nnz_upper () const
 Get the number of non-zeros in the upper triangular half. More...
 
casadi_int nnz_diag () const
 Get get the number of non-zeros on the diagonal. More...
 
casadi_int numel () const
 Get the number of elements. More...
 
casadi_int size1 () const
 Get the first dimension (i.e. number of rows) More...
 
casadi_int rows () const
 Get the number of rows, Octave-style syntax. More...
 
casadi_int size2 () const
 Get the second dimension (i.e. number of columns) More...
 
casadi_int columns () const
 Get the number of columns, Octave-style syntax. More...
 
std::string dim (bool with_nz=false) const
 Get string representation of dimensions. More...
 
std::pair< casadi_int, casadi_int > size () const
 Get the shape. More...
 
casadi_int size (casadi_int axis) const
 Get the size along a particular dimensions. More...
 
bool is_empty (bool both=false) const
 Check if the sparsity is empty, i.e. if one of the dimensions is zero. More...
 
bool is_dense () const
 Check if the matrix expression is dense. More...
 
bool is_scalar (bool scalar_and_dense=false) const
 Check if the matrix expression is scalar. More...
 
bool is_square () const
 Check if the matrix expression is square. More...
 
bool is_vector () const
 Check if the matrix is a row or column vector. More...
 
bool is_row () const
 Check if the matrix is a row vector (i.e. size1()==1) More...
 
bool is_column () const
 Check if the matrix is a column vector (i.e. size2()==1) More...
 
bool is_triu () const
 Check if the matrix is upper triangular. More...
 
bool is_tril () const
 Check if the matrix is lower triangular. More...
 
Sparsity sparsity () const
 Get the sparsity pattern. More...
 
std::vector< casadi_int > get_row () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
std::vector< casadi_int > get_colind () const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
casadi_int row (casadi_int el) const
 Get the sparsity pattern. See the Sparsity class for details. More...
 
casadi_int colind (casadi_int col) const
 Get the sparsity pattern. See the Sparsity class for details. More...
 

Static Public Member Functions

static MatType logsumexp (const MatType &x)
 
static MatType jtimes (const MatType &ex, const MatType &arg, const MatType &v, bool tr=false, const Dict &opts=Dict())
 
static MatType gradient (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 
static MatType tangent (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 
static MatType linearize (const MatType &f, const MatType &x, const MatType &x0, const Dict &opts=Dict())
 
static MatType mpower (const MatType &x, const MatType &y)
 
static MatType soc (const MatType &x, const MatType &y)
 
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 MatType sym (const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
 Create an nrow-by-ncol symbolic primitive. More...
 
static MatType sym (const std::string &name, const std::pair< casadi_int, casadi_int > &rc)
 Construct a symbolic primitive with given dimensions. More...
 
static MatType sym (const std::string &name, const Sparsity &sp)
 Create symbolic primitive with a given sparsity pattern. More...
 
static std::vector< MatType > sym (const std::string &name, const Sparsity &sp, casadi_int p)
 Create a vector of length p with with matrices. More...
 
static std::vector< MatType > sym (const std::string &name, casadi_int nrow, casadi_int ncol, casadi_int p)
 Create a vector of length p with nrow-by-ncol symbolic primitives. More...
 
static std::vector< std::vector< MatType > > sym (const std::string &name, const Sparsity &sp, casadi_int p, casadi_int r)
 Create a vector of length r of vectors of length p with. More...
 
static std::vector< std::vector< MatType > > sym (const std::string &name, casadi_int nrow, casadi_int ncol, casadi_int p, casadi_int r)
 Create a vector of length r of vectors of length p. More...
 
static MatType 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 MatType zeros (const Sparsity &sp)
 Create a dense matrix or a matrix with specified sparsity with all entries zero. More...
 
static MatType 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 MatType 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 MatType ones (const Sparsity &sp)
 Create a dense matrix or a matrix with specified sparsity with all entries one. More...
 
static MatType ones (const std::pair< casadi_int, casadi_int > &rc)
 Create a dense matrix or a matrix with specified sparsity with all entries one. More...
 

Friends

MatType interp1d (const std::vector< double > &x, const MatType &v, const std::vector< double > &xq, const std::string &mode, bool equidistant=false)
 Performs 1d linear interpolation. More...
 
MatType mpower (const MatType &x, const MatType &n)
 Matrix power x^n. More...
 
MatType soc (const MatType &x, const MatType &y)
 Construct second-order-convex. More...
 
MatType mrdivide (const MatType &x, const MatType &n)
 Matrix divide (cf. slash '/' in MATLAB) More...
 
MatType mldivide (const MatType &x, const MatType &n)
 Matrix divide (cf. backslash '\' in MATLAB) More...
 
std::vector< MatType > symvar (const MatType &x)
 Get all symbols contained in the supplied expression. More...
 
MatType sumsqr (const MatType &x)
 Calculate sum of squares: sum_ij X_ij^2. More...
 
MatType logsumexp (const MatType &x)
 x -> log(sum_i exp(x_i)) More...
 
MatType logsumexp (const MatType &x, const MatType &margin)
 Scaled version of logsumexp. More...
 
MatType linspace (const MatType &a, const MatType &b, casadi_int nsteps)
 Matlab's linspace command. More...
 
MatType cross (const MatType &a, const MatType &b, casadi_int dim=-1)
 Matlab's cross command. More...
 
MatType skew (const MatType &a)
 Generate a skew symmetric matrix from a 3-vector. More...
 
MatType inv_skew (const MatType &a)
 Generate the 3-vector progenitor of a skew symmetric matrix. More...
 
MatType det (const MatType &A)
 Matrix determinant (experimental) More...
 
MatType inv_minor (const MatType &A)
 Matrix inverse (experimental) More...
 
MatType inv (const MatType &A)
 Matrix inverse. More...
 
MatType inv (const MatType &A, const std::string &lsolver, const Dict &options=Dict())
 Matrix inverse. More...
 
MatType trace (const MatType &x)
 Matrix trace. More...
 
MatType tril2symm (const MatType &a)
 Convert a lower triangular matrix to a symmetric one. More...
 
MatType triu2symm (const MatType &a)
 Convert a upper triangular matrix to a symmetric one. More...
 
MatType norm_fro (const MatType &x)
 Frobenius norm. More...
 
MatType norm_2 (const MatType &x)
 2-norm More...
 
MatType norm_1 (const MatType &x)
 1-norm More...
 
MatType norm_inf (const MatType &x)
 Infinity-norm. More...
 
MatType diff (const MatType &x, casadi_int n=1, casadi_int axis=-1)
 Returns difference (n-th order) along given axis (MATLAB convention) More...
 
MatType cumsum (const MatType &x, casadi_int axis=-1)
 Returns cumulative sum along given axis (MATLAB convention) More...
 
MatType dot (const MatType &x, const MatType &y)
 Inner product of two matrices. More...
 
MatType nullspace (const MatType &A)
 Computes the nullspace of a matrix A. More...
 
MatType polyval (const MatType &p, const MatType &x)
 Evaluate a polynomial with coefficients p in x. More...
 
MatType diag (const MatType &A)
 Get the diagonal of a matrix or construct a diagonal. More...
 
MatType unite (const MatType &A, const MatType &B)
 Unite two matrices no overlapping sparsity. More...
 
MatType densify (const MatType &x)
 Make the matrix dense if not already. More...
 
MatType densify (const MatType &x, const MatType &val)
 Make the matrix dense and assign nonzeros to a value. More...
 
MatType project (const MatType &A, const Sparsity &sp, bool intersect=false)
 Create a new matrix with a given sparsity pattern but with the. More...
 
MatType if_else (const MatType &cond, const MatType &if_true, const MatType &if_false, bool short_circuit=false)
 Branching on MX nodes. More...
 
MatType conditional (const MatType &ind, const std::vector< MatType > &x, const MatType &x_default, bool short_circuit=false)
 Create a switch. More...
 
bool depends_on (const MatType &f, const MatType &arg)
 Check if expression depends on the argument. More...
 
MatType substitute (const MatType &ex, const MatType &v, const MatType &vdef)
 Substitute variable v with expression vdef in an expression ex. More...
 
std::vector< MatType > substitute (const std::vector< MatType > &ex, const std::vector< MatType > &v, const std::vector< MatType > &vdef)
 Substitute variable var with expression expr in multiple expressions. More...
 
void substitute_inplace (const std::vector< MatType > &v, std::vector< MatType > &inout_vdef, std::vector< MatType > &inout_ex, bool reverse=false)
 Inplace substitution with piggyback expressions. More...
 
MatType cse (const MatType &e)
 Common subexpression elimination. More...
 
std::vector< MatType > cse (const std::vector< MatType > &e)
 Common subexpression elimination. More...
 
MatType solve (const MatType &A, const MatType &b)
 Solve a system of equations: A*x = b. More...
 
MatType solve (const MatType &A, const MatType &b, const std::string &lsolver, const Dict &dict=Dict())
 Solve a system of equations: A*x = b. More...
 
MatType linearize (const MatType &f, const MatType &x, const MatType &x0, const Dict &opts=Dict())
 Linearize an expression. More...
 
MatType pinv (const MatType &A)
 Computes the Moore-Penrose pseudo-inverse. More...
 
MatType pinv (const MatType &A, const std::string &lsolver, const Dict &dict=Dict())
 Computes the Moore-Penrose pseudo-inverse. More...
 
MatType expm_const (const MatType &A, const MatType &t)
 Calculate Matrix exponential. More...
 
MatType expm (const MatType &A)
 Calculate Matrix exponential. More...
 
MatType jacobian (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 Calculate Jacobian. More...
 
MatType gradient (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 Calculate the gradient of an expression. More...
 
MatType tangent (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 Calculate the tangent of an expression. More...
 
MatType jtimes (const MatType &ex, const MatType &arg, const MatType &v, bool tr=false, const Dict &opts=Dict())
 Calculate the Jacobian and multiply by a vector from the right. More...
 
std::vector< std::vector< MatType > > forward (const std::vector< MatType > &ex, const std::vector< MatType > &arg, const std::vector< std::vector< MatType > > &v, const Dict &opts=Dict())
 Forward directional derivative. More...
 
std::vector< std::vector< MatType > > reverse (const std::vector< MatType > &ex, const std::vector< MatType > &arg, const std::vector< std::vector< MatType > > &v, const Dict &opts=Dict())
 Reverse directional derivative. More...
 
std::vector< bool > which_depends (const MatType &expr, const MatType &var, casadi_int order, bool tr)
 Find out which variables enter with some order. More...
 
Sparsity jacobian_sparsity (const MatType &f, const MatType &x)
 Get the sparsity pattern of a jacobian. More...
 
bool is_linear (const MatType &expr, const MatType &var)
 Is expr linear in var? More...
 
bool is_quadratic (const MatType &expr, const MatType &var)
 Is expr quadratic in var? More...
 
void quadratic_coeff (const MatType &expr, const MatType &var, MatType &A, MatType &b, MatType &c, bool check=true)
 Recognizes quadratic form in scalar expression. More...
 
void linear_coeff (const MatType &expr, const MatType &var, MatType &A, MatType &b, bool check=true)
 Recognizes linear form in vector expression. More...
 
casadi_int n_nodes (const MatType &A)
 
MatType simplify (const MatType &x)
 Simplify an expression. More...
 
std::string print_operator (const MatType &xb, const std::vector< std::string > &args)
 Get a string representation for a binary MatType, using custom arguments. More...
 
void extract (std::vector< MatType > &ex, std::vector< MatType > &v, std::vector< MatType > &vdef, const Dict &opts=Dict())
 Introduce intermediate variables for selected nodes in a graph. More...
 
void shared (std::vector< MatType > &ex, std::vector< MatType > &v, std::vector< MatType > &vdef, const std::string &v_prefix="v_", const std::string &v_suffix="")
 Extract shared subexpressions from an set of expressions. More...
 
MatType repsum (const MatType &A, casadi_int n, casadi_int m=1)
 Given a repeated matrix, computes the sum of repeated parts. More...
 
MatType einstein (const MatType &A, const MatType &B, const MatType &C, const std::vector< casadi_int > &dim_a, const std::vector< casadi_int > &dim_b, const std::vector< casadi_int > &dim_c, const std::vector< casadi_int > &a, const std::vector< casadi_int > &b, const std::vector< casadi_int > &c)
 Compute any contraction of two dense tensors, using index/einstein notation. More...
 
MatType einstein (const MatType &A, const MatType &B, const std::vector< casadi_int > &dim_a, const std::vector< casadi_int > &dim_b, const std::vector< casadi_int > &dim_c, const std::vector< casadi_int > &a, const std::vector< casadi_int > &b, const std::vector< casadi_int > &c)
 Compute any contraction of two dense tensors, using index/einstein notation. More...
 
MatType hessian (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 Hessian and (optionally) gradient. More...
 
MatType hessian (const MatType &ex, const MatType &arg, MatType &output_g, const Dict &opts=Dict())
 Hessian and (optionally) gradient. More...
 
MatType mmin (const MatType &x)
 Smallest element in a matrix. More...
 
MatType mmax (const MatType &x)
 Largest element in a matrix. More...
 
MatType bilin (const MatType &A, const MatType &x, const MatType &y)
 Calculate bilinear/quadratic form x^T A y. More...
 
MatType bilin (const MatType &A, const MatType &x)
 Calculate bilinear/quadratic form x^T A y. More...
 
static MatType bilin (const MatType &A, const MatType &x, const MatType &y)
 Calculate bilinear/quadratic form x^T A y. More...
 
MatType rank1 (const MatType &A, const MatType &alpha, const MatType &x, const MatType &y)
 Make a rank-1 update to a matrix A. More...
 
static MatType rank1 (const MatType &A, const MatType &alpha, const MatType &x, const MatType &y)
 Make a rank-1 update to a matrix A. More...
 

Member Function Documentation

◆ colind()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::colind ( casadi_int  col) const
inline

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

Definition at line 201 of file generic_matrix.hpp.

◆ columns()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::columns ( ) const
inline

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

Definition at line 124 of file generic_matrix.hpp.

◆ dim()

template<typename MatType >
std::string casadi::GenericMatrix< MatType >::dim ( bool  with_nz = false) const

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

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

◆ get_colind()

template<typename MatType >
std::vector<casadi_int> casadi::GenericMatrix< MatType >::get_colind ( ) const
inline

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

Definition at line 195 of file generic_matrix.hpp.

◆ get_row()

template<typename MatType >
std::vector<casadi_int> casadi::GenericMatrix< MatType >::get_row ( ) const
inline

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

Definition at line 194 of file generic_matrix.hpp.

◆ is_column()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_column ( ) const
inline

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

Definition at line 178 of file generic_matrix.hpp.

◆ is_dense()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_dense ( ) const
inline

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

Definition at line 153 of file generic_matrix.hpp.

◆ is_empty()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_empty ( bool  both = false) const
inline

(or optionally both dimensions)

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

Definition at line 148 of file generic_matrix.hpp.

◆ is_row()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_row ( ) const
inline

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

Definition at line 173 of file generic_matrix.hpp.

◆ is_scalar()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_scalar ( bool  scalar_and_dense = false) const

◆ is_square()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_square ( ) const
inline

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

Definition at line 163 of file generic_matrix.hpp.

◆ is_tril()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_tril ( ) const
inline

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

Definition at line 188 of file generic_matrix.hpp.

◆ is_triu()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_triu ( ) const
inline

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

Definition at line 183 of file generic_matrix.hpp.

◆ is_vector()

template<typename MatType >
bool casadi::GenericMatrix< MatType >::is_vector ( ) const
inline

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

Definition at line 168 of file generic_matrix.hpp.

◆ nnz()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::nnz ( ) const

◆ nnz_diag()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::nnz_diag ( ) const

◆ nnz_lower()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::nnz_lower ( ) const

◆ nnz_upper()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::nnz_upper ( ) const

◆ numel()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::numel ( ) const

◆ ones() [1/3]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::ones ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
inlinestatic

◆ ones() [2/3]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::ones ( const Sparsity sp)
inlinestatic

◆ ones() [3/3]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::ones ( const std::pair< casadi_int, casadi_int > &  rc)
inlinestatic

◆ row()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::row ( casadi_int  el) const
inline

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

Definition at line 200 of file generic_matrix.hpp.

◆ rows()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::rows ( ) const
inline

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

Definition at line 114 of file generic_matrix.hpp.

◆ size() [1/2]

template<typename MatType >
std::pair<casadi_int, casadi_int> casadi::GenericMatrix< MatType >::size ( ) const

◆ size() [2/2]

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::size ( casadi_int  axis) const

◆ size1()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::size1 ( ) const

◆ size2()

template<typename MatType >
casadi_int casadi::GenericMatrix< MatType >::size2 ( ) const

◆ sparsity()

template<typename MatType >
Sparsity casadi::GenericMatrix< MatType >::sparsity ( ) const

◆ sym() [1/7]

template<typename MatType >
static std::vector<MatType > casadi::GenericMatrix< MatType >::sym ( const std::string &  name,
casadi_int  nrow,
casadi_int  ncol,
casadi_int  p 
)
inlinestatic

◆ sym() [2/7]

template<typename MatType >
static std::vector<std::vector<MatType> > casadi::GenericMatrix< MatType >::sym ( const std::string &  name,
casadi_int  nrow,
casadi_int  ncol,
casadi_int  p,
casadi_int  r 
)
inlinestatic

with nrow-by-ncol symbolic primitives

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

Definition at line 1074 of file generic_matrix.hpp.

◆ sym() [3/7]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::sym ( const std::string &  name,
casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
inlinestatic

◆ sym() [4/7]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::sym ( const std::string &  name,
const Sparsity sp 
)
inlinestatic

◆ sym() [5/7]

template<typename MatType >
std::vector< MatType > casadi::GenericMatrix< MatType >::sym ( const std::string &  name,
const Sparsity sp,
casadi_int  p 
)
static

with symbolic primitives of given sparsity

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

Definition at line 1176 of file generic_matrix.hpp.

◆ sym() [6/7]

template<typename MatType >
std::vector< std::vector< MatType > > casadi::GenericMatrix< MatType >::sym ( const std::string &  name,
const Sparsity sp,
casadi_int  p,
casadi_int  r 
)
static

symbolic primitives with given sparsity

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

Definition at line 1189 of file generic_matrix.hpp.

◆ sym() [7/7]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::sym ( const std::string &  name,
const std::pair< casadi_int, casadi_int > &  rc 
)
inlinestatic

◆ zeros() [1/3]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::zeros ( casadi_int  nrow = 1,
casadi_int  ncol = 1 
)
inlinestatic

◆ zeros() [2/3]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::zeros ( const Sparsity sp)
inlinestatic

◆ zeros() [3/3]

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::zeros ( const std::pair< casadi_int, casadi_int > &  rc)
inlinestatic

The documentation for this class was generated from the following file: