Functions | Friends
Expression tools

Functions

static ExType casadi::GenericExpression< ExType >::plus (const ExType &x, const ExType &y)
 Addition: (x,y) -> x + y. More...
 
ExType & casadi::GenericExpression< ExType >::operator+= (const ExType &y)
 
static MatType casadi::GenericMatrix< MatType >::logsumexp (const MatType &x)
 

Friends

ExType casadi::GenericExpression< ExType >::plus (const ExType &x, const ExType &y)
 
ExType casadi::GenericExpression< ExType >::operator+ (const ExType &x, const ExType &y)
 
MatType casadi::GenericMatrix< 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 casadi::GenericMatrix< MatType >::mpower (const MatType &x, const MatType &n)
 Matrix power x^n. More...
 
MatType casadi::GenericMatrix< MatType >::soc (const MatType &x, const MatType &y)
 Construct second-order-cone matrix. More...
 
MatType casadi::GenericMatrix< MatType >::mrdivide (const MatType &x, const MatType &n)
 Matrix right divide, x/A (cf. slash '/' in MATLAB) More...
 
MatType casadi::GenericMatrix< MatType >::mldivide (const MatType &x, const MatType &n)
 Matrix left divide, A\b (cf. backslash '\' in MATLAB) More...
 
std::vector< MatType > casadi::GenericMatrix< MatType >::symvar (const MatType &x)
 Get all symbolic primitives present in an expression. More...
 
MatType casadi::GenericMatrix< MatType >::sumsqr (const MatType &x)
 Calculate sum of squares: sum_ij X_ij^2. More...
 
MatType casadi::GenericMatrix< MatType >::logsumexp (const MatType &x)
 x -> log(sum_i exp(x_i)) More...
 
MatType casadi::GenericMatrix< MatType >::logsumexp (const MatType &x, const MatType &margin)
 Scaled version of logsumexp. More...
 
MatType casadi::GenericMatrix< MatType >::linspace (const MatType &a, const MatType &b, casadi_int nsteps)
 Matlab's linspace command. More...
 
MatType casadi::GenericMatrix< MatType >::cross (const MatType &a, const MatType &b, casadi_int dim=-1)
 Matlab's cross command. More...
 
MatType casadi::GenericMatrix< MatType >::skew (const MatType &a)
 Generate a skew symmetric matrix from a 3-vector. More...
 
MatType casadi::GenericMatrix< MatType >::inv_skew (const MatType &a)
 Generate the 3-vector progenitor of a skew symmetric matrix. More...
 
MatType casadi::GenericMatrix< MatType >::det (const MatType &A)
 Matrix determinant (experimental) More...
 
MatType casadi::GenericMatrix< MatType >::det (const MatType &A, const std::string &lsolver, const Dict &dict=Dict())
 Matrix determinant through a linear solver factorization (experimental) More...
 
MatType casadi::GenericMatrix< MatType >::inv_minor (const MatType &A)
 Matrix inverse by the adjugate (experimental) More...
 
MatType casadi::GenericMatrix< MatType >::inv (const MatType &A)
 Matrix inverse. More...
 
MatType casadi::GenericMatrix< MatType >::inv (const MatType &A, const std::string &lsolver, const Dict &options=Dict())
 Matrix inverse using a linear solver plugin. More...
 
MatType casadi::GenericMatrix< MatType >::trace (const MatType &x)
 Matrix trace. More...
 
MatType casadi::GenericMatrix< MatType >::tril2symm (const MatType &a)
 Convert a lower triangular matrix to a symmetric one. More...
 
MatType casadi::GenericMatrix< MatType >::triu2symm (const MatType &a)
 Convert a upper triangular matrix to a symmetric one. More...
 
MatType casadi::GenericMatrix< MatType >::norm_fro (const MatType &x)
 Frobenius norm. More...
 
MatType casadi::GenericMatrix< MatType >::norm_2 (const MatType &x)
 2-norm More...
 
MatType casadi::GenericMatrix< MatType >::norm_1 (const MatType &x)
 1-norm More...
 
MatType casadi::GenericMatrix< MatType >::norm_inf (const MatType &x)
 Infinity-norm. More...
 
MatType casadi::GenericMatrix< 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 casadi::GenericMatrix< MatType >::cumsum (const MatType &x, casadi_int axis=-1)
 Returns cumulative sum along given axis (MATLAB convention) More...
 
MatType casadi::GenericMatrix< MatType >::dot (const MatType &x, const MatType &y)
 Inner product of two matrices. More...
 
MatType casadi::GenericMatrix< MatType >::nullspace (const MatType &A)
 Computes the nullspace of a matrix A. More...
 
MatType casadi::GenericMatrix< MatType >::polyval (const MatType &p, const MatType &x)
 Evaluate a polynomial with coefficients p in x. More...
 
MatType casadi::GenericMatrix< MatType >::diag (const MatType &A)
 Get the diagonal of a matrix or construct a diagonal. More...
 
MatType casadi::GenericMatrix< MatType >::unite (const MatType &A, const MatType &B)
 Unite two matrices with non-overlapping sparsity patterns. More...
 
MatType casadi::GenericMatrix< MatType >::densify (const MatType &x)
 Make the matrix dense if not already. More...
 
MatType casadi::GenericMatrix< MatType >::densify (const MatType &x, const MatType &val)
 Make the matrix dense and assign nonzeros to a value. More...
 
MatType casadi::GenericMatrix< 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 casadi::GenericMatrix< MatType >::if_else (const MatType &cond, const MatType &if_true, const MatType &if_false, bool short_circuit=false)
 Branching on MX nodes. More...
 
MatType casadi::GenericMatrix< MatType >::conditional (const MatType &ind, const std::vector< MatType > &x, const MatType &x_default, bool short_circuit=false)
 Create a switch. More...
 
bool casadi::GenericMatrix< MatType >::depends_on (const MatType &f, const MatType &arg)
 Check if expression depends on the argument. More...
 
MatType casadi::GenericMatrix< 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 > casadi::GenericMatrix< MatType >::substitute (const std::vector< MatType > &ex, const std::vector< MatType > &v, const std::vector< MatType > &vdef)
 Substitute variables v with expressions vdef in multiple expressions ex. More...
 
void casadi::GenericMatrix< MatType >::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 casadi::GenericMatrix< MatType >::cse (const MatType &e)
 Common subexpression elimination. More...
 
std::vector< MatType > casadi::GenericMatrix< MatType >::cse (const std::vector< MatType > &e)
 Common subexpression elimination over a list of expressions. More...
 
MatType casadi::GenericMatrix< MatType >::solve (const MatType &A, const MatType &b)
 Solve a system of equations: A*x = b. More...
 
MatType casadi::GenericMatrix< MatType >::solve (const MatType &A, const MatType &b, const std::string &lsolver, const Dict &dict=Dict())
 Solve a system of equations: A*x = b, using a linear solver plugin. More...
 
MatType casadi::GenericMatrix< MatType >::linearize (const MatType &f, const MatType &x, const MatType &x0, const Dict &opts=Dict())
 Linearize an expression. More...
 
MatType casadi::GenericMatrix< MatType >::pinv (const MatType &A)
 Computes the Moore-Penrose pseudo-inverse. More...
 
MatType casadi::GenericMatrix< MatType >::pinv (const MatType &A, const std::string &lsolver, const Dict &dict=Dict())
 Computes the Moore-Penrose pseudo-inverse using a linear solver plugin. More...
 
MatType casadi::GenericMatrix< MatType >::expm_const (const MatType &A, const MatType &t)
 Calculate Matrix exponential. More...
 
MatType casadi::GenericMatrix< MatType >::expm (const MatType &A)
 Calculate Matrix exponential. More...
 
MatType casadi::GenericMatrix< MatType >::jacobian (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 Calculate Jacobian. More...
 
MatType casadi::GenericMatrix< MatType >::gradient (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 Calculate the gradient of an expression. More...
 
MatType casadi::GenericMatrix< MatType >::tangent (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 Calculate the tangent of an expression. More...
 
MatType casadi::GenericMatrix< 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 > > casadi::GenericMatrix< 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 > > casadi::GenericMatrix< 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 > casadi::GenericMatrix< MatType >::which_depends (const MatType &expr, const MatType &var, casadi_int order, bool tr)
 Find out which variables enter with some order. More...
 
Sparsity casadi::GenericMatrix< MatType >::jacobian_sparsity (const MatType &f, const MatType &x)
 Get the sparsity pattern of a jacobian. More...
 
bool casadi::GenericMatrix< MatType >::is_linear (const MatType &expr, const MatType &var)
 Is expr linear in var? More...
 
bool casadi::GenericMatrix< MatType >::is_quadratic (const MatType &expr, const MatType &var)
 Is expr quadratic in var? More...
 
void casadi::GenericMatrix< MatType >::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 casadi::GenericMatrix< MatType >::linear_coeff (const MatType &expr, const MatType &var, MatType &A, MatType &b, bool check=true)
 Recognizes linear form in vector expression. More...
 
void casadi::GenericMatrix< MatType >::extract_parametric (const MatType &expr, const MatType &par, MatType &expr_ret, std::vector< MatType > &symbols, std::vector< MatType > &parametric, const Dict &opts=Dict())
 Extract purely parametric parts from an expression graph. More...
 
void casadi::GenericMatrix< MatType >::extract_parametric (const std::vector< MatType > &expr, const MatType &par, std::vector< MatType > &expr_ret, std::vector< MatType > &symbols, std::vector< MatType > &parametric, const Dict &opts=Dict())
 
void casadi::GenericMatrix< MatType >::extract_parametric (const std::vector< MatType > &expr, const std::vector< MatType > &par, std::vector< MatType > &expr_ret, std::vector< MatType > &symbols, std::vector< MatType > &parametric, const Dict &opts=Dict())
 
void casadi::GenericMatrix< MatType >::extract_parametric (const MatType &expr, const std::vector< MatType > &par, MatType &expr_ret, std::vector< MatType > &symbols, std::vector< MatType > &parametric, const Dict &opts=Dict())
 
void casadi::GenericMatrix< MatType >::separate_linear (const MatType &expr, const MatType &sym_lin, const MatType &sym_const, MatType &expr_const, MatType &expr_lin, MatType &expr_nonlin)
 Separate an expression into subexpressions that are constant, linear and nonlinear. More...
 
void casadi::GenericMatrix< MatType >::separate_linear (const MatType &expr, const std::vector< MatType > &sym_lin, const std::vector< MatType > &sym_const, MatType &expr_const, MatType &expr_lin, MatType &expr_nonlin)
 
casadi_int casadi::GenericMatrix< MatType >::n_nodes (const MatType &A)
 Count the number of nodes in an expression graph. More...
 
MatType casadi::GenericMatrix< MatType >::simplify (const MatType &x)
 Simplify an expression. More...
 
std::string casadi::GenericMatrix< MatType >::print_operator (const MatType &xb, const std::vector< std::string > &args)
 Get a string representation for a binary MatType, using custom arguments. More...
 
void casadi::GenericMatrix< MatType >::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 casadi::GenericMatrix< MatType >::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 casadi::GenericMatrix< MatType >::repsum (const MatType &A, casadi_int n, casadi_int m=1)
 Given a repeated matrix, computes the sum of repeated parts. More...
 
Matrix< Scalar > casadi::Matrix< Scalar >::adj (const Matrix< Scalar > &A)
 Adjugate (classical adjoint) of a square matrix. More...
 
Matrix< Scalar > casadi::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 > casadi::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 casadi::Matrix< Scalar >::qr (const Matrix< Scalar > &A, Matrix< Scalar > &Q, Matrix< Scalar > &R)
 Dense QR factorization using the modified Gram-Schmidt algorithm. More...
 
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)
 Sparse direct QR factorization with Householder reflections. More...
 
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)
 Solve A*x = b (or A^T*x = b) with the factors returned by qr_sparse. More...
 
Matrix< Scalar > casadi::Matrix< Scalar >::chol (const Matrix< Scalar > &A)
 Cholesky factorization of a symmetric positive definite matrix. More...
 
void casadi::Matrix< Scalar >::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 > casadi::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 > casadi::Matrix< Scalar >::any (const Matrix< Scalar > &x)
 True if any entry equals one. More...
 
Matrix< Scalar > casadi::Matrix< Scalar >::all (const Matrix< Scalar > &x)
 True if every entry equals one. More...
 
Matrix< Scalar > casadi::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 > casadi::Matrix< Scalar >::sparsify (const Matrix< Scalar > &A, double tol=0)
 Make a matrix sparse by turning numerical zeros into structural zeros. More...
 
void casadi::Matrix< Scalar >::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 > casadi::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 > casadi::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 > casadi::Matrix< Scalar >::heaviside (const Matrix< Scalar > &x)
 Heaviside step function, elementwise. More...
 
Matrix< Scalar > casadi::Matrix< Scalar >::rectangle (const Matrix< Scalar > &x)
 Rectangle (gate, pulse, window) function, elementwise. More...
 
Matrix< Scalar > casadi::Matrix< Scalar >::triangle (const Matrix< Scalar > &x)
 Triangle (hat) function, elementwise. More...
 
Matrix< Scalar > casadi::Matrix< Scalar >::ramp (const Matrix< Scalar > &x)
 Ramp function, elementwise. More...
 
Matrix< Scalar > casadi::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 > 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)
 Multivariate Taylor series expansion with weighted degrees. More...
 
Matrix< Scalar > casadi::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 > casadi::Matrix< Scalar >::poly_roots (const Matrix< Scalar > &p)
 Roots of a polynomial given by its coefficients, in closed form. More...
 
Matrix< Scalar > casadi::Matrix< Scalar >::eig_symbolic (const Matrix< Scalar > &m)
 Eigenvalues of a square SX matrix in closed form. More...
 
Matrix< double > casadi::Matrix< Scalar >::evalf (const Matrix< Scalar > &expr)
 Evaluate a constant expression numerically, returning a DM. More...
 
MX casadi::MX::find (const MX &x)
 Find the index of the first nonzero entry of a vector. More...
 
MX casadi::MX::low (const MX &v, const MX &p, const Dict &options=Dict())
 Find the interval of a sorted grid that contains each query value. More...
 
MX casadi::MX::graph_substitute (const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
 Substitute nodes in the expression graph, by node identity. More...
 
MX casadi::MX::graph_substitute (const MX &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
 
std::vector< MX > casadi::MX::graph_substitute (const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef)
 Substitute nodes in several expressions at once, by node identity. More...
 
std::vector< MX > casadi::MX::graph_substitute (const std::vector< MX > &ex, const std::vector< MX > &v, const std::vector< MX > &vdef, bool &updated)
 
MX casadi::MX::matrix_expand (const MX &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
 Expand an MX expression into a call to an SX Function. More...
 
std::vector< MX > casadi::MX::matrix_expand (const std::vector< MX > &e, const std::vector< MX > &boundary=std::vector< MX >(), const Dict &options=Dict())
 Expand several MX expressions into one call to an SX Function. More...
 
MX casadi::MX::bspline (const MX &x, const DM &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
 Evaluate a tensor-product B-spline with fixed coefficients. More...
 
MX casadi::MX::bspline (const MX &x, const MX &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
 Evaluate a B-spline whose coefficients are an expression. More...
 
MX casadi::MX::bspline (const MX &x, const MX &coeffs, const std::vector< MX > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
 Evaluate a B-spline whose knots and coefficients are expressions. More...
 
DM casadi::MX::bspline_dual (const std::vector< double > &x, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, const Dict &opts=Dict())
 Evaluate the B-spline basis functions at given points, see MX::bspline_dual. More...
 
MX casadi::MX::convexify (const MX &H, const Dict &opts=Dict())
 Make a symmetric matrix positive definite at evaluation time. More...
 
MX casadi::MX::lift (const MX &x, const MX &x_guess)
 Lift the expression. More...
 
MX casadi::MX::inv_node (const MX &x)
 Symbolic matrix inverse node. More...
 
DM casadi::MX::evalf (const MX &expr)
 Evaluate an expression without free symbols numerically. More...
 
MX casadi::MX::stop_diff (const MX &expr, casadi_int order)
 Stop derivatives of an expression with respect to all its symbolic variables. More...
 
MX casadi::MX::no_grad (const MX &expr)
 Treat an expression as constant for differentiation (stop first derivatives) More...
 
MX casadi::MX::no_hess (const MX &expr)
 Stop second derivatives of an expression wrt all its symbolic variables. More...
 
MX casadi::MX::stop_diff (const MX &expr, const MX &var, casadi_int order)
 Stop derivatives of an expression wrt a selected set of symbolic variables. More...
 
std::vector< MX > casadi::MX::difference (const std::vector< MX > &a, const std::vector< MX > &b)
 Return all elements of a that do not occur in b, preserving order. More...
 
MatType casadi::SparsityInterface< MatType >::horzcat (const std::vector< MatType > &v)
 Concatenate a list of matrices horizontally. More...
 
MatType casadi::SparsityInterface< MatType >::vertcat (const std::vector< MatType > &v)
 Concatenate a list of matrices vertically. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::horzsplit (const MatType &x, const std::vector< casadi_int > &offset)
 Split a matrix horizontally into groups of columns. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::horzsplit (const MatType &x, casadi_int incr=1)
 Split a matrix horizontally into fixed-size groups of columns. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::horzsplit_n (const MatType &x, casadi_int n)
 Split a matrix horizontally into a given number of equal groups of columns. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::vertsplit (const MatType &x, const std::vector< casadi_int > &offset)
 Split a matrix vertically into groups of rows. More...
 
std::vector< casadi_int > casadi::SparsityInterface< MatType >::offset (const std::vector< MatType > &v, bool vert=true)
 Get the block offsets of a list of matrices. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::vertsplit (const MatType &x, casadi_int incr=1)
 Split a matrix vertically into fixed-size groups of rows. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::vertsplit_n (const MatType &x, casadi_int n)
 Split a matrix vertically into a given number of equal groups of rows. More...
 
MatType casadi::SparsityInterface< MatType >::blockcat (const std::vector< std::vector< MatType > > &v)
 Construct a matrix from a list of lists of blocks. More...
 
MatType casadi::SparsityInterface< MatType >::blockcat (const MatType &A, const MatType &B, const MatType &C, const MatType &D)
 Construct a matrix from 4 blocks. More...
 
std::vector< std::vector< MatType > > casadi::SparsityInterface< MatType >::blocksplit (const MatType &x, const std::vector< casadi_int > &vert_offset, const std::vector< casadi_int > &horz_offset)
 Split a matrix into a grid of blocks. More...
 
std::vector< std::vector< MatType > > casadi::SparsityInterface< MatType >::blocksplit (const MatType &x, casadi_int vert_incr=1, casadi_int horz_incr=1)
 Split a matrix into a grid of fixed-size blocks. More...
 
MatType casadi::SparsityInterface< MatType >::diagcat (const std::vector< MatType > &A)
 Construct a block-diagonal matrix from a list of matrices. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::diagsplit (const MatType &x, const std::vector< casadi_int > &output_offset1, const std::vector< casadi_int > &output_offset2)
 Split a matrix into diagonal blocks of given row and column boundaries. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::diagsplit (const MatType &x, const std::vector< casadi_int > &output_offset)
 Split a square matrix into square diagonal blocks of given boundaries. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::diagsplit (const MatType &x, casadi_int incr=1)
 Split a square matrix into square diagonal blocks of fixed size. More...
 
std::vector< MatType > casadi::SparsityInterface< MatType >::diagsplit (const MatType &x, casadi_int incr1, casadi_int incr2)
 Split a matrix into diagonal blocks of fixed size. More...
 
MatType casadi::SparsityInterface< MatType >::veccat (const std::vector< MatType > &x)
 Concatenate vertically after vectorizing every argument. More...
 
MatType casadi::SparsityInterface< MatType >::mtimes (const MatType &x, const MatType &y, const std::string &blas="reference")
 Matrix product of two matrices. More...
 
MatType casadi::SparsityInterface< MatType >::mtimes (const std::vector< MatType > &args, const std::string &blas="reference")
 Matrix product of n matrices. More...
 
MatType casadi::SparsityInterface< MatType >::mac (const MatType &x, const MatType &y, const MatType &z, const std::string &blas="reference")
 Multiply-accumulate operation. More...
 
MatType casadi::SparsityInterface< MatType >::transpose (const MatType &X)
 Transpose. More...
 
MatType casadi::SparsityInterface< MatType >::vec (const MatType &x)
 Reshape a matrix into a column vector. More...
 
MatType casadi::SparsityInterface< MatType >::reshape (const MatType &x, casadi_int nrow, casadi_int ncol)
 Returns a reshaped version of the matrix. More...
 
MatType casadi::SparsityInterface< MatType >::reshape (const MatType &x, std::pair< casadi_int, casadi_int > rc)
 Returns a reshaped version of the matrix, dimensions as a vector. More...
 
MatType casadi::SparsityInterface< MatType >::reshape (const MatType &x, const Sparsity &sp)
 Reshape the matrix to a given sparsity pattern. More...
 
MatType casadi::SparsityInterface< MatType >::sparsity_cast (const MatType &x, const Sparsity &sp)
 Cast matrix nonzeros to different Sparsity. More...
 
casadi_int casadi::SparsityInterface< MatType >::sprank (const MatType &x)
 Obtain the structural rank of a sparsity-pattern. More...
 
casadi_int casadi::SparsityInterface< MatType >::norm_0_mul (const MatType &x, const MatType &y)
 0-norm (nonzero count) of a Matrix-matrix product More...
 
MatType casadi::SparsityInterface< MatType >::triu (const MatType &x, bool includeDiagonal=true)
 Get the upper triangular part of a matrix. More...
 
MatType casadi::SparsityInterface< MatType >::tril (const MatType &x, bool includeDiagonal=true)
 Get the lower triangular part of a matrix. More...
 
MatType casadi::SparsityInterface< MatType >::kron (const MatType &a, const MatType &b)
 Kronecker tensor product. More...
 
MatType casadi::SparsityInterface< MatType >::repmat (const MatType &A, casadi_int n, casadi_int m=1)
 Repeat matrix A n times vertically and m times horizontally. More...
 
MatType casadi::SparsityInterface< MatType >::repmat (const MatType &A, const std::pair< casadi_int, casadi_int > &rc)
 Repeat matrix A n times vertically and m times horizontally. More...
 
MatType casadi::SparsityInterface< MatType >::horzcat (const MatType &x, const MatType &y)
 Concatenate horizontally, two matrices. More...
 
MatType casadi::SparsityInterface< MatType >::horzcat (const MatType &x, const MatType &y, const MatType &z)
 Concatenate horizontally, three matrices. More...
 
MatType casadi::SparsityInterface< MatType >::horzcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w)
 Concatenate horizontally, four matrices. More...
 
MatType casadi::SparsityInterface< MatType >::horzcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v)
 Concatenate horizontally, five matrices. More...
 
MatType casadi::SparsityInterface< MatType >::horzcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v, const MatType &u)
 Concatenate horizontally, six matrices. More...
 
MatType casadi::SparsityInterface< MatType >::vertcat (const MatType &x, const MatType &y)
 Concatenate vertically, two matrices. More...
 
MatType casadi::SparsityInterface< MatType >::vertcat (const MatType &x, const MatType &y, const MatType &z)
 Concatenate vertically, three matrices. More...
 
MatType casadi::SparsityInterface< MatType >::vertcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w)
 Concatenate vertically, four matrices. More...
 
MatType casadi::SparsityInterface< MatType >::vertcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v)
 Concatenate vertically, five matrices. More...
 
MatType casadi::SparsityInterface< MatType >::vertcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v, const MatType &u)
 Concatenate vertically, six matrices. More...
 
MatType casadi::SparsityInterface< MatType >::diagcat (const MatType &x, const MatType &y)
 Concatenate along diagonal, two matrices. More...
 
MatType casadi::SparsityInterface< MatType >::diagcat (const MatType &x, const MatType &y, const MatType &z)
 Concatenate along diagonal, three matrices. More...
 
MatType casadi::SparsityInterface< MatType >::diagcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w)
 Concatenate along diagonal, four matrices. More...
 
MatType casadi::SparsityInterface< MatType >::diagcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v)
 Concatenate along diagonal, five matrices. More...
 
MatType casadi::SparsityInterface< MatType >::diagcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v, const MatType &u)
 Concatenate along diagonal, six matrices. More...
 
MatType casadi::SparsityInterface< MatType >::sum1 (const MatType &x)
 Sum the elements of every column. More...
 
MatType casadi::SparsityInterface< MatType >::sum2 (const MatType &x)
 Sum the elements of every row. More...
 
MatType casadi::SparsityInterface< MatType >::sum (const MatType &x)
 Sum all elements. More...
 
static ExType casadi::GenericExpression< ExType >::minus (const ExType &x, const ExType &y)
 Subtraction: (x,y) -> x - y. More...
 
ExType & casadi::GenericExpression< ExType >::operator-= (const ExType &y)
 Subtraction: (x,y) -> x - y. More...
 
ExType casadi::GenericExpression< ExType >::minus (const ExType &x, const ExType &y)
 Subtraction: (x,y) -> x - y. More...
 
ExType casadi::GenericExpression< ExType >::operator- (const ExType &x, const ExType &y)
 Subtraction: (x,y) -> x - y. More...
 
static ExType casadi::GenericExpression< ExType >::times (const ExType &x, const ExType &y)
 Elementwise multiplication: (x,y) -> x .* y. More...
 
ExType & casadi::GenericExpression< ExType >::operator*= (const ExType &y)
 Elementwise multiplication: (x,y) -> x .* y. More...
 
ExType casadi::GenericExpression< ExType >::times (const ExType &x, const ExType &y)
 Elementwise multiplication: (x,y) -> x .* y. More...
 
ExType casadi::GenericExpression< ExType >::operator* (const ExType &x, const ExType &y)
 Elementwise multiplication: (x,y) -> x .* y. More...
 
static ExType casadi::GenericExpression< ExType >::rdivide (const ExType &x, const ExType &y)
 Elementwise division: (x,y) -> x ./ y. More...
 
ExType & casadi::GenericExpression< ExType >::operator/= (const ExType &y)
 Elementwise division: (x,y) -> x ./ y. More...
 
ExType casadi::GenericExpression< ExType >::rdivide (const ExType &x, const ExType &y)
 Elementwise division: (x,y) -> x ./ y. More...
 
ExType casadi::GenericExpression< ExType >::operator/ (const ExType &x, const ExType &y)
 Elementwise division: (x,y) -> x ./ y. More...
 
static ExType casadi::GenericExpression< ExType >::lt (const ExType &x, const ExType &y)
 Logical less than: (x,y) -> x < y. More...
 
ExType casadi::GenericExpression< ExType >::lt (const ExType &x, const ExType &y)
 Logical less than: (x,y) -> x < y. More...
 
ExType casadi::GenericExpression< ExType >::operator< (const ExType &x, const ExType &y)
 Logical less than: (x,y) -> x < y. More...
 
static ExType casadi::GenericExpression< ExType >::le (const ExType &x, const ExType &y)
 Logical less or equal to: (x,y) -> x <= y. More...
 
ExType casadi::GenericExpression< ExType >::le (const ExType &x, const ExType &y)
 Logical less or equal to: (x,y) -> x <= y. More...
 
ExType casadi::GenericExpression< ExType >::operator<= (const ExType &x, const ExType &y)
 Logical less or equal to: (x,y) -> x <= y. More...
 
static ExType casadi::GenericExpression< ExType >::gt (const ExType &x, const ExType &y)
 Logical greater than: (x,y) -> x > y. More...
 
ExType casadi::GenericExpression< ExType >::gt (const ExType &x, const ExType &y)
 Logical greater than: (x,y) -> x > y. More...
 
ExType casadi::GenericExpression< ExType >::operator> (const ExType &x, const ExType &y)
 Logical greater than: (x,y) -> x > y. More...
 
static ExType casadi::GenericExpression< ExType >::ge (const ExType &x, const ExType &y)
 Logical greater or equal to: (x,y) -> x >= y. More...
 
ExType casadi::GenericExpression< ExType >::ge (const ExType &x, const ExType &y)
 Logical greater or equal to: (x,y) -> x >= y. More...
 
ExType casadi::GenericExpression< ExType >::operator>= (const ExType &x, const ExType &y)
 Logical greater or equal to: (x,y) -> x >= y. More...
 
static ExType casadi::GenericExpression< ExType >::eq (const ExType &x, const ExType &y)
 Logical equal to: (x,y) -> x == y. More...
 
ExType casadi::GenericExpression< ExType >::eq (const ExType &x, const ExType &y)
 Logical equal to: (x,y) -> x == y. More...
 
ExType casadi::GenericExpression< ExType >::operator== (const ExType &x, const ExType &y)
 Logical equal to: (x,y) -> x == y. More...
 
static ExType casadi::GenericExpression< ExType >::ne (const ExType &x, const ExType &y)
 Logical not equal to: (x,y) -> x != y. More...
 
ExType casadi::GenericExpression< ExType >::ne (const ExType &x, const ExType &y)
 Logical not equal to: (x,y) -> x != y. More...
 
ExType casadi::GenericExpression< ExType >::operator!= (const ExType &x, const ExType &y)
 Logical not equal to: (x,y) -> x != y. More...
 
static ExType casadi::GenericExpression< ExType >::logic_and (const ExType &x, const ExType &y)
 Logical and More...
 
ExType casadi::GenericExpression< ExType >::logic_and (const ExType &x, const ExType &y)
 Logical and More...
 
ExType casadi::GenericExpression< ExType >::operator&& (const ExType &x, const ExType &y)
 Logical and More...
 
static ExType casadi::GenericExpression< ExType >::logic_or (const ExType &x, const ExType &y)
 Logical or More...
 
ExType casadi::GenericExpression< ExType >::logic_or (const ExType &x, const ExType &y)
 Logical or More...
 
ExType casadi::GenericExpression< ExType >::operator|| (const ExType &x, const ExType &y)
 Logical or More...
 
static ExType casadi::GenericExpression< ExType >::logic_not (const ExType &x)
 Logical not x -> !x. More...
 
ExType casadi::GenericExpression< ExType >::operator! () const
 Logical not x -> !x. More...
 
ExType casadi::GenericExpression< ExType >::logic_not (const ExType &x)
 Logical not x -> !x. More...
 
static ExType casadi::GenericExpression< ExType >::abs (const ExType &x)
 Absolute value: x -> abs(x) More...
 
ExType casadi::GenericExpression< ExType >::abs (const ExType &x)
 Absolute value: x -> abs(x) More...
 
ExType casadi::GenericExpression< ExType >::fabs (const ExType &x)
 Absolute value: x -> abs(x) More...
 
static ExType casadi::GenericExpression< ExType >::sqrt (const ExType &x)
 Square root: x -> sqrt(x) More...
 
ExType casadi::GenericExpression< ExType >::sqrt (const ExType &x)
 Square root: x -> sqrt(x) More...
 
static ExType casadi::GenericExpression< ExType >::sq (const ExType &x)
 Square: x -> x^2. More...
 
ExType casadi::GenericExpression< ExType >::sq (const ExType &x)
 Square: x -> x^2. More...
 
static ExType casadi::GenericExpression< ExType >::sin (const ExType &x)
 Sine: x -> sin(x) More...
 
ExType casadi::GenericExpression< ExType >::sin (const ExType &x)
 Sine: x -> sin(x) More...
 
static ExType casadi::GenericExpression< ExType >::cos (const ExType &x)
 Cosine: x -> cos(x) More...
 
ExType casadi::GenericExpression< ExType >::cos (const ExType &x)
 Cosine: x -> cos(x) More...
 
static ExType casadi::GenericExpression< ExType >::tan (const ExType &x)
 Tangent: x -> tan(x) More...
 
ExType casadi::GenericExpression< ExType >::tan (const ExType &x)
 Tangent: x -> tan(x) More...
 
static ExType casadi::GenericExpression< ExType >::atan (const ExType &x)
 Arc tangent: x -> atan(x) More...
 
ExType casadi::GenericExpression< ExType >::atan (const ExType &x)
 Arc tangent: x -> atan(x) More...
 
static ExType casadi::GenericExpression< ExType >::asin (const ExType &x)
 Arc sine: x -> asin(x) More...
 
ExType casadi::GenericExpression< ExType >::asin (const ExType &x)
 Arc sine: x -> asin(x) More...
 
static ExType casadi::GenericExpression< ExType >::acos (const ExType &x)
 Arc cosine: x -> acos(x) More...
 
ExType casadi::GenericExpression< ExType >::acos (const ExType &x)
 Arc cosine: x -> acos(x) More...
 
static ExType casadi::GenericExpression< ExType >::tanh (const ExType &x)
 Hyperbolic tangent: x -> tanh(x) More...
 
ExType casadi::GenericExpression< ExType >::tanh (const ExType &x)
 Hyperbolic tangent: x -> tanh(x) More...
 
static ExType casadi::GenericExpression< ExType >::sinh (const ExType &x)
 Hyperbolic sin: x -> sinh(x) More...
 
ExType casadi::GenericExpression< ExType >::sinh (const ExType &x)
 Hyperbolic sin: x -> sinh(x) More...
 
static ExType casadi::GenericExpression< ExType >::cosh (const ExType &x)
 Hyperbolic cosine: x -> cosh(x) More...
 
ExType casadi::GenericExpression< ExType >::cosh (const ExType &x)
 Hyperbolic cosine: x -> cosh(x) More...
 
static ExType casadi::GenericExpression< ExType >::atanh (const ExType &x)
 Inverse hyperbolic tangent: x -> atanh(x) More...
 
ExType casadi::GenericExpression< ExType >::atanh (const ExType &x)
 Inverse hyperbolic tangent: x -> atanh(x) More...
 
static ExType casadi::GenericExpression< ExType >::asinh (const ExType &x)
 Inverse hyperbolic sin: x -> asinh(x) More...
 
ExType casadi::GenericExpression< ExType >::asinh (const ExType &x)
 Inverse hyperbolic sin: x -> asinh(x) More...
 
static ExType casadi::GenericExpression< ExType >::acosh (const ExType &x)
 Inverse hyperbolic cosine: x -> acosh(x) More...
 
ExType casadi::GenericExpression< ExType >::acosh (const ExType &x)
 Inverse hyperbolic cosine: x -> acosh(x) More...
 
static ExType casadi::GenericExpression< ExType >::exp (const ExType &x)
 Elementwise exponential: x -> exp(x) More...
 
ExType casadi::GenericExpression< ExType >::exp (const ExType &x)
 Elementwise exponential: x -> exp(x) More...
 
static ExType casadi::GenericExpression< ExType >::log (const ExType &x)
 Natural logarithm: x -> log(x) More...
 
ExType casadi::GenericExpression< ExType >::log (const ExType &x)
 Natural logarithm: x -> log(x) More...
 
static ExType casadi::GenericExpression< ExType >::log10 (const ExType &x)
 Base-10 logarithm: x -> log10(x) More...
 
ExType casadi::GenericExpression< ExType >::log10 (const ExType &x)
 Base-10 logarithm: x -> log10(x) More...
 
static ExType casadi::GenericExpression< ExType >::log1p (const ExType &x)
 Precision variant for natural logarithm: x -> log(x+1) More...
 
ExType casadi::GenericExpression< ExType >::log1p (const ExType &x)
 Precision variant for natural logarithm: x -> log(x+1) More...
 
static ExType casadi::GenericExpression< ExType >::expm1 (const ExType &x)
 Precision variant for elementwise exponential: x -> exp(x)-1. More...
 
ExType casadi::GenericExpression< ExType >::expm1 (const ExType &x)
 Precision variant for elementwise exponential: x -> exp(x)-1. More...
 
static ExType casadi::GenericExpression< ExType >::floor (const ExType &x)
 Round down to nearest integer: x -> floor(x) More...
 
ExType casadi::GenericExpression< ExType >::floor (const ExType &x)
 Round down to nearest integer: x -> floor(x) More...
 
static ExType casadi::GenericExpression< ExType >::ceil (const ExType &x)
 Round up to nearest integer: x -> ceil(x) More...
 
ExType casadi::GenericExpression< ExType >::ceil (const ExType &x)
 Round up to nearest integer: x -> ceil(x) More...
 
static ExType casadi::GenericExpression< ExType >::erf (const ExType &x)
 Error function: x -> erf(x) More...
 
ExType casadi::GenericExpression< ExType >::erf (const ExType &x)
 Error function: x -> erf(x) More...
 
static ExType casadi::GenericExpression< ExType >::erfinv (const ExType &x)
 Inverse error function: x -> erfinv(x) More...
 
ExType casadi::GenericExpression< ExType >::erfinv (const ExType &x)
 Inverse error function: x -> erfinv(x) More...
 
static ExType casadi::GenericExpression< ExType >::sign (const ExType &x)
 Sign function: More...
 
ExType casadi::GenericExpression< ExType >::sign (const ExType &x)
 Sign function: More...
 
static ExType casadi::GenericExpression< ExType >::pow (const ExType &x, const ExType &y)
 Elementwise power: (x,y) -> x.^y. More...
 
ExType casadi::GenericExpression< ExType >::pow (const ExType &x, const ExType &y)
 Elementwise power: (x,y) -> x.^y. More...
 
static ExType casadi::GenericExpression< ExType >::mod (const ExType &x, const ExType &y)
 Remainder after division: (x,y) -> fmod(x,y) More...
 
ExType casadi::GenericExpression< ExType >::mod (const ExType &x, const ExType &y)
 Remainder after division: (x,y) -> fmod(x,y) More...
 
ExType casadi::GenericExpression< ExType >::fmod (const ExType &x, const ExType &y)
 Remainder after division: (x,y) -> fmod(x,y) More...
 
static ExType casadi::GenericExpression< ExType >::remainder (const ExType &x, const ExType &y)
 Remainder after division: (x,y) -> remainder(x,y) More...
 
ExType casadi::GenericExpression< ExType >::remainder (const ExType &x, const ExType &y)
 Remainder after division: (x,y) -> remainder(x,y) More...
 
static ExType casadi::GenericExpression< ExType >::atan2 (const ExType &y, const ExType &x)
 Two argument arc tangent: (y,x) -> atan2(y,x) More...
 
ExType casadi::GenericExpression< ExType >::atan2 (const ExType &y, const ExType &x)
 Two argument arc tangent: (y,x) -> atan2(y,x) More...
 
static ExType casadi::GenericExpression< ExType >::if_else_zero (const ExType &x, const ExType &y)
 Conditional assignment: (x,y) -> x ? y : 0. More...
 
ExType casadi::GenericExpression< ExType >::if_else_zero (const ExType &x, const ExType &y)
 Conditional assignment: (x,y) -> x ? y : 0. More...
 
static ExType casadi::GenericExpression< ExType >::fmin (const ExType &x, const ExType &y)
 Smallest of two values: (x,y) -> min(x,y) More...
 
ExType casadi::GenericExpression< ExType >::fmin (const ExType &x, const ExType &y)
 Smallest of two values: (x,y) -> min(x,y) More...
 
static ExType casadi::GenericExpression< ExType >::fmax (const ExType &x, const ExType &y)
 Largest of two values: (x,y) -> max(x,y) More...
 
ExType casadi::GenericExpression< ExType >::fmax (const ExType &x, const ExType &y)
 Largest of two values: (x,y) -> max(x,y) More...
 
static ExType casadi::GenericExpression< ExType >::copysign (const ExType &x, const ExType &y)
 Copy sign: (x,y) -> copysign(x,y) More...
 
ExType casadi::GenericExpression< ExType >::copysign (const ExType &x, const ExType &y)
 Copy sign: (x,y) -> copysign(x,y) More...
 
static ExType casadi::GenericExpression< ExType >::constpow (const ExType &x, const ExType &y)
 Elementwise power with const power: (x,y) -> x^y with y constant. More...
 
ExType casadi::GenericExpression< ExType >::constpow (const ExType &x, const ExType &y)
 Elementwise power with const power: (x,y) -> x^y with y constant. More...
 
static ExType casadi::GenericExpression< ExType >::printme (const ExType &x, const ExType &y)
 Debug printing: (x,y) -> x, printing x when evaluated. More...
 
ExType casadi::GenericExpression< ExType >::printme (const ExType &x, const ExType &y)
 Debug printing: (x,y) -> x, printing x when evaluated. More...
 
static ExType casadi::GenericExpression< ExType >::hypot (const ExType &x, const ExType &y)
 Precision variant for 2 norm: (x,y) -> sqrt(x^2+y^2) More...
 
ExType casadi::GenericExpression< ExType >::hypot (const ExType &x, const ExType &y)
 Precision variant for 2 norm: (x,y) -> sqrt(x^2+y^2) More...
 
static MatType casadi::GenericMatrix< MatType >::bilin (const MatType &A, const MatType &x, const MatType &y)
 Calculate bilinear/quadratic form x^T A y. More...
 
MatType casadi::GenericMatrix< MatType >::bilin (const MatType &A, const MatType &x, const MatType &y)
 Calculate bilinear/quadratic form x^T A y. More...
 
MatType casadi::GenericMatrix< MatType >::bilin (const MatType &A, const MatType &x)
 Calculate bilinear/quadratic form x^T A y. More...
 
static MatType casadi::GenericMatrix< MatType >::rank1 (const MatType &A, const MatType &alpha, const MatType &x, const MatType &y)
 Make a rank-1 update to a matrix A. More...
 
MatType casadi::GenericMatrix< 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 casadi::GenericMatrix< MatType >::jtimes (const MatType &ex, const MatType &arg, const MatType &v, bool tr=false, const Dict &opts=Dict())
 
static MatType casadi::GenericMatrix< MatType >::gradient (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 
static MatType casadi::GenericMatrix< MatType >::tangent (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 
static MatType casadi::GenericMatrix< MatType >::linearize (const MatType &f, const MatType &x, const MatType &x0, const Dict &opts=Dict())
 
static MatType casadi::GenericMatrix< MatType >::mpower (const MatType &x, const MatType &y)
 
static MatType casadi::GenericMatrix< MatType >::soc (const MatType &x, const MatType &y)
 
bool casadi::GenericExpression< ExType >::is_equal (const ExType &x, const ExType &y, casadi_int depth=0)
 Check if two nodes are equivalent up to a given depth. More...
 
MatType casadi::GenericMatrix< 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 casadi::GenericMatrix< 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...
 
bool casadi::GenericMatrix< MatType >::contains (const std::vector< MatType > &v, const MatType &n)
 Check if expression n is listed in v. More...
 
bool casadi::GenericMatrix< MatType >::contains_all (const std::vector< MatType > &v, const std::vector< MatType > &n)
 Check if expression n is listed in v. More...
 
bool casadi::GenericMatrix< MatType >::contains_any (const std::vector< MatType > &v, const std::vector< MatType > &n)
 Check if expression n is listed in v. More...
 
MatType casadi::GenericMatrix< MatType >::hessian (const MatType &ex, const MatType &arg, const Dict &opts=Dict())
 Hessian and (optionally) gradient. More...
 
MatType casadi::GenericMatrix< MatType >::hessian (const MatType &ex, const MatType &arg, MatType &output_g, const Dict &opts=Dict())
 Hessian and (optionally) gradient. More...
 
MatType casadi::GenericMatrix< MatType >::transform (const MatType &x, const Dict &opts=Dict())
 Apply transformation passes to an expression. More...
 
MatType casadi::GenericMatrix< MatType >::transform (const MatType &x, const std::vector< std::vector< GenericType > > &passes, const Dict &opts=Dict())
 Apply transformation passes to an expression. More...
 
std::vector< MatType > casadi::GenericMatrix< MatType >::transform (const std::vector< MatType > &x, const Dict &opts=Dict())
 Apply transformation passes to an expression. More...
 
std::vector< MatType > casadi::GenericMatrix< MatType >::transform (const std::vector< MatType > &x, const std::vector< std::vector< GenericType > > &passes, const Dict &opts=Dict())
 Apply transformation passes to an expression. More...
 
MatType casadi::GenericMatrix< MatType >::mmin (const MatType &x)
 Smallest element in a matrix. More...
 
MatType casadi::GenericMatrix< MatType >::mmax (const MatType &x)
 Largest element in a matrix. More...
 
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)
 Integrate a scalar SX expression over [a, b] with 5-point Gauss-Legendre quadrature. More...
 
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)
 Integrate a scalar SX expression over [a, b] with 5-point Gauss-Legendre quadrature. More...
 
Matrix< Scalar > casadi::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 > casadi::Matrix< Scalar >::taylor (const Matrix< Scalar > &ex, const Matrix< Scalar > &x)
 Univariate Taylor series expansion. More...
 

Detailed Description

Functions for manipulating DM, SX, MX or Sparsity

Function Documentation

◆ abs()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::abs ( const ExType &  x)
inlinestatic

Elementwise; structural zeros are preserved. The unary functions of this group operate on every entry, keep the shape and build a symbolic expression for SX and MX. Those that map 0 to 0 (abs, sqrt, sin, tan, atan, asin, sinh, tanh, asinh, atanh, floor, ceil, sign, erf, erfinv, log1p, expm1) keep the sparsity pattern; the others (cos, cosh, exp, log, log10, acos, acosh, logic_not) return a dense result. Named fabs in Python, where the built-in abs also applies.

>>> fabs(DM([-1, 0, 2]))
DM([1, 0, 2])
>>> fabs(DM.eye(2) * -1)
DM(
[[1, 00],
 [00, 1]])
>>> abs(SX.sym("x"))
SX(fabs(x))

>> abs(DM([-1; 0; 2]))
[1, 0, 2]
>> abs(-DM.eye(2))
[[1, 00],
 [00, 1]]
>> abs(SX.sym('x'))
fabs(x)

julia> abs.(ca.DM([-1, 0, 2]))
DM([1, 0, 2])
julia> abs(-ca.DM.eye(2))
DM(
[[1, 00],
 [00, 1]])
julia> abs(ca.SX.sym("x"))
SX(fabs(x))

See also
sign, copysign

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

Definition at line 925 of file generic_expression.hpp.

925  {
926  return ExType::unary(OP_FABS, x);
927  }
@ OP_FABS
Definition: calculus.hpp:71

References casadi::OP_FABS.

◆ acos()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::acos ( const ExType &  x)
inlinestatic

Elementwise; the result is dense (acos(0) == pi/2). Arguments outside [-1, 1] give NaN.

>>> acos(DM([0, 1]))
DM([1.5708, 0])

>> acos(DM([0; 1]))
[1.5708, 0]

julia> acos.(ca.DM([0, 1]))
DM([1.5708, 0])

See also
cos, asin

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

Definition at line 1261 of file generic_expression.hpp.

1261  {
1262  return ExType::unary(OP_ACOS, x);
1263  }
@ OP_ACOS
Definition: calculus.hpp:69

References casadi::OP_ACOS.

◆ acosh()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::acosh ( const ExType &  x)
inlinestatic

Elementwise; the result is dense. Defined for x >= 1, NaN below.

>>> acosh(DM([1, 2]))
DM([0, 1.31696])

>> acosh(DM([1; 2]))
[0, 1.31696]

julia> acosh.(ca.DM([1, 2]))
DM([0, 1.31696])

See also
cosh

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

Definition at line 1483 of file generic_expression.hpp.

1483  {
1484  return ExType::unary(OP_ACOSH, x);
1485  }
@ OP_ACOSH
Definition: calculus.hpp:75

References casadi::OP_ACOSH.

◆ asin()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::asin ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved. Arguments outside [-1, 1] give NaN.

>>> asin(DM([0, 1]))
DM([0, 1.5708])

>> asin(DM([0; 1]))
[0, 1.5708]

julia> asin.(ca.DM([0, 1]))
DM([0, 1.5708])

See also
sin, acos

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

Definition at line 1224 of file generic_expression.hpp.

1224  {
1225  return ExType::unary(OP_ASIN, x);
1226  }
@ OP_ASIN
Definition: calculus.hpp:69

References casadi::OP_ASIN.

◆ asinh()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::asinh ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved.

>>> asinh(DM([-1, 1]))
DM([-0.881374, 0.881374])

>> asinh(DM([-1; 1]))
[-0.881374, 0.881374]

julia> asinh.(ca.DM([-1, 1]))
DM([-0.881374, 0.881374])

See also
sinh

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

Definition at line 1446 of file generic_expression.hpp.

1446  {
1447  return ExType::unary(OP_ASINH, x);
1448  }
@ OP_ASINH
Definition: calculus.hpp:75

References casadi::OP_ASINH.

◆ atan()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::atan ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved. Result in (-pi/2, pi/2); use atan2 for a quadrant-aware angle.

>>> atan(DM([0, 1]))
DM([0, 0.785398])

>> atan(DM([0; 1]))
[0, 0.785398]

julia> atan.(ca.DM([0, 1]))
DM([0, 0.785398])

See also
atan2, tan

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

Definition at line 1187 of file generic_expression.hpp.

1187  {
1188  return ExType::unary(OP_ATAN, x);
1189  }
@ OP_ATAN
Definition: calculus.hpp:69

References casadi::OP_ATAN.

◆ atan2()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::atan2 ( const ExType &  y,
const ExType &  x 
)
inlinestatic

theta = atan2(y,x) corresponds to x = r cos(theta), y = r sin(theta)

Angle in (-pi, pi] of the point (x, y), taking the quadrant into account; the first argument is the y coordinate, as in C and numpy. Elementwise, shape rules as for plus. atan2(0, 0) is 0.

 >>> atan2(DM([1, -1, 0]), DM([-1, -1, 1]))
 DM([2.35619, -2.35619, 0])
 >>> atan2(SX.sym("y"), SX.sym("x"))
 SX(atan2(y,x))

 >> atan2(DM([1; -1; 0]), DM([-1; -1; 1]))
 [2.35619, -2.35619, 0]
 >> atan2(SX.sym('y'), SX.sym('x'))
 atan2(y,x)

 julia> atan.(ca.DM([1, -1, 0]), ca.DM([-1, -1, 1]))
 DM([2.35619, -2.35619, 0])
 julia> atan(ca.SX.sym("y"), ca.SX.sym("x"))
 SX(atan2(y,x))

See also
atan

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

Definition at line 2146 of file generic_expression.hpp.

2146  {
2147  return ExType::binary(OP_ATAN2, y, x);
2148  }
@ OP_ATAN2
Definition: calculus.hpp:76

References casadi::OP_ATAN2.

◆ atanh()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::atanh ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved. Defined for |x| < 1, infinite at +-1.

>>> atanh(DM([0, 0.5]))
DM([0, 0.549306])

>> atanh(DM([0; 0.5]))
[0, 0.549306]

julia> atanh.(ca.DM([0, 0.5]))
DM([0, 0.549306])

See also
tanh

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

Definition at line 1409 of file generic_expression.hpp.

1409  {
1410  return ExType::unary(OP_ATANH, x);
1411  }
@ OP_ATANH
Definition: calculus.hpp:75

References casadi::OP_ATANH.

◆ bilin()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::bilin ( const MatType &  A,
const MatType &  x,
const MatType &  y 
)
static

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

◆ ceil()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::ceil ( const ExType &  x)
inlinestatic

Elementwise, towards plus infinity; sparsity preserved. The derivative is zero everywhere.

>>> ceil(DM([-1.5, 1.5]))
DM([-1, 2])

>> ceil(DM([-1.5; 1.5]))
[-1, 2]

julia> ceil.(ca.DM([-1.5, 1.5]))
DM([-1, 2])

See also
floor

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

Definition at line 1789 of file generic_expression.hpp.

1789  {
1790  return ExType::unary(OP_CEIL, x);
1791  }
@ OP_CEIL
Definition: calculus.hpp:71

References casadi::OP_CEIL.

◆ constpow()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::constpow ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Same value as pow, but the exponent is assumed constant: the derivative with respect to y is taken as zero. pow itself produces this operation for a non-integer constant exponent, so it is rarely needed directly. Passing a symbolic exponent gives a wrong derivative for MX; SX falls back to pow.

>>> constpow(DM(2), 3)
DM(8)
>>> x = MX.sym("x")
>>> constpow(x, 2.5), jacobian(constpow(x, 2.5), x)
(MX(pow(x,2.5)), MX((2.5*pow(x,1.5))))

>> constpow(DM(2), 3)
8
>> x = MX.sym('x');
>> constpow(x, 2.5), jacobian(constpow(x, 2.5), x)
pow(x,2.5)
(2.5*pow(x,1.5))

julia> ca.constpow(ca.DM(2), 3)
DM(8)
julia> x = ca.MX.sym("x");
julia> ca.constpow(x, 2.5), ca.jacobian(ca.constpow(x, 2.5), x)
(pow(x,2.5), (2.5*pow(x,1.5)))

See also
pow

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

Definition at line 2467 of file generic_expression.hpp.

2467  {
2468  return ExType::binary(OP_CONSTPOW, x, y);
2469  }
@ OP_CONSTPOW
Definition: calculus.hpp:66

References casadi::OP_CONSTPOW.

◆ copysign()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::copysign ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise value with the magnitude of x and the sign of y, as C copysign; a negative zero y counts as negative. Shape rules as for plus. The derivative with respect to y is zero.

>>> copysign(DM([1, -2]), DM([-1, 1]))
DM([-1, 2])
>>> copysign(DM(3), DM(-0.0))
DM(-3)
>>> copysign(SX.sym("x"), SX.sym("y"))
SX(copysign(x,y))

>> copysign(DM([1; -2]), DM([-1; 1]))
[-1, 2]
>> copysign(DM(3), DM(-0.0))
-3
>> copysign(SX.sym('x'), SX.sym('y'))
copysign(x,y)

julia> copysign.(ca.DM([1, -2]), ca.DM([-1, 1]))
DM([-1, 2])
julia> copysign.(ca.DM(3), ca.DM(-0.0))
DM(-3)
julia> ca.copysign(ca.SX.sym("x"), ca.SX.sym("y"))
SX(copysign(x,y))

See also
sign, abs

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

Definition at line 2417 of file generic_expression.hpp.

2417  {
2418  return ExType::binary(OP_COPYSIGN, x, y);
2419  }
@ OP_COPYSIGN
Definition: calculus.hpp:71

References casadi::OP_COPYSIGN.

◆ cos()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::cos ( const ExType &  x)
inlinestatic

Elementwise; structural zeros become 1, so the result is dense.

>>> cos(DM.eye(2))
DM(
[[0.540302, 1],
 [1, 0.540302]])
>>> cos(SX.sym("s", Sparsity.diag(2)))
SX(@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]])

>> cos(DM.eye(2))
[[0.540302, 1],
 [1, 0.540302]]
>> cos(SX.sym('s', Sparsity.diag(2)))
@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]]

julia> cos.(ca.DM.eye(2))
DM(
[[0.540302, 1],
 [1, 0.540302]])
julia> cos.(ca.SX.sym("s", ca.Sparsity.diag(2)))
SX(@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]])

See also
sin, tan, acos

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

Definition at line 1112 of file generic_expression.hpp.

1112  {
1113  return ExType::unary(OP_COS, x);
1114  }
@ OP_COS
Definition: calculus.hpp:68

References casadi::OP_COS.

◆ cosh()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::cosh ( const ExType &  x)
inlinestatic

Elementwise; the result is dense (cosh(0) == 1).

>>> cosh(DM([0, 1]))
DM([1, 1.54308])

>> cosh(DM([0; 1]))
[1, 1.54308]

julia> cosh.(ca.DM([0, 1]))
DM([1, 1.54308])

See also
acosh, sinh, tanh

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

Definition at line 1372 of file generic_expression.hpp.

1372  {
1373  return ExType::unary(OP_COSH, x);
1374  }
@ OP_COSH
Definition: calculus.hpp:74

References casadi::OP_COSH.

◆ eq()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::eq ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise comparison with 0/1 result, see lt: this is value equality of the entries, not identity of expressions; use is_equal to test whether two symbolic expressions are the same. For symbolic operands x == x with the same node simplifies to 1. NaN is not equal to anything, including itself.

>>> eq(DM([1, 2, 3]), 2)
DM([0, 1, 0])
>>> DM.eye(2) == DM([[1, 0], [0, 1]])
DM(
[[1, 1],
 [1, 1]])
>>> x = SX.sym("x")
>>> x == x, x == SX.sym("x")
(SX(1), SX((x==x)))
>>> DM(float("nan")) == DM(float("nan"))
DM(0)

>> eq(DM([1; 2; 3]), 2)
[0, 1, 0]
>> DM.eye(2) == DM([1 0; 0 1])
[[1, 1],
 [1, 1]]
>> x = SX.sym('x');
>> x == x, x == SX.sym('x')
1
(x==x)
>> DM(nan) == DM(nan)
0

julia> ca.eq(ca.DM([1, 2, 3]), 2)
DM([0, 1, 0])
julia> ca.DM.eye(2) == ca.DM([1 0; 0 1])
DM(
[[1, 1],
 [1, 1]])
julia> x = ca.SX.sym("x");
julia> x == x, x == ca.SX.sym("x")
(1, (x==x))
julia> ca.DM(NaN) == ca.DM(NaN)
DM(0)

See also
ne, is_equal

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

Definition at line 641 of file generic_expression.hpp.

641  {
642  return ExType::binary(OP_EQ, x, y);
643  }
@ OP_EQ
Definition: calculus.hpp:70

References casadi::OP_EQ.

◆ erf()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::erf ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved (erf(0) == 0). Range (-1, 1).

>>> erf(DM([0, 1]))
DM([0, 0.842701])

>> erf(DM([0; 1]))
[0, 0.842701]

julia> ca.erf(ca.DM([0, 1]))
DM([0, 0.842701])

See also
erfinv

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

Definition at line 1826 of file generic_expression.hpp.

1826  {
1827  return ExType::unary(OP_ERF, x);
1828  }
@ OP_ERF
Definition: calculus.hpp:72

References casadi::OP_ERF.

◆ erfinv()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::erfinv ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved. Defined on [-1, 1] with erfinv(+-1) infinite; arguments outside give NaN.

>>> erfinv(DM([0, 0.5, 1]))
DM([0, 0.476936, inf])

>> erfinv(DM([0; 0.5; 1]))
[0, 0.476936, inf]

julia> ca.erfinv(ca.DM([0, 0.5, 1]))
DM([0, 0.476936, inf])

See also
erf

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

Definition at line 1864 of file generic_expression.hpp.

1864  {
1865  return ExType::unary(OP_ERFINV, x);
1866  }
@ OP_ERFINV
Definition: calculus.hpp:189

References casadi::OP_ERFINV.

◆ exp()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::exp ( const ExType &  x)
inlinestatic

Elementwise; the result is dense (exp(0) == 1).

>>> exp(DM.eye(2))
DM(
[[2.71828, 1],
 [1, 2.71828]])
>>> exp(MX.sym("m", Sparsity.diag(2)))
MX(exp(dense(m)))

>> exp(DM.eye(2))
[[2.71828, 1],
 [1, 2.71828]]
>> exp(MX.sym('m', Sparsity.diag(2)))
exp(dense(m))

julia> exp.(ca.DM.eye(2))
DM(
[[2.71828, 1],
 [1, 2.71828]])
julia> exp.(ca.MX.sym("m", ca.Sparsity.diag(2)))
MX(exp(dense(m)))

See also
log, expm1

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

Definition at line 1531 of file generic_expression.hpp.

1531  {
1532  return ExType::unary(OP_EXP, x);
1533  }
@ OP_EXP
Definition: calculus.hpp:66

References casadi::OP_EXP.

◆ expm1()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::expm1 ( const ExType &  x)
inlinestatic

Elementwise; accurate for small x where exp(x) - 1 would lose digits. Sparsity preserved (expm1(0) == 0).

>>> expm1(DM([0, 1e-10]))
DM([0, 1e-10])

>> expm1(DM([0; 1e-10]))
[0, 1e-10]

julia> expm1.(ca.DM([0, 1e-10]))
DM([0, 1e-10])

See also
exp, log1p

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

Definition at line 1701 of file generic_expression.hpp.

1701  {
1702  return ExType::unary(OP_EXPM1, x);
1703  }
@ OP_EXPM1
Definition: calculus.hpp:204

References casadi::OP_EXPM1.

◆ floor()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::floor ( const ExType &  x)
inlinestatic

Elementwise, towards minus infinity; sparsity preserved. The derivative is zero everywhere. Floor division x // y in Python is floor(x / y).

>>> floor(DM([-1.5, 1.5]))
DM([-2, 1])
>>> DM([7, -7]) // 2
DM([3, -4])
>>> SX.sym("x") // 2
SX(floor((x/2)))

>> floor(DM([-1.5; 1.5]))
[-2, 1]
>> floor(DM([7; -7]) ./ 2)
[3, -4]
>> floor(SX.sym('x') / 2)
floor((x/2))

julia> floor.(ca.DM([-1.5, 1.5]))
DM([-2, 1])
julia> fld.(ca.DM([7, -7]), 2)
DM([3, -4])
julia> floor(ca.SX.sym("x") / 2)
SX(floor((x/2)))

See also
ceil, sign

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

Definition at line 1751 of file generic_expression.hpp.

1751  {
1752  return ExType::unary(OP_FLOOR, x);
1753  }
@ OP_FLOOR
Definition: calculus.hpp:71

References casadi::OP_FLOOR.

◆ fmax()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::fmax ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise maximum; shape rules as for plus. Follows C fmax: when one operand is NaN the other is returned. The derivative is 1 with respect to the larger operand (split evenly on ties). For the maximum over the entries of one matrix use mmax.

>>> fmax(DM([1, 5]), DM([3, 2]))
DM([3, 5])
>>> fmax(DM([1, 5]), 3)
DM([3, 5])
>>> fmax(DM([1, float("nan")]), DM([float("nan"), 2]))
DM([1, 2])
>>> fmax(MX.sym("x"), MX.sym("y"))
MX(fmax(x,y))

>> fmax(DM([1; 5]), DM([3; 2]))
[3, 5]
>> max(DM([1; 5]), 3)
[3, 5]
>> fmax(DM([1; nan]), DM([nan; 2]))
[1, 2]
>> max(MX.sym('x'), MX.sym('y'))
fmax(x,y)

julia> max.(ca.DM([1, 5]), ca.DM([3, 2]))
DM([3, 5])
julia> max.(ca.DM([1, 5]), 3)
DM([3, 5])
julia> max.(ca.DM([1, NaN]), ca.DM([NaN, 2]))
DM([1, 2])
julia> max(ca.MX.sym("x"), ca.MX.sym("y"))
MX(fmax(x,y))

See also
fmin, mmax

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

Definition at line 2289 of file generic_expression.hpp.

2289  {
2290  return ExType::binary(OP_FMAX, x, y);
2291  }
@ OP_FMAX
Definition: calculus.hpp:72

References casadi::OP_FMAX.

◆ fmin()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::fmin ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise minimum; shape rules as for plus. Follows C fmin: when one operand is NaN the other is returned, NaN results only when both are NaN. The derivative is 1 with respect to the smaller operand (split evenly on ties). The sparsity pattern is kept where both operands are structurally zero; fmin with a negative scalar densifies. For the minimum over the entries of one matrix use mmin.

>>> fmin(DM([1, 5]), DM([3, 2]))
DM([1, 2])
>>> fmin(DM([1, float("nan")]), DM([float("nan"), 2]))
DM([1, 2])
>>> fmin(DM.eye(2), 1)
DM(
[[1, 00],
 [00, 1]])
>>> fmin(SX.sym("x"), SX.sym("y"))
SX(fmin(x,y))

>> fmin(DM([1; 5]), DM([3; 2]))
[1, 2]
>> min(DM([1; 5]), DM([3; 2]))
[1, 2]
>> fmin(DM([1; nan]), DM([nan; 2]))
[1, 2]
>> fmin(DM.eye(2), 1)
[[1, 00],
 [00, 1]]
>> min(SX.sym('x'), SX.sym('y'))
fmin(x,y)

julia> min.(ca.DM([1, 5]), ca.DM([3, 2]))
DM([1, 2])
julia> min.(ca.DM([1, NaN]), ca.DM([NaN, 2]))
DM([1, 2])
julia> min.(ca.DM.eye(2), 1)
DM(
[[1, 00],
 [00, 1]])
julia> min(ca.SX.sym("x"), ca.SX.sym("y"))
SX(fmin(x,y))

See also
fmax, mmin

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

Definition at line 2232 of file generic_expression.hpp.

2232  {
2233  return ExType::binary(OP_FMIN, x, y);
2234  }
@ OP_FMIN
Definition: calculus.hpp:72

References casadi::OP_FMIN.

◆ ge()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::ge ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise comparison with 0/1 result, see lt. Implemented as y <= x.

>>> ge(DM([1, 2, 3]), 2)
DM([0, 1, 1])
>>> ge(MX.sym("x"), MX.sym("y"))
MX((y<=x))

>> ge(DM([1; 2; 3]), 2)
[0, 1, 1]
>> MX.sym('x') >= MX.sym('y')
(y<=x)

julia> ca.ge(ca.DM([1, 2, 3]), 2)
DM([0, 1, 1])
julia> ca.ge(ca.MX.sym("x"), ca.MX.sym("y"))
MX((y<=x))

See also
lt

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

Definition at line 571 of file generic_expression.hpp.

571  {
572  return ExType::le(y, x);
573  }

◆ gradient()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::gradient ( const MatType &  ex,
const MatType &  arg,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined here

◆ gt()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::gt ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise comparison with 0/1 result, see lt. Implemented as y < x, which is how a symbolic expression prints.

>>> gt(DM([1, 2, 3]), 2)
DM([0, 0, 1])
>>> gt(SX.sym("x"), SX.sym("y"))
SX((y<x))

>> gt(DM([1; 2; 3]), 2)
[0, 0, 1]
>> SX.sym('x') > SX.sym('y')
(y<x)

julia> ca.gt(ca.DM([1, 2, 3]), 2)
DM([0, 0, 1])
julia> ca.gt(ca.SX.sym("x"), ca.SX.sym("y"))
SX((y<x))

See also
lt

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

Definition at line 525 of file generic_expression.hpp.

525  {
526  return ExType::lt(y, x);
527  }

◆ hypot()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::hypot ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise; avoids the overflow and underflow of forming the squares, as C hypot. Shape rules as for plus; the result is structurally zero where both operands are.

>>> hypot(DM(3), DM(4))
DM(5)
>>> hypot(DM(1e200), DM(1e200))
DM(1.41421e+200)
>>> hypot(SX.sym("x"), SX.sym("y"))
SX(hypot(x,y))

>> hypot(DM(3), DM(4))
5
>> hypot(DM(1e200), DM(1e200))
1.41421e+200
>> hypot(SX.sym('x'), SX.sym('y'))
hypot(x,y)

julia> hypot(ca.DM(3), ca.DM(4))
DM(5)
julia> hypot(ca.DM(1e200), ca.DM(1e200))
DM(1.41421e+200)
julia> hypot(ca.SX.sym("x"), ca.SX.sym("y"))
SX(hypot(x,y))

See also
norm_2

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

Definition at line 2565 of file generic_expression.hpp.

2565  {
2566  return ExType::binary(OP_HYPOT, x, y);
2567  }
@ OP_HYPOT
Definition: calculus.hpp:206

References casadi::OP_HYPOT.

◆ if_else_zero()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::if_else_zero ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise: y where x is nonzero, 0 elsewhere; the result is structurally zero wherever either operand is. The value of y in the inactive branch does not propagate, so a NaN or inf there yields 0 rather than NaN, and the derivatives are treated accordingly. This is the building block of if_else; it is not exposed as a function in Python, use if_else(x, y, 0) there.

See also
if_else

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

Definition at line 2166 of file generic_expression.hpp.

2166  {
2167  return ExType::binary(OP_IF_ELSE_ZERO, x, y);
2168  }
@ OP_IF_ELSE_ZERO
Definition: calculus.hpp:71

References casadi::OP_IF_ELSE_ZERO.

◆ jtimes()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::jtimes ( const MatType &  ex,
const MatType &  arg,
const MatType &  v,
bool  tr = false,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined here

◆ le()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::le ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise comparison with 0/1 result, see lt. Chained inequalities such as lb <= (x <= ub) are recognised by Opti as bound constraints.

>>> le(DM([1, 2, 3]), 2)
DM([1, 1, 0])
>>> x = SX.sym("x")
>>> x <= x
SX(1)

>> le(DM([1; 2; 3]), 2)
[1, 1, 0]
>> x = SX.sym('x');
>> x <= x
1

julia> ca.le(ca.DM([1, 2, 3]), 2)
DM([1, 1, 0])
julia> x = ca.SX.sym("x");
julia> x <= x
SX(1)

See also
lt

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

Definition at line 478 of file generic_expression.hpp.

478  {
479  return ExType::binary(OP_LE, x, y);
480  }
@ OP_LE
Definition: calculus.hpp:70

References casadi::OP_LE.

◆ linearize()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::linearize ( const MatType &  f,
const MatType &  x,
const MatType &  x0,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined here

◆ log()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::log ( const ExType &  x)
inlinestatic

Elementwise; the result is dense with -inf at structural zeros. Negative arguments give NaN; no error is raised.

>>> log(DM([1, 0]))
DM([0, -inf])
>>> log(SX.sym("x"))
SX(log(x))

>> log(DM([1; 0]))
[0, -inf]
>> log(SX.sym('x'))
log(x)

julia> log.(ca.DM([1, 0]))
DM([0, -inf])
julia> log(ca.SX.sym("x"))
SX(log(x))

See also
exp, log10, log1p

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

Definition at line 1575 of file generic_expression.hpp.

1575  {
1576  return ExType::unary(OP_LOG, x);
1577  }
@ OP_LOG
Definition: calculus.hpp:66

References casadi::OP_LOG.

Referenced by casadi::GenericExpression< ExType >::log10().

◆ log10()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::log10 ( const ExType &  x)
inlinestatic

Computed as log(x) * (1 / log(10)); there is no dedicated operation, which is visible in symbolic expressions. Dense result like log.

>>> log10(DM([1, 10, 1000]))
DM([0, 1, 3])
>>> log10(SX.sym("x"))
SX((0.434294*log(x)))

>> log10(DM([1; 10; 1000]))
[0, 1, 3]
>> log10(SX.sym('x'))
(0.434294*log(x))

julia> log10.(ca.DM([1, 10, 1000]))
DM([0, 1, 3])
julia> log10(ca.SX.sym("x"))
SX((0.434294*log(x)))

See also
log

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

Definition at line 1619 of file generic_expression.hpp.

1619  {
1620  return log(x)*(1/std::log(10.));
1621  }
static ExType log(const ExType &x)
Natural logarithm: x -> log(x)

References casadi::GenericExpression< ExType >::log().

◆ log1p()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::log1p ( const ExType &  x)
inlinestatic

Elementwise; accurate for small x where log(1 + x) would lose digits. Sparsity preserved (log1p(0) == 0).

>>> log1p(DM([0, 1e-10]))
DM([0, 1e-10])
>>> log1p(SX.sym("x"))
SX(log1p(x))

>> log1p(DM([0; 1e-10]))
[0, 1e-10]
>> log1p(SX.sym('x'))
log1p(x)

julia> log1p.(ca.DM([0, 1e-10]))
DM([0, 1e-10])
julia> log1p(ca.SX.sym("x"))
SX(log1p(x))

See also
log, expm1

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

Definition at line 1663 of file generic_expression.hpp.

1663  {
1664  return ExType::unary(OP_LOG1P, x);
1665  }
@ OP_LOG1P
Definition: calculus.hpp:202

References casadi::OP_LOG1P.

◆ logic_and()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::logic_and ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Returns (an expression evaluating to) 1 where both entries are nonzero and 0 otherwise, elementwise

Any nonzero value, including NaN, counts as true. Shape rules as for plus; the result is structurally zero wherever either operand is. The Python keyword and does not work on matrices (it needs a single truth value); use this function.

 >>> logic_and(DM([1, 2, 0]), DM([3, 0, 0]))
 DM([1, 0, 0])
 >>> logic_and(SX.sym("x"), SX.sym("y"))
 SX((x&&y))
 >>> logic_and(DM.eye(2), DM.ones(2, 2))
 DM(
 [[1, 00],
  [00, 1]])

 >> DM([1; 2; 0]) & DM([3; 0; 0])
 [1, 0, 0]
 >> SX.sym('x') & SX.sym('y')
 (x&&y)
 >> and(DM.eye(2), DM.ones(2, 2))
 [[1, 00],
  [00, 1]]

 julia> ca.logic_and(ca.DM([1, 2, 0]), ca.DM([3, 0, 0]))
 DM([1, 0, 0])
 julia> ca.logic_and(ca.SX.sym("x"), ca.SX.sym("y"))
 SX((x&&y))
 julia> ca.logic_and(ca.DM.eye(2), ca.DM.ones(2, 2))
 DM(
 [[1, 00],
  [00, 1]])

See also
logic_or, logic_not

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

Definition at line 753 of file generic_expression.hpp.

753  {
754  return ExType::binary(OP_AND, x, y);
755  }
@ OP_AND
Definition: calculus.hpp:70

References casadi::OP_AND.

◆ logic_not()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::logic_not ( const ExType &  x)
inlinestatic

Returns (an expression evaluating to) 1 where the entry is zero and 0 otherwise, elementwise

Structural zeros map to 1, so a sparse input gives a dense result. The Python keyword not does not work on matrices; use this function.

>>> logic_not(DM([1, 0, -1]))
DM([0, 1, 0])
>>> logic_not(DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
>>> logic_not(SX.sym("x"))
SX((!x))

>> ~DM([1; 0; -1])
[0, 1, 0]
>> not(DM.eye(2))
[[0, 1],
 [1, 0]]
>> ~SX.sym('x')
(!x)

julia> ca.logic_not(ca.DM([1, 0, -1]))
DM([0, 1, 0])
julia> ca.logic_not(ca.DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
julia> ca.logic_not(ca.SX.sym("x"))
SX((!x))

See also
logic_and, logic_or

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

Definition at line 863 of file generic_expression.hpp.

863  {
864  return ExType::unary(OP_NOT, x);
865  }
@ OP_NOT
Definition: calculus.hpp:70

References casadi::OP_NOT.

Referenced by casadi::GenericExpression< ExType >::operator!().

◆ logic_or()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::logic_or ( const ExType &  x,
const ExType &  y 
)
inlinestatic

returns (an expression evaluating to) 1 where at least one entry is nonzero and 0 otherwise, elementwise

Shape rules as for plus; the result is structurally zero where both operands are.

 >>> logic_or(DM([1, 0, 0]), DM([0, 0, 2]))
 DM([1, 0, 1])
 >>> logic_or(MX.sym("x"), MX.sym("y"))
 MX((x||y))

 >> DM([1; 0; 0]) | DM([0; 0; 2])
 [1, 0, 1]
 >> or(MX.sym('x'), MX.sym('y'))
 (x||y)

 julia> ca.logic_or(ca.DM([1, 0, 0]), ca.DM([0, 0, 2]))
 DM([1, 0, 1])
 julia> ca.logic_or(ca.MX.sym("x"), ca.MX.sym("y"))
 MX((x||y))

See also
logic_and, logic_not

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

Definition at line 802 of file generic_expression.hpp.

802  {
803  return ExType::binary(OP_OR, x, y);
804  }
@ OP_OR
Definition: calculus.hpp:70

References casadi::OP_OR.

◆ logsumexp()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::logsumexp ( const MatType &  x)
static

◆ lt()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::lt ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise comparison; shape rules as for plus. Every comparison in this group returns a matrix of the same type as the operands with entries 1 (true) and 0 (false): for DM a numeric 0/1 matrix, not a boolean, and for SX and MX a symbolic expression that is only decided at evaluation time. Comparisons with NaN are false. The result is sparse where both operands are structurally zero (0 < 0 is false).

>>> lt(DM([1, 2, 3]), 2)
DM([1, 0, 0])
>>> DM([1, 2, 3]) < DM([3, 2, 1])
DM([1, 0, 0])
>>> SX.sym("x", 2) < SX.sym("y", 2)
SX([(x_0<y_0), (x_1<y_1)])
>>> DM.eye(2) < 1
DM(
[[0, 1],
 [1, 0]])

>> lt(DM([1; 2; 3]), 2)
[1, 0, 0]
>> DM([1; 2; 3]) < DM([3; 2; 1])
[1, 0, 0]
>> SX.sym('x', 2) < SX.sym('y', 2)
[(x_0<y_0), (x_1<y_1)]
>> DM.eye(2) < 1
[[0, 1],
 [1, 0]]

julia> ca.lt(ca.DM([1, 2, 3]), 2)
DM([1, 0, 0])
julia> ca.DM([1, 2, 3]) < ca.DM([3, 2, 1])
DM([1, 0, 0])
julia> ca.SX.sym("x", 2) < ca.SX.sym("y", 2)
SX([(x_0<y_0), (x_1<y_1)])
julia> ca.DM.eye(2) < 1
DM(
[[0, 1],
 [1, 0]])

See also
le, gt, ge, eq, ne, if_else

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

Definition at line 428 of file generic_expression.hpp.

428  {
429  return ExType::binary(OP_LT, x, y);
430  }
@ OP_LT
Definition: calculus.hpp:70

References casadi::OP_LT.

◆ minus()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::minus ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise difference; shape and sparsity rules as for plus. For symbolic arguments, x - x with the same node simplifies to 0.

>>> minus(DM([5, 6]), DM([1, 2]))
DM([4, 4])
>>> DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
>>> x = SX.sym("x")
>>> x - x
SX(0)

>> minus(DM([5; 6]), DM([1; 2]))
[4, 4]
>> DM.eye(2) - 1
[[0, -1],
 [-1, 0]]
>> x = SX.sym('x');
>> x - x
0

julia> ca.minus(ca.DM([5, 6]), ca.DM([1, 2]))
DM([4, 4])
julia> ca.DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
julia> x = ca.SX.sym("x");
julia> x - x
SX(0)

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

Definition at line 221 of file generic_expression.hpp.

221  {
222  return ExType::binary(OP_SUB, x, y);
223  }
@ OP_SUB
Definition: calculus.hpp:65

References casadi::OP_SUB.

◆ mod()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::mod ( const ExType &  x,
const ExType &  y 
)
inlinestatic

This Function follows the convention of https://en.cppreference.com/w/c/numeric/math/fmod: the result has the sign of x and magnitude below |y|.

This is equivalent to Python's numpy.fmod and Matlab's rem. It differs from the Python operator x % y on these objects, which follows the Python convention x - y*floor(x/y) (result with the sign of y). Elementwise, shape rules as for plus; structural zeros of x stay zero. A zero divisor gives NaN.

>>> fmod(DM([5, 5, -5, -5]), DM([3, -3, 3, -3]))
DM([2, 2, -2, -2])
>>> DM([5, 5, -5, -5]) % DM([3, -3, 3, -3])
DM([2, -1, 1, -2])
>>> fmod(SX.sym("x"), 2), SX.sym("x") % 2
(SX(fmod(x,2)), SX((x-(2.*floor((x/2))))))

>> x = DM([5; 5; -5; -5]); y = DM([3; -3; 3; -3]);
>> fmod(x, y)
[2, 2, -2, -2]
>> rem(x, y)
[2, 2, -2, -2]
>> x - y .* floor(x ./ y)
[2, -1, 1, -2]
>> rem(SX.sym('x'), 2)
fmod(x,2)

julia> ca.fmod(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([2, 2, -2, -2])
julia> mod.(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([2, -1, 1, -2])
julia> ca.fmod(ca.SX.sym("x"), 2)
SX(fmod(x,2))

See also
remainder, floor
Extra doc: https://github.com/casadi/casadi/wiki/L_pq 

Definition at line 2036 of file generic_expression.hpp.

2036  {
2037  return ExType::binary(OP_FMOD, x, y);
2038  }
@ OP_FMOD
Definition: calculus.hpp:71

References casadi::OP_FMOD.

◆ mpower()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::mpower ( const MatType &  x,
const MatType &  y 
)
static

Functions called by friend functions defined here

◆ ne()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::ne ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise comparison with 0/1 result, see lt and eq. For symbolic operands x != x with the same node simplifies to 0, so it cannot be used to detect NaN symbolically.

>>> ne(DM([1, 2, 3]), 2)
DM([1, 0, 1])
>>> x = MX.sym("x")
>>> x != x
MX(0)

>> ne(DM([1; 2; 3]), 2)
[1, 0, 1]
>> x = MX.sym('x');
>> x ~= x
0

julia> ca.ne(ca.DM([1, 2, 3]), 2)
DM([1, 0, 1])
julia> x = ca.MX.sym("x");
julia> x != x
MX(0)

See also
eq

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

Definition at line 691 of file generic_expression.hpp.

691  {
692  return ExType::binary(OP_NE, x, y);
693  }
@ OP_NE
Definition: calculus.hpp:70

References casadi::OP_NE.

◆ operator!()

template<typename ExType >
ExType casadi::GenericExpression< ExType >::operator! ( ) const
inline

Returns (an expression evaluating to) 1 where the entry is zero and 0 otherwise, elementwise

Structural zeros map to 1, so a sparse input gives a dense result. The Python keyword not does not work on matrices; use this function.

>>> logic_not(DM([1, 0, -1]))
DM([0, 1, 0])
>>> logic_not(DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
>>> logic_not(SX.sym("x"))
SX((!x))

>> ~DM([1; 0; -1])
[0, 1, 0]
>> not(DM.eye(2))
[[0, 1],
 [1, 0]]
>> ~SX.sym('x')
(!x)

julia> ca.logic_not(ca.DM([1, 0, -1]))
DM([0, 1, 0])
julia> ca.logic_not(ca.DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
julia> ca.logic_not(ca.SX.sym("x"))
SX((!x))

See also
logic_and, logic_or

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

Definition at line 869 of file generic_expression.hpp.

869  {
870  return logic_not(self());
871  }
static ExType logic_not(const ExType &x)
Logical not x -> !x.

References casadi::GenericExpression< ExType >::logic_not().

◆ operator*=()

template<typename ExType >
ExType& casadi::GenericExpression< ExType >::operator*= ( const ExType &  y)
inline

Elementwise (Hadamard) product, not the matrix product; use mtimes for that. Shape rules as for plus. The result is structurally zero wherever either operand is, so the sparsity pattern is the intersection of the patterns.

>>> times(DM([1, 2]), DM([3, 4]))
DM([3, 8])
>>> DM([[1, 2], [3, 4]]) * DM([[1, 2], [3, 4]])
DM(
[[1, 4],
 [9, 16]])
>>> DM.eye(2) * DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

>> times(DM([1; 2]), DM([3; 4]))
[3, 8]
>> DM([1 2; 3 4]) .* DM([1 2; 3 4])
[[1, 4],
 [9, 16]]
>> DM([1 2; 3 4]) * DM([1 2; 3 4])
[[7, 10],
 [15, 22]]
>> DM.eye(2) .* DM.ones(2, 2)
[[1, 00],
 [00, 1]]

julia> ca.times(ca.DM([1, 2]), ca.DM([3, 4]))
DM([3, 8])
julia> ca.DM([1 2; 3 4]) .* ca.DM([1 2; 3 4])
DM(
[[1, 4],
 [9, 16]])
julia> ca.DM.eye(2) .* ca.DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

See also
mtimes

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

Definition at line 298 of file generic_expression.hpp.

298 {return self() = self() * y;}

◆ operator+=()

template<typename ExType >
ExType& casadi::GenericExpression< ExType >::operator+= ( const ExType &  y)
inline

Definition at line 170 of file generic_expression.hpp.

170 { return self() = self() + y; }

◆ operator-=()

template<typename ExType >
ExType& casadi::GenericExpression< ExType >::operator-= ( const ExType &  y)
inline

Elementwise difference; shape and sparsity rules as for plus. For symbolic arguments, x - x with the same node simplifies to 0.

>>> minus(DM([5, 6]), DM([1, 2]))
DM([4, 4])
>>> DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
>>> x = SX.sym("x")
>>> x - x
SX(0)

>> minus(DM([5; 6]), DM([1; 2]))
[4, 4]
>> DM.eye(2) - 1
[[0, -1],
 [-1, 0]]
>> x = SX.sym('x');
>> x - x
0

julia> ca.minus(ca.DM([5, 6]), ca.DM([1, 2]))
DM([4, 4])
julia> ca.DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
julia> x = ca.SX.sym("x");
julia> x - x
SX(0)

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

Definition at line 230 of file generic_expression.hpp.

230 { return self() = self() - y; }

◆ operator/=()

template<typename ExType >
ExType& casadi::GenericExpression< ExType >::operator/= ( const ExType &  y)
inline

Elementwise quotient, not a linear solve; use solve or mldivide for that. Shape rules as for plus. Division by zero follows IEEE arithmetic (inf or nan), no error is raised. Structural zeros of x stay structural zeros; dividing by a sparse y densifies.

>>> rdivide(DM([1, 2]), 4)
DM([0.25, 0.5])
>>> DM([1, 2]) / DM([4, 8])
DM([0.25, 0.25])
>>> 1 / DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
>>> DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

>> rdivide(DM([1; 2]), 4)
[0.25, 0.5]
>> DM([1; 2]) ./ DM([4; 8])
[0.25, 0.25]
>> 1 ./ DM.eye(2)
[[1, inf],
 [inf, 1]]
>> DM.eye(2) ./ 2
[[0.5, 00],
 [00, 0.5]]

julia> ca.rdivide(ca.DM([1, 2]), 4)
DM([0.25, 0.5])
julia> ca.DM([1, 2]) ./ ca.DM([4, 8])
DM([0.25, 0.25])
julia> 1 ./ ca.DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
julia> ca.DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

See also
mldivide, mrdivide, solve

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

Definition at line 369 of file generic_expression.hpp.

369 {return self() = self() / y;}

◆ plus()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::plus ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise sum. The shape rules below hold for every binary operation in this group (minus, times, rdivide, the comparisons, logic_and, ...):

  • both operands have the same shape, or
  • one operand is a scalar (1-by-1), which is combined with every element, or
  • the operands have the same number of rows and the column count of one is a multiple of the other's; the narrower one is repeated horizontally (so a column vector can be added to every column of a matrix, but a row vector cannot be added to every row). Any other combination raises an error; there is no further broadcasting. Empty matrices follow the same rules and give an empty result.

The result is sparse where both operands are structurally zero; an entry that is a structural zero in only one operand becomes an explicit value. Operations that map (0, y) or (x, 0) to a nonzero, such as x + 1 or cos, densify the result. Numeric (DM) operands are evaluated, symbolic ones (SX, MX) build an expression; DM and SX or MX can be mixed, SX and MX cannot.

>>> plus(DM([1, 2]), DM([10, 20]))
DM([11, 22])
>>> DM([1, 2]) + 1
DM([2, 3])
>>> DM.eye(2) + DM.eye(2)
DM(
[[2, 00],
 [00, 2]])
>>> DM.eye(2) + 1
DM(
[[2, 1],
 [1, 2]])
>>> SX.sym("x", 2, 2) + SX.sym("y", 2)
SX(
[[(x_0+y_0), (x_2+y_0)],
 [(x_1+y_1), (x_3+y_1)]])
>>> (MX.sym("x", 2, 0) + 1).shape
(2, 0)
>>> DM.ones(2, 2) + DM.ones(1, 2)
Traceback (most recent call last):
  ...
RuntimeError: ...Dimension mismatch...

>> plus(DM([1; 2]), DM([10; 20]))
[11, 22]
>> DM([1; 2]) + 1
[2, 3]
>> DM.eye(2) + DM.eye(2)
[[2, 00],
 [00, 2]]
>> DM.eye(2) + 1
[[2, 1],
 [1, 2]]
>> SX.sym('x', 2, 2) + SX.sym('y', 2)
[[(x_0+y_0), (x_2+y_0)],
 [(x_1+y_1), (x_3+y_1)]]
>> size(MX.sym('x', 2, 0) + 1)
2 0
>> try, DM.ones(2, 2) + DM.ones(1, 2); catch e, disp(e.message); end
...Dimension mismatch...

julia> ca.plus(ca.DM([1, 2]), ca.DM([10, 20]))
DM([11, 22])
julia> ca.DM([1, 2]) + 1
DM([2, 3])
julia> ca.DM.eye(2) + ca.DM.eye(2)
DM(
[[2, 00],
 [00, 2]])
julia> ca.DM.eye(2) + 1
DM(
[[2, 1],
 [1, 2]])
julia> ca.SX.sym("x", 2, 2) + ca.SX.sym("y", 2)
SX(
[[(x_0+y_0), (x_2+y_0)],
 [(x_1+y_1), (x_3+y_1)]])
julia> size(ca.MX.sym("x", 2, 0) + 1)
(2, 0)
julia> try
           ca.DM.ones(2, 2) + ca.DM.ones(1, 2)
       catch e
           println(occursin("Dimension mismatch", sprint(showerror, e)))
       end
true

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

Definition at line 161 of file generic_expression.hpp.

161  {
162  return ExType::binary(OP_ADD, x, y);
163  }
@ OP_ADD
Definition: calculus.hpp:65

References casadi::OP_ADD.

◆ pow()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::pow ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise; shape rules as for plus. Named power in Python, where the ** operator maps to it as well. A constant exponent is simplified symbolically: integer powers become repeated squaring and multiplication, 0.5 becomes sqrt, -1 a reciprocal, other constants a constpow operation. Structural zeros of x stay zero for a nonzero exponent; x**0 is dense (0**0 == 1).

>>> power(DM([1, 2, 3]), 2)
DM([1, 4, 9])
>>> DM([1, 2, 3])**DM([3, 2, 1]), 2**DM([1, 2, 3])
(DM([1, 4, 3]), DM([2, 4, 8]))
>>> x = SX.sym("x")
>>> x**2, x**3, x**-1, x**x
(SX(sq(x)), SX((x*sq(x))), SX((1./x)), SX(pow(x,x)))
>>> x**2.5
SX(pow(x,2.5))

>> power(DM([1; 2; 3]), 2)
[1, 4, 9]
>> DM([1; 2; 3]).^DM([3; 2; 1]), 2.^DM([1; 2; 3])
[1, 4, 3]
[2, 4, 8]
>> x = SX.sym('x');
>> x.^2, x.^3, x.^-1, x.^x
sq(x)
(x*sq(x))
(1./x)
pow(x,x)
>> x.^2.5
pow(x,2.5)

julia> ca.power(ca.DM([1, 2, 3]), 2)
DM([1, 4, 9])
julia> ca.DM([1, 2, 3]) .^ ca.DM([3, 2, 1]), 2 .^ ca.DM([1, 2, 3])
([1, 4, 3], [2, 4, 8])
julia> x = ca.SX.sym("x");
julia> x^2, x^3, x^-1, x^x
(sq(x), (x*sq(x)), (1./x), pow(x,x))
julia> x^2.5
SX(pow(x,2.5))

See also
sq, sqrt, constpow, mpower

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

Definition at line 1978 of file generic_expression.hpp.

1978  {
1979  return ExType::binary(OP_POW, x, y);
1980  }
@ OP_POW
Definition: calculus.hpp:66

References casadi::OP_POW.

◆ printme()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::printme ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Identity operation with a side effect: whenever the expression is evaluated inside a Function (and hence inside a solver), every nonzero of x is printed on its own line as "|> y : value" in storage (column-major) order, with y an identifier of choice to tell several printme nodes apart. Available as a method x.printme(y) in Python; the derivative is that of x. Use monitor to print a whole matrix at once.

>>> x = MX.sym("x")
>>> f = Function("f", [x], [x.printme(7) * 2])
>>> f(3)
|> 7 : 3.0000000000000000e+00
DM(6)

>> x = MX.sym('x');
>> f = Function('f', {x}, {x.printme(7) * 2});
>> f(3)
|> 7 : 3.0000000000000000e+00
6

julia> x = ca.MX.sym("x");
julia> f = ca.Function("f", [x], [ca.printme(x, 7) * 2]);
julia> f(3)
|> 7 : 3.0000000000000000e+00
DM(6)

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

Definition at line 2515 of file generic_expression.hpp.

2515  {
2516  return ExType::binary(OP_PRINTME, x, y);
2517  }
@ OP_PRINTME
Definition: calculus.hpp:190

References casadi::OP_PRINTME.

◆ rank1()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::rank1 ( const MatType &  A,
const MatType &  alpha,
const MatType &  x,
const MatType &  y 
)
static

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

◆ rdivide()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::rdivide ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise quotient, not a linear solve; use solve or mldivide for that. Shape rules as for plus. Division by zero follows IEEE arithmetic (inf or nan), no error is raised. Structural zeros of x stay structural zeros; dividing by a sparse y densifies.

>>> rdivide(DM([1, 2]), 4)
DM([0.25, 0.5])
>>> DM([1, 2]) / DM([4, 8])
DM([0.25, 0.25])
>>> 1 / DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
>>> DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

>> rdivide(DM([1; 2]), 4)
[0.25, 0.5]
>> DM([1; 2]) ./ DM([4; 8])
[0.25, 0.25]
>> 1 ./ DM.eye(2)
[[1, inf],
 [inf, 1]]
>> DM.eye(2) ./ 2
[[0.5, 00],
 [00, 0.5]]

julia> ca.rdivide(ca.DM([1, 2]), 4)
DM([0.25, 0.5])
julia> ca.DM([1, 2]) ./ ca.DM([4, 8])
DM([0.25, 0.25])
julia> 1 ./ ca.DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
julia> ca.DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

See also
mldivide, mrdivide, solve

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

Definition at line 360 of file generic_expression.hpp.

360  {
361  return ExType::binary(OP_DIV, x, y);
362  }
@ OP_DIV
Definition: calculus.hpp:65

References casadi::OP_DIV.

◆ remainder()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::remainder ( const ExType &  x,
const ExType &  y 
)
inlinestatic

This Function follows the convention of https://en.cppreference.com/w/c/numeric/math/remainder: x - n*y with n the integer nearest to x/y (ties to even), so the result lies in [-|y|/2, |y|/2].

Notably:

  • remainder(5,3) -> -1
  • remainder(5,-3) -> -1
  • remainder(-5,3) -> 1
  • remainder(-5,-3) -> 1

This is equivalent to Python's math.remainder. There is no equivalence in Matlab. Elementwise, shape rules as for plus; structural zeros of x stay zero.

>>> remainder(DM([5, 5, -5, -5]), DM([3, -3, 3, -3]))
DM([-1, -1, 1, 1])
>>> remainder(DM([2.5, 3.5]), 2)
DM([0.5, -0.5])
>>> remainder(MX.sym("x"), 2)
MX(remainder(x,2))

>> remainder(DM([5; 5; -5; -5]), DM([3; -3; 3; -3]))
[-1, -1, 1, 1]
>> remainder(DM([2.5; 3.5]), 2)
[0.5, -0.5]
>> remainder(MX.sym('x'), 2)
remainder(x,2)

julia> ca.remainder(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([-1, -1, 1, 1])
julia> ca.remainder(ca.DM([2.5, 3.5]), 2)
DM([0.5, -0.5])
julia> ca.remainder(ca.MX.sym("x"), 2)
MX(remainder(x,2))

See also
fmod
Extra doc: https://github.com/casadi/casadi/wiki/L_24x 

Definition at line 2099 of file generic_expression.hpp.

2099  {
2100  return ExType::binary(OP_REMAINDER, x, y);
2101  }
@ OP_REMAINDER
Definition: calculus.hpp:210

References casadi::OP_REMAINDER.

◆ sign()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::sign ( const ExType &  x)
inlinestatic

sign(x) := -1 for x<0 sign(x) := 1 for x>0, sign(0) := 0 sign(NaN) := NaN

Elementwise; sparsity preserved. The derivative is zero everywhere.

>>> sign(DM([-2, 0, 3]))
DM([-1, 0, 1])
>>> sign(SX.sym("x"))
SX(sign(x))

>> sign(DM([-2; 0; 3]))
[-1, 0, 1]
>> sign(SX.sym('x'))
sign(x)

julia> sign.(ca.DM([-2, 0, 3]))
DM([-1, 0, 1])
julia> sign(ca.SX.sym("x"))
SX(sign(x))

See also
abs, copysign

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

Definition at line 1912 of file generic_expression.hpp.

1912  {
1913  return ExType::unary(OP_SIGN, x);
1914  }
@ OP_SIGN
Definition: calculus.hpp:71

References casadi::OP_SIGN.

◆ sin()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::sin ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved (sin(0) == 0).

>>> sin(DM.eye(2))
DM(
[[0.841471, 00],
 [00, 0.841471]])
>>> sin(MX.sym("x", 2))
MX(sin(x))

>> sin(DM.eye(2))
[[0.841471, 00],
 [00, 0.841471]]
>> sin(MX.sym('x', 2))
sin(x)

julia> sin.(ca.DM.eye(2))
DM(
[[0.841471, 00],
 [00, 0.841471]])
julia> sin.(ca.MX.sym("x", 2))
MX(sin(x))

See also
cos, tan, asin

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

Definition at line 1058 of file generic_expression.hpp.

1058  {
1059  return ExType::unary(OP_SIN, x);
1060  }
@ OP_SIN
Definition: calculus.hpp:68

References casadi::OP_SIN.

◆ sinh()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::sinh ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved.

>>> sinh(DM([0, 1]))
DM([0, 1.1752])

>> sinh(DM([0; 1]))
[0, 1.1752]

julia> sinh.(ca.DM([0, 1]))
DM([0, 1.1752])

See also
asinh, cosh, tanh

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

Definition at line 1335 of file generic_expression.hpp.

1335  {
1336  return ExType::unary(OP_SINH, x);
1337  }
@ OP_SINH
Definition: calculus.hpp:74

References casadi::OP_SINH.

◆ soc()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::soc ( const MatType &  x,
const MatType &  y 
)
static

Functions called by friend functions defined here

◆ sq()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::sq ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved. Not exposed as a function in Python; a symbolic power with exponent 2 is turned into this operation.

>>> SX.sym("x")**2
SX(sq(x))

>> SX.sym('x').^2
sq(x)

julia> ca.SX.sym("x")^2
SX(sq(x))

See also
sqrt, pow

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

Definition at line 1010 of file generic_expression.hpp.

1010  {
1011  return ExType::unary(OP_SQ, x);
1012  }
@ OP_SQ
Definition: calculus.hpp:67

References casadi::OP_SQ.

◆ sqrt()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::sqrt ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved. A negative argument gives NaN, no error is raised. A symbolic power x**0.5 simplifies to sqrt(x).

>>> sqrt(DM([4, 9]))
DM([2, 3])
>>> SX.sym("x")**0.5
SX(sqrt(x))

>> sqrt(DM([4; 9]))
[2, 3]
>> SX.sym('x').^0.5
sqrt(x)

julia> sqrt.(ca.DM([4, 9]))
DM([2, 3])
julia> ca.SX.sym("x")^0.5
SX(sqrt(x))

See also
sq, pow

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

Definition at line 972 of file generic_expression.hpp.

972  {
973  return ExType::unary(OP_SQRT, x);
974  }
@ OP_SQRT
Definition: calculus.hpp:67

References casadi::OP_SQRT.

◆ tan()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::tan ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved.

>>> tan(DM([0, 1]))
DM([0, 1.55741])

>> tan(DM([0; 1]))
[0, 1.55741]

julia> tan.(ca.DM([0, 1]))
DM([0, 1.55741])

See also
atan, sin, cos

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

Definition at line 1149 of file generic_expression.hpp.

1149  {
1150  return ExType::unary(OP_TAN, x);
1151  }
@ OP_TAN
Definition: calculus.hpp:68

References casadi::OP_TAN.

◆ tangent()

template<typename MatType >
static MatType casadi::GenericMatrix< MatType >::tangent ( const MatType &  ex,
const MatType &  arg,
const Dict &  opts = Dict() 
)
static

Functions called by friend functions defined here

◆ tanh()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::tanh ( const ExType &  x)
inlinestatic

Elementwise; sparsity preserved.

>>> tanh(DM([0, 1]))
DM([0, 0.761594])

>> tanh(DM([0; 1]))
[0, 0.761594]

julia> tanh.(ca.DM([0, 1]))
DM([0, 0.761594])

See also
atanh, sinh, cosh

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

Definition at line 1298 of file generic_expression.hpp.

1298  {
1299  return ExType::unary(OP_TANH, x);
1300  }
@ OP_TANH
Definition: calculus.hpp:74

References casadi::OP_TANH.

◆ times()

template<typename ExType >
static ExType casadi::GenericExpression< ExType >::times ( const ExType &  x,
const ExType &  y 
)
inlinestatic

Elementwise (Hadamard) product, not the matrix product; use mtimes for that. Shape rules as for plus. The result is structurally zero wherever either operand is, so the sparsity pattern is the intersection of the patterns.

>>> times(DM([1, 2]), DM([3, 4]))
DM([3, 8])
>>> DM([[1, 2], [3, 4]]) * DM([[1, 2], [3, 4]])
DM(
[[1, 4],
 [9, 16]])
>>> DM.eye(2) * DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

>> times(DM([1; 2]), DM([3; 4]))
[3, 8]
>> DM([1 2; 3 4]) .* DM([1 2; 3 4])
[[1, 4],
 [9, 16]]
>> DM([1 2; 3 4]) * DM([1 2; 3 4])
[[7, 10],
 [15, 22]]
>> DM.eye(2) .* DM.ones(2, 2)
[[1, 00],
 [00, 1]]

julia> ca.times(ca.DM([1, 2]), ca.DM([3, 4]))
DM([3, 8])
julia> ca.DM([1 2; 3 4]) .* ca.DM([1 2; 3 4])
DM(
[[1, 4],
 [9, 16]])
julia> ca.DM.eye(2) .* ca.DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

See also
mtimes

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

Definition at line 289 of file generic_expression.hpp.

289  {
290  return ExType::binary(OP_MUL, x, y);
291  }
@ OP_MUL
Definition: calculus.hpp:65

References casadi::OP_MUL.

Friends

◆ abs

template<typename ExType >
ExType abs ( const ExType &  x)
friend

Elementwise; structural zeros are preserved. The unary functions of this group operate on every entry, keep the shape and build a symbolic expression for SX and MX. Those that map 0 to 0 (abs, sqrt, sin, tan, atan, asin, sinh, tanh, asinh, atanh, floor, ceil, sign, erf, erfinv, log1p, expm1) keep the sparsity pattern; the others (cos, cosh, exp, log, log10, acos, acosh, logic_not) return a dense result. Named fabs in Python, where the built-in abs also applies.

>>> fabs(DM([-1, 0, 2]))
DM([1, 0, 2])
>>> fabs(DM.eye(2) * -1)
DM(
[[1, 00],
 [00, 1]])
>>> abs(SX.sym("x"))
SX(fabs(x))

>> abs(DM([-1; 0; 2]))
[1, 0, 2]
>> abs(-DM.eye(2))
[[1, 00],
 [00, 1]]
>> abs(SX.sym('x'))
fabs(x)

julia> abs.(ca.DM([-1, 0, 2]))
DM([1, 0, 2])
julia> abs(-ca.DM.eye(2))
DM(
[[1, 00],
 [00, 1]])
julia> abs(ca.SX.sym("x"))
SX(fabs(x))

See also
sign, copysign

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

Definition at line 928 of file generic_expression.hpp.

928  {
929  return ExType::abs(x);
930  }

◆ acos

template<typename ExType >
ExType acos ( const ExType &  x)
friend

Elementwise; the result is dense (acos(0) == pi/2). Arguments outside [-1, 1] give NaN.

>>> acos(DM([0, 1]))
DM([1.5708, 0])

>> acos(DM([0; 1]))
[1.5708, 0]

julia> acos.(ca.DM([0, 1]))
DM([1.5708, 0])

See also
cos, asin

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

Definition at line 1264 of file generic_expression.hpp.

1264  {
1265  return ExType::acos(x);
1266  }

◆ acosh

template<typename ExType >
ExType acosh ( const ExType &  x)
friend

Elementwise; the result is dense. Defined for x >= 1, NaN below.

>>> acosh(DM([1, 2]))
DM([0, 1.31696])

>> acosh(DM([1; 2]))
[0, 1.31696]

julia> acosh.(ca.DM([1, 2]))
DM([0, 1.31696])

See also
cosh

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

Definition at line 1486 of file generic_expression.hpp.

1486  {
1487  return ExType::acosh(x);
1488  }

◆ adj

template<typename Scalar >
Matrix<Scalar> adj ( const Matrix< Scalar > &  A)
friend

Transpose of the cofactor matrix, computed by Laplace expansion. For an invertible matrix inv(A) == adj(A)/det(A). Cost grows factorially with the size; intended for small (symbolic) matrices. A structural zero cofactor stays structurally zero. A non-square input raises an error.

>>> adj(DM([[1, 2], [3, 4]]))
DM(
[[4, -2],
 [-3, 1]])
>>> X = SX.sym("X", 2, 2)
>>> adj(X)
SX(
[[X_3, (-X_2)],
 [(-X_1), X_0]])
>>> adj(DM(5)), adj(DM(Sparsity.diag(3))).nnz()
(DM(1), 3)

>> adj(DM([1 2; 3 4]))
[[4, -2],
 [-3, 1]]
>> X = SX.sym('X', 2, 2);
>> adj(X)
[[X_3, (-X_2)],
 [(-X_1), X_0]]
>> adj(DM(5)), adj(DM(Sparsity.diag(3))).nnz()
1
3

julia> ca.adj(ca.DM([1 2; 3 4]))
DM(
[[4, -2],
 [-3, 1]])
julia> X = ca.SX.sym("X", 2, 2);
julia> ca.adj(X)
SX(
[[X_3, (-X_2)],
 [(-X_1), X_0]])
julia> ca.adj(ca.DM(5)), ca.nnz(ca.adj(ca.DM(ca.Sparsity.diag(3))))
(1, 3)

See also
inv_minor, cofactor, det

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

Definition at line 1423 of file matrix_decl.hpp.

1423  {
1424  return Matrix<Scalar>::adj(A);
1425  }
static Matrix< Scalar > adj(const Matrix< Scalar > &x)

◆ all

template<typename Scalar >
Matrix<Scalar> all ( const Matrix< Scalar > &  x)
friend

Returns a 1-by-1 matrix. A matrix with structural zeros gives false. Not available in Python (shadowed by the builtin); use logic_all.

>> all(DM([1; 1])), all(DM([0; 1]))
1
0
>> all(DM(Sparsity.diag(2)))
0

See also
any, logic_all

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

Definition at line 1916 of file matrix_decl.hpp.

1916  {
1917  return Matrix<Scalar>::all(x);
1918  }
static Matrix< Scalar > all(const Matrix< Scalar > &x)

◆ any

template<typename Scalar >
Matrix<Scalar> any ( const Matrix< Scalar > &  x)
friend

Returns a 1-by-1 matrix. A matrix with structural zeros gives false. Not available in Python (shadowed by the builtin); use logic_any or sum1/sum2.

>> any(DM([0; 1])), any(DM([0; 0]))
1
0
>> any(DM(Sparsity.diag(2)))
0

See also
all, logic_any

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

Definition at line 1894 of file matrix_decl.hpp.

1894  {
1895  return Matrix<Scalar>::any(x);
1896  }
static Matrix< Scalar > any(const Matrix< Scalar > &x)

◆ asin

template<typename ExType >
ExType asin ( const ExType &  x)
friend

Elementwise; sparsity preserved. Arguments outside [-1, 1] give NaN.

>>> asin(DM([0, 1]))
DM([0, 1.5708])

>> asin(DM([0; 1]))
[0, 1.5708]

julia> asin.(ca.DM([0, 1]))
DM([0, 1.5708])

See also
sin, acos

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

Definition at line 1227 of file generic_expression.hpp.

1227  {
1228  return ExType::asin(x);
1229  }

◆ asinh

template<typename ExType >
ExType asinh ( const ExType &  x)
friend

Elementwise; sparsity preserved.

>>> asinh(DM([-1, 1]))
DM([-0.881374, 0.881374])

>> asinh(DM([-1; 1]))
[-0.881374, 0.881374]

julia> asinh.(ca.DM([-1, 1]))
DM([-0.881374, 0.881374])

See also
sinh

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

Definition at line 1449 of file generic_expression.hpp.

1449  {
1450  return ExType::asinh(x);
1451  }

◆ atan

template<typename ExType >
ExType atan ( const ExType &  x)
friend

Elementwise; sparsity preserved. Result in (-pi/2, pi/2); use atan2 for a quadrant-aware angle.

>>> atan(DM([0, 1]))
DM([0, 0.785398])

>> atan(DM([0; 1]))
[0, 0.785398]

julia> atan.(ca.DM([0, 1]))
DM([0, 0.785398])

See also
atan2, tan

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

Definition at line 1190 of file generic_expression.hpp.

1190  {
1191  return ExType::atan(x);
1192  }

◆ atan2

template<typename ExType >
ExType atan2 ( const ExType &  y,
const ExType &  x 
)
friend

theta = atan2(y,x) corresponds to x = r cos(theta), y = r sin(theta)

Angle in (-pi, pi] of the point (x, y), taking the quadrant into account; the first argument is the y coordinate, as in C and numpy. Elementwise, shape rules as for plus. atan2(0, 0) is 0.

 >>> atan2(DM([1, -1, 0]), DM([-1, -1, 1]))
 DM([2.35619, -2.35619, 0])
 >>> atan2(SX.sym("y"), SX.sym("x"))
 SX(atan2(y,x))

 >> atan2(DM([1; -1; 0]), DM([-1; -1; 1]))
 [2.35619, -2.35619, 0]
 >> atan2(SX.sym('y'), SX.sym('x'))
 atan2(y,x)

 julia> atan.(ca.DM([1, -1, 0]), ca.DM([-1, -1, 1]))
 DM([2.35619, -2.35619, 0])
 julia> atan(ca.SX.sym("y"), ca.SX.sym("x"))
 SX(atan2(y,x))

See also
atan

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

Definition at line 2149 of file generic_expression.hpp.

2149  {
2150  return ExType::atan2(y, x);
2151  }

◆ atanh

template<typename ExType >
ExType atanh ( const ExType &  x)
friend

Elementwise; sparsity preserved. Defined for |x| < 1, infinite at +-1.

>>> atanh(DM([0, 0.5]))
DM([0, 0.549306])

>> atanh(DM([0; 0.5]))
[0, 0.549306]

julia> atanh.(ca.DM([0, 0.5]))
DM([0, 0.549306])

See also
tanh

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

Definition at line 1412 of file generic_expression.hpp.

1412  {
1413  return ExType::atanh(x);
1414  }

◆ bilin [1/2]

template<typename MatType >
MatType bilin ( const MatType &  A,
const MatType &  x 
)
friend

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

Definition at line 1210 of file generic_matrix.hpp.

1210  {
1211  return MatType::bilin(A, x, x);
1212  }

◆ bilin [2/2]

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::bilin ( const MatType &  A,
const MatType &  x,
const MatType &  y 
)
friend

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

Definition at line 1207 of file generic_matrix.hpp.

1207  {
1208  return MatType::bilin(A, x, y);
1209  }

◆ blockcat [1/2]

template<typename MatType >
MatType blockcat ( const MatType &  A,
const MatType &  B,
const MatType &  C,
const MatType &  D 
)
friend

Shorthand for the two-by-two block layout [[A, B], [C, D]].

>>> blockcat(1, 2, 3, 4)
DM(
[[1, 2],
 [3, 4]])

>> [DM(1), 2; 3, 4]
[[1, 2],
 [3, 4]]

julia> ca.blockcat(ca.DM(1), ca.DM(2), ca.DM(3), ca.DM(4))
DM(
[[1, 2],
 [3, 4]])

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

Definition at line 814 of file sparsity_interface.hpp.

814  {
815  return vertcat(horzcat(A, B), horzcat(C, D));
816  }
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.

◆ blockcat [2/2]

template<typename MatType >
MatType blockcat ( const std::vector< std::vector< MatType > > &  v)
friend

Each inner list is a block row: its entries are concatenated horizontally, and the resulting block rows are concatenated vertically. The usual concatenation rules apply: blocks in one block row need the same number of rows, block rows need the same total number of columns, otherwise an error is raised. Inverse of blocksplit: blockcat(blocksplit(x, ...)) == x.

Parameters
vList of block rows, each a list of matrices
Returns
Matrix of the same type as the blocks

>>> blockcat([[DM(1), DM([[2, 3]])], [DM([4, 5]), DM.eye(2)]])
DM(
[[1, 2, 3],
 [4, 1, 00],
 [5, 00, 1]])
>>> a, b, c, d = MX.sym("a", 2, 2), MX.sym("b", 2, 1), MX.sym("c", 1, 2), MX.sym("d")
>>> blockcat([[a, b], [c, d]])
MX(horzcat(horzcat(a, b)', horzcat(c, d)')')

>> blockcat({{DM(1), DM([2 3])}, {DM([4; 5]), DM.eye(2)}})
[[1, 2, 3],
 [4, 1, 00],
 [5, 00, 1]]
>> a = MX.sym('a', 2, 2); b = MX.sym('b', 2, 1); c = MX.sym('c', 1, 2); d = MX.sym('d');
>> blockcat({{a, b}, {c, d}})
horzcat(horzcat(a, b)', horzcat(c, d)')'
>> [a, b; c, d]
horzcat(horzcat(a, b)', horzcat(c, d)')'

julia> ca.blockcat([[ca.DM(1), ca.DM([2 3])], [ca.DM([4, 5]), ca.DM.eye(2)]])
DM(
[[1, 2, 3],
 [4, 1, 00],
 [5, 00, 1]])
julia> a, b = ca.MX.sym("a", 2, 2), ca.MX.sym("b", 2, 1);
julia> c, d = ca.MX.sym("c", 1, 2), ca.MX.sym("d");
julia> ca.blockcat([[a, b], [c, d]])
MX(horzcat(horzcat(a, b)', horzcat(c, d)')')
julia> print([a b; c d])
horzcat(horzcat(a, b)', horzcat(c, d)')'

See also
horzcat, vertcat, blocksplit

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

Definition at line 778 of file sparsity_interface.hpp.

778  {
779  return MatType::blockcat(v);
780  }

◆ blocksplit [1/2]

template<typename MatType >
std::vector< std::vector< MatType > > blocksplit ( const MatType &  x,
casadi_int  vert_incr = 1,
casadi_int  horz_incr = 1 
)
friend

Every block has vert_incr rows and horz_incr columns; the blocks in the last block row or column are smaller when the dimensions are not multiples of the increments. Inverse of blockcat: blockcat(blocksplit(x, vert_incr, horz_incr)) == x.

Parameters
xMatrix (or Sparsity) to split; any shape
vert_incrRows per block, must be positive
horz_incrColumns per block, must be positive
Returns
List of block rows, each a list of matrices of the same type as x

>>> x = DM([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> blocksplit(x, 2, 2)
[[DM(
[[1, 2],
 [4, 5]]), DM([3, 6])], [DM([[7, 8]]), DM(9)]]
>>> blockcat(blocksplit(x, 2, 2))
DM(
[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]])

>> x = DM([1 2 3; 4 5 6; 7 8 9]);
>> b = blocksplit(x, 2, 2); disp(b{1}{1}), disp(b{1}{2}), disp(b{2}{1}), disp(b{2}{2})
[[1, 2],
 [4, 5]]
[3, 6]
[[7, 8]]
9
>> blockcat(blocksplit(x, 2, 2))
[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]]

julia> x = ca.DM([1 2 3; 4 5 6; 7 8 9]);
julia> ca.blocksplit(x, 2, 2)
2-element Vector{Vector{DM}}:
 DM[
[[1, 2],
 [4, 5]], [3, 6]]
 DM[[[7, 8]], 9]
julia> ca.blockcat(ca.blocksplit(x, 2, 2))
DM(
[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]])

See also
blockcat

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

Definition at line 939 of file sparsity_interface.hpp.

939  {
940  return MatType::blocksplit(x, vert_incr, horz_incr);
941  }

◆ blocksplit [2/2]

template<typename MatType >
std::vector< std::vector< MatType > > blocksplit ( const MatType &  x,
const std::vector< casadi_int > &  vert_offset,
const std::vector< casadi_int > &  horz_offset 
)
friend

The rows are split at vert_offset and every row block is then split at horz_offset. Both offset lists follow the rules of vertsplit and horzsplit: start at 0, non-decreasing, end at the row (column) count. Inverse of blockcat: blockcat(blocksplit(x, vert_offset, horz_offset)) == x.

Parameters
xMatrix (or Sparsity) to split; any shape
vert_offsetRow boundaries of the block rows
horz_offsetColumn boundaries of the block columns
Returns
List of block rows, each a list of matrices of the same type as x

>>> x = DM([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> blocksplit(x, [0, 1, 3], [0, 2, 3])
[[DM([[1, 2]]), DM(3)], [DM(
[[4, 5],
 [7, 8]]), DM([6, 9])]]

>> x = DM([1 2 3; 4 5 6; 7 8 9]);
>> b = blocksplit(x, [0, 1, 3], [0, 2, 3]); disp(size(b)), disp(b{1}{1}), disp(b{2}{1})
1 2
[[1, 2]]
[[4, 5],
 [7, 8]]
>> b{1}{2}, b{2}{2}
3
[6, 9]

julia> x = ca.DM([1 2 3; 4 5 6; 7 8 9]);
julia> ca.blocksplit(x, [0, 1, 3], [0, 2, 3])
2-element Vector{Vector{DM}}:
 DM[[[1, 2]], 3]
 DM[
[[4, 5],
 [7, 8]], [6, 9]]

See also
blockcat, vertsplit, horzsplit

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

Definition at line 870 of file sparsity_interface.hpp.

872  {
873  return MatType::blocksplit(x, vert_offset, horz_offset);
874  }

◆ bspline [1/3]

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

Returns an m-by-1 expression with the spline values at x. The spline is defined by one knot vector and one degree per dimension; the number of basis functions per dimension is len(knots[i]) - degree[i] - 1 and the coefficient vector must hold m times the product of these counts, in column-major order. Outside the knot range the spline is zero. Derivatives are available.

Parameters
xColumn vector with one entry per dimension
coeffsCoefficient vector; its length is not validated
knotsOne knot vector per dimension, numeric
degreeDegree per dimension; must have as many entries as knots
mNumber of outputs per evaluation
opts"lookup_mode" (see low) and "inline" (bool, default false: build an explicit expression instead of a BSpline node). Unknown keys are ignored.

>>> x = MX.sym("x")
>>> knots = [[0, 0, 0, 0, 1, 2, 3, 3, 3, 3]]
>>> c = DM(range(6))
>>> y = bspline(x, c, knots, [3], 1)
>>> f = Function("f", [x], [y])
>>> y, f(1.5), f(4)
(MX(BSpline(x)), DM(2.5), DM(0))
>>> Function("df", [x], [jacobian(y, x)])(1.5)
DM(1.125)
>>> try:
...   bspline(MX.sym("v", 2), c, knots, [3], 1)
... except Exception as e:
...   print("must match" in str(e))
True

>> x = MX.sym('x');
>> knots = {[0 0 0 0 1 2 3 3 3 3]};
>> c = DM((0:5)');
>> y = bspline(x, c, knots, 3, 1);
>> f = Function('f', {x}, {y});
>> y, f(1.5), f(4)
BSpline(x)
2.5
0
>> df = Function('df', {x}, {jacobian(y, x)});
>> df(1.5)
1.125
>> try, bspline(MX.sym('v', 2), c, knots, 3, 1); catch e, disp('must match'); end
must match

julia> x = ca.MX.sym("x");
julia> knots = [[0, 0, 0, 0, 1, 2, 3, 3, 3, 3]];
julia> c = ca.DM(0:5);
julia> y = ca.bspline(x, c, knots, [3], 1);
julia> f = ca.Function("f", [x], [y]);
julia> y, f(1.5), f(4)
(BSpline(x), 2.5, 0)
julia> ca.Function("df", [x], [ca.jacobian(y, x)])(1.5)
DM(1.125)
julia> try
           ca.bspline(ca.MX.sym("v", 2), c, knots, [3], 1)
       catch e
           println(occursin("must match", sprint(showerror, e)))
       end
true

See also
bspline_dual, interpolant

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

Definition at line 4115 of file mx.hpp.

4120  {
4121  return MX::bspline(x, coeffs, knots, degree, m, opts);
4122  }
static MX bspline(const MX &x, const DM &coeffs, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, casadi_int m, const Dict &opts=Dict())
Definition: mx.cpp:2217

◆ bspline [2/3]

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

With symbolic knots the evaluation is always inlined into explicit MX operations.

>>> x = MX.sym("x")
>>> C = MX.sym("C", 6)
>>> K = MX.sym("K", 10)
>>> y = bspline(x, C, [K], [3], 1)
>>> f = Function("f", [x, C, K], [y])
>>> f(1.5, DM(range(6)), DM([0, 0, 0, 0, 1, 2, 3, 3, 3, 3]))
DM(2.5)

>> x = MX.sym('x');
>> C = MX.sym('C', 6);
>> K = MX.sym('K', 10);
>> y = bspline(x, C, {K}, 3, 1);
>> f = Function('f', {x, C, K}, {y});
>> f(1.5, DM((0:5)'), DM([0; 0; 0; 0; 1; 2; 3; 3; 3; 3]))
2.5

julia> x = ca.MX.sym("x"); C = ca.MX.sym("C", 6); K = ca.MX.sym("K", 10);
julia> y = ca.bspline(x, C, [K], [3], 1);
julia> f = ca.Function("f", [x, C, K], [y]);
julia> f(1.5, 0:5, [0, 0, 0, 0, 1, 2, 3, 3, 3, 3])
DM(2.5)

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

Definition at line 4216 of file mx.hpp.

4220  {
4221  return MX::bspline(x, coeffs, knots, degree, m, opts);
4222  }

◆ bspline [3/3]

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

Same as the fixed-coefficient overload, but coeffs may be symbolic, so the spline is differentiable with respect to its coefficients as well.

>>> x = MX.sym("x")
>>> C = MX.sym("C", 6)
>>> knots = [[0, 0, 0, 0, 1, 2, 3, 3, 3, 3]]
>>> y = bspline(x, C, knots, [3], 1)
>>> y
MX(BSplineParametric(x, C))
>>> Function("f", [x, C], [y])(1.5, DM(range(6)))
DM(2.5)

>> x = MX.sym('x');
>> C = MX.sym('C', 6);
>> knots = {[0 0 0 0 1 2 3 3 3 3]};
>> y = bspline(x, C, knots, 3, 1);
>> y
BSplineParametric(x, C)
>> f = Function('f', {x, C}, {y});
>> f(1.5, DM((0:5)'))
2.5

julia> x = ca.MX.sym("x"); C = ca.MX.sym("C", 6);
julia> knots = [[0, 0, 0, 0, 1, 2, 3, 3, 3, 3]];
julia> y = ca.bspline(x, C, knots, [3], 1);
julia> y
MX(BSplineParametric(x, C))
julia> ca.Function("f", [x, C], [y])(1.5, 0:5)
DM(2.5)

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

Definition at line 4169 of file mx.hpp.

4173  {
4174  return MX::bspline(x, coeffs, knots, degree, m, opts);
4175  }

◆ bspline_dual

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

Definition at line 4225 of file mx.hpp.

4228  {
4229  return MX::bspline_dual(x, knots, degree, opts);
4230  }
static DM bspline_dual(const std::vector< double > &x, const std::vector< std::vector< double > > &knots, const std::vector< casadi_int > &degree, const Dict &opts=Dict())
Evaluate the B-spline basis functions at given points.
Definition: mx.cpp:2242

◆ ceil

template<typename ExType >
ExType ceil ( const ExType &  x)
friend

Elementwise, towards plus infinity; sparsity preserved. The derivative is zero everywhere.

>>> ceil(DM([-1.5, 1.5]))
DM([-1, 2])

>> ceil(DM([-1.5; 1.5]))
[-1, 2]

julia> ceil.(ca.DM([-1.5, 1.5]))
DM([-1, 2])

See also
floor

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

Definition at line 1792 of file generic_expression.hpp.

1792  {
1793  return ExType::ceil(x);
1794  }

◆ chol

template<typename Scalar >
Matrix<Scalar> chol ( const Matrix< Scalar > &  A)
friend

Returns the upper triangular factor R with R^T*R == A, obtained from the sparse LDL^T factorization (without fill-reducing permutation) as diag(sqrt(D))*L^T. The sparsity pattern of A must be symmetric; otherwise an error is raised. An indefinite A gives nan entries without an error. Works symbolically on SX.

>>> R = chol(DM([[4, 2], [2, 3]]))
>>> R
DM(
[[2, 1],
 [00, 1.41421]])
>>> mtimes(R.T, R)
DM(
[[4, 2],
 [2, 3]])
>>> chol(DM(Sparsity.diag(3)) * 4).nnz()
3

>> R = chol(DM([4 2; 2 3]));
>> R
[[2, 1],
 [00, 1.41421]]
>> R'*R
[[4, 2],
 [2, 3]]
>> chol(DM(Sparsity.diag(3)) * 4).nnz()
3

julia> R = ca.chol(ca.DM([4 2; 2 3]))
DM(
[[2, 1],
 [00, 1.41421]])
julia> R' * R
DM(
[[4, 2],
 [2, 3]])
julia> ca.nnz(ca.chol(ca.DM(ca.Sparsity.diag(3)) * 4))
3

See also
ldl, qr

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

Definition at line 1750 of file matrix_decl.hpp.

1750  {
1751  return Matrix<Scalar>::chol(A);
1752  }
static Matrix< Scalar > chol(const Matrix< Scalar > &A)

◆ cofactor

template<typename Scalar >
Matrix<Scalar> cofactor ( const Matrix< Scalar > &  x,
casadi_int  i,
casadi_int  j 
)
friend

Same argument convention as minor: i is a column index, j a row index.

>>> A = DM([[1, 2, 3], [0, 1, 4], [5, 6, 0]])
>>> cofactor(A, 0, 1), cofactor(A, 0, 0)
(DM(18), DM(-24))

>> A = DM([1 2 3; 0 1 4; 5 6 0]);
>> cofactor(A, 0, 1), cofactor(A, 0, 0)
18
-24

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

See also
minor, adj

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

Definition at line 1508 of file matrix_decl.hpp.

1508  {
1509  return Matrix<Scalar>::cofactor(x, i, j);
1510  }
static Matrix< Scalar > cofactor(const Matrix< Scalar > &A, casadi_int i, casadi_int j)

◆ conditional

template<typename MatType >
MatType conditional ( const MatType &  ind,
const std::vector< MatType > &  x,
const MatType &  x_default,
bool  short_circuit = false 
)
friend

If the scalar condition ind evaluates to the integer k, where 0<=k<x.size(), then x[k] is returned, otherwise x_default is returned (also for negative or non-integer values). By default this is a chain of if_else expressions, so all cases are evaluated. With short_circuit true (MX only; SX raises an error) a Switch function evaluates only the selected case.

>>> conditional(DM(1), [DM(10), DM(20)], DM(-1))
DM(20)
>>> conditional(DM(5), [DM(10), DM(20)], DM(-1))
DM(-1)
>>> x = SX.sym("x"); y = SX.sym("y"); z = SX.sym("z")
>>> conditional(x, [y, z], 0)
SX(@1=(x==1), ((@1?z:0)+((!@1)?((x==0)?y:0):0)))
>>> X = MX.sym("X"); Y = MX.sym("Y")
>>> conditional(X, [Y, 2*Y], 0, True)
MX(switch(X, Y){0})

>> conditional(DM(1), {DM(10), DM(20)}, DM(-1))
20
>> conditional(DM(5), {DM(10), DM(20)}, DM(-1))
-1
>> x = SX.sym('x'); y = SX.sym('y'); z = SX.sym('z');
>> conditional(x, {y, z}, 0)
@1=(x==1), ((@1?z:0)+((!@1)?((x==0)?y:0):0))
>> X = MX.sym('X'); Y = MX.sym('Y');
>> conditional(X, {Y, 2*Y}, 0, true)
switch(X, Y){0}

julia> ca.conditional(ca.DM(1), [ca.DM(10), ca.DM(20)], ca.DM(-1))
DM(20)
julia> ca.conditional(ca.DM(5), [ca.DM(10), ca.DM(20)], ca.DM(-1))
DM(-1)
julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); z = ca.SX.sym("z");
julia> ca.conditional(x, [y, z], 0)
SX(@1=(x==1), ((@1?z:0)+((!@1)?((x==0)?y:0):0)))
julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
julia> ca.conditional(X, [Y, 2 * Y], 0, true)
MX(switch(X, Y){0})

See also
if_else, Function::conditional

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

Definition at line 2711 of file generic_matrix.hpp.

2712  {
2713  return MatType::conditional(ind, x, x_default, short_circuit);
2714  }

◆ constpow

template<typename ExType >
ExType constpow ( const ExType &  x,
const ExType &  y 
)
friend

Same value as pow, but the exponent is assumed constant: the derivative with respect to y is taken as zero. pow itself produces this operation for a non-integer constant exponent, so it is rarely needed directly. Passing a symbolic exponent gives a wrong derivative for MX; SX falls back to pow.

>>> constpow(DM(2), 3)
DM(8)
>>> x = MX.sym("x")
>>> constpow(x, 2.5), jacobian(constpow(x, 2.5), x)
(MX(pow(x,2.5)), MX((2.5*pow(x,1.5))))

>> constpow(DM(2), 3)
8
>> x = MX.sym('x');
>> constpow(x, 2.5), jacobian(constpow(x, 2.5), x)
pow(x,2.5)
(2.5*pow(x,1.5))

julia> ca.constpow(ca.DM(2), 3)
DM(8)
julia> x = ca.MX.sym("x");
julia> ca.constpow(x, 2.5), ca.jacobian(ca.constpow(x, 2.5), x)
(pow(x,2.5), (2.5*pow(x,1.5)))

See also
pow

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

Definition at line 2470 of file generic_expression.hpp.

2470  {
2471  return ExType::constpow(x, y);
2472  }

◆ contains

template<typename MatType >
bool contains ( const std::vector< MatType > &  v,
const MatType &  n 
)
friend

This function checks for correspondence between nodes. The detection does not descend into the expression graph.

contains_all checks that every entry of n is listed in v, contains_any that at least one is. For SX the entries must be scalar. Not defined for DM.

For mathematical dependency, see depends_on

See also
depends_on

* >>> x = SX.sym("x"); y = SX.sym("y"); z = SX.sym("z")
* >>> e = x + y
* >>> contains([x, y], y), contains([e], y), contains([e], e)
* (True, False, True)
* >>> contains([SX.sym("x")], x)
* False
* >>> contains_all([x, y, z], [x, y]), contains_all([x, y], [x, z])
* (True, False)
* >>> contains_any([x, y], [x, z]), contains_any([x, y], [z])
* (True, False)
* >>> X = MX.sym("X"); Y = MX.sym("Y")
* >>> contains([X, Y], Y), contains([X + Y], Y)
* (True, False)
* 

* >> x = SX.sym('x'); y = SX.sym('y');
* >> e = x + y;
* >> fprintf('%d %d %d\n', contains({x, y}, y), contains({e}, y), contains({e}, e))
* 1 0 1
* >> contains({SX.sym('x')}, x)
* 0
* >> X = MX.sym('X'); Y = MX.sym('Y');
* >> fprintf('%d %d\n', contains({X, Y}, Y), contains({X + Y}, Y))
* 1 0
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); z = ca.SX.sym("z");
* julia> e = x + y;
* julia> ca.contains([x, y], y), ca.contains([e], y), ca.contains([e], e)
* (true, false, true)
* julia> ca.contains([ca.SX.sym("x")], x)
* false
* julia> ca.contains_all([x, y, z], [x, y]), ca.contains_all([x, y], [x, z])
* (true, false)
* julia> ca.contains_any([x, y], [x, z]), ca.contains_any([x, y], [z])
* (true, false)
* julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
* julia> ca.contains([X, Y], Y), ca.contains([X + Y], Y)
* (true, false)
* 

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

Definition at line 2846 of file generic_matrix.hpp.

2846  {
2847  return contains_all(v, std::vector<MatType>{n});
2848  }
friend bool contains_all(const std::vector< MatType > &v, const std::vector< MatType > &n)
Check if expression n is listed in v.

◆ contains_all

template<typename MatType >
bool contains_all ( const std::vector< MatType > &  v,
const std::vector< MatType > &  n 
)
friend

This function checks for correspondence between nodes. The detection does not descend into the expression graph.

contains_all checks that every entry of n is listed in v, contains_any that at least one is. For SX the entries must be scalar. Not defined for DM.

For mathematical dependency, see depends_on

See also
depends_on

* >>> x = SX.sym("x"); y = SX.sym("y"); z = SX.sym("z")
* >>> e = x + y
* >>> contains([x, y], y), contains([e], y), contains([e], e)
* (True, False, True)
* >>> contains([SX.sym("x")], x)
* False
* >>> contains_all([x, y, z], [x, y]), contains_all([x, y], [x, z])
* (True, False)
* >>> contains_any([x, y], [x, z]), contains_any([x, y], [z])
* (True, False)
* >>> X = MX.sym("X"); Y = MX.sym("Y")
* >>> contains([X, Y], Y), contains([X + Y], Y)
* (True, False)
* 

* >> x = SX.sym('x'); y = SX.sym('y');
* >> e = x + y;
* >> fprintf('%d %d %d\n', contains({x, y}, y), contains({e}, y), contains({e}, e))
* 1 0 1
* >> contains({SX.sym('x')}, x)
* 0
* >> X = MX.sym('X'); Y = MX.sym('Y');
* >> fprintf('%d %d\n', contains({X, Y}, Y), contains({X + Y}, Y))
* 1 0
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); z = ca.SX.sym("z");
* julia> e = x + y;
* julia> ca.contains([x, y], y), ca.contains([e], y), ca.contains([e], e)
* (true, false, true)
* julia> ca.contains([ca.SX.sym("x")], x)
* false
* julia> ca.contains_all([x, y, z], [x, y]), ca.contains_all([x, y], [x, z])
* (true, false)
* julia> ca.contains_any([x, y], [x, z]), ca.contains_any([x, y], [z])
* (true, false)
* julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
* julia> ca.contains([X, Y], Y), ca.contains([X + Y], Y)
* (true, false)
* 

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

Definition at line 2850 of file generic_matrix.hpp.

2850  {
2851  return MatType::contains_all(v, n);
2852  }

◆ contains_any

template<typename MatType >
bool contains_any ( const std::vector< MatType > &  v,
const std::vector< MatType > &  n 
)
friend

This function checks for correspondence between nodes. The detection does not descend into the expression graph.

contains_all checks that every entry of n is listed in v, contains_any that at least one is. For SX the entries must be scalar. Not defined for DM.

For mathematical dependency, see depends_on

See also
depends_on

* >>> x = SX.sym("x"); y = SX.sym("y"); z = SX.sym("z")
* >>> e = x + y
* >>> contains([x, y], y), contains([e], y), contains([e], e)
* (True, False, True)
* >>> contains([SX.sym("x")], x)
* False
* >>> contains_all([x, y, z], [x, y]), contains_all([x, y], [x, z])
* (True, False)
* >>> contains_any([x, y], [x, z]), contains_any([x, y], [z])
* (True, False)
* >>> X = MX.sym("X"); Y = MX.sym("Y")
* >>> contains([X, Y], Y), contains([X + Y], Y)
* (True, False)
* 

* >> x = SX.sym('x'); y = SX.sym('y');
* >> e = x + y;
* >> fprintf('%d %d %d\n', contains({x, y}, y), contains({e}, y), contains({e}, e))
* 1 0 1
* >> contains({SX.sym('x')}, x)
* 0
* >> X = MX.sym('X'); Y = MX.sym('Y');
* >> fprintf('%d %d\n', contains({X, Y}, Y), contains({X + Y}, Y))
* 1 0
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); z = ca.SX.sym("z");
* julia> e = x + y;
* julia> ca.contains([x, y], y), ca.contains([e], y), ca.contains([e], e)
* (true, false, true)
* julia> ca.contains([ca.SX.sym("x")], x)
* false
* julia> ca.contains_all([x, y, z], [x, y]), ca.contains_all([x, y], [x, z])
* (true, false)
* julia> ca.contains_any([x, y], [x, z]), ca.contains_any([x, y], [z])
* (true, false)
* julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
* julia> ca.contains([X, Y], Y), ca.contains([X + Y], Y)
* (true, false)
* 

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

Definition at line 2854 of file generic_matrix.hpp.

2854  {
2855  return MatType::contains_any(v, n);
2856  }

◆ convexify

MX convexify ( const MX &  H,
const Dict &  opts = Dict() 
)
friend

Returns an expression with the sparsity pattern of H that, when evaluated, modifies the value of H so that it is positive definite. Intended for Hessians in SQP-type methods. H must be square and symmetric or triangular (a triangular pattern is interpreted as one half of a symmetric matrix); other patterns raise an error. The node has no derivatives.

Parameters
HSquare expression with a symmetric or triangular sparsity pattern
optsOptions: "strategy": "eigen-clip" (default, eigenvalues below the margin are set to the margin), "eigen-reflect" (their absolute value is taken) or "regularize" (a multiple of the identity is added; symmetric patterns only); "margin": smallest allowed eigenvalue, default 1e-7, must be nonnegative; "max_iter_eig": iteration limit of the eigenvalue computation, default 200; "verbose": print the block structure found. Unknown keys raise an error.

>>> H = MX.sym("H", 2, 2)
>>> f = Function("f", [H], [convexify(H)])
>>> f(DM([[1, 0], [0, -2]]))
DM(
[[1, 0],
 [0, 1e-07]])
>>> f = Function("f", [H], [convexify(H, {"strategy": "eigen-reflect"})])
>>> f(DM([[1, 0], [0, -2]]))
DM(
[[1, 0],
 [0, 2]])
>>> f = Function("f", [H], [convexify(H, {"strategy": "regularize", "margin": 0.5})])
>>> f(DM([[1, 0], [0, -2]]))
DM(
[[3.5, 0],
 [0, 0.5]])
>>> try:
...   convexify(H, {"strategy": "foo"})
... except Exception as e:
...   print("Invalid convexify strategy" in str(e))
True

>> H = MX.sym('H', 2, 2);
>> f = Function('f', {H}, {convexify(H)});
>> f(DM([1 0; 0 -2]))
[[1, 0],
 [0, 1e-07]]
>> f = Function('f', {H}, {convexify(H, struct('strategy', 'eigen-reflect'))});
>> f(DM([1 0; 0 -2]))
[[1, 0],
 [0, 2]]
>> f = Function('f', {H}, {convexify(H, struct('strategy', 'regularize', 'margin', 0.5))});
>> f(DM([1 0; 0 -2]))
[[3.5, 0],
 [0, 0.5]]
>> try, convexify(H, struct('strategy', 'foo')); catch e, disp('invalid strategy'); end
invalid strategy

julia> H = ca.MX.sym("H", 2, 2);
julia> f = ca.Function("f", [H], [ca.convexify(H)]);
julia> print(f([1 0; 0 -2]))
[[1, 0],
 [0, 1e-07]]
julia> f = ca.Function("f", [H], [ca.convexify(H, Dict("strategy" => "eigen-reflect"))]);
julia> f([1 0; 0 -2])
DM(
[[1, 0],
 [0, 2]])
julia> opts = Dict("strategy" => "regularize", "margin" => 0.5);
julia> f = ca.Function("f", [H], [ca.convexify(H, opts)]);
julia> f([1 0; 0 -2])
DM(
[[3.5, 0],
 [0, 0.5]])
julia> try
           ca.convexify(H, Dict("strategy" => "foo"))
       catch e
           println(occursin("Invalid convexify strategy", sprint(showerror, e)))
       end
true

See also
nlpsol

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

Definition at line 4326 of file mx.hpp.

4327  {
4328  return MX::convexify(H, opts);
4329  }
static MX convexify(const MX &H, const Dict &opts=Dict())
Definition: mx.cpp:2249

◆ copysign

template<typename ExType >
ExType copysign ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise value with the magnitude of x and the sign of y, as C copysign; a negative zero y counts as negative. Shape rules as for plus. The derivative with respect to y is zero.

>>> copysign(DM([1, -2]), DM([-1, 1]))
DM([-1, 2])
>>> copysign(DM(3), DM(-0.0))
DM(-3)
>>> copysign(SX.sym("x"), SX.sym("y"))
SX(copysign(x,y))

>> copysign(DM([1; -2]), DM([-1; 1]))
[-1, 2]
>> copysign(DM(3), DM(-0.0))
-3
>> copysign(SX.sym('x'), SX.sym('y'))
copysign(x,y)

julia> copysign.(ca.DM([1, -2]), ca.DM([-1, 1]))
DM([-1, 2])
julia> copysign.(ca.DM(3), ca.DM(-0.0))
DM(-3)
julia> ca.copysign(ca.SX.sym("x"), ca.SX.sym("y"))
SX(copysign(x,y))

See also
sign, abs

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

Definition at line 2420 of file generic_expression.hpp.

2420  {
2421  return ExType::copysign(x, y);
2422  }

◆ cos

template<typename ExType >
ExType cos ( const ExType &  x)
friend

Elementwise; structural zeros become 1, so the result is dense.

>>> cos(DM.eye(2))
DM(
[[0.540302, 1],
 [1, 0.540302]])
>>> cos(SX.sym("s", Sparsity.diag(2)))
SX(@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]])

>> cos(DM.eye(2))
[[0.540302, 1],
 [1, 0.540302]]
>> cos(SX.sym('s', Sparsity.diag(2)))
@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]]

julia> cos.(ca.DM.eye(2))
DM(
[[0.540302, 1],
 [1, 0.540302]])
julia> cos.(ca.SX.sym("s", ca.Sparsity.diag(2)))
SX(@1=1,
[[cos(s_0), @1],
 [@1, cos(s_1)]])

See also
sin, tan, acos

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

Definition at line 1115 of file generic_expression.hpp.

1115  {
1116  return ExType::cos(x);
1117  }

◆ cosh

template<typename ExType >
ExType cosh ( const ExType &  x)
friend

Elementwise; the result is dense (cosh(0) == 1).

>>> cosh(DM([0, 1]))
DM([1, 1.54308])

>> cosh(DM([0; 1]))
[1, 1.54308]

julia> cosh.(ca.DM([0, 1]))
DM([1, 1.54308])

See also
acosh, sinh, tanh

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

Definition at line 1375 of file generic_expression.hpp.

1375  {
1376  return ExType::cosh(x);
1377  }

◆ cross

template<typename MatType >
MatType cross ( const MatType &  a,
const MatType &  b,
casadi_int  dim = -1 
)
friend

Cross product of 3-vectors. a and b must have the same shape and one of the dimensions must be 3; for 3-by-n or n-by-3 matrices the products are taken per column or per row respectively.

Parameters
dim1 to take the products along the rows (3-by-n input), 2 along the columns, -1 (default) to pick the dimension of length 3 (rows first)

>>> cross(DM([1, 0, 0]), DM([0, 1, 0]))
DM([0, 0, 1])
>>> cross(DM([[1, 0, 0], [0, 1, 0]]), DM([[0, 1, 0], [0, 0, 1]]))
DM(
[[0, 0, 1],
 [1, 0, 0]])
>>> cross(SX.sym("a", 3), SX.sym("b", 3))
SX([((a_1*b_2)-(a_2*b_1)), ((a_2*b_0)-(a_0*b_2)), ((a_0*b_1)-(a_1*b_0))])

>> cross(DM([1; 0; 0]), DM([0; 1; 0]))
[0, 0, 1]
>> cross(DM([1 0 0; 0 1 0]), DM([0 1 0; 0 0 1]))
[[0, 0, 1],
 [1, 0, 0]]
>> cross(SX.sym('a', 3), SX.sym('b', 3))
[((a_1*b_2)-(a_2*b_1)), ((a_2*b_0)-(a_0*b_2)), ((a_0*b_1)-(a_1*b_0))]

julia> ca.cross(ca.DM([1, 0, 0]), ca.DM([0, 1, 0]))
DM([0, 0, 1])
julia> ca.cross(ca.DM([1 0 0; 0 1 0]), ca.DM([0 1 0; 0 0 1]))
DM(
[[0, 0, 1],
 [1, 0, 0]])
julia> ca.cross(ca.SX.sym("a", 3), ca.SX.sym("b", 3))
SX([((a_1*b_2)-(a_2*b_1)), ((a_2*b_0)-(a_0*b_2)), ((a_0*b_1)-(a_1*b_0))])

See also
skew, dot

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

Definition at line 1501 of file generic_matrix.hpp.

1501  {
1502  return MatType::cross(a, b, dim);
1503  }
std::string dim(bool with_nz=false) const
Get string representation of dimensions.

◆ cse [1/2]

template<typename MatType >
MatType cse ( const MatType &  e)
friend

Returns an equivalent expression in which identical subexpressions that were built separately share a single node, reducing the number of nodes and the evaluation cost. The shape is preserved. DM is returned unchanged.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> e = sin(x + y)*cos(x + y)
>>> cse(e)
SX(@1=(x+y), (sin(@1)*cos(@1)))
>>> n_nodes(e), n_nodes(cse(e))
(7, 6)

>> x = SX.sym('x'); y = SX.sym('y');
>> e = sin(x + y)*cos(x + y);
>> cse(e)
@1=(x+y), (sin(@1)*cos(@1))
>> fprintf('%d %d\n', n_nodes(e), n_nodes(cse(e)))
7 6

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> e = sin(x + y) * cos(x + y);
julia> ca.cse(e)
SX(@1=(x+y), (sin(@1)*cos(@1)))
julia> ca.n_nodes(e), ca.n_nodes(ca.cse(e))
(7, 6)

See also
n_nodes, shared, extract

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

Definition at line 3072 of file generic_matrix.hpp.

3072  {
3073  return MatType::cse({e}).at(0);
3074  }

◆ cse [2/2]

template<typename MatType >
std::vector<MatType> cse ( const std::vector< MatType > &  e)
friend

Subexpressions are shared across all entries of the list.

>>> X = MX.sym("X"); Y = MX.sym("Y")
>>> vertcat(sin(X + Y), cos(X + Y))
MX(vertcat(sin((X+Y)), cos((X+Y))))
>>> vertcat(*cse([sin(X + Y), cos(X + Y)]))
MX(@1=(X+Y), vertcat(sin(@1), cos(@1)))

>> X = MX.sym('X'); Y = MX.sym('Y');
>> [sin(X + Y); cos(X + Y)]
vertcat(sin((X+Y)), cos((X+Y)))
>> r = cse({sin(X + Y), cos(X + Y)});
>> vertcat(r{:})
@1=(X+Y), vertcat(sin(@1), cos(@1))

julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
julia> [sin(X + Y); cos(X + Y)]
MX(vertcat(sin((X+Y)), cos((X+Y))))
julia> vcat(ca.cse([sin(X + Y), cos(X + Y)])...)
MX(@1=(X+Y), vertcat(sin(@1), cos(@1)))

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

Definition at line 3113 of file generic_matrix.hpp.

3113  {
3114  return MatType::cse(e);
3115  }

◆ cumsum

template<typename MatType >
MatType cumsum ( const MatType &  x,
casadi_int  axis = -1 
)
friend

Running sum down the rows (axis 0) or along the columns (axis 1); the shape is preserved. With axis -1, the columns are summed for a row vector and the rows otherwise. MATLAB numbers the axes 1 (rows) and 2 (columns).

>>> cumsum(DM([1, 2, 3]))
DM([1, 3, 6])
>>> cumsum(DM([[1, 2], [3, 4]]))
DM(
[[1, 2],
 [4, 6]])
>>> cumsum(DM([[1, 2], [3, 4]]), 1)
DM(
[[1, 3],
 [3, 7]])
>>> cumsum(SX.sym("x", 3))
SX(@1=(x_1+x_0), [x_0, @1, (x_2+@1)])

>> cumsum(DM([1; 2; 3]))
[1, 3, 6]
>> cumsum(DM([1 2; 3 4]))
[[1, 2],
 [4, 6]]
>> cumsum(DM([1 2; 3 4]), 2)
[[1, 3],
 [3, 7]]
>> cumsum(SX.sym('x', 3))
@1=(x_1+x_0), [x_0, @1, (x_2+@1)]

julia> cumsum(ca.DM([1, 2, 3]))
DM([1, 3, 6])
julia> cumsum(ca.DM([1 2; 3 4]); dims=1)
DM(
[[1, 2],
 [4, 6]])
julia> cumsum(ca.DM([1 2; 3 4]); dims=2)
DM(
[[1, 3],
 [3, 7]])
julia> cumsum(ca.SX.sym("x", 3))
SX(@1=(x_1+x_0), [x_0, @1, (x_2+@1)])

See also
diff, sum1, sum2

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

Definition at line 2190 of file generic_matrix.hpp.

2190  {
2191  return MatType::cumsum(x, axis);
2192  }

◆ densify [1/2]

template<typename MatType >
MatType densify ( const MatType &  x)
friend

Structural zeros become stored numerical zeros; the shape and the values of the existing nonzeros are unchanged. A dense input is returned as is.

>>> densify(DM.eye(2))
DM(
[[1, 0],
 [0, 1]])
>>> densify(SX.sym("x", Sparsity.diag(2)))
SX(@1=0,
[[x_0, @1],
 [@1, x_1]])
>>> densify(MX.sym("x", Sparsity.diag(2)))
MX(dense(x))

>> densify(DM.eye(2))
[[1, 0],
 [0, 1]]
>> densify(SX.sym('x', Sparsity.diag(2)))
@1=0,
[[x_0, @1],
 [@1, x_1]]
>> densify(MX.sym('x', Sparsity.diag(2)))
dense(x)

julia> ca.densify(ca.DM.eye(2))
DM(
[[1, 0],
 [0, 1]])
julia> ca.densify(ca.SX.sym("x", ca.Sparsity.diag(2)))
SX(@1=0,
[[x_0, @1],
 [@1, x_1]])
julia> ca.densify(ca.MX.sym("x", ca.Sparsity.diag(2)))
MX(dense(x))

See also
project, sparsify, is_dense

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

Definition at line 2501 of file generic_matrix.hpp.

2501  {
2502  return MatType::densify(x);
2503  }

◆ densify [2/2]

template<typename MatType >
MatType densify ( const MatType &  x,
const MatType &  val 
)
friend

Like densify(x) but the structural zeros get the scalar val instead of zero.

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

Definition at line 2510 of file generic_matrix.hpp.

2510  {
2511  return MatType::densify(x, val);
2512  }

◆ depends_on

template<typename MatType >
bool depends_on ( const MatType &  f,
const MatType &  arg 
)
friend

The argument must be symbolic (for MX: symbolic primitives as created by MX.sym, not elements of them).

The dependency is in a mathematical way: Does the value of the argument affect the value of the expression? Equivalently, will the corresponding entry of the Jacobian by a non-zero? This is decided by sparsity propagation through the graph, so 0*x and x-x do not depend on x.

The symbol could still be present in the expression graph.

To check that kind of dependency, use contains(symvar(f),arg)

>>> x = SX.sym("x"); y = SX.sym("y")
>>> depends_on(x*y, x), depends_on(x*y, SX.sym("z"))
(True, False)
>>> depends_on(0*x, x), depends_on(x - x, x), depends_on(SX(3), x)
(False, False, False)
>>> depends_on(vertcat(x, 3), vertcat(x, y))
True
>>> v = MX.sym("v", 2)
>>> depends_on(v[0], v)
True

>> x = SX.sym('x'); y = SX.sym('y');
>> fprintf('%d %d\n', depends_on(x*y, x), depends_on(x*y, SX.sym('z')))
1 0
>> fprintf('%d %d %d\n', depends_on(0*x, x), depends_on(x - x, x), depends_on(SX(3), x))
0 0 0
>> depends_on(vertcat(x, 3), vertcat(x, y))
1
>> v = MX.sym('v', 2);
>> depends_on(v(1), v)
1

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.depends_on(x * y, x), ca.depends_on(x * y, ca.SX.sym("z"))
(true, false)
julia> ca.depends_on(0 * x, x), ca.depends_on(x - x, x), ca.depends_on(ca.SX(3), x)
(false, false, false)
julia> ca.depends_on([x; 3], [x; y])
true
julia> v = ca.MX.sym("v", 2);
julia> ca.depends_on(v[1], v)
true

See also
which_depends, symvar, contains, jacobian_sparsity

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

Definition at line 2779 of file generic_matrix.hpp.

2779  {
2780  return MatType::depends_on(f, arg);
2781  }

◆ det [1/2]

template<typename MatType >
MatType det ( const MatType &  A)
friend

For DM and SX the determinant is formed by minor expansion along the sparsest row or column (a 2-by-2 directly, a scalar is returned as is); the cost grows quickly with the matrix size. For MX a determinant node is created that is evaluated through the 'qr' linear solver. A non-square argument raises an error.

>>> det(DM([[1, 2], [3, 4]]))
DM(-2)
>>> det(SX.sym("A", 2, 2))
SX(((A_0*A_3)-(A_2*A_1)))
>>> det(MX.sym("A", 2, 2))
MX(det(A))

>> det(DM([1 2; 3 4]))
-2
>> det(SX.sym('A', 2, 2))
((A_0*A_3)-(A_2*A_1))
>> det(MX.sym('A', 2, 2))
det(A)

julia> ca.det(ca.DM([1 2; 3 4]))
DM(-2)
julia> ca.det(ca.SX.sym("A", 2, 2))
SX(((A_0*A_3)-(A_2*A_1)))
julia> ca.det(ca.MX.sym("A", 2, 2))
MX(det(A))

See also
inv, inv_minor

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

Definition at line 1607 of file generic_matrix.hpp.

1607 { return MatType::det(A);}

◆ det [2/2]

template<typename MatType >
MatType det ( const MatType &  A,
const std::string &  lsolver,
const Dict &  dict = Dict() 
)
friend
Parameters
lsolverName of a Linsol plugin, e.g. "qr" or "symbolicqr" (for SX only plugins with a symbolic determinant, such as "symbolicqr", are accepted)
dictOptions passed to the linear solver

>>> det(DM([[1, 2], [3, 4]]), "qr")
DM(-2)

>> det(DM([1 2; 3 4]), 'qr')
-2

julia> ca.det(ca.DM([1 2; 3 4]), "qr")
DM(-2)

See also
Linsol

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

Definition at line 1639 of file generic_matrix.hpp.

1640  {
1641  return MatType::det(A, lsolver, dict);
1642  }

◆ diag

template<typename MatType >
MatType diag ( const MatType &  A)
friend

If the input is a vector (row or column), a square matrix with the vector on its diagonal and structural zeros elsewhere is constructed. Otherwise the diagonal entries are returned as a column vector of length min(size1(), size2()); only structurally nonzero entries are kept.

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

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

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

See also
trace, diagcat, Sparsity::diag

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

Definition at line 2393 of file generic_matrix.hpp.

2393  {
2394  return MatType::diag(A);
2395  }

◆ diagcat [1/6]

template<typename MatType >
MatType diagcat ( const MatType &  x,
const MatType &  y 
)
friend

Same as the list form of diagcat.

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

Definition at line 2301 of file sparsity_interface.hpp.

2301  {
2302  return diagcat(std::vector<MatType>{x, y});
2303  }
friend MatType diagcat(const std::vector< MatType > &A)
Construct a block-diagonal matrix from a list of matrices.

◆ diagcat [2/6]

template<typename MatType >
MatType diagcat ( const MatType &  x,
const MatType &  y,
const MatType &  z 
)
friend

Same as the list form of diagcat.

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

Definition at line 2310 of file sparsity_interface.hpp.

2310  {
2311  return diagcat(std::vector<MatType>{x, y, z});
2312  }

◆ diagcat [3/6]

template<typename MatType >
MatType diagcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w 
)
friend

Same as the list form of diagcat.

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

Definition at line 2319 of file sparsity_interface.hpp.

2320  {
2321  return diagcat(std::vector<MatType>{x, y, z, w});
2322  }

◆ diagcat [4/6]

template<typename MatType >
MatType diagcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w,
const MatType &  v 
)
friend

Same as the list form of diagcat.

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

Definition at line 2329 of file sparsity_interface.hpp.

2330  {
2331  return diagcat(std::vector<MatType>{x, y, z, w, v});
2332  }

◆ diagcat [5/6]

template<typename MatType >
MatType diagcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w,
const MatType &  v,
const MatType &  u 
)
friend

Same as the list form of diagcat.

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

Definition at line 2339 of file sparsity_interface.hpp.

2340  {
2341  return diagcat(std::vector<MatType>{x, y, z, w, v, u});
2342  }

◆ diagcat [6/6]

template<typename MatType >
MatType diagcat ( const std::vector< MatType > &  A)
friend

Places the matrices along the diagonal, in order; all other entries are structural zeros. The blocks need not be square: the result has the sum of the row counts by the sum of the column counts. Type rules are those of horzcat. With no arguments the result is a 0-by-0 matrix. Inverse of diagsplit: diagcat(diagsplit(x, ...)) == x.

Parameters
AList of matrices of any shape
Returns
Block-diagonal matrix of the same type as the blocks

>>> diagcat(DM.ones(1, 2), 3)
DM(
[[1, 1, 00],
 [00, 00, 3]])
>>> diagcat(SX.sym("x"), SX.sym("y", 2))
SX(
[[x, 00],
 [00, y_0],
 [00, y_1]])
>>> diagcat(MX.sym("x"), MX.sym("y", 2, 2))
MX(diagcat(x, y))
>>> diagcat().shape
(0, 0)

>> diagcat(DM.ones(1, 2), 3)
[[1, 1, 00],
 [00, 00, 3]]
>> diagcat(SX.sym('x'), SX.sym('y', 2))
[[x, 00],
 [00, y_0],
 [00, y_1]]
>> diagcat(MX.sym('x'), MX.sym('y', 2, 2))
diagcat(x, y)

julia> ca.diagcat(ca.DM.ones(1, 2), 3)
DM(
[[1, 1, 00],
 [00, 00, 3]])
julia> ca.diagcat(ca.SX.sym("x"), ca.SX.sym("y", 2))
SX(
[[x, 00],
 [00, y_0],
 [00, y_1]])
julia> ca.diagcat(ca.MX.sym("x"), ca.MX.sym("y", 2, 2))
MX(diagcat(x, y))

See also
horzcat, vertcat, diagsplit

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

Definition at line 1005 of file sparsity_interface.hpp.

1005  {
1006  return MatType::diagcat(A);
1007  }

◆ diagsplit [1/4]

template<typename MatType >
std::vector< MatType > diagsplit ( const MatType &  x,
casadi_int  incr1,
casadi_int  incr2 
)
friend

Block i covers rows i*incr1 onwards and columns i*incr2 onwards. The number of row blocks must equal the number of column blocks.

Parameters
xMatrix (or Sparsity) to split
incr1Rows per block, must be positive
incr2Columns per block, must be positive

>>> diagsplit(DM([[1, 2, 0, 0], [0, 0, 7, 8]]), 1, 2)
[DM([[1, 2]]), DM([[7, 8]])]

>> v = diagsplit(DM([1 2 0 0; 0 0 7 8]), 1, 2); disp(v{1}), disp(v{2})
[[1, 2]]
[[7, 8]]

julia> ca.diagsplit(ca.DM([1 2 0 0; 0 0 7 8]), 1, 2)
2-element Vector{DM}:
 DM([[1, 2]])
 DM([[7, 8]])

See also
diagcat

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

Definition at line 1242 of file sparsity_interface.hpp.

1242  {
1243  return MatType::diagsplit(x, incr1, incr2);
1244  }

◆ diagsplit [2/4]

template<typename MatType >
std::vector< MatType > diagsplit ( const MatType &  x,
casadi_int  incr = 1 
)
friend

Returns the consecutive incr-by-incr diagonal blocks of x; the last block is smaller when the dimension is not a multiple of incr. An error is raised when x is not square. Entries outside the blocks must be zero (structurally zero for MX and Sparsity). Inverse of diagcat: diagcat(diagsplit(x, incr)) == x for block-diagonal x.

Parameters
xSquare matrix (or Sparsity) to split
incrSize of each block, must be positive

>>> diagsplit(DM([[1, 0], [0, 4]]))
[DM(1), DM(4)]
>>> diagsplit(diagcat(SX.sym("a", 2, 2), SX.sym("b", 2, 2)), 2)
[SX(
[[a_0, a_2],
 [a_1, a_3]]), SX(
[[b_0, b_2],
 [b_1, b_3]])]
>>> diagsplit(MX.sym("x", Sparsity.diag(4)), 2)
[MX(diagsplit(x){0}), MX(diagsplit(x){1})]
>>> diagsplit(MX.sym("x", 4, 4), 2)
Traceback (most recent call last):
  ...
RuntimeError: ...nonzeros outside the diagonal blocks...

>> v = diagsplit(DM([1 0; 0 4])); disp(v{1}), disp(v{2})
1
4
>> v = diagsplit(diagcat(SX.sym('a', 2, 2), SX.sym('b', 2, 2)), 2); disp(v{1}), disp(v{2})
[[a_0, a_2],
 [a_1, a_3]]
[[b_0, b_2],
 [b_1, b_3]]
>> v = diagsplit(MX.sym('x', Sparsity.diag(4)), 2); disp(v{1}), disp(v{2})
diagsplit(x){0}
diagsplit(x){1}
>> try, diagsplit(MX.sym('x', 4, 4), 2); catch e, disp(e.message); end
...nonzeros outside the diagonal blocks...

julia> ca.diagsplit(ca.DM([1 0; 0 4]))
2-element Vector{DM}:
 DM(1)
 DM(4)
julia> v = ca.diagsplit(ca.diagcat(ca.SX.sym("a", 2, 2), ca.SX.sym("b", 2, 2)), 2);
julia> v[1]
SX(
[[a_0, a_2],
 [a_1, a_3]])
julia> v[2]
SX(
[[b_0, b_2],
 [b_1, b_3]])
julia> ca.diagsplit(ca.MX.sym("x", ca.Sparsity.diag(4)), 2)
2-element Vector{MX}:
 MX(diagsplit(x){0})
 MX(diagsplit(x){1})
julia> try
           ca.diagsplit(ca.MX.sym("x", 4, 4), 2)
       catch err
           println(occursin("nonzeros outside the diagonal blocks", sprint(showerror, err)))
       end
true

See also
diagcat

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

Definition at line 1201 of file sparsity_interface.hpp.

1201  {
1202  return MatType::diagsplit(x, incr);
1203  }

◆ diagsplit [3/4]

template<typename MatType >
std::vector< MatType > diagsplit ( const MatType &  x,
const std::vector< casadi_int > &  output_offset 
)
friend

Same as the two-offset variant with output_offset used for both rows and columns. An error is raised when x is not square.

Parameters
xSquare matrix (or Sparsity) to split
output_offsetBoundaries of the blocks

>>> x = DM([[1, 2, 0], [3, 4, 0], [0, 0, 5]])
>>> diagsplit(x, [0, 2, 3])
[DM(
[[1, 2],
 [3, 4]]), DM(5)]
>>> diagsplit(DM.ones(2, 3), [0, 2])
Traceback (most recent call last):
  ...
RuntimeError: ...must be square...

>> x = DM([1 2 0; 3 4 0; 0 0 5]);
>> v = diagsplit(x, [0, 2, 3]); disp(v{1}), disp(v{2})
[[1, 2],
 [3, 4]]
5
>> try, diagsplit(DM.ones(2, 3), [0, 2]); catch e, disp(e.message); end
...must be square...

julia> x = ca.DM([1 2 0; 3 4 0; 0 0 5]);
julia> v = ca.diagsplit(x, [0, 2, 3]);
julia> v[1]
DM(
[[1, 2],
 [3, 4]])
julia> v[2]
DM(5)
julia> try
           ca.diagsplit(ca.DM.ones(2, 3), [0, 2])
       catch err
           println(occursin("must be square", sprint(showerror, err)))
       end
true

See also
diagcat

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

Definition at line 1117 of file sparsity_interface.hpp.

1117  {
1118  return MatType::diagsplit(x, output_offset);
1119  }

◆ diagsplit [4/4]

template<typename MatType >
std::vector< MatType > diagsplit ( const MatType &  x,
const std::vector< casadi_int > &  output_offset1,
const std::vector< casadi_int > &  output_offset2 
)
friend

Block i is x[output_offset1[i]:output_offset1[i+1], output_offset2[i]:output_offset2[i+1]]. Both lists must have the same length, start at 0, be non-decreasing and end at the row (column) count of x. Entries of x outside the diagonal blocks must be zero (structurally zero for MX and Sparsity), otherwise an error is raised. Inverse of diagcat: diagcat(diagsplit(x, ...)) == x for block-diagonal x.

Parameters
xMatrix (or Sparsity) to split; any shape
output_offset1Row boundaries of the blocks
output_offset2Column boundaries of the blocks
Returns
List of matrices of the same type as x, one per block

>>> x = DM([[1, 2, 0], [0, 0, 6]])
>>> diagsplit(x, [0, 1, 2], [0, 2, 3])
[DM([[1, 2]]), DM(6)]

>> x = DM([1 2 0; 0 0 6]);
>> v = diagsplit(x, [0, 1, 2], [0, 2, 3]); disp(v{1}), disp(v{2})
[[1, 2]]
6

julia> x = ca.DM([1 2 0; 0 0 6]);
julia> ca.diagsplit(x, [0, 1, 2], [0, 2, 3])
2-element Vector{DM}:
 DM([[1, 2]])
 DM(6)

See also
diagcat

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

Definition at line 1054 of file sparsity_interface.hpp.

1056  {
1057  return MatType::diagsplit(x, output_offset1, output_offset2);
1058  }

◆ diff

template<typename MatType >
MatType diff ( const MatType &  x,
casadi_int  n = 1,
casadi_int  axis = -1 
)
friend

Each pass subtracts neighbouring rows (axis 0) or columns (axis 1), shrinking that dimension by one; n passes are applied. With axis -1 the first non-singleton dimension is used (columns for a row vector, rows otherwise) and, as in MATLAB, a scalar gives an empty 0-by-0 result.

Parameters
nOrder, at least 1
axis0 (rows), 1 (columns) or -1 (automatic); 1 and 2 in MATLAB

>>> diff(DM([1, 4, 9, 16]))
DM([3, 5, 7])
>>> diff(DM([1, 4, 9, 16]), 2)
DM([2, 2])
>>> diff(DM([[1, 2], [4, 8], [9, 10]]), 1, 1)
DM([1, 4, 1])
>>> diff(SX.sym("x", 3))
SX([(x_1-x_0), (x_2-x_1)])
>>> diff(DM(3)).dim()
'0x0'

>> diff(DM([1; 4; 9; 16]))
[3, 5, 7]
>> diff(DM([1; 4; 9; 16]), 2)
[2, 2]
>> diff(DM([1 2; 4 8; 9 10]), 1, 2)
[1, 4, 1]
>> diff(SX.sym('x', 3))
[(x_1-x_0), (x_2-x_1)]
>> d = diff(DM(3)); disp(d.dim())
0x0

julia> diff(ca.DM([1, 4, 9, 16]))
DM([3, 5, 7])
julia> ca.diff(ca.DM([1, 4, 9, 16]), 2)
DM([2, 2])
julia> diff(ca.DM([1 2; 4 8; 9 10]); dims=2)
DM([1, 4, 1])
julia> diff(ca.SX.sym("x", 3))
SX([(x_1-x_0), (x_2-x_1)])
julia> ca.dim(diff(ca.DM(3)))
"0x1"

See also
cumsum

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

Definition at line 2128 of file generic_matrix.hpp.

2128  {
2129  return MatType::diff(x, n, axis);
2130  }

◆ difference

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

Comparison is by node identity, not by value: two separately built expressions with the same formula are different nodes. Duplicates in a are kept.

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> difference([x, y, x], [y]), difference([x + 1], [x + 1])
([MX(x), MX(x)], [MX((1+x))])

>> x = MX.sym('x'); y = MX.sym('y');
>> d = difference({x, y, x}, {y});
>> numel(d), d{1}, d{2}
2
x
x
>> d = difference({x + 1}, {x + 1});
>> numel(d), d{1}
1
(1+x)

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> ca.difference([x, y, x], [y]), ca.difference([x + 1], [x + 1])
(MX[x, x], MX[(1+x)])

See also
symvar

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

Definition at line 4674 of file mx.hpp.

4674  {
4675  return MX::difference(a, b);
4676  }
static std::vector< MX > difference(const std::vector< MX > &a, const std::vector< MX > &b)
Definition: mx.cpp:3092

◆ dot

template<typename MatType >
MatType dot ( const MatType &  x,
const MatType &  y 
)
friend

Sum of the elementwise products over all entries, with x and y of the same shape (a matrix is treated as the vector of its entries). Only entries that are structurally nonzero in both arguments contribute. A shape mismatch raises an error.

>>> dot(DM([1, 2]), DM([3, 4]))
DM(11)
>>> dot(DM([[1, 2], [3, 4]]), DM([[1, 1], [1, 1]]))
DM(10)
>>> dot(DM(Sparsity.diag(2), [1, 2]), DM([[1, 1], [1, 1]]))
DM(3)
>>> dot(SX.sym("x", 2), SX.sym("y", 2))
SX(((x_0*y_0)+(x_1*y_1)))

>> dot(DM([1; 2]), DM([3; 4]))
11
>> dot(DM([1 2; 3 4]), DM([1 1; 1 1]))
10
>> dot(DM(Sparsity.diag(2), [1; 2]), DM([1 1; 1 1]))
3
>> dot(SX.sym('x', 2), SX.sym('y', 2))
((x_0*y_0)+(x_1*y_1))

julia> ca.dot(ca.DM([1, 2]), ca.DM([3, 4]))
DM(11)
julia> ca.dot(ca.DM([1 2; 3 4]), ca.DM([1 1; 1 1]))
DM(10)
julia> ca.dot(ca.DM(ca.Sparsity.diag(2), [1, 2]), ca.DM([1 1; 1 1]))
DM(3)
julia> ca.dot(ca.SX.sym("x", 2), ca.SX.sym("y", 2))
SX(((x_0*y_0)+(x_1*y_1)))

See also
mtimes, sumsqr, bilin

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

Definition at line 2242 of file generic_matrix.hpp.

2242  {
2243  return MatType::dot(x, y);
2244  }

Referenced by casadi::GenericMatrix< MatType >::sumsqr().

◆ eig_symbolic

template<typename Scalar >
Matrix<Scalar> eig_symbolic ( const Matrix< Scalar > &  m)
friend

Splits the matrix into its strongly connected (block diagonal) components and solves the characteristic polynomial of each block with poly_roots, so every block may be at most 4-by-4. Larger blocks raise an error. Eigenvalues are assumed real. Returns a column vector; the order follows the block structure, not the magnitude. Only defined for SX; pass SX(m) for a numeric matrix.

>>> evalf(eig_symbolic(SX([[2, 1], [1, 2]])))
DM([1, 3])
>>> M = SX.sym("M", 2, 2)
>>> eig_symbolic(M)
SX(@1=(M_3+M_0), @2=sqrt((sq(@1)-(4*((M_0*M_3)-(M_2*M_1))))), @3=2, [((@1-@2)/@3),
((@1+@2)/@3)])
>>> evalf(eig_symbolic(SX.eye(5) * 3)).T
DM([[3, 3, 3, 3, 3]])

>> evalf(eig_symbolic(SX([2 1; 1 2])))
[1, 3]
>> a = SX.sym('a'); b = SX.sym('b'); c = SX.sym('c'); d = SX.sym('d');
>> eig_symbolic([a b; c d])
@1=(d+a), @2=sqrt((sq(@1)-(4*((a*d)-(b*c))))), @3=2, [((@1-@2)/@3), ((@1+@2)/@3)]
>> evalf(eig_symbolic(SX.eye(5) * 3))'
[[3, 3, 3, 3, 3]]

julia> ca.evalf(ca.eig_symbolic(ca.SX([2 1; 1 2])))
DM([1, 3])
julia> M = ca.SX.sym("M", 2, 2);
julia> l = ca.eig_symbolic(M); size(l)
(2, 1)
julia> l[1]
SX(@1=(M_3+M_0), ((@1-sqrt((sq(@1)-(4*((M_0*M_3)-(M_2*M_1))))))/2))
julia> l[2]
SX(@1=(M_3+M_0), ((@1+sqrt((sq(@1)-(4*((M_0*M_3)-(M_2*M_1))))))/2))
julia> ca.evalf(ca.eig_symbolic(ca.SX.eye(5) * 3))'
DM([[3, 3, 3, 3, 3]])

See also
poly_roots, Sparsity::scc

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

Definition at line 2720 of file matrix_decl.hpp.

2720  {
2721  return Matrix<Scalar>::eig_symbolic(m);
2722  }
static Matrix< Scalar > eig_symbolic(const Matrix< Scalar > &m)

◆ einstein [1/2]

template<typename MatType >
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 
)
friend

Given two tensors, A and B, computes a third tensor C such that:

C_c = A_a * B_b

With a, b, c representing einstein indices. Instead of the classical index labels i,j,k,... we employ -1,-2,-3,... An index that appears in a or b but not in c is summed over.

A, B, C are represented as CasADi column vectors (column-major flattening), with dim_a, dim_b, dim_c indicating their tensorial dimensions. The product of dim_a must equal the number of elements of A, and likewise for B and C.

In the overload with an explicit C argument, the contraction is added to C.

>>> A = DM([1, 2, 3, 4])
>>> einstein(A, A, [2, 2], [2, 2], [2, 2], [-1, -2], [-2, -3], [-1, -3])
DM([7, 10, 15, 22])
>>> mtimes(DM([[1, 3], [2, 4]]), DM([[1, 3], [2, 4]]))
DM(
[[7, 15],
 [10, 22]])
>>> einstein(DM([1, 2, 3]), DM([4, 5, 6]), [3], [3], [], [-1], [-1], [])
DM(32)

>> A = DM([1; 2; 3; 4]);
>> einstein(A, A, [2, 2], [2, 2], [2, 2], [-1, -2], [-2, -3], [-1, -3])
[7, 10, 15, 22]
>> DM([1 3; 2 4])*DM([1 3; 2 4])
[[7, 15],
 [10, 22]]
>> einstein(DM([1; 2; 3]), DM([4; 5; 6]), 3, 3, [], -1, -1, [])
32

julia> A = ca.DM([1, 2, 3, 4]);
julia> ca.einstein(A, A, [2, 2], [2, 2], [2, 2], [-1, -2], [-2, -3], [-1, -3])
DM([7, 10, 15, 22])
julia> ca.DM([1 3; 2 4]) * ca.DM([1 3; 2 4])
DM(
[[7, 15],
 [10, 22]])
julia> ca.einstein(ca.DM([1, 2, 3]), ca.DM([4, 5, 6]), [3], [3], Int[], [-1], [-1], Int[])
DM(32)

See also
mtimes, dot

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

Definition at line 1002 of file generic_matrix.hpp.

1006  {
1007  return MatType::einstein(A, B, C, dim_a, dim_b, dim_c, a, b, c);
1008  }

◆ einstein [2/2]

template<typename MatType >
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 
)
friend

Given two tensors, A and B, computes a third tensor C such that:

C_c = A_a * B_b

With a, b, c representing einstein indices. Instead of the classical index labels i,j,k,... we employ -1,-2,-3,... An index that appears in a or b but not in c is summed over.

A, B, C are represented as CasADi column vectors (column-major flattening), with dim_a, dim_b, dim_c indicating their tensorial dimensions. The product of dim_a must equal the number of elements of A, and likewise for B and C.

In the overload with an explicit C argument, the contraction is added to C.

>>> A = DM([1, 2, 3, 4])
>>> einstein(A, A, [2, 2], [2, 2], [2, 2], [-1, -2], [-2, -3], [-1, -3])
DM([7, 10, 15, 22])
>>> mtimes(DM([[1, 3], [2, 4]]), DM([[1, 3], [2, 4]]))
DM(
[[7, 15],
 [10, 22]])
>>> einstein(DM([1, 2, 3]), DM([4, 5, 6]), [3], [3], [], [-1], [-1], [])
DM(32)

>> A = DM([1; 2; 3; 4]);
>> einstein(A, A, [2, 2], [2, 2], [2, 2], [-1, -2], [-2, -3], [-1, -3])
[7, 10, 15, 22]
>> DM([1 3; 2 4])*DM([1 3; 2 4])
[[7, 15],
 [10, 22]]
>> einstein(DM([1; 2; 3]), DM([4; 5; 6]), 3, 3, [], -1, -1, [])
32

julia> A = ca.DM([1, 2, 3, 4]);
julia> ca.einstein(A, A, [2, 2], [2, 2], [2, 2], [-1, -2], [-2, -3], [-1, -3])
DM([7, 10, 15, 22])
julia> ca.DM([1 3; 2 4]) * ca.DM([1 3; 2 4])
DM(
[[7, 15],
 [10, 22]])
julia> ca.einstein(ca.DM([1, 2, 3]), ca.DM([4, 5, 6]), [3], [3], Int[], [-1], [-1], Int[])
DM(32)

See also
mtimes, dot

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

Definition at line 1011 of file generic_matrix.hpp.

1015  {
1016  return MatType::einstein(A, B, dim_a, dim_b, dim_c, a, b, c);
1017  }

◆ eq

template<typename ExType >
ExType eq ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt: this is value equality of the entries, not identity of expressions; use is_equal to test whether two symbolic expressions are the same. For symbolic operands x == x with the same node simplifies to 1. NaN is not equal to anything, including itself.

>>> eq(DM([1, 2, 3]), 2)
DM([0, 1, 0])
>>> DM.eye(2) == DM([[1, 0], [0, 1]])
DM(
[[1, 1],
 [1, 1]])
>>> x = SX.sym("x")
>>> x == x, x == SX.sym("x")
(SX(1), SX((x==x)))
>>> DM(float("nan")) == DM(float("nan"))
DM(0)

>> eq(DM([1; 2; 3]), 2)
[0, 1, 0]
>> DM.eye(2) == DM([1 0; 0 1])
[[1, 1],
 [1, 1]]
>> x = SX.sym('x');
>> x == x, x == SX.sym('x')
1
(x==x)
>> DM(nan) == DM(nan)
0

julia> ca.eq(ca.DM([1, 2, 3]), 2)
DM([0, 1, 0])
julia> ca.DM.eye(2) == ca.DM([1 0; 0 1])
DM(
[[1, 1],
 [1, 1]])
julia> x = ca.SX.sym("x");
julia> x == x, x == ca.SX.sym("x")
(1, (x==x))
julia> ca.DM(NaN) == ca.DM(NaN)
DM(0)

See also
ne, is_equal

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

Definition at line 644 of file generic_expression.hpp.

644  {
645  return ExType::eq(x, y);
646  }

◆ erf

template<typename ExType >
ExType erf ( const ExType &  x)
friend

Elementwise; sparsity preserved (erf(0) == 0). Range (-1, 1).

>>> erf(DM([0, 1]))
DM([0, 0.842701])

>> erf(DM([0; 1]))
[0, 0.842701]

julia> ca.erf(ca.DM([0, 1]))
DM([0, 0.842701])

See also
erfinv

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

Definition at line 1829 of file generic_expression.hpp.

1829  {
1830  return ExType::erf(x);
1831  }

◆ erfinv

template<typename ExType >
ExType erfinv ( const ExType &  x)
friend

Elementwise; sparsity preserved. Defined on [-1, 1] with erfinv(+-1) infinite; arguments outside give NaN.

>>> erfinv(DM([0, 0.5, 1]))
DM([0, 0.476936, inf])

>> erfinv(DM([0; 0.5; 1]))
[0, 0.476936, inf]

julia> ca.erfinv(ca.DM([0, 0.5, 1]))
DM([0, 0.476936, inf])

See also
erf

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

Definition at line 1867 of file generic_expression.hpp.

1867  {
1868  return ExType::erfinv(x);
1869  }

◆ evalf [1/2]

template<typename Scalar >
Matrix<double> evalf ( const Matrix< Scalar > &  expr)
friend

Works on SX and MX expressions without free symbols (including constant results of function calls). The sparsity pattern is preserved. An expression that still depends on a symbol raises an error, unlike the DM(SX) constructor which yields nan.

>>> evalf(SX([[1, 2], [3, 4]]) * 2)
DM(
[[2, 4],
 [6, 8]])
>>> evalf(MX(DM([1, 2])) + 1)
DM([2, 3])
>>> try: evalf(SX.sym("x"))
... except Exception as e: print("error")
error

>> evalf(SX([1 2; 3 4]) * 2)
[[2, 4],
 [6, 8]]
>> evalf(MX(DM([1; 2])) + 1)
[2, 3]
>> try, evalf(SX.sym('x')); catch e, disp('error'); end
error

julia> ca.evalf(ca.SX([1 2; 3 4]) * 2)
DM(
[[2, 4],
 [6, 8]])
julia> ca.evalf(ca.MX(ca.DM([1, 2])) + 1)
DM([2, 3])
julia> try ca.evalf(ca.SX.sym("x")); catch; println("error") end
error

See also
substitute, Function

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

Definition at line 2773 of file matrix_decl.hpp.

2773  {
2774  return Matrix<Scalar>::evalf(expr);
2775  }
static Matrix< double > evalf(const Matrix< Scalar > &m)

◆ evalf [2/2]

DM evalf ( const MX &  expr)
friend

Builds a Function without inputs around expr and evaluates it. Function calls, solver calls and sparse constants are handled. An error is raised when the expression depends on a symbolic primitive.

>>> evalf(MX(DM([[1, 2], [3, 4]])) @ MX([1, 1]))
DM([3, 7])
>>> evalf(MX(Sparsity.lower(2)) * 2)
DM(
[[2, 00],
 [2, 2]])
>>> try:
...   evalf(MX.sym("x") + 1)
... except Exception as e:
...   print("are free" in str(e))
True

>> evalf(MX(DM([1 2; 3 4])) * MX([1; 1]))
[3, 7]
>> evalf(MX(Sparsity.lower(2)) * 2)
[[2, 00],
 [2, 2]]
>> try, evalf(MX.sym('x') + 1); catch e, disp('free variables'); end
free variables

julia> ca.evalf(ca.MX([1 2; 3 4]) * ca.MX([1, 1]))
DM([3, 7])
julia> ca.evalf(ca.MX(ca.Sparsity.lower(2)) * 2)
DM(
[[2, 00],
 [2, 2]])
julia> try
           ca.evalf(ca.MX.sym("x") + 1)
       catch e
           println(occursin("are free", sprint(showerror, e)))
       end
true

See also
to_DM, Function

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

Definition at line 4451 of file mx.hpp.

4451  {
4452  return MX::evalf(expr);
4453  }
static DM evalf(const MX &m)
Definition: mx.cpp:771

◆ exp

template<typename ExType >
ExType exp ( const ExType &  x)
friend

Elementwise; the result is dense (exp(0) == 1).

>>> exp(DM.eye(2))
DM(
[[2.71828, 1],
 [1, 2.71828]])
>>> exp(MX.sym("m", Sparsity.diag(2)))
MX(exp(dense(m)))

>> exp(DM.eye(2))
[[2.71828, 1],
 [1, 2.71828]]
>> exp(MX.sym('m', Sparsity.diag(2)))
exp(dense(m))

julia> exp.(ca.DM.eye(2))
DM(
[[2.71828, 1],
 [1, 2.71828]])
julia> exp.(ca.MX.sym("m", ca.Sparsity.diag(2)))
MX(exp(dense(m)))

See also
log, expm1

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

Definition at line 1534 of file generic_expression.hpp.

1534  {
1535  return ExType::exp(x);
1536  }

◆ expand

template<typename Scalar >
void expand ( const Matrix< Scalar > &  ex,
Matrix< Scalar > &  weights,
Matrix< Scalar > &  terms 
)
friend

Collects additions, subtractions and multiplications by constants so that ex == dot(weights, terms), merging repeated terms. Any other operation (a product of two non-constant factors, a function call, ...) is kept as a single term with weight 1. The input must be a scalar SX; DM is not supported.

Returns
weights and terms, both column vectors (a tuple in Python)

>>> x = SX.sym("x"); y = SX.sym("y")
>>> w, t = expand(3*x - x + 5*y)
>>> w, t
(SX([2, 5]), SX([x, y]))
>>> w, t = expand(3*(x + y) + 1)
>>> t
SX([x, y, 1])

>> x = SX.sym('x'); y = SX.sym('y');
>> [w, t] = expand(3*x - x + 5*y);
>> w, t
[2, 5]
[x, y]
>> [w, t] = expand(3*(x + y) + 1);
>> t
[x, y, 1]

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> w, t = ca.expand(3*x - x + 5*y);
julia> w
SX([2, 5])
julia> t
SX([x, y])
julia> w, t = ca.expand(3*(x + y) + 1);
julia> t
SX([x, y, 1])

See also
linear_coeff

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

Definition at line 2071 of file matrix_decl.hpp.

2072  {
2073  Matrix<Scalar>::expand(ex, weights, terms);
2074  }
static void expand(const Matrix< Scalar > &x, Matrix< Scalar > &weights, Matrix< Scalar > &terms)

◆ expm

template<typename MatType >
MatType expm ( const MatType &  A)
friend

Computes expm(A) for a square matrix through the 'slicot' expm plugin, which must be available in the build. Only defined for DM and MX (SX raises an error).

See also
expm_const, expmsol, mpower

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

Definition at line 3434 of file generic_matrix.hpp.

3434  {
3435  return MatType::expm(A);
3436  }

◆ expm1

template<typename ExType >
ExType expm1 ( const ExType &  x)
friend

Elementwise; accurate for small x where exp(x) - 1 would lose digits. Sparsity preserved (expm1(0) == 0).

>>> expm1(DM([0, 1e-10]))
DM([0, 1e-10])

>> expm1(DM([0; 1e-10]))
[0, 1e-10]

julia> expm1.(ca.DM([0, 1e-10]))
DM([0, 1e-10])

See also
exp, log1p

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

Definition at line 1704 of file generic_expression.hpp.

1704  {
1705  return ExType::expm1(x);
1706  }

◆ expm_const

template<typename MatType >
MatType expm_const ( const MatType &  A,
const MatType &  t 
)
friend

Computes expm(A*t) with A constant, through the 'slicot' expm plugin, which must be available in the build. Only defined for DM and MX (SX raises an error).

Parameters
ASquare matrix
tScalar
See also
expm, expmsol

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

Definition at line 3422 of file generic_matrix.hpp.

3422  {
3423  return MatType::expm_const(A, t);
3424  }

◆ extract

template<typename MatType >
void extract ( std::vector< MatType > &  ex,
std::vector< MatType > &  v,
std::vector< MatType > &  vdef,
const Dict &  opts = Dict() 
)
friend

Rewrites the expressions ex so that selected intermediate results are replaced by new symbols v with definitions vdef (ex, v and vdef are outputs; in Python the call is extract(ex, opts) -> (ex, v, vdef)). Substituting vdef for v in the result gives back the original expressions. Only nodes that are physically shared in the graph are detected: two separately constructed copies of x+y are distinct nodes (apply cse first to merge them). Not defined for DM.

Options:

  • lift_shared (bool), default true: extract nodes used more than once
  • lift_calls (bool), default false: extract every Function call and its arguments (MX only; SX raises for lift_calls true or lift_shared false)
  • prefix (str), default 'v_', suffix (str), default '', offset (int), default 0: naming of the new symbols

>>> x = SX.sym("x"); y = SX.sym("y")
>>> e = x + y
>>> extract([sin(e), cos(e)])
([SX(sin(v_0)), SX(cos(v_0))], [SX(v_0)], [SX((x+y))])
>>> extract([sin(e), cos(e)], {"prefix": "w_", "offset": 3})
([SX(sin(w_3)), SX(cos(w_3))], [SX(w_3)], [SX((x+y))])
>>> extract([sin(x + y), cos(x + y)])
([SX(sin((x+y))), SX(cos((x+y)))], [], [])
>>> X = MX.sym("X"); Y = MX.sym("Y")
>>> f = Function("f", [X], [sin(X)]); E = X + Y
>>> extract([f(E)*f(E)], {"lift_shared": False, "lift_calls": True})
([MX((v_1*v_2))], [MX(v_0), MX(v_1), MX(v_2)], [MX((X+Y)), MX(f(v_0){0}), MX(f(v_0){0})])

>> x = SX.sym('x'); y = SX.sym('y');
>> e = x + y;
>> [ex, v, vdef] = extract({sin(e), cos(e)});
>> ex{1}, ex{2}, v{1}, vdef{1}
sin(v_0)
cos(v_0)
v_0
(x+y)
>> [ex, v, vdef] = extract({sin(e), cos(e)}, struct('prefix', 'w_', 'offset', 3));
>> ex{1}, v{1}
sin(w_3)
w_3
>> [ex, v, vdef] = extract({sin(x + y), cos(x + y)});
>> ex{1}, numel(v)
sin((x+y))
0
>> X = MX.sym('X'); Y = MX.sym('Y');
>> f = Function('f', {X}, {sin(X)}); E = X + Y;
>> opts = struct('lift_shared', false, 'lift_calls', true);
>> [ex, v, vdef] = extract({f(E)*f(E)}, opts);
>> ex{1}, numel(v), vdef{2}
(v_1*v_2)
3
f(v_0){0}

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> e = x + y;
julia> ca.extract([sin(e), cos(e)])
(SX[sin(v_0), cos(v_0)], SX[v_0], SX[(x+y)])
julia> ca.extract([sin(e), cos(e)], Dict("prefix" => "w_", "offset" => 3))
(SX[sin(w_3), cos(w_3)], SX[w_3], SX[(x+y)])
julia> ca.extract([sin(x + y), cos(x + y)])
(SX[sin((x+y)), cos((x+y))], SX[], SX[])
julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
julia> f = ca.Function("f", [X], [sin(X)]); E = X + Y;
julia> ca.extract([f(E) * f(E)], Dict("lift_shared" => false, "lift_calls" => true))
(MX[(v_1*v_2)], MX[v_0, v_1, v_2], MX[(X+Y), f(v_0){0}, f(v_0){0}])

See also
shared, cse, substitute

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

Definition at line 4666 of file generic_matrix.hpp.

4669  {
4670  MatType::extract(ex, v, vdef, opts);
4671  }

◆ extract_parametric [1/4]

template<typename MatType >
void extract_parametric ( const MatType &  expr,
const MatType &  par,
MatType &  expr_ret,
std::vector< MatType > &  symbols,
std::vector< MatType > &  parametric,
const Dict &  opts = Dict() 
)
friend

The purpose of extract_parametric is ultimately to save on evaluation time of an expression, by extracting out the parts that are only solely dependent on parameters.

For any: [expr_ret, symbols, parametric] = extract_parametric(expr, par) It holds that: substitute(expr_ret,symbols,parametric) == expr

parametric is only dependant on par expr_ret is not dependant on par, but is dependant on symbols

Example: [expr_ret, symbols, parametric] = extract_parametric((x-sqrt(p))*y+cos(p)**2, p)

expr_ret: (((x-extracted1)*y)+extracted2) symbols: [extracted1, extracted2] parametric: [sqrt(p),cos(p)**2]

Options to control the name of new symbols:

  • offset (int), default 0: Offset for new symbol counter
  • prefix (str), default 'e_': Prefix for new symbols
  • suffix (str), default '': Suffix for new symbols

Option 'extract_trivial' controls the behaviour for extracted parts that consist of pure symbols. If false (default), these parts are not extracted.

expr may also be a list of expressions and par a list of parameter symbols (concatenated internally). An unknown option raises an error. Not defined for DM.

* >>> x = SX.sym("x"); y = SX.sym("y"); p = SX.sym("p")
* >>> expr_ret, symbols, parametric = extract_parametric((x - sqrt(p))*y + cos(p)**2, p)
* >>> expr_ret
* SX((((x-e_0)*y)+e_1))
* >>> symbols, parametric
* ([SX(e_0), SX(e_1)], [SX(sqrt(p)), SX(sq(cos(p)))])
* >>> extract_parametric(x*p, p)
* (SX((x*p)), [], [])
* >>> extract_parametric(x*p, p, {"extract_trivial": True})
* (SX((x*e_0)), [SX(e_0)], [SX(p)])
* 

* >> x = SX.sym('x'); y = SX.sym('y'); p = SX.sym('p');
* >> [expr_ret, symbols, parametric] = extract_parametric((x - sqrt(p))*y + cos(p)^2, p);
* >> expr_ret
* (((x-e_0)*y)+e_1)
* >> vertcat(symbols{:}), vertcat(parametric{:})
* [e_0, e_1]
* [sqrt(p), sq(cos(p))]
* >> [expr_ret, symbols, parametric] = extract_parametric(x*p, p);
* >> expr_ret, numel(symbols)
* (x*p)
* 0
* >> opts = struct('extract_trivial', true);
* >> [expr_ret, symbols, parametric] = extract_parametric(x*p, p, opts);
* >> expr_ret, symbols{1}, parametric{1}
* (x*e_0)
* e_0
* p
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); p = ca.SX.sym("p");
* julia> e = (x - sqrt(p)) * y + cos(p)^2;
* julia> expr_ret, symbols, parametric = ca.extract_parametric(e, p);
* julia> expr_ret
* SX((((x-e_0)*y)+e_1))
* julia> symbols, parametric
* (SX[e_0, e_1], SX[sqrt(p), sq(cos(p))])
* julia> ca.extract_parametric(x * p, p)
* ((x*p), SX[], SX[])
* julia> ca.extract_parametric(x * p, p, Dict("extract_trivial" => true))
* ((x*e_0), SX[e_0], SX[p])
* 

See also
separate_linear, substitute

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

Definition at line 4272 of file generic_matrix.hpp.

4276  {
4277  MatType::extract_parametric(expr, par, expr_ret, symbols, parametric, opts);
4278  }

◆ extract_parametric [2/4]

template<typename MatType >
void extract_parametric ( const MatType &  expr,
const std::vector< MatType > &  par,
MatType &  expr_ret,
std::vector< MatType > &  symbols,
std::vector< MatType > &  parametric,
const Dict &  opts = Dict() 
)
friend

Definition at line 4316 of file generic_matrix.hpp.

4320  {
4321  extract_parametric(expr, veccat(par), expr_ret, symbols, parametric, opts);
4322  }
static MatType veccat(const std::vector< MatType > &x)
friend void extract_parametric(const MatType &expr, const MatType &par, MatType &expr_ret, std::vector< MatType > &symbols, std::vector< MatType > &parametric, const Dict &opts=Dict())
Extract purely parametric parts from an expression graph.

◆ extract_parametric [3/4]

template<typename MatType >
void extract_parametric ( const std::vector< MatType > &  expr,
const MatType &  par,
std::vector< MatType > &  expr_ret,
std::vector< MatType > &  symbols,
std::vector< MatType > &  parametric,
const Dict &  opts = Dict() 
)
friend

Definition at line 4280 of file generic_matrix.hpp.

4284  {
4285  // Concatenate all vector elements
4286  MatType expr_cat = veccat(expr);
4287  MatType expr_ret_cat;
4288 
4289  // Concatenated extract_parametric
4290  MatType::extract_parametric(expr_cat, par, expr_ret_cat, symbols, parametric, opts);
4291 
4292  // Compute edges of vertsplit needed to undo concatenate
4293  std::vector<casadi_int> edges = {0};
4294  for (const MatType& e : expr) {
4295  edges.push_back(edges.back() + e.numel());
4296  }
4297  // Perform vertsplit
4298  std::vector<MatType> expr_ret_catv = MatType::vertsplit(expr_ret_cat, edges);
4299 
4300  // Reshape all elements back into original size
4301  expr_ret.resize(expr_ret_catv.size());
4302  for (casadi_int i=0; i<expr_ret_catv.size(); ++i) {
4303  expr_ret[i] = reshape(expr_ret_catv[i], expr[i].size1(), expr[i].size2());
4304  }
4305  }
casadi_int size2() const
Get the second dimension (i.e. number of columns)
casadi_int size1() const
Get the first dimension (i.e. number of rows)
friend MatType reshape(const MatType &x, casadi_int nrow, casadi_int ncol)
Returns a reshaped version of the matrix.

◆ extract_parametric [4/4]

template<typename MatType >
void extract_parametric ( const std::vector< MatType > &  expr,
const std::vector< MatType > &  par,
std::vector< MatType > &  expr_ret,
std::vector< MatType > &  symbols,
std::vector< MatType > &  parametric,
const Dict &  opts = Dict() 
)
friend

Definition at line 4307 of file generic_matrix.hpp.

4312  {
4313  extract_parametric(expr, veccat(par), expr_ret, symbols, parametric, opts);
4314  }

◆ fabs

template<typename ExType >
ExType fabs ( const ExType &  x)
friend

Elementwise; structural zeros are preserved. The unary functions of this group operate on every entry, keep the shape and build a symbolic expression for SX and MX. Those that map 0 to 0 (abs, sqrt, sin, tan, atan, asin, sinh, tanh, asinh, atanh, floor, ceil, sign, erf, erfinv, log1p, expm1) keep the sparsity pattern; the others (cos, cosh, exp, log, log10, acos, acosh, logic_not) return a dense result. Named fabs in Python, where the built-in abs also applies.

>>> fabs(DM([-1, 0, 2]))
DM([1, 0, 2])
>>> fabs(DM.eye(2) * -1)
DM(
[[1, 00],
 [00, 1]])
>>> abs(SX.sym("x"))
SX(fabs(x))

>> abs(DM([-1; 0; 2]))
[1, 0, 2]
>> abs(-DM.eye(2))
[[1, 00],
 [00, 1]]
>> abs(SX.sym('x'))
fabs(x)

julia> abs.(ca.DM([-1, 0, 2]))
DM([1, 0, 2])
julia> abs(-ca.DM.eye(2))
DM(
[[1, 00],
 [00, 1]])
julia> abs(ca.SX.sym("x"))
SX(fabs(x))

See also
sign, copysign

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

Definition at line 931 of file generic_expression.hpp.

931  {
932  return abs(x);
933  }
static ExType abs(const ExType &x)
Absolute value: x -> abs(x)

◆ find

MX find ( const MX &  x)
friend

Returns a 1-by-1 expression that evaluates to the index of the first entry of x whose value is not zero, or to the length of x when all entries are zero. Structural zeros are skipped. The result is piecewise constant, so its derivative is zero. x must be a column or row vector; matrices raise an error.

>>> v = MX.sym("v", 3)
>>> f = Function("f", [v], [find(v)])
>>> find(v).shape, f(DM([0, 3, 1])), f(DM([0, 0, 0]))
((1, 1), DM(1), DM(3))
>>> evalf(find(MX(DM(Sparsity.unit(3, 2), [1]))))
DM(2)
>>> try:
...   find(MX.sym("A", 2, 2))
... except Exception as e:
...   print("Argument must be vector" in str(e))
True

>> v = MX.sym('v', 3);
>> f = Function('f', {v}, {find(v)});
>> size(find(v)), f(DM([0; 3; 1])), f(DM([0; 0; 0]))
1 1
1
3
>> evalf(find(MX(DM(Sparsity.unit(3, 2), 1))))
2
>> try, find(MX.sym('A', 2, 2)); catch e, disp('argument must be vector'); end
argument must be vector

julia> v = ca.MX.sym("v", 3);
julia> f = ca.Function("f", [v], [ca.find(v)]);
julia> size(ca.find(v)), f([0, 3, 1]), f([0, 0, 0])
((1, 1), 1, 3)
julia> ca.evalf(ca.find(ca.MX(ca.DM(ca.Sparsity.unit(3, 2), [1]))))
DM(2)
julia> try
           ca.find(ca.MX.sym("A", 2, 2))
       catch e
           println(occursin("Argument must be vector", sprint(showerror, e)))
       end
true

See also
low

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

Definition at line 3732 of file mx.hpp.

3732  {
3733  return MX::find(x);
3734  }
static MX find(const MX &x)
Definition: mx.cpp:2209

◆ floor

template<typename ExType >
ExType floor ( const ExType &  x)
friend

Elementwise, towards minus infinity; sparsity preserved. The derivative is zero everywhere. Floor division x // y in Python is floor(x / y).

>>> floor(DM([-1.5, 1.5]))
DM([-2, 1])
>>> DM([7, -7]) // 2
DM([3, -4])
>>> SX.sym("x") // 2
SX(floor((x/2)))

>> floor(DM([-1.5; 1.5]))
[-2, 1]
>> floor(DM([7; -7]) ./ 2)
[3, -4]
>> floor(SX.sym('x') / 2)
floor((x/2))

julia> floor.(ca.DM([-1.5, 1.5]))
DM([-2, 1])
julia> fld.(ca.DM([7, -7]), 2)
DM([3, -4])
julia> floor(ca.SX.sym("x") / 2)
SX(floor((x/2)))

See also
ceil, sign

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

Definition at line 1754 of file generic_expression.hpp.

1754  {
1755  return ExType::floor(x);
1756  }

◆ fmax

template<typename ExType >
ExType fmax ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise maximum; shape rules as for plus. Follows C fmax: when one operand is NaN the other is returned. The derivative is 1 with respect to the larger operand (split evenly on ties). For the maximum over the entries of one matrix use mmax.

>>> fmax(DM([1, 5]), DM([3, 2]))
DM([3, 5])
>>> fmax(DM([1, 5]), 3)
DM([3, 5])
>>> fmax(DM([1, float("nan")]), DM([float("nan"), 2]))
DM([1, 2])
>>> fmax(MX.sym("x"), MX.sym("y"))
MX(fmax(x,y))

>> fmax(DM([1; 5]), DM([3; 2]))
[3, 5]
>> max(DM([1; 5]), 3)
[3, 5]
>> fmax(DM([1; nan]), DM([nan; 2]))
[1, 2]
>> max(MX.sym('x'), MX.sym('y'))
fmax(x,y)

julia> max.(ca.DM([1, 5]), ca.DM([3, 2]))
DM([3, 5])
julia> max.(ca.DM([1, 5]), 3)
DM([3, 5])
julia> max.(ca.DM([1, NaN]), ca.DM([NaN, 2]))
DM([1, 2])
julia> max(ca.MX.sym("x"), ca.MX.sym("y"))
MX(fmax(x,y))

See also
fmin, mmax

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

Definition at line 2292 of file generic_expression.hpp.

2292  {
2293  return ExType::fmax(x, y);
2294  }

◆ fmin

template<typename ExType >
ExType fmin ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise minimum; shape rules as for plus. Follows C fmin: when one operand is NaN the other is returned, NaN results only when both are NaN. The derivative is 1 with respect to the smaller operand (split evenly on ties). The sparsity pattern is kept where both operands are structurally zero; fmin with a negative scalar densifies. For the minimum over the entries of one matrix use mmin.

>>> fmin(DM([1, 5]), DM([3, 2]))
DM([1, 2])
>>> fmin(DM([1, float("nan")]), DM([float("nan"), 2]))
DM([1, 2])
>>> fmin(DM.eye(2), 1)
DM(
[[1, 00],
 [00, 1]])
>>> fmin(SX.sym("x"), SX.sym("y"))
SX(fmin(x,y))

>> fmin(DM([1; 5]), DM([3; 2]))
[1, 2]
>> min(DM([1; 5]), DM([3; 2]))
[1, 2]
>> fmin(DM([1; nan]), DM([nan; 2]))
[1, 2]
>> fmin(DM.eye(2), 1)
[[1, 00],
 [00, 1]]
>> min(SX.sym('x'), SX.sym('y'))
fmin(x,y)

julia> min.(ca.DM([1, 5]), ca.DM([3, 2]))
DM([1, 2])
julia> min.(ca.DM([1, NaN]), ca.DM([NaN, 2]))
DM([1, 2])
julia> min.(ca.DM.eye(2), 1)
DM(
[[1, 00],
 [00, 1]])
julia> min(ca.SX.sym("x"), ca.SX.sym("y"))
SX(fmin(x,y))

See also
fmax, mmin

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

Definition at line 2235 of file generic_expression.hpp.

2235  {
2236  return ExType::fmin(x, y);
2237  }

◆ fmod

template<typename ExType >
ExType fmod ( const ExType &  x,
const ExType &  y 
)
friend

This Function follows the convention of https://en.cppreference.com/w/c/numeric/math/fmod: the result has the sign of x and magnitude below |y|.

This is equivalent to Python's numpy.fmod and Matlab's rem. It differs from the Python operator x % y on these objects, which follows the Python convention x - y*floor(x/y) (result with the sign of y). Elementwise, shape rules as for plus; structural zeros of x stay zero. A zero divisor gives NaN.

>>> fmod(DM([5, 5, -5, -5]), DM([3, -3, 3, -3]))
DM([2, 2, -2, -2])
>>> DM([5, 5, -5, -5]) % DM([3, -3, 3, -3])
DM([2, -1, 1, -2])
>>> fmod(SX.sym("x"), 2), SX.sym("x") % 2
(SX(fmod(x,2)), SX((x-(2.*floor((x/2))))))

>> x = DM([5; 5; -5; -5]); y = DM([3; -3; 3; -3]);
>> fmod(x, y)
[2, 2, -2, -2]
>> rem(x, y)
[2, 2, -2, -2]
>> x - y .* floor(x ./ y)
[2, -1, 1, -2]
>> rem(SX.sym('x'), 2)
fmod(x,2)

julia> ca.fmod(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([2, 2, -2, -2])
julia> mod.(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([2, -1, 1, -2])
julia> ca.fmod(ca.SX.sym("x"), 2)
SX(fmod(x,2))

See also
remainder, floor
Extra doc: https://github.com/casadi/casadi/wiki/L_pq 

Definition at line 2042 of file generic_expression.hpp.

2042  {
2043  return mod(x, y);
2044  }
static ExType mod(const ExType &x, const ExType &y)
Remainder after division: (x,y) -> fmod(x,y)

◆ forward

template<typename MatType >
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() 
)
friend

For each seed direction v[d] (a list with one seed per entry of arg, with matching shapes), computes the forward sensitivities of every entry of ex: the result is a list over directions of lists over ex, containing jtimes(ex[i], arg, v[d]) summed over all arguments. Not defined for DM.

Parameters
exList of expressions
argList of symbolic expressions
vList of directions, each a list of seeds matching arg
optsOptions, see jacobian

>>> x = SX.sym("x"); y = SX.sym("y")
>>> forward([x*y, x**2], [x, y], [[1, 0]])
[[SX(y), SX((2.*x))]]
>>> forward([x*y, x**2], [x, y], [[1, 0], [0, 1]])
[[SX(y), SX((2.*x))], [SX(x), SX(0)]]

>> x = SX.sym('x'); y = SX.sym('y');
>> r = forward({x*y, x^2}, {x, y}, {{1, 0}});
>> r{1}{1}, r{1}{2}
y
(2.*x)
>> r = forward({x*y, x^2}, {x, y}, {{1, 0}, {0, 1}});
>> r{2}{1}, r{2}{2}
x
0

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.forward([x*y, x^2], [x, y], [[ca.SX(1), ca.SX(0)]])
1-element Vector{Vector{SX}}:
 SX[y, (2.*x)]
julia> ca.forward([x*y, x^2], [x, y], [[ca.SX(1), ca.SX(0)], [ca.SX(0), ca.SX(1)]])
2-element Vector{Vector{SX}}:
 SX[y, (2.*x)]
 SX[x, 0]

See also
reverse, jtimes, Function::forward

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

Definition at line 3735 of file generic_matrix.hpp.

3737  {
3738  return MatType::forward(ex, arg, v, opts);
3739  }

◆ gauss_quadrature [1/2]

template<typename Scalar >
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 
)
friend

Evaluates f at the five Gauss points of [a, b] and returns the weighted sum, which is exact for polynomials in x up to degree 9. Only defined for SX.

Parameters
fScalar expression to integrate; it may depend only on x
xScalar symbolic integration variable
aLower bound, a numeric constant
bUpper bound, a numeric constant
orderMust be 5; any other value raises an error
wReserved; must be empty

>>> x = SX.sym("x")
>>> try: gauss_quadrature(x**2, x, 0, 1, 3)
... except Exception as e: print("error")
error

>> x = SX.sym('x');
>> try, gauss_quadrature(x^2, x, 0, 1, 3); catch e, disp('error'); end
error

julia> x = ca.SX.sym("x");
julia> try ca.gauss_quadrature(x^2, x, 0, 1, 3); catch; println("error") end
error

See also
integrator

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

Definition at line 2390 of file matrix_decl.hpp.

2392  {
2393  return Matrix<Scalar>::gauss_quadrature(f, x, a, b, order, w);
2394  }
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)

◆ gauss_quadrature [2/2]

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

Evaluates f at the five Gauss points of [a, b] and returns the weighted sum, which is exact for polynomials in x up to degree 9. Only defined for SX.

Parameters
fScalar expression to integrate; it may depend only on x
xScalar symbolic integration variable
aLower bound, a numeric constant
bUpper bound, a numeric constant
orderMust be 5; any other value raises an error
wReserved; must be empty

>>> x = SX.sym("x")
>>> try: gauss_quadrature(x**2, x, 0, 1, 3)
... except Exception as e: print("error")
error

>> x = SX.sym('x');
>> try, gauss_quadrature(x^2, x, 0, 1, 3); catch e, disp('error'); end
error

julia> x = ca.SX.sym("x");
julia> try ca.gauss_quadrature(x^2, x, 0, 1, 3); catch; println("error") end
error

See also
integrator

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

Definition at line 2384 of file matrix_decl.hpp.

2386  {
2387  return Matrix<Scalar>::gauss_quadrature(f, x, a, b, order);
2388  }

◆ ge

template<typename ExType >
ExType ge ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt. Implemented as y <= x.

>>> ge(DM([1, 2, 3]), 2)
DM([0, 1, 1])
>>> ge(MX.sym("x"), MX.sym("y"))
MX((y<=x))

>> ge(DM([1; 2; 3]), 2)
[0, 1, 1]
>> MX.sym('x') >= MX.sym('y')
(y<=x)

julia> ca.ge(ca.DM([1, 2, 3]), 2)
DM([0, 1, 1])
julia> ca.ge(ca.MX.sym("x"), ca.MX.sym("y"))
MX((y<=x))

See also
lt

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

Definition at line 574 of file generic_expression.hpp.

574  {
575  return ExType::ge(x, y);
576  }

◆ gradient

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::gradient ( const MatType &  ex,
const MatType &  arg,
const Dict &  opts = Dict() 
)
friend

Computed with one reverse-mode sweep (jtimes with tr true) and projected onto the sparsity of arg: the result has the shape and pattern of arg, also for a matrix arg. A non-scalar ex raises an error (use jacobian).

Parameters
exScalar expression to take the gradient of
argSymbolic expression (any shape)
optsOptions, see jacobian
Returns
Matrix with the sparsity of arg

>>> x = SX.sym("x"); y = SX.sym("y")
>>> gradient(x*y + x**2, vertcat(x, y))
SX([((2.*x)+y), x])
>>> gradient(x**2, vertcat(x, y))
SX([(2.*x), 0])
>>> A = SX.sym("A", 2, 2)
>>> gradient(sumsqr(A), A)
SX(
[[(2.*A_0), (2.*A_2)],
 [(2.*A_1), (2.*A_3)]])

>> x = SX.sym('x'); y = SX.sym('y');
>> gradient(x*y + x^2, vertcat(x, y))
[((2.*x)+y), x]
>> gradient(x^2, vertcat(x, y))
[(2.*x), 0]
>> A = SX.sym('A', 2, 2);
>> gradient(sumsqr(A), A)
[[(2.*A_0), (2.*A_2)],
 [(2.*A_1), (2.*A_3)]]

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.gradient(x * y + x^2, [x; y])
SX([((2.*x)+y), x])
julia> ca.gradient(x^2, [x; y])
SX([(2.*x), 0])
julia> A = ca.SX.sym("A", 2, 2);
julia> ca.gradient(ca.sumsqr(A), A)
SX(
[[(2.*A_0), (2.*A_2)],
 [(2.*A_1), (2.*A_3)]])

See also
jacobian, hessian, jtimes

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

Definition at line 3570 of file generic_matrix.hpp.

3570  {
3571  return MatType::gradient(ex, arg, opts);
3572  }

◆ graph_substitute [1/4]

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

Replaces every occurrence of the node v[i] in ex by vdef[i]. Unlike substitute, which only accepts symbolic primitives for v, graph_substitute matches any node, so intermediate results and Function call results can be replaced. Matching is by identity: an expression that was rebuilt (x + y created twice) is a different node and is not matched. Nodes that do not depend on any substituted node are reused, not copied. Shapes of v[i] and vdef[i] must agree.

Parameters
exExpression to substitute in
vNodes to replace
vdefReplacements, same length as v
updatedSet to true if any substitution took place (C++ only)

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> s = x + y
>>> e = sin(s) + s
>>> graph_substitute(e, [s], [MX(3)])
MX(3.14112)
>>> graph_substitute(e, [x + y], [MX(3)])
MX(@1=(x+y), (sin(@1)+@1))
>>> try:
...   substitute(e, s, MX(3))
... except Exception as err:
...   print("not symbolic" in str(err))
True
>>> try:
...   graph_substitute(e, [s], [MX.sym("z", 2)])
... except Exception as err:
...   print("Inconsistent shapes" in str(err))
True

>> x = MX.sym('x'); y = MX.sym('y');
>> s = x + y;
>> e = sin(s) + s;
>> graph_substitute(e, {s}, {MX(3)})
3.14112
>> graph_substitute(e, {x + y}, {MX(3)})
@1=(x+y), (sin(@1)+@1)
>> try, substitute(e, s, MX(3)); catch err, disp('not symbolic'); end
not symbolic
>> z = MX.sym('z', 2);
>> try, graph_substitute(e, {s}, {z}); catch err, disp('inconsistent shapes'); end
inconsistent shapes

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> s = x + y;
julia> e = sin(s) + s;
julia> ca.graph_substitute(e, [s], [ca.MX(3)])
MX(3.14112)
julia> ca.graph_substitute(e, [x + y], [ca.MX(3)])
MX(@1=(x+y), (sin(@1)+@1))
julia> try
           ca.substitute(e, s, ca.MX(3))
       catch err
           println(occursin("not symbolic", sprint(showerror, err)))
       end
true
julia> try
           ca.graph_substitute(e, [s], [ca.MX.sym("z", 2)])
       catch err
           println(occursin("Inconsistent shapes", sprint(showerror, err)))
       end
true

See also
substitute

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

Definition at line 3887 of file mx.hpp.

3888  {
3889  return MX::graph_substitute(ex, v, vdef);
3890  }
static MX graph_substitute(const MX &x, const std::vector< MX > &v, const std::vector< MX > &vdef)
Definition: mx.cpp:1544

◆ graph_substitute [2/4]

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

Definition at line 3892 of file mx.hpp.

3893  {
3894  return MX::graph_substitute(ex, v, vdef, updated);
3895  }

◆ graph_substitute [3/4]

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

Same as the single-expression overload, applied to a list of expressions sharing one traversal.

>>> x = MX.sym("x")
>>> s = x + 1
>>> graph_substitute([sin(s), 2*s], [s], [MX(3)])
[MX(0.14112), MX(6)]

>> x = MX.sym('x');
>> s = x + 1;
>> r = graph_substitute({sin(s), 2*s}, {s}, {MX(3)});
>> r{1}, r{2}
0.14112
6

julia> x = ca.MX.sym("x");
julia> s = x + 1;
julia> ca.graph_substitute([sin(s), 2 * s], [s], [ca.MX(3)])
2-element Vector{MX}:
 MX(0.14112)
 MX(6)

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

Definition at line 3935 of file mx.hpp.

3937  {
3938  return MX::graph_substitute(ex, v, vdef);
3939  }

◆ graph_substitute [4/4]

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

Definition at line 3942 of file mx.hpp.

3945  {
3946  return MX::graph_substitute(ex, v, vdef, updated);
3947  }

◆ gt

template<typename ExType >
ExType gt ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt. Implemented as y < x, which is how a symbolic expression prints.

>>> gt(DM([1, 2, 3]), 2)
DM([0, 0, 1])
>>> gt(SX.sym("x"), SX.sym("y"))
SX((y<x))

>> gt(DM([1; 2; 3]), 2)
[0, 0, 1]
>> SX.sym('x') > SX.sym('y')
(y<x)

julia> ca.gt(ca.DM([1, 2, 3]), 2)
DM([0, 0, 1])
julia> ca.gt(ca.SX.sym("x"), ca.SX.sym("y"))
SX((y<x))

See also
lt

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

Definition at line 528 of file generic_expression.hpp.

528  {
529  return ExType::gt(x, y);
530  }

◆ heaviside

template<typename Scalar >
Matrix<Scalar> heaviside ( const Matrix< Scalar > &  x)
friend

H(x) = 0 for x < 0, 1/2 for x == 0 and 1 for x > 0, computed as (1 + sign(x))/2. Structural zeros of a sparse input become 1/2. Not defined for MX.

>>> heaviside(DM([-1, 0, 1]))
DM([0, 0.5, 1])
>>> heaviside(SX.sym("x"))
SX(((1+sign(x))/2))

>> heaviside(DM([-1; 0; 1]))
[0, 0.5, 1]
>> heaviside(SX.sym('x'))
((1+sign(x))/2)

julia> ca.heaviside(ca.DM([-1, 0, 1]))
DM([0, 0.5, 1])
julia> ca.heaviside(ca.SX.sym("x"))
SX(((1+sign(x))/2))

See also
ramp, rectangle, sign

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

Definition at line 2234 of file matrix_decl.hpp.

2234  {
2235  return Matrix<Scalar>::heaviside(x);
2236  }
static Matrix< Scalar > heaviside(const Matrix< Scalar > &x)

◆ hessian [1/2]

template<typename MatType >
MatType hessian ( const MatType &  ex,
const MatType &  arg,
const Dict &  opts = Dict() 
)
friend

Computes the gradient g = gradient(ex, arg) and the Hessian jacobian(g, arg), an arg.numel()-by-arg.numel() symmetric matrix; the "symmetric" option is set unless given in opts. ex must be scalar. The overload with output_g also returns the gradient; in Python, MATLAB and Julia this is the variant that is exposed, so the result is always the pair (H, g). Not defined for DM.

Parameters
optsOptions, see jacobian

>>> x = SX.sym("x"); y = SX.sym("y")
>>> H, g = hessian(x**2*y, vertcat(x, y))
>>> H
SX(@1=(2.*x),
[[(2.*y), @1],
 [@1, 00]])
>>> g
SX([((2.*x)*y), sq(x)])

>> x = SX.sym('x'); y = SX.sym('y');
>> [H, g] = hessian(x^2*y, vertcat(x, y));
>> H
@1=(2.*x),
[[(2.*y), @1],
 [@1, 00]]
>> g
[((2.*x)*y), sq(x)]

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> H, g = ca.hessian(x^2 * y, [x; y]);
julia> H
SX(@1=(2.*x),
[[(2.*y), @1],
 [@1, 00]])
julia> g
SX([((2.*x)*y), sq(x)])

See also
gradient, jacobian, Function::hessian

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

Definition at line 3851 of file generic_matrix.hpp.

3852  {
3853  return MatType::hessian(ex, arg, opts);
3854  }

◆ hessian [2/2]

template<typename MatType >
MatType hessian ( const MatType &  ex,
const MatType &  arg,
MatType &  output_g,
const Dict &  opts = Dict() 
)
friend

Computes the gradient g = gradient(ex, arg) and the Hessian jacobian(g, arg), an arg.numel()-by-arg.numel() symmetric matrix; the "symmetric" option is set unless given in opts. ex must be scalar. The overload with output_g also returns the gradient; in Python, MATLAB and Julia this is the variant that is exposed, so the result is always the pair (H, g). Not defined for DM.

Parameters
optsOptions, see jacobian

>>> x = SX.sym("x"); y = SX.sym("y")
>>> H, g = hessian(x**2*y, vertcat(x, y))
>>> H
SX(@1=(2.*x),
[[(2.*y), @1],
 [@1, 00]])
>>> g
SX([((2.*x)*y), sq(x)])

>> x = SX.sym('x'); y = SX.sym('y');
>> [H, g] = hessian(x^2*y, vertcat(x, y));
>> H
@1=(2.*x),
[[(2.*y), @1],
 [@1, 00]]
>> g
[((2.*x)*y), sq(x)]

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> H, g = ca.hessian(x^2 * y, [x; y]);
julia> H
SX(@1=(2.*x),
[[(2.*y), @1],
 [@1, 00]])
julia> g
SX([((2.*x)*y), sq(x)])

See also
gradient, jacobian, Function::hessian

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

Definition at line 3855 of file generic_matrix.hpp.

3856  {
3857  return MatType::hessian(ex, arg, output_g, opts);
3858  }

◆ horzcat [1/6]

template<typename MatType >
MatType horzcat ( const MatType &  x,
const MatType &  y 
)
friend

Same as the list form of horzcat.

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

Definition at line 2205 of file sparsity_interface.hpp.

2205  {
2206  return horzcat(std::vector<MatType>{x, y});
2207  }

◆ horzcat [2/6]

template<typename MatType >
MatType horzcat ( const MatType &  x,
const MatType &  y,
const MatType &  z 
)
friend

Same as the list form of horzcat.

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

Definition at line 2214 of file sparsity_interface.hpp.

2214  {
2215  return horzcat(std::vector<MatType>{x, y, z});
2216  }

◆ horzcat [3/6]

template<typename MatType >
MatType horzcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w 
)
friend

Same as the list form of horzcat.

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

Definition at line 2223 of file sparsity_interface.hpp.

2224  {
2225  return horzcat(std::vector<MatType>{x, y, z, w});
2226  }

◆ horzcat [4/6]

template<typename MatType >
MatType horzcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w,
const MatType &  v 
)
friend

Same as the list form of horzcat.

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

Definition at line 2233 of file sparsity_interface.hpp.

2234  {
2235  return horzcat(std::vector<MatType>{x, y, z, w, v});
2236  }

◆ horzcat [5/6]

template<typename MatType >
MatType horzcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w,
const MatType &  v,
const MatType &  u 
)
friend

Same as the list form of horzcat.

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

Definition at line 2243 of file sparsity_interface.hpp.

2244  {
2245  return horzcat(std::vector<MatType>{x, y, z, w, v, u});
2246  }

◆ horzcat [6/6]

template<typename MatType >
MatType horzcat ( const std::vector< MatType > &  v)
friend

Places the matrices side by side, in order: the result has the row count of the inputs and a column count equal to the sum of the column counts. All inputs must have the same number of rows; a 0-by-0 matrix is ignored. The result is of the type of the arguments, numeric arguments are converted to the symbolic type (DM mixes with SX or MX, but SX and MX cannot be mixed). The sparsity pattern of each block is preserved. With no arguments the result is a 1-by-0 matrix.

Alternative terminology: horizontal stack, hstack, horizontal append, [a b]. Inverse of horzsplit: horzcat(horzsplit(x, ...)) == x.

Parameters
vList of matrices with matching row count
Returns
Matrix with the blocks of v side by side

>>> horzcat(DM([1, 2]), DM([3, 4]), DM([5, 6]))
DM(
[[1, 3, 5],
 [2, 4, 6]])
>>> horzcat(SX.sym("x", 2), DM.ones(2, 1))
SX(@1=1,
[[x_0, @1],
 [x_1, @1]])
>>> horzcat(MX.sym("x", 2, 2), MX.sym("y", 2, 0))
MX(x)
>>> horzcat().shape, horzcat(DM(2, 3), DM(0, 0)).shape
((1, 0), (2, 3))
>>> horzcat(DM.ones(2, 2), DM.ones(3, 1))
Traceback (most recent call last):
  ...
RuntimeError: ...Mismatching number of rows...

>> [DM([1; 2]), DM([3; 4]), DM([5; 6])]
[[1, 3, 5],
 [2, 4, 6]]
>> horzcat(SX.sym('x', 2), DM.ones(2, 1))
@1=1,
[[x_0, @1],
 [x_1, @1]]
>> [MX.sym('x', 2, 2), MX.sym('y', 2, 0)]
x
>> size([DM(2, 3), DM(0, 0)])
2 3
>> try, [DM.ones(2, 2), DM.ones(3, 1)]; catch e, disp(e.message); end
...Mismatching number of rows

julia> [ca.DM([1, 2]) ca.DM([3, 4]) ca.DM([5, 6])]
DM(
[[1, 3, 5],
 [2, 4, 6]])
julia> [ca.SX.sym("x", 2) ca.DM.ones(2, 1)]
SX(@1=1,
[[x_0, @1],
 [x_1, @1]])
julia> [ca.MX.sym("x", 2, 2) ca.MX.sym("y", 2, 0)]
MX(x)
julia> size([ca.DM(2, 3) ca.DM(0, 0)])
(2, 3)
julia> try
           [ca.DM.ones(2, 2) ca.DM.ones(3, 1)]
       catch err
           println(occursin("dimension mismatch", sprint(showerror, err)))
       end
false

See also
vertcat, diagcat, veccat, horzsplit

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

Definition at line 171 of file sparsity_interface.hpp.

171  {
172  return MatType::horzcat(v);
173  }

◆ horzsplit [1/2]

template<typename MatType >
std::vector<MatType > horzsplit ( const MatType &  x,
casadi_int  incr = 1 
)
friend

Returns the consecutive column blocks of x, each with incr columns, in order. The last block is smaller when the column count is not a multiple of incr. A matrix without columns gives an empty list. Each block carries the corresponding part of the sparsity pattern. Inverse of horzcat: horzcat(horzsplit(x, incr)) == x.

Parameters
xMatrix (or Sparsity) to split; any shape
incrNumber of columns per block, must be positive
Returns
List of matrices of the same type as x

>>> horzsplit(SX.sym("x", 1, 3))
[SX(x_0), SX(x_1), SX(x_2)]
>>> horzsplit(DM([[1, 2, 3, 4, 5]]), 2)
[DM([[1, 2]]), DM([[3, 4]]), DM(5)]
>>> horzsplit(MX.sym("x", 2, 4), 2)
[MX(horzsplit(x){0}), MX(horzsplit(x){1})]
>>> horzsplit(Sparsity.diag(3), 2)
[Sparsity(3x2,2nz), Sparsity(3x1,1nz)]
>>> horzsplit(MX.sym("x", 2, 0))
[]

>> v = horzsplit(SX.sym('x', 1, 3)); disp(numel(v)), disp(v{1}), disp(v{3})
3
x_0
x_2
>> v = horzsplit(DM([1 2 3 4 5]), 2); disp(numel(v)), disp(v{1}), disp(v{3})
3
[[1, 2]]
5
>> v = horzsplit(MX.sym('x', 2, 4), 2); disp(v{1}), disp(v{2})
horzsplit(x){0}
horzsplit(x){1}
>> v = horzsplit(Sparsity.diag(3), 2); disp(v{1}), disp(v{2})
3x2,2nz
3x1,1nz
>> v = horzsplit(MX.sym('x', 2, 0)); disp(numel(v))
0

julia> ca.horzsplit(ca.SX.sym("x", 1, 3))
3-element Vector{SX}:
 SX(x_0)
 SX(x_1)
 SX(x_2)
julia> ca.horzsplit(ca.DM([1 2 3 4 5]), 2)
3-element Vector{DM}:
 DM([[1, 2]])
 DM([[3, 4]])
 DM(5)
julia> ca.horzsplit(ca.MX.sym("x", 2, 4), 2)
2-element Vector{MX}:
 MX(horzsplit(x){0})
 MX(horzsplit(x){1})
julia> ca.horzsplit(ca.Sparsity.diag(3), 2)
2-element Vector{Sparsity}:
 Sparsity(3x2,2nz)
 Sparsity(3x1,1nz)
julia> ca.horzsplit(ca.MX.sym("x", 2, 0))
MX[]

See also
horzsplit_n, vertsplit

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

Definition at line 408 of file sparsity_interface.hpp.

408  {
409  return MatType::horzsplit(x, incr);
410  }

◆ horzsplit [2/2]

template<typename MatType >
std::vector<MatType > horzsplit ( const MatType &  x,
const std::vector< casadi_int > &  offset 
)
friend

Returns the column blocks x[:, offset[i]:offset[i+1]], in order. The offsets must start at 0, be non-decreasing and end at the number of columns of x, otherwise an error is raised. Two equal consecutive offsets give an empty block. Each block carries the corresponding part of the sparsity pattern. Inverse of horzcat: horzcat(horzsplit(x, offset)) == x.

Parameters
xMatrix (or Sparsity) to split; any shape
offsetStart column of every block followed by the column count of x
Returns
List of matrices of the same type as x, one per block

>>> x = DM([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> horzsplit(x, [0, 2, 5])
[DM(
[[1, 2],
 [6, 7]]), DM(
[[3, 4, 5],
 [8, 9, 10]])]
>>> horzsplit(x, [0, 0, 5])[0].shape
(2, 0)
>>> horzsplit(x, [0, 2])
Traceback (most recent call last):
  ...
RuntimeError: ...must start at 0, be nondecreasing and end at 5...

>> x = DM([1 2 3 4 5; 6 7 8 9 10]);
>> v = horzsplit(x, [0, 2, 5]); disp(numel(v)), disp(v{1}), disp(v{2})
2
[[1, 2],
 [6, 7]]
[[3, 4, 5],
 [8, 9, 10]]
>> v = horzsplit(x, [0, 0, 5]); disp(size(v{1}))
2 0
>> try, horzsplit(x, [0, 2]); catch e, disp(e.message); end
...must start at 0, be nondecreasing and end at 5...

julia> x = ca.DM([1 2 3 4 5; 6 7 8 9 10]);
julia> v = ca.horzsplit(x, [0, 2, 5]);
julia> length(v)
2
julia> v[1]
DM(
[[1, 2],
 [6, 7]])
julia> v[2]
DM(
[[3, 4, 5],
 [8, 9, 10]])
julia> size(ca.horzsplit(x, [0, 0, 5])[1])
(2, 0)
julia> try
           ca.horzsplit(x, [0, 2])
       catch err
           println(occursin("end at 5", sprint(showerror, err)))
       end
true

See also
horzsplit_n, vertsplit, offset

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

Definition at line 328 of file sparsity_interface.hpp.

328  {
329  return MatType::horzsplit(x, offset);
330  }
static std::vector< casadi_int > offset(const std::vector< MatType > &v, bool vert=true)

◆ horzsplit_n

template<typename MatType >
std::vector<MatType > horzsplit_n ( const MatType &  x,
casadi_int  n 
)
friend

Returns n column blocks of equal width, in order. An error is raised when the column count of x is not a multiple of n or n is negative. A matrix without columns gives n copies of itself. Inverse of horzcat: horzcat(horzsplit_n(x, n)) == x.

Parameters
xMatrix (or Sparsity) to split; any shape
nNumber of blocks
Returns
List of n matrices of the same type as x

>>> horzsplit_n(DM([[1, 2, 3, 4]]), 2)
[DM([[1, 2]]), DM([[3, 4]])]
>>> horzsplit_n(DM(2, 0), 3)
[DM(2x0), DM(2x0), DM(2x0)]
>>> horzsplit_n(DM([[1, 2, 3]]), 2)
Traceback (most recent call last):
  ...
RuntimeError: ...must be a multiple of n...

>> v = horzsplit_n(DM([1 2 3 4]), 2); disp(v{1}), disp(v{2})
[[1, 2]]
[[3, 4]]
>> v = horzsplit_n(DM(2, 0), 3); disp(numel(v)), disp(size(v{1}))
3
2 0
>> try, horzsplit_n(DM([1 2 3]), 2); catch e, disp(e.message); end
...must be a multiple of n...

julia> ca.horzsplit_n(ca.DM([1 2 3 4]), 2)
2-element Vector{DM}:
 DM([[1, 2]])
 DM([[3, 4]])
julia> ca.horzsplit_n(ca.DM(2, 0), 3)
3-element Vector{DM}:
 DM(2x0)
 DM(2x0)
 DM(2x0)
julia> try
           ca.horzsplit_n(ca.DM([1 2 3]), 2)
       catch err
           println(occursin("must be a multiple of n", sprint(showerror, err)))
       end
true

See also
horzsplit, vertsplit_n

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

Definition at line 472 of file sparsity_interface.hpp.

472  {
473  return MatType::horzsplit_n(x, n);
474  }

◆ hypot

template<typename ExType >
ExType hypot ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise; avoids the overflow and underflow of forming the squares, as C hypot. Shape rules as for plus; the result is structurally zero where both operands are.

>>> hypot(DM(3), DM(4))
DM(5)
>>> hypot(DM(1e200), DM(1e200))
DM(1.41421e+200)
>>> hypot(SX.sym("x"), SX.sym("y"))
SX(hypot(x,y))

>> hypot(DM(3), DM(4))
5
>> hypot(DM(1e200), DM(1e200))
1.41421e+200
>> hypot(SX.sym('x'), SX.sym('y'))
hypot(x,y)

julia> hypot(ca.DM(3), ca.DM(4))
DM(5)
julia> hypot(ca.DM(1e200), ca.DM(1e200))
DM(1.41421e+200)
julia> hypot(ca.SX.sym("x"), ca.SX.sym("y"))
SX(hypot(x,y))

See also
norm_2

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

Definition at line 2568 of file generic_expression.hpp.

2568  {
2569  return ExType::hypot(x, y);
2570  }

◆ if_else

template<typename MatType >
MatType if_else ( const MatType &  cond,
const MatType &  if_true,
const MatType &  if_false,
bool  short_circuit = false 
)
friend

Ternary operator, "cond ? if_true : if_false"

Selects if_true where cond is nonzero and if_false elsewhere. cond may be a scalar or have the shape of the branches (elementwise selection); a scalar branch is broadcast. By default the result is if_else_zero(cond, if_true) + if_else_zero(!cond, if_false): both branches are part of the expression and are evaluated, but the value of the unselected branch is discarded, so a nan or inf there does not propagate. With short_circuit true (MX only; ignored for SX and DM) the branches are wrapped in functions and only the selected one is evaluated.

>>> if_else(DM(1), DM(10), DM(20)), if_else(DM(0), DM(10), DM(20))
(DM(10), DM(20))
>>> if_else(DM([1, 0]), DM([1, 2]), DM([3, 4]))
DM([1, 4])
>>> x = SX.sym("x")
>>> if_else(x > 0, x, -x)
SX(@1=(0<x), ((@1?x:0)+((!@1)?(-x):0)))
>>> f = Function("f", [x], [if_else(x >= 0, sqrt(x), 0)])
>>> f(-1)
DM(0)
>>> X = MX.sym("X")
>>> if_else(X > 0, X, -X, True)
MX(switch((0<X), X){0})

>> if_else(DM(1), DM(10), DM(20)), if_else(DM(0), DM(10), DM(20))
10
20
>> if_else(DM([1; 0]), DM([1; 2]), DM([3; 4]))
[1, 4]
>> x = SX.sym('x');
>> if_else(x > 0, x, -x)
@1=(0<x), ((@1?x:0)+((!@1)?(-x):0))
>> f = Function('f', {x}, {if_else(x >= 0, sqrt(x), 0)});
>> f(-1)
0
>> X = MX.sym('X');
>> if_else(X > 0, X, -X, true)
switch((0<X), X){0}

julia> a = ca.DM(10); b = ca.DM(20);
julia> ca.if_else(ca.DM(1), a, b), ca.if_else(ca.DM(0), a, b)
(10, 20)
julia> ca.if_else(ca.DM([1, 0]), ca.DM([1, 2]), ca.DM([3, 4]))
DM([1, 4])
julia> x = ca.SX.sym("x");
julia> ca.if_else(x > 0, x, -x)
SX(@1=(0<x), ((@1?x:0)+((!@1)?(-x):0)))
julia> f = ca.Function("f", [x], [ca.if_else(x >= 0, sqrt(x), 0)]);
julia> f(-1)
DM(0)
julia> X = ca.MX.sym("X");
julia> ca.if_else(X > 0, X, -X, true)
MX(switch((0<X), X){0})

See also
conditional, if_else_zero, Function::if_else

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

Definition at line 2650 of file generic_matrix.hpp.

2651  {
2652  return MatType::if_else(cond, if_true, if_false, short_circuit);
2653  }

◆ if_else_zero

template<typename ExType >
ExType if_else_zero ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise: y where x is nonzero, 0 elsewhere; the result is structurally zero wherever either operand is. The value of y in the inactive branch does not propagate, so a NaN or inf there yields 0 rather than NaN, and the derivatives are treated accordingly. This is the building block of if_else; it is not exposed as a function in Python, use if_else(x, y, 0) there.

See also
if_else

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

Definition at line 2169 of file generic_expression.hpp.

2169  {
2170  return ExType::if_else_zero(x, y);
2171  }

◆ interp1d

template<typename MatType >
MatType interp1d ( const std::vector< double > &  x,
const MatType &  v,
const std::vector< double > &  xq,
const std::string &  mode,
bool  equidistant = false 
)
friend

The data points are (x[i], v[i, :]): row i of v holds the values at abscissa x[i], so v has as many rows as x has entries and every column is interpolated independently. The result has one row per query point xq[j].

Queries outside the range of x are clamped to the first or last data point (no extrapolation). The result is a constant sparse matrix times v, so for symbolic v the expression is linear in v.

Parameters
xAbscissae, strictly increasing, at least two entries
vData values, x.size()-by-m
xqQuery abscissae, any order
mode"linear" (interpolate), "floor" (value at the data point at or below the query) or "ceil" (at or above); any other string raises an error
equidistantWhen true, x is assumed uniformly spaced and the interval is located arithmetically from x[0] and x[1] instead of by search
Returns
Matrix of size xq.size()-by-m

>>> interp1d([0, 1, 2], DM([0, 10, 20]), [0.5, 1.5])
DM([5, 15])
>>> interp1d([0, 1, 2], DM([0, 10, 20]), [0.5, 1.5], "floor")
DM([0, 10])
>>> interp1d([0, 1, 2], DM([[0, 1], [10, 11], [20, 21]]), [0.5, 2])
DM(
[[5, 6],
 [20, 21]])
>>> interp1d([0, 1, 2], DM([0, 10, 20]), [-1, 3])
DM([0, 20])
>>> interp1d([0, 1, 2], SX.sym("v", 3), [0.5])
SX(@1=0.5, ((@1*v_0)+(@1*v_1)))

>> interp1d([0, 1, 2], DM([0; 10; 20]), [0.5, 1.5])
[5, 15]
>> interp1d([0, 1, 2], DM([0; 10; 20]), [0.5, 1.5], 'floor')
[0, 10]
>> interp1d([0, 1, 2], DM([0 1; 10 11; 20 21]), [0.5, 2])
[[5, 6],
 [20, 21]]
>> interp1d([0, 1, 2], DM([0; 10; 20]), [-1, 3])
[0, 20]
>> interp1d([0, 1, 2], SX.sym('v', 3), 0.5)
@1=0.5, ((@1*v_0)+(@1*v_1))

julia> ca.interp1d([0, 1, 2], ca.DM([0, 10, 20]), [0.5, 1.5])
DM([5, 15])
julia> ca.interp1d([0, 1, 2], ca.DM([0, 10, 20]), [0.5, 1.5], "floor")
DM([0, 10])
julia> ca.interp1d([0, 1, 2], ca.DM([0 1; 10 11; 20 21]), [0.5, 2])
DM(
[[5, 6],
 [20, 21]])
julia> ca.interp1d([0, 1, 2], ca.DM([0, 10, 20]), [-1, 3])
DM([0, 20])
julia> ca.interp1d([0, 1, 2], ca.SX.sym("v", 3), [0.5])
SX(@1=0.5, ((@1*v_0)+(@1*v_1)))

See also
interpolant

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

Definition at line 812 of file generic_matrix.hpp.

813  {
814  return MatType::interp1d(x, v, xq, mode, equidistant);
815  }

◆ inv [1/2]

template<typename MatType >
MatType inv ( const MatType &  A)
friend

Computed as solve(A, eye(n)), see solve for the algorithm. A non-square argument raises an error. A numerically singular DM does not raise; the result contains inf or nan. For MX the result is a solve node.

>>> inv(DM([[1, 2], [3, 4]]))
DM(
[[-2, 1],
 [1.5, -0.5]])
>>> inv(SX.sym("A", 2, 2))
SX(@1=((A_0*A_3)-(A_2*A_1)),
[[(A_3/@1), (-(A_2/@1))],
 [(-(A_1/@1)), (A_0/@1)]])
>>> inv(DM(Sparsity.diag(2), [2, 4]))
DM(
[[0.5, 00],
 [00, 0.25]])

>> inv(DM([1 2; 3 4]))
[[-2, 1],
 [1.5, -0.5]]
>> inv(SX.sym('A', 2, 2))
@1=((A_0*A_3)-(A_2*A_1)),
[[(A_3/@1), (-(A_2/@1))],
 [(-(A_1/@1)), (A_0/@1)]]
>> inv(DM(Sparsity.diag(2), [2; 4]))
[[0.5, 00],
 [00, 0.25]]

julia> print(inv(ca.DM([1 2; 3 4])))
[[-2, 1],
 [1.5, -0.5]]
julia> inv(ca.SX.sym("A", 2, 2))
SX(@1=((A_0*A_3)-(A_2*A_1)),
[[(A_3/@1), (-(A_2/@1))],
 [(-(A_1/@1)), (A_0/@1)]])
julia> inv(ca.DM(ca.Sparsity.diag(2), [2, 4]))
DM(
[[0.5, 00],
 [00, 0.25]])

See also
solve, pinv, det, mldivide

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

Definition at line 1737 of file generic_matrix.hpp.

1737  {
1738  return MatType::inv(A);
1739  }

◆ inv [2/2]

template<typename MatType >
MatType inv ( const MatType &  A,
const std::string &  lsolver,
const Dict &  options = Dict() 
)
friend

Computes solve(A, eye(n), lsolver, options). Available for DM and MX; SX raises.

Parameters
lsolverName of a Linsol plugin, e.g. "qr", "csparse", "lapacklu"
optionsOptions passed to the linear solver

>>> inv(DM([[1, 2], [3, 4]]), "qr")
DM(
[[-2, 1],
 [1.5, -0.5]])

>> inv(DM([1 2; 3 4]), 'qr')
[[-2, 1],
 [1.5, -0.5]]

julia> print(ca.inv(ca.DM([1 2; 3 4]), "qr"))
[[-2, 1],
 [1.5, -0.5]]

See also
solve, Linsol

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

Definition at line 1776 of file generic_matrix.hpp.

1778  {
1779  return MatType::inv(A, lsolver, options);
1780  }

◆ inv_minor

template<typename MatType >
MatType inv_minor ( const MatType &  A)
friend

Computes adj(A)/det(A) by minor expansion. Available for DM and SX; MX raises "Not implemented". Use inv for the general case.

>>> inv_minor(DM([[1, 2], [3, 4]]))
DM(
[[-2, 1],
 [1.5, -0.5]])

>> inv_minor(DM([1 2; 3 4]))
[[-2, 1],
 [1.5, -0.5]]

julia> ca.inv_minor(ca.DM([1 2; 3 4]))
DM(
[[-2, 1],
 [1.5, -0.5]])

See also
inv, det

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

Definition at line 1678 of file generic_matrix.hpp.

1678 { return MatType::inv_minor(A);}

◆ inv_node

MX inv_node ( const MX &  x)
friend

Creates a single node representing the inverse of the square matrix x. The node defines its derivatives (via -inv(x) dx inv(x)) but cannot be evaluated numerically or expanded to SX; it is meant as a placeholder in symbolic manipulations. For an inverse that can be evaluated use inv or solve.

>>> A = MX.sym("A", 2, 2)
>>> inv_node(A), inv_node(A).is_op(OP_INVERSE), jacobian(inv_node(A), A).shape
(MX(inv(A)), True, (4, 4))
>>> try:
...   inv_node(MX.sym("B", 2, 3))
... except Exception as e:
...   print("must be square" in str(e))
True

>> A = MX.sym('A', 2, 2);
>> e = inv_node(A); J = jacobian(e, A);
>> e, e.is_op(OP_INVERSE), size(J)
inv(A)
1
4 4
>> try, inv_node(MX.sym('B', 2, 3)); catch e, disp('must be square'); end
must be square

julia> A = ca.MX.sym("A", 2, 2);
julia> B = ca.inv_node(A);
julia> B, ca.is_op(B, ca.OP_INVERSE), size(ca.jacobian(B, A))
(inv(A), true, (4, 4))
julia> try
           ca.inv_node(ca.MX.sym("B", 2, 3))
       catch e
           println(occursin("must be square", sprint(showerror, e)))
       end
true

See also
inv, solve

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

Definition at line 4393 of file mx.hpp.

4393  {
4394  return MX::inv_node(x);
4395  }
static MX inv_node(const MX &A)
Definition: mx.cpp:2022

◆ inv_skew

template<typename MatType >
MatType inv_skew ( const MatType &  a)
friend

Inverse of skew: for a 3-by-3 matrix S, returns 0.5*[S(2,1)-S(1,2); S(0,2)-S(2,0); S(1,0)-S(0,1)], which recovers a from skew(a). A non-3-by-3 argument raises an error.

See also
skew

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

Definition at line 1560 of file generic_matrix.hpp.

1560  {
1561  return MatType::inv_skew(a);
1562  }

◆ is_equal

template<typename ExType >
bool is_equal ( const ExType &  x,
const ExType &  y,
casadi_int  depth = 0 
)
friend

Depth=0 checks if the expressions are identical, i.e. points to the same node.

a = x*x b = x*x

is_equal(a,b,0) will return false, but is_equal(a,b,1) will return true

Returns a plain boolean, unlike the elementwise comparison eq. With depth > 0 two different nodes are equivalent when they have the same operation and their dependencies are equivalent up to depth-1. For SX and DM matrices the test is applied entry by entry; matrices of different shape raise an error, and for DM it is value equality. Both arguments must be of the same type.

Parameters
xFirst expression
ySecond expression
depthNumber of levels of the expression graph to compare, 0 for identity

 >>> x = SX.sym("x")
 >>> is_equal(x, x), is_equal(x, SX.sym("x"))
 (True, False)
 >>> is_equal(x*x, x*x), is_equal(x*x, x*x, 1)
 (False, True)
 >>> is_equal(DM([1, 2]), DM([1, 2])), is_equal(DM([1, 2]), DM([1, 3]))
 (True, False)
 >>> y = MX.sym("y")
 >>> is_equal(y * y, y * y), is_equal(y * y, y * y, 1)
 (False, True)

 >> x = SX.sym('x');
 >> is_equal(x, x), is_equal(x, SX.sym('x'))
 1
 0
 >> is_equal(x*x, x*x), is_equal(x*x, x*x, 1)
 0
 1
 >> is_equal(DM([1; 2]), DM([1; 2])), is_equal(DM([1; 2]), DM([1; 3]))
 1
 0
 >> y = MX.sym('y');
 >> is_equal(y * y, y * y), is_equal(y * y, y * y, 1)
 0
 1

 julia> x = ca.SX.sym("x");
 julia> ca.is_equal(x, x), ca.is_equal(x, ca.SX.sym("x"))
 (true, false)
 julia> ca.is_equal(x*x, x*x), ca.is_equal(x*x, x*x, 1)
 (false, true)
 julia> ca.is_equal(ca.DM([1, 2]), ca.DM([1, 2])), ca.is_equal(ca.DM([1, 2]), ca.DM([1, 3]))
 (true, false)
 julia> y = ca.MX.sym("y");
 julia> ca.is_equal(y * y, y * y), ca.is_equal(y * y, y * y, 1)
 (false, true)

See also
eq

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

Definition at line 2369 of file generic_expression.hpp.

2369  {
2370  return ExType::is_equal(x, y, depth);
2371  }

◆ is_linear

template<typename MatType >
bool is_linear ( const MatType &  expr,
const MatType &  var 
)
friend

Defined as: no entry of expr depends on var with order 2 (see which_depends). Only the structure of the graph is inspected, not the values: false negatives are possible (an expression may not be recognised as linear while it really is, e.g. x*x/x). Operations whose derivative is structurally zero, such as floor, sign or comparisons, are treated as linear. A constant expression counts as linear.

* >>> x = SX.sym("x"); y = SX.sym("y")
* >>> is_linear(2*x + y, vertcat(x, y)), is_linear(x*y, vertcat(x, y))
* (True, False)
* >>> is_linear(sin(y)*x, x), is_linear(x*x/x, x)
* (True, False)
* 

* >> x = SX.sym('x'); y = SX.sym('y');
* >> fprintf('%d %d\n', is_linear(2*x + y, vertcat(x, y)), is_linear(x*y, vertcat(x, y)))
* 1 0
* >> fprintf('%d %d\n', is_linear(sin(y)*x, x), is_linear(x*x/x, x))
* 1 0
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
* julia> ca.is_linear(2 * x + y, [x; y]), ca.is_linear(x * y, [x; y])
* (true, false)
* julia> ca.is_linear(sin(y) * x, x), ca.is_linear(x * x / x, x)
* (true, false)
* 

See also
is_quadratic, linear_coeff, which_depends

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

Definition at line 4010 of file generic_matrix.hpp.

4010  {
4011  return MatType::is_linear(expr, var);
4012  }

◆ is_quadratic

template<typename MatType >
bool is_quadratic ( const MatType &  expr,
const MatType &  var 
)
friend

Defined as: gradient(expr, var) is linear in var; hence expr must be scalar. False negatives are possible (an expression may not be recognised as quadratic while it really is), see is_linear for the structural nature of the check. Linear and constant expressions count as quadratic.

* >>> x = SX.sym("x"); y = SX.sym("y")
* >>> is_quadratic(x*y + x**2, vertcat(x, y)), is_quadratic(x**3, x)
* (True, False)
* >>> is_quadratic(x, x)
* True
* 

* >> x = SX.sym('x'); y = SX.sym('y');
* >> fprintf('%d %d\n', is_quadratic(x*y + x^2, vertcat(x, y)), is_quadratic(x^3, x))
* 1 0
* >> is_quadratic(x, x)
* 1
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
* julia> ca.is_quadratic(x * y + x^2, [x; y]), ca.is_quadratic(x^3, x)
* (true, false)
* julia> ca.is_quadratic(x, x)
* true
* 

See also
is_linear, quadratic_coeff

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

Definition at line 4055 of file generic_matrix.hpp.

4055  {
4056  return MatType::is_quadratic(expr, var);
4057  }

◆ jacobian

template<typename MatType >
MatType jacobian ( const MatType &  ex,
const MatType &  arg,
const Dict &  opts = Dict() 
)
friend

The derivative of ex with respect to arg, with both treated as column-major vectors of their elements: the result is ex.numel()-by-arg.numel(), one row per element of ex and one column per element of arg. Entries that are structurally zero are left out of the sparsity pattern.

arg must be symbolic (for MX: symbolic primitives as created by MX.sym, or a concatenation of them; elements of a primitive are rejected). Not defined for DM.

Parameters
optsOptions: "allow_forward" and "allow_reverse" (bool, default true) restrict the AD mode; "symmetric" (bool, default false) hints that the Jacobian is symmetric; "verbose"
Returns
Sparse matrix

>>> x = SX.sym("x"); y = SX.sym("y")
>>> J = jacobian(vertcat(x*y, x**2), vertcat(x, y))
>>> J
SX(
[[y, x],
 [(2.*x), 00]])
>>> J.dim(True)
'2x2,3nz'
>>> A = SX.sym("A", 2, 2)
>>> jacobian(2*A, A).dim()
'4x4'
>>> jacobian(SX(0, 1), vertcat(x, y)).dim()
'0x2'

>> x = SX.sym('x'); y = SX.sym('y');
>> J = jacobian(vertcat(x*y, x^2), vertcat(x, y));
>> J
[[y, x],
 [(2.*x), 00]]
>> J.dim(true)
2x2,3nz
>> A = SX.sym('A', 2, 2);
>> J = jacobian(2*A, A); disp(J.dim())
4x4
>> J = jacobian(SX(0, 1), vertcat(x, y)); disp(J.dim())
0x2

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> J = ca.jacobian([x * y; x^2], [x; y])
SX(
[[y, x],
 [(2.*x), 00]])
julia> ca.dim(J, true)
"2x2,3nz"
julia> A = ca.SX.sym("A", 2, 2);
julia> ca.dim(ca.jacobian(2 * A, A))
"4x4"
julia> ca.dim(ca.jacobian(ca.SX(0, 1), [x; y]))
"0x2"

See also
gradient, hessian, jtimes, jacobian_sparsity, Function::jacobian

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

Definition at line 3508 of file generic_matrix.hpp.

3509  {
3510  return MatType::jacobian(ex, arg, opts);
3511  }

◆ jacobian_sparsity

template<typename MatType >
Sparsity jacobian_sparsity ( const MatType &  f,
const MatType &  x 
)
friend

Equivalent to, but cheaper to compute than, jacobian(f,x).sparsity()

>>> x = SX.sym("x"); y = SX.sym("y")
>>> jacobian_sparsity(vertcat(x*y, x**2, 3), vertcat(x, y))
Sparsity(3x2,3nz)

>> x = SX.sym('x'); y = SX.sym('y');
>> jacobian_sparsity(vertcat(x*y, x^2, 3), vertcat(x, y))
3x2,3nz

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.jacobian_sparsity([x * y; x^2; 3], [x; y])
Sparsity(3x2,3nz)

See also
jacobian, which_depends

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

Definition at line 3963 of file generic_matrix.hpp.

3963  {
3964  return MatType::jacobian_sparsity(f, x);
3965  }

◆ jtimes

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::jtimes ( const MatType &  ex,
const MatType &  arg,
const MatType &  v,
bool  tr = false,
const Dict &  opts = Dict() 
)
friend

This is equivalent to mul(jacobian(ex, arg), v) or mul(jacobian(ex, arg).T, v) for tr set to false and true respectively. If contrast to these expressions, it will use directional derivatives which is typically (but not necessarily) more efficient if the complete Jacobian is not needed and v has few rows.

The matrix shapes of ex and arg are kept: v must have the shape of arg (or of ex when tr is true), and several directions can be passed at once by concatenating them horizontally, i.e. v.size2() a multiple of arg.size2(). The result is ex.size1()-by-ex.size2()*n_dir (arg.size1()-by-arg.size2()*n_dir when tr is true). An inconsistent shape of v raises an error.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> ex = vertcat(x*y, x**2); arg = vertcat(x, y)
>>> jtimes(ex, arg, DM([1, 0]))
SX([y, (2.*x)])
>>> jtimes(ex, arg, DM([1, 0]), True)
SX([y, x])
>>> jtimes(ex, arg, DM.eye(2))
SX(
[[y, x],
 [(2.*x), 0]])
>>> jtimes(ex, arg, SX.sym("v", 2))
SX([((y*v_0)+(x*v_1)), ((2.*x)*v_0)])

>> x = SX.sym('x'); y = SX.sym('y');
>> ex = vertcat(x*y, x^2); arg = vertcat(x, y);
>> jtimes(ex, arg, DM([1; 0]))
[y, (2.*x)]
>> jtimes(ex, arg, DM([1; 0]), true)
[y, x]
>> jtimes(ex, arg, DM.eye(2))
[[y, x],
 [(2.*x), 0]]
>> jtimes(ex, arg, SX.sym('v', 2))
[((y*v_0)+(x*v_1)), ((2.*x)*v_0)]

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ex = [x * y; x^2]; arg = [x; y];
julia> ca.jtimes(ex, arg, ca.DM([1, 0]))
SX([y, (2.*x)])
julia> ca.jtimes(ex, arg, ca.DM([1, 0]), true)
SX([y, x])
julia> ca.jtimes(ex, arg, ca.DM.eye(2))
SX(
[[y, x],
 [(2.*x), 0]])
julia> ca.jtimes(ex, arg, ca.SX.sym("v", 2))
SX([((y*v_0)+(x*v_1)), ((2.*x)*v_0)])

See also
jacobian, forward, reverse

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

Definition at line 3677 of file generic_matrix.hpp.

3678  {
3679  return MatType::jtimes(ex, arg, v, tr, opts);
3680  }

◆ kron

template<typename MatType >
MatType kron ( const MatType &  a,
const MatType &  b 
)
friend

Creates a block matrix in which block (i, j) is a_ij*b, for any shapes of a and b. The result is a.size1()*b.size1() by a.size2()*b.size2() with the Kronecker product of the sparsity patterns.

>>> kron(DM([[1, 2]]), DM([[1], [10]]))
DM(
[[1, 2],
 [10, 20]])
>>> kron(DM.eye(2), DM([[1, 2], [3, 4]]))
DM(
[[1, 2, 00, 00],
 [3, 4, 00, 00],
 [00, 00, 1, 2],
 [00, 00, 3, 4]])
>>> kron(MX.sym("a", 2, 2), MX.sym("b", 1, 2)).shape
(2, 4)

>> kron(DM([1 2]), DM([1; 10]))
[[1, 2],
 [10, 20]]
>> kron(DM.eye(2), DM([1 2; 3 4]))
[[1, 2, 00, 00],
 [3, 4, 00, 00],
 [00, 00, 1, 2],
 [00, 00, 3, 4]]
>> size(kron(MX.sym('a', 2, 2), MX.sym('b', 1, 2)))
2 4

julia> kron(ca.DM([1 2]), ca.DM([1; 10]))
DM(
[[1, 2],
 [10, 20]])
julia> kron(ca.DM.eye(2), ca.DM([1 2; 3 4]))
DM(
[[1, 2, 00, 00],
 [3, 4, 00, 00],
 [00, 00, 1, 2],
 [00, 00, 3, 4]])
julia> size(kron(ca.MX.sym("a", 2, 2), ca.MX.sym("b", 1, 2)))
(2, 4)

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

Definition at line 2096 of file sparsity_interface.hpp.

2096  {
2097  return MatType::kron(a, b);
2098  }

◆ ldl

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

Computes A(p, p) = L*diag(D)*L^T with L unit lower triangular. Returns D as a vector, LT holding the strictly upper triangular entries of L^T (the unit diagonal is omitted), and the permutation p. Only the upper triangle of A is referenced. The sparsity pattern of A must be symmetric. Only guaranteed to work for positive definite matrices (no pivoting). Works symbolically on SX.

Parameters
ASquare matrix with symmetric sparsity pattern
amdUse an approximate minimum degree ordering to reduce fill-in
Returns
D, LT, p (a tuple in Python)

>>> D, LT, p = ldl(DM([[4, 2], [2, 3]]))
>>> D, p
(DM([4, 2]), [0, 1])
>>> LT
DM(
[[00, 0.5],
 [00, 00]])
>>> L = DM.eye(2) + LT.T
>>> mtimes(mtimes(L, diag(D)), L.T)
DM(
[[4, 2],
 [2, 3]])

>> [D, LT, p] = ldl(DM([4 2; 2 3]));
>> D, p
[4, 2]
0 1
>> LT
[[00, 0.5],
 [00, 00]]
>> L = DM.eye(2) + LT';
>> L*diag(D)*L'
[[4, 2],
 [2, 3]]

julia> D, LT, p = ca.ldl(ca.DM([4 2; 2 3]));
julia> D
DM([4, 2])
julia> p
2-element Vector{Int64}:
 0
 1
julia> LT
DM(
[[00, 0.5],
 [00, 00]])
julia> L = ca.DM.eye(2) + LT';
julia> L * ca.diag(D) * L'
DM(
[[4, 2],
 [2, 3]])

See also
ldl_solve, chol, Sparsity::ldl

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

Definition at line 1823 of file matrix_decl.hpp.

1824  {
1825  return Matrix<Scalar>::ldl(A, D, LT, p, amd);
1826  }
static void ldl(const Matrix< Scalar > &A, Matrix< Scalar > &D, Matrix< Scalar > &LT, std::vector< casadi_int > &p, bool amd=true)

◆ ldl_solve

template<typename Scalar >
Matrix<Scalar> ldl_solve ( const Matrix< Scalar > &  b,
const Matrix< Scalar > &  D,
const Matrix< Scalar > &  LT,
const std::vector< casadi_int > &  p 
)
friend
Parameters
bRight-hand side, one column per system; a sparse b is densified
D,LT,pOutput of ldl(A)
Returns
Dense solution with the shape of b

>>> A = DM([[4, 2], [2, 3]])
>>> D, LT, p = ldl(A)
>>> ldl_solve(DM([1, 2]), D, LT, p)
DM([-0.125, 0.75])
>>> solve(A, DM([1, 2]))
DM([-0.125, 0.75])

>> A = DM([4 2; 2 3]);
>> [D, LT, p] = ldl(A);
>> ldl_solve(DM([1; 2]), D, LT, p)
[-0.125, 0.75]
>> solve(A, DM([1; 2]))
[-0.125, 0.75]

julia> A = ca.DM([4 2; 2 3]);
julia> D, LT, p = ca.ldl(A);
julia> ca.ldl_solve(ca.DM([1, 2]), D, LT, p)
DM([-0.125, 0.75])
julia> A \ [1, 2]
DM([-0.125, 0.75])

See also
ldl, solve

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

Definition at line 1871 of file matrix_decl.hpp.

1872  {
1873  return Matrix<Scalar>::ldl_solve(b, D, LT, p);
1874  }
static Matrix< Scalar > ldl_solve(const Matrix< Scalar > &b, const Matrix< Scalar > &D, const Matrix< Scalar > &LT, const std::vector< casadi_int > &p)

◆ le

template<typename ExType >
ExType le ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt. Chained inequalities such as lb <= (x <= ub) are recognised by Opti as bound constraints.

>>> le(DM([1, 2, 3]), 2)
DM([1, 1, 0])
>>> x = SX.sym("x")
>>> x <= x
SX(1)

>> le(DM([1; 2; 3]), 2)
[1, 1, 0]
>> x = SX.sym('x');
>> x <= x
1

julia> ca.le(ca.DM([1, 2, 3]), 2)
DM([1, 1, 0])
julia> x = ca.SX.sym("x");
julia> x <= x
SX(1)

See also
lt

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

Definition at line 481 of file generic_expression.hpp.

481  {
482  return ExType::le(x, y);
483  }

◆ lift

MX lift ( const MX &  x,
const MX &  x_guess 
)
friend

Experimental feature. Marks x as a lifted intermediate variable with initial guess x_guess, for use by lifted Newton-type methods. Evaluates to x. Both arguments must have the same sparsity pattern.

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

Definition at line 4338 of file mx.hpp.

4338  {
4339  return MX::lift(x, x_guess);
4340  }
static MX lift(const MX &x, const MX &x_guess)
Definition: mx.cpp:766

◆ linear_coeff

template<typename MatType >
void linear_coeff ( const MatType &  expr,
const MatType &  var,
MatType &  A,
MatType &  b,
bool  check = true 
)
friend

A x + b

Returns A = jacobian(expr, var) at var = 0 and b = vec(expr) at var = 0. expr must be a vector (a matrix raises an error).

Parameters
check[in]When true (default), expr is checked to be linear in var (is_linear) and an error is raised otherwise. Provided to deal with false positive dependency checks.

* >>> x = SX.sym("x"); y = SX.sym("y")
* >>> A, b = linear_coeff(vertcat(2*x + y + 1, 3*y), vertcat(x, y))
* >>> A
* SX(
* [[2, 1],
*  [00, 3]])
* >>> b
* SX([1, 0])
* 

* >> x = SX.sym('x'); y = SX.sym('y');
* >> [A, b] = linear_coeff(vertcat(2*x + y + 1, 3*y), vertcat(x, y));
* >> A
* [[2, 1],
*  [00, 3]]
* >> b
* [1, 0]
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
* julia> A, b = ca.linear_coeff([2 * x + y + 1; 3 * y], [x; y]);
* julia> A
* SX(
* [[2, 1],
*  [00, 3]])
* julia> b
* SX([1, 0])
* 

See also
quadratic_coeff, is_linear, jacobian

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

Definition at line 4179 of file generic_matrix.hpp.

4180  {
4181  MatType::linear_coeff(expr, var, A, b, check);
4182  }

◆ linearize

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::linearize ( const MatType &  f,
const MatType &  x,
const MatType &  x0,
const Dict &  opts = Dict() 
)
friend

[DEPRECATED] confusing behaviour

This function linearizes an expression around a point x0, using x as delta around that point:

linearize(sin(x),x,x0) -> sin(x0)+cos(x0)*x

x0 must have the shape of x or be a scalar (broadcast). For a variant that returns sin(x0)+cos(x0)*(x-x0), see taylor.

* >>> x = SX.sym("x"); y = SX.sym("y")
* >>> linearize(sin(x), x, y)
* SX((sin(y)+(cos(y)*x)))
* 

* >> x = SX.sym('x'); y = SX.sym('y');
* >> linearize(sin(x), x, y)
* (sin(y)+(cos(y)*x))
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
* julia> ca.linearize(sin(x), x, y)
* SX((sin(y)+(cos(y)*x)))
* 

See also
taylor, linear_coeff

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

Definition at line 3293 of file generic_matrix.hpp.

3294  {
3295  return MatType::linearize(f, x, x0, opts);
3296  }

◆ linspace

template<typename MatType >
MatType linspace ( const MatType &  a,
const MatType &  b,
casadi_int  nsteps 
)
friend

Returns the column vector [a, a + (b-a)/(nsteps-1), ..., b] with nsteps entries, the endpoints included. For nsteps == 1 the result is b. When a and b are matrices, the nsteps intermediate matrices are stacked vertically (vertcat). nsteps must be at least 1.

>>> linspace(0, 1, 5)
DM([0, 0.25, 0.5, 0.75, 1])
>>> linspace(SX.sym("a"), SX.sym("b"), 3)
SX([a, (a+((b-a)/2)), b])
>>> linspace(DM([0, 0]), DM([1, 2]), 3)
DM([0, 0, 0.5, 1, 1, 2])

>> linspace(DM(0), DM(1), 5)
[0, 0.25, 0.5, 0.75, 1]
>> linspace(SX.sym('a'), SX.sym('b'), 3)
[a, (a+((b-a)/2)), b]
>> linspace(DM([0; 0]), DM([1; 2]), 3)
[0, 0, 0.5, 1, 1, 2]

julia> ca.linspace(0, 1, 5)
DM([0, 0.25, 0.5, 0.75, 1])
julia> ca.linspace(ca.SX.sym("a"), ca.SX.sym("b"), 3)
SX([a, (a+((b-a)/2)), b])
julia> ca.linspace(ca.DM([0, 0]), ca.DM([1, 2]), 3)
DM([0, 0, 0.5, 1, 1, 2])

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

Definition at line 1447 of file generic_matrix.hpp.

1447  {
1448  return MatType::linspace(a, b, nsteps);
1449  }

◆ log

template<typename ExType >
ExType log ( const ExType &  x)
friend

Elementwise; the result is dense with -inf at structural zeros. Negative arguments give NaN; no error is raised.

>>> log(DM([1, 0]))
DM([0, -inf])
>>> log(SX.sym("x"))
SX(log(x))

>> log(DM([1; 0]))
[0, -inf]
>> log(SX.sym('x'))
log(x)

julia> log.(ca.DM([1, 0]))
DM([0, -inf])
julia> log(ca.SX.sym("x"))
SX(log(x))

See also
exp, log10, log1p

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

Definition at line 1578 of file generic_expression.hpp.

1578  {
1579  return ExType::log(x);
1580  }

◆ log10

template<typename ExType >
ExType log10 ( const ExType &  x)
friend

Computed as log(x) * (1 / log(10)); there is no dedicated operation, which is visible in symbolic expressions. Dense result like log.

>>> log10(DM([1, 10, 1000]))
DM([0, 1, 3])
>>> log10(SX.sym("x"))
SX((0.434294*log(x)))

>> log10(DM([1; 10; 1000]))
[0, 1, 3]
>> log10(SX.sym('x'))
(0.434294*log(x))

julia> log10.(ca.DM([1, 10, 1000]))
DM([0, 1, 3])
julia> log10(ca.SX.sym("x"))
SX((0.434294*log(x)))

See also
log

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

Definition at line 1622 of file generic_expression.hpp.

1622  {
1623  return ExType::log10(x);
1624  }

◆ log1p

template<typename ExType >
ExType log1p ( const ExType &  x)
friend

Elementwise; accurate for small x where log(1 + x) would lose digits. Sparsity preserved (log1p(0) == 0).

>>> log1p(DM([0, 1e-10]))
DM([0, 1e-10])
>>> log1p(SX.sym("x"))
SX(log1p(x))

>> log1p(DM([0; 1e-10]))
[0, 1e-10]
>> log1p(SX.sym('x'))
log1p(x)

julia> log1p.(ca.DM([0, 1e-10]))
DM([0, 1e-10])
julia> log1p(ca.SX.sym("x"))
SX(log1p(x))

See also
log, expm1

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

Definition at line 1666 of file generic_expression.hpp.

1666  {
1667  return ExType::log1p(x);
1668  }

◆ logic_and

template<typename ExType >
ExType logic_and ( const ExType &  x,
const ExType &  y 
)
friend

Returns (an expression evaluating to) 1 where both entries are nonzero and 0 otherwise, elementwise

Any nonzero value, including NaN, counts as true. Shape rules as for plus; the result is structurally zero wherever either operand is. The Python keyword and does not work on matrices (it needs a single truth value); use this function.

 >>> logic_and(DM([1, 2, 0]), DM([3, 0, 0]))
 DM([1, 0, 0])
 >>> logic_and(SX.sym("x"), SX.sym("y"))
 SX((x&&y))
 >>> logic_and(DM.eye(2), DM.ones(2, 2))
 DM(
 [[1, 00],
  [00, 1]])

 >> DM([1; 2; 0]) & DM([3; 0; 0])
 [1, 0, 0]
 >> SX.sym('x') & SX.sym('y')
 (x&&y)
 >> and(DM.eye(2), DM.ones(2, 2))
 [[1, 00],
  [00, 1]]

 julia> ca.logic_and(ca.DM([1, 2, 0]), ca.DM([3, 0, 0]))
 DM([1, 0, 0])
 julia> ca.logic_and(ca.SX.sym("x"), ca.SX.sym("y"))
 SX((x&&y))
 julia> ca.logic_and(ca.DM.eye(2), ca.DM.ones(2, 2))
 DM(
 [[1, 00],
  [00, 1]])

See also
logic_or, logic_not

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

Definition at line 756 of file generic_expression.hpp.

756  {
757  return ExType::logic_and(x, y);
758  }

◆ logic_not

template<typename ExType >
ExType logic_not ( const ExType &  x)
friend

Returns (an expression evaluating to) 1 where the entry is zero and 0 otherwise, elementwise

Structural zeros map to 1, so a sparse input gives a dense result. The Python keyword not does not work on matrices; use this function.

>>> logic_not(DM([1, 0, -1]))
DM([0, 1, 0])
>>> logic_not(DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
>>> logic_not(SX.sym("x"))
SX((!x))

>> ~DM([1; 0; -1])
[0, 1, 0]
>> not(DM.eye(2))
[[0, 1],
 [1, 0]]
>> ~SX.sym('x')
(!x)

julia> ca.logic_not(ca.DM([1, 0, -1]))
DM([0, 1, 0])
julia> ca.logic_not(ca.DM.eye(2))
DM(
[[0, 1],
 [1, 0]])
julia> ca.logic_not(ca.SX.sym("x"))
SX((!x))

See also
logic_and, logic_or

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

Definition at line 866 of file generic_expression.hpp.

866  {
867  return ExType::logic_not(x);
868  }

◆ logic_or

template<typename ExType >
ExType logic_or ( const ExType &  x,
const ExType &  y 
)
friend

returns (an expression evaluating to) 1 where at least one entry is nonzero and 0 otherwise, elementwise

Shape rules as for plus; the result is structurally zero where both operands are.

 >>> logic_or(DM([1, 0, 0]), DM([0, 0, 2]))
 DM([1, 0, 1])
 >>> logic_or(MX.sym("x"), MX.sym("y"))
 MX((x||y))

 >> DM([1; 0; 0]) | DM([0; 0; 2])
 [1, 0, 1]
 >> or(MX.sym('x'), MX.sym('y'))
 (x||y)

 julia> ca.logic_or(ca.DM([1, 0, 0]), ca.DM([0, 0, 2]))
 DM([1, 0, 1])
 julia> ca.logic_or(ca.MX.sym("x"), ca.MX.sym("y"))
 MX((x||y))

See also
logic_and, logic_not

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

Definition at line 805 of file generic_expression.hpp.

805  {
806  return ExType::logic_or(x, y);
807  }

◆ logsumexp [1/2]

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::logsumexp ( const MatType &  x)
friend

Can be used to achieve a smooth max.

The argument must be a dense column vector; a scalar is returned unchanged. Implementations for DM/SX/MX are hardened against overflow by shifting with the largest entry. Implementations for DM/MX are more accurate with log1p (*).

(*) https://nhigham.com/2021/01/05/what-is-the-log-sum-exp-function/

>>> logsumexp(DM([1, 2, 3]))
DM(3.40761)
>>> logsumexp(DM([1000, 1000]))
DM(1000.69)
>>> logsumexp(MX.sym("x", 3))
MX(logsumexp(x))
>>> try: logsumexp(DM([1, 2, 3]).T)
... except Exception as e: print("error")
error

>> logsumexp(DM([1; 2; 3]))
3.40761
>> logsumexp(DM([1000; 1000]))
1000.69
>> logsumexp(MX.sym('x', 3))
logsumexp(x)
>> try, logsumexp(DM([1; 2; 3])'); catch e, disp('error'); end
error

julia> ca.logsumexp(ca.DM([1, 2, 3]))
DM(3.40761)
julia> ca.logsumexp(ca.DM([1000, 1000]))
DM(1000.69)
julia> ca.logsumexp(ca.MX.sym("x", 3))
MX(logsumexp(x))
julia> try ca.logsumexp(ca.DM([1, 2, 3])') catch e; println("error") end
error

See also
mmax

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

Definition at line 1369 of file generic_matrix.hpp.

1369  {
1370  return MatType::logsumexp(x);
1371  }

◆ logsumexp [2/2]

template<typename MatType >
MatType logsumexp ( const MatType &  x,
const MatType &  margin 
)
friend

Scaled such that max(x) <= logsumexp(x, margin) <= max(x)+margin, computed as logsumexp(alpha*x)/alpha with alpha = log(n)/margin for n = x.size1(). margin must be of the same type as x (MX for MX).

>>> logsumexp(DM([1, 2, 3]), 0.1)
DM(3)

>> logsumexp(DM([1; 2; 3]), 0.1)
3

julia> ca.logsumexp(ca.DM([1, 2, 3]), 0.1)
DM(3)

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

Definition at line 1400 of file generic_matrix.hpp.

1400  {
1401  MatType alpha = log(x.size1()) / margin;
1402  return MatType::logsumexp(alpha*x)/alpha;
1403  }
const long double alpha[8][8]

◆ low

MX low ( const MX &  v,
const MX &  p,
const Dict &  options = Dict() 
)
friend

For each entry p_i of p, returns the index k such that v[k] <= p_i < v[k+1] for the ascending grid v, clipped to [0, len(v)-2] so that values outside the grid map to the first or last interval. This is the lookup step of linear interpolation. The result has the shape of p and is piecewise constant, so its derivative is zero.

Parameters
vDense ascending grid, vector
pQuery values, dense vector
options"lookup_mode": "linear" (scan), "binary" (bisection), "exact" (assumes an equidistant grid and computes the index arithmetically) or "auto" (default: "binary" for grids longer than 100 entries, else "linear"). Any other key or mode raises an error.

>>> g = MX.sym("g", 4)
>>> p = MX.sym("p", 5)
>>> f = Function("f", [g, p], [low(g, p)])
>>> f(DM([0, 1, 2, 3]), DM([-1, 0, 1.5, 3, 4]))
DM([0, 0, 1, 2, 2])
>>> jacobian(low(g, p), p).nnz()
0
>>> try:
...   low(g, p, {"lookup_mode": "foo"})
... except Exception as e:
...   print("Invalid lookup mode" in str(e))
True

>> g = MX.sym('g', 4); p = MX.sym('p', 5);
>> f = Function('f', {g, p}, {low(g, p)});
>> f(DM([0; 1; 2; 3]), DM([-1; 0; 1.5; 3; 4]))
[0, 0, 1, 2, 2]
>> J = jacobian(low(g, p), p);
>> J.nnz()
0
>> try, low(g, p, struct('lookup_mode', 'foo')); catch e, disp('invalid lookup mode'); end
invalid lookup mode

julia> g = ca.MX.sym("g", 4); p = ca.MX.sym("p", 5);
julia> f = ca.Function("f", [g, p], [ca.low(g, p)]);
julia> f([0, 1, 2, 3], [-1, 0, 1.5, 3, 4])
DM([0, 0, 1, 2, 2])
julia> ca.nnz(ca.jacobian(ca.low(g, p), p))
0
julia> try
           ca.low(g, p, Dict("lookup_mode" => "foo"))
       catch e
           println(occursin("Invalid lookup mode", sprint(showerror, e)))
       end
true

See also
find, interpn_linear, interpolant

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

Definition at line 3802 of file mx.hpp.

3802  {
3803  return MX::low(v, p, options);
3804  }
static MX low(const MX &v, const MX &p, const Dict &options=Dict())
Definition: mx.cpp:2213

◆ lt

template<typename ExType >
ExType lt ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison; shape rules as for plus. Every comparison in this group returns a matrix of the same type as the operands with entries 1 (true) and 0 (false): for DM a numeric 0/1 matrix, not a boolean, and for SX and MX a symbolic expression that is only decided at evaluation time. Comparisons with NaN are false. The result is sparse where both operands are structurally zero (0 < 0 is false).

>>> lt(DM([1, 2, 3]), 2)
DM([1, 0, 0])
>>> DM([1, 2, 3]) < DM([3, 2, 1])
DM([1, 0, 0])
>>> SX.sym("x", 2) < SX.sym("y", 2)
SX([(x_0<y_0), (x_1<y_1)])
>>> DM.eye(2) < 1
DM(
[[0, 1],
 [1, 0]])

>> lt(DM([1; 2; 3]), 2)
[1, 0, 0]
>> DM([1; 2; 3]) < DM([3; 2; 1])
[1, 0, 0]
>> SX.sym('x', 2) < SX.sym('y', 2)
[(x_0<y_0), (x_1<y_1)]
>> DM.eye(2) < 1
[[0, 1],
 [1, 0]]

julia> ca.lt(ca.DM([1, 2, 3]), 2)
DM([1, 0, 0])
julia> ca.DM([1, 2, 3]) < ca.DM([3, 2, 1])
DM([1, 0, 0])
julia> ca.SX.sym("x", 2) < ca.SX.sym("y", 2)
SX([(x_0<y_0), (x_1<y_1)])
julia> ca.DM.eye(2) < 1
DM(
[[0, 1],
 [1, 0]])

See also
le, gt, ge, eq, ne, if_else

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

Definition at line 431 of file generic_expression.hpp.

431  {
432  return ExType::lt(x, y);
433  }

◆ mac

template<typename MatType >
MatType mac ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const std::string &  blas = "reference" 
)
friend

Matrix product of two matrices (x and y), adding the result to a third matrix z. The result has the same sparsity pattern as z, meaning that other entries of the product are ignored. The operation is equivalent to z + project(mtimes(x, y), z.sparsity()). The dimensions of z must match the product, otherwise an error is raised. When x or y is a scalar the result is z + x*y. See mtimes for the meaning of the blas argument.

mtimes is implemented as mac with a structurally zero z of the product pattern.

Parameters
xLeft factor
yRight factor
zMatrix to add to; its sparsity pattern selects the computed entries
Returns
Matrix with the sparsity pattern of z

>>> A = DM([[1, 2], [3, 4]])
>>> mac(A, A, DM.eye(2))
DM(
[[8, 00],
 [00, 23]])
>>> mac(A, A, DM.ones(2, 2))
DM(
[[8, 11],
 [16, 23]])
>>> mac(MX.sym("x", 2, 3), MX.sym("y", 3, 2), MX.sym("z", Sparsity.diag(2)))
MX(mac(x,y,z))

>> A = DM([1 2; 3 4]);
>> mac(A, A, DM.eye(2))
[[8, 00],
 [00, 23]]
>> mac(A, A, DM.ones(2, 2))
[[8, 11],
 [16, 23]]
>> mac(MX.sym('x', 2, 3), MX.sym('y', 3, 2), MX.sym('z', Sparsity.diag(2)))
mac(x,y,z)

julia> A = ca.DM([1 2; 3 4]);
julia> ca.mac(A, A, ca.DM.eye(2))
DM(
[[8, 00],
 [00, 23]])
julia> ca.mac(A, A, ca.DM.ones(2, 2))
DM(
[[8, 11],
 [16, 23]])
julia> x, y = ca.MX.sym("x", 2, 3), ca.MX.sym("y", 3, 2);
julia> ca.mac(x, y, ca.MX.sym("z", ca.Sparsity.diag(2)))
MX(mac(x,y,z))

See also
mtimes

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

Definition at line 1489 of file sparsity_interface.hpp.

1490  {
1491  return MatType::mac(x, y, z, blas);
1492  }

◆ matrix_expand [1/2]

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

Converts the whole matrix graph of e to scalar SX operations (including inlined Function calls, as Function.expand does) and returns a call to the resulting SXFunction, with the free symbols of e as arguments. Expressions listed in boundary are not expanded: they stay as MX inputs of the call.

Parameters
eExpression to expand
boundarySub-expressions at which the expansion stops
optionsOptions for the SXFunction, forwarded to Function.expand
Returns
An OutputNode of the SXFunction call

>>> x = MX.sym("x")
>>> g = Function("g", [x], [x**2 + 1])
>>> sx = sin(x)
>>> e = g(x) + sx
>>> r = matrix_expand(e)
>>> r, r.is_output(), r.dep(0).which_function()
(MX(expand_tmp_matrix_expand(x){0}), True, Function(expand_tmp_matrix_expand:(i0)->(o0)
SXFunction))
>>> Function("f", [x], [r])(2)
DM(5.9093)
>>> matrix_expand(e, [sx]).dep(0).which_function()
Function(expand_tmp_matrix_expand:(i0,i1)->(o0) SXFunction)

>> x = MX.sym('x');
>> g = Function('g', {x}, {x^2 + 1});
>> sx = sin(x);
>> e = g(x) + sx;
>> r = matrix_expand(e);
>> r, r.is_output(), r.dep(0).which_function()
expand_tmp_matrix_expand(x){0}
1
expand_tmp_matrix_expand:(i0)->(o0) SXFunction
>> f = Function('f', {x}, {r});
>> f(2)
5.9093
>> r = matrix_expand(e, {sx});
>> r.dep(0).which_function()
expand_tmp_matrix_expand:(i0,i1)->(o0) SXFunction

julia> x = ca.MX.sym("x");
julia> g = ca.Function("g", [x], [x^2 + 1]);
julia> sx = sin(x);
julia> e = g(x) + sx;
julia> r = ca.matrix_expand(e);
julia> r, ca.is_output(r)
(expand_tmp_matrix_expand(x){0}, true)
julia> ca.which_function(ca.dep(r, 0))
Function(expand_tmp_matrix_expand:(i0)->(o0) SXFunction)
julia> ca.Function("f", [x], [r])(2)
DM(5.9093)
julia> ca.which_function(ca.dep(ca.matrix_expand(e, [sx]), 0))
Function(expand_tmp_matrix_expand:(i0,i1)->(o0) SXFunction)

See also
Function.expand

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

Definition at line 4020 of file mx.hpp.

4021  {
4022  return MX::matrix_expand(e, boundary, options);
4023  }
static MX matrix_expand(const MX &e, const std::vector< MX > &boundary, const Dict &options)
Definition: mx.cpp:2039

◆ matrix_expand [2/2]

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

Same as the single-expression overload; the result is one output per expression.

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

Definition at line 4031 of file mx.hpp.

4033  {
4034  return MX::matrix_expand(e, boundary, options);
4035  }

◆ minor

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

Note the argument order: i is the column that is removed, j the row. The input must be square; a 1-by-1 input gives 1.

Parameters
xSquare matrix
iColumn index to remove (zero-based)
jRow index to remove (zero-based)

>>> A = DM([[1, 2, 3], [0, 1, 4], [5, 6, 0]])
>>> minor(A, 0, 1)
DM(-18)
>>> X = SX.sym("X", 2, 2)
>>> minor(X, 0, 1)
SX(X_2)

>> A = DM([1 2 3; 0 1 4; 5 6 0]);
>> minor(A, 0, 1)
-18
>> X = SX.sym('X', 2, 2);
>> minor(X, 0, 1)
X_2

julia> A = ca.DM([1 2 3; 0 1 4; 5 6 0]);
julia> ca.minor(A, 0, 1)
DM(-18)
julia> X = ca.SX.sym("X", 2, 2);
julia> ca.minor(X, 0, 1)
SX(X_2)

See also
cofactor, adj, det

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

Definition at line 1472 of file matrix_decl.hpp.

1472  {
1473  return Matrix<Scalar>::minor(x, i, j);
1474  }
static Matrix< Scalar > minor(const Matrix< Scalar > &x, casadi_int i, casadi_int j)

◆ minus

template<typename ExType >
ExType minus ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise difference; shape and sparsity rules as for plus. For symbolic arguments, x - x with the same node simplifies to 0.

>>> minus(DM([5, 6]), DM([1, 2]))
DM([4, 4])
>>> DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
>>> x = SX.sym("x")
>>> x - x
SX(0)

>> minus(DM([5; 6]), DM([1; 2]))
[4, 4]
>> DM.eye(2) - 1
[[0, -1],
 [-1, 0]]
>> x = SX.sym('x');
>> x - x
0

julia> ca.minus(ca.DM([5, 6]), ca.DM([1, 2]))
DM([4, 4])
julia> ca.DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
julia> x = ca.SX.sym("x");
julia> x - x
SX(0)

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

Definition at line 224 of file generic_expression.hpp.

224  {
225  return ExType::minus(x, y);
226  }

◆ mldivide

template<typename MatType >
MatType mldivide ( const MatType &  x,
const MatType &  n 
)
friend

Computes inv(A)*b as solve(A, b). If either argument is scalar, the elementwise division b/A is returned instead.

>>> A = DM([[1, 2], [3, 4]])
>>> mldivide(A, DM([1, 2]))
DM([0, 0.5])

>> A = DM([1 2; 3 4]);
>> A\DM([1; 2])
[0, 0.5]

julia> A = ca.DM([1 2; 3 4]);
julia> A \ ca.DM([1, 2])
DM([0, 0.5])

See also
mrdivide, solve, inv

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

Definition at line 1088 of file generic_matrix.hpp.

1088  {
1089  return MatType::mldivide(x, n);
1090  }

◆ mmax

template<typename MatType >
MatType mmax ( const MatType &  x)
friend

Over all entries, structural zeros included. See mmin.

>>> mmax(DM(Sparsity.diag(2), [-3, -4]))
DM(0)
>>> mmax(MX.sym("x", 3))
MX(max(x))

>> mmax(DM(Sparsity.diag(2), [-3; -4]))
0
>> mmax(MX.sym('x', 3))
max(x)

julia> ca.mmax(ca.DM(ca.Sparsity.diag(2), [-3, -4]))
DM(0)
julia> ca.mmax(ca.MX.sym("x", 3))
MX(max(x))

See also
mmin, fmax, logsumexp

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

Definition at line 4861 of file generic_matrix.hpp.

4861  {
4862  return MatType::mmax(x);
4863  }

◆ mmin

template<typename MatType >
MatType mmin ( const MatType &  x)
friend

Over all entries, structural zeros included (so a sparse matrix with only positive nonzeros has minimum 0). An empty matrix gives an empty 0-by-0 result. For SX a chain of fmin operations is built; for MX a single node.

>>> mmin(DM([[3, -1], [2, 5]])), mmax(DM([[3, -1], [2, 5]]))
(DM(-1), DM(5))
>>> mmin(DM(Sparsity.diag(2), [3, 4]))
DM(0)
>>> mmin(SX.sym("x", 3))
SX(fmin(fmin(x_0,x_1),x_2))

>> mmin(DM([3 -1; 2 5])), mmax(DM([3 -1; 2 5]))
-1
5
>> mmin(DM(Sparsity.diag(2), [3; 4]))
0
>> mmin(SX.sym('x', 3))
fmin(fmin(x_0,x_1),x_2)

julia> ca.mmin(ca.DM([3 -1; 2 5])), ca.mmax(ca.DM([3 -1; 2 5]))
(-1, 5)
julia> minimum(ca.DM([3 -1; 2 5])), maximum(ca.DM([3 -1; 2 5]))
(-1, 5)
julia> ca.mmin(ca.DM(ca.Sparsity.diag(2), [3, 4]))
DM(0)
julia> ca.mmin(ca.SX.sym("x", 3))
SX(fmin(fmin(x_0,x_1),x_2))

See also
mmax, fmin, norm_inf

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

Definition at line 4821 of file generic_matrix.hpp.

4821  {
4822  return MatType::mmin(x);
4823  }

◆ mod

template<typename ExType >
ExType mod ( const ExType &  x,
const ExType &  y 
)
friend

This Function follows the convention of https://en.cppreference.com/w/c/numeric/math/fmod: the result has the sign of x and magnitude below |y|.

This is equivalent to Python's numpy.fmod and Matlab's rem. It differs from the Python operator x % y on these objects, which follows the Python convention x - y*floor(x/y) (result with the sign of y). Elementwise, shape rules as for plus; structural zeros of x stay zero. A zero divisor gives NaN.

>>> fmod(DM([5, 5, -5, -5]), DM([3, -3, 3, -3]))
DM([2, 2, -2, -2])
>>> DM([5, 5, -5, -5]) % DM([3, -3, 3, -3])
DM([2, -1, 1, -2])
>>> fmod(SX.sym("x"), 2), SX.sym("x") % 2
(SX(fmod(x,2)), SX((x-(2.*floor((x/2))))))

>> x = DM([5; 5; -5; -5]); y = DM([3; -3; 3; -3]);
>> fmod(x, y)
[2, 2, -2, -2]
>> rem(x, y)
[2, 2, -2, -2]
>> x - y .* floor(x ./ y)
[2, -1, 1, -2]
>> rem(SX.sym('x'), 2)
fmod(x,2)

julia> ca.fmod(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([2, 2, -2, -2])
julia> mod.(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([2, -1, 1, -2])
julia> ca.fmod(ca.SX.sym("x"), 2)
SX(fmod(x,2))

See also
remainder, floor
Extra doc: https://github.com/casadi/casadi/wiki/L_pq 

Definition at line 2039 of file generic_expression.hpp.

2039  {
2040  return ExType::mod(x, y);
2041  }

◆ mpower

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::mpower ( const MatType &  x,
const MatType &  n 
)
friend

For a square matrix x and a constant integer scalar n, computes the repeated matrix product by squaring. n == 0 gives the (sparse) identity, negative n gives the inverse of the positive power (via inv). If both arguments are scalar, this is the elementwise pow. A non-integer or symbolic n for a matrix raises an error.

>>> A = DM([[1, 2], [3, 4]])
>>> mpower(A, 2)
DM(
[[7, 10],
 [15, 22]])
>>> mpower(A, 0)
DM(
[[1, 00],
 [00, 1]])
>>> mpower(A, -1)
DM(
[[-2, 1],
 [1.5, -0.5]])
>>> mpower(DM(2), 0.5)
DM(1.41421)
>>> try: mpower(A, 0.5)
... except Exception as e: print("error")
error

>> A = DM([1 2; 3 4]);
>> A^2
[[7, 10],
 [15, 22]]
>> A^0
[[1, 00],
 [00, 1]]
>> mpower(A, -1)
[[-2, 1],
 [1.5, -0.5]]
>> DM(2)^0.5
1.41421
>> try, A^0.5; catch e, disp('error'); end
error

julia> A = ca.DM([1 2; 3 4]);
julia> A^2
DM(
[[7, 10],
 [15, 22]])
julia> A^0
DM(
[[1, 00],
 [00, 1]])
julia> A^-1
DM(
[[-2, 1],
 [1.5, -0.5]])
julia> ca.DM(2)^0.5
DM(1.41421)
julia> try A^0.5 catch e; println("error") end
error

See also
mtimes, inv, expm

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

Definition at line 891 of file generic_matrix.hpp.

891  {
892  return MatType::mpower(x, n);
893  }

◆ mrdivide

template<typename MatType >
MatType mrdivide ( const MatType &  x,
const MatType &  n 
)
friend

Computes x*inv(A) as solve(A', x')'. If either argument is scalar, the elementwise division x/A is returned instead.

>>> A = DM([[1, 2], [3, 4]])
>>> mrdivide(DM([[1, 2]]), A)
DM([[1, 0]])

>> A = DM([1 2; 3 4]);
>> DM([1 2])/A
[[1, 0]]

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

See also
mldivide, solve, inv

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

Definition at line 1052 of file generic_matrix.hpp.

1052  {
1053  return MatType::mrdivide(x, n);
1054  }

◆ mtaylor [1/2]

template<typename Scalar >
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 
)
friend

Same as mtaylor, but each variable contributes a given weight to the total degree: with x = [x, y] and order_contributions = [1, 2] the degree of x^n*y^m is n + 2*m. The length of order_contributions must equal the number of entries of x.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> mtaylor(sin(x + y), vertcat(x, y), vertcat(0, 0), 3, [1, 2])
SX(((x-((sq(x)*x)/6))+y))

>> x = SX.sym('x'); y = SX.sym('y');
>> mtaylor(sin(x + y), [x; y], [0; 0], 3, [1 2])
((x-((sq(x)*x)/6))+y)

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.mtaylor(sin(x + y), [x; y], [0; 0], 3, [1, 2])
SX(((x-((sq(x)*x)/6))+y))

See also
taylor

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

Definition at line 2549 of file matrix_decl.hpp.

2551  {
2552  return Matrix<Scalar>::mtaylor(ex, x, a, order, order_contributions);
2553  }
static Matrix< Scalar > mtaylor(const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order)

◆ mtaylor [2/2]

template<typename Scalar >
Matrix<Scalar> mtaylor ( const Matrix< Scalar > &  ex,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  a,
casadi_int  order = 1 
)
friend

Expands ex around the point a with respect to the symbols in x, keeping all terms whose total degree is at most order. The total degree of x^n*y^m is n+m. ex and x must be dense; a must have the same number of entries as x. Only defined for SX.

Parameters
exDense SX expression to expand
xVector of SX symbols
aExpansion point, a vector of the same length as x
orderMaximum total degree, default 1 (linearization)

>>> x = SX.sym("x"); y = SX.sym("y"); a = SX.sym("a"); b = SX.sym("b")
>>> mtaylor(sin(x + y), vertcat(x, y), vertcat(a, b), 1)
SX(((sin((a+b))+(cos((a+b))*(x-a)))+(cos((a+b))*(y-b))))
>>> mtaylor(exp(x * y), vertcat(x, y), DM([0, 0]), 2)
SX(@1=2, ((1+((x*y)/@1))+((y*x)/@1)))

>> x = SX.sym('x'); y = SX.sym('y'); a = SX.sym('a'); b = SX.sym('b');
>> mtaylor(sin(x + y), [x; y], [a; b], 1)
((sin((a+b))+(cos((a+b))*(x-a)))+(cos((a+b))*(y-b)))
>> mtaylor(exp(x * y), [x; y], DM([0; 0]), 2)
@1=2, ((1+((x*y)/@1))+((y*x)/@1))

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); a = ca.SX.sym("a"); b = ca.SX.sym("b");
julia> ca.mtaylor(sin(x + y), [x; y], [a; b], 1)
SX(((sin((a+b))+(cos((a+b))*(x-a)))+(cos((a+b))*(y-b))))
julia> ca.mtaylor(exp(x * y), [x; y], ca.DM([0, 0]), 2)
SX(@1=2, ((1+((x*y)/@1))+((y*x)/@1)))

See also
taylor

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

Definition at line 2511 of file matrix_decl.hpp.

2512  {
2513  return Matrix<Scalar>::mtaylor(ex, x, a, order);
2514  }

◆ mtimes [1/2]

template<typename MatType >
MatType mtimes ( const MatType &  x,
const MatType &  y,
const std::string &  blas = "reference" 
)
friend

Computes x*y in the linear-algebra sense; the column count of x must equal the row count of y, otherwise an error is raised. When either argument is a scalar the result is the elementwise product instead. The sparsity pattern of the result is the structural product of the patterns (entries that are structurally zero in every term stay structural zeros). For Sparsity arguments only the pattern of the product is computed.

Optional blas selects the BLAS plugin used for dense numeric kernels. The choice only takes effect on numeric DM / MX dense * dense; symbolic types (Sparsity, SX) and the sparse fallback path ignore it.

Values of blas:

  • reference (default): built-in reference kernel (triple loop)
  • classic: whatever BLAS was built into casadi via WITH_LAPACK (typically OpenBLAS)
  • blasfeo: BLASFEO

Plugins (classic, blasfeo) are loaded lazily on first use; if the corresponding plugin DLL isn't present in the build, calling them raises an error.

Parameters
xLeft factor
yRight factor
blasName of the BLAS plugin for dense numeric products
Returns
Matrix of size x.size1() by y.size2()

>>> A = DM([[1, 2], [3, 4]])
>>> mtimes(A, A)
DM(
[[7, 10],
 [15, 22]])
>>> mtimes(A, 2)
DM(
[[2, 4],
 [6, 8]])
>>> mtimes(Sparsity.diag(2), Sparsity.dense(2, 1))
Sparsity(2x1)
>>> mtimes(MX.sym("x", 2, 3), MX.sym("y", 3, 1))
MX(mac(x,y,zeros(2x1)))
>>> mtimes(A, DM.ones(3, 1))
Traceback (most recent call last):
  ...
RuntimeError: ...incompatible dimensions...

>> A = DM([1 2; 3 4]);
>> A * A
[[7, 10],
 [15, 22]]
>> mtimes(A, 2)
[[2, 4],
 [6, 8]]
>> MX.sym('x', 2, 3) * MX.sym('y', 3, 1)
mac(x,y,zeros(2x1))
>> try, A * DM.ones(3, 1); catch e, disp(e.message); end
...incompatible dimensions...

julia> A = ca.DM([1 2; 3 4]);
julia> A * A
DM(
[[7, 10],
 [15, 22]])
julia> A * 2
DM(
[[2, 4],
 [6, 8]])
julia> ca.mtimes(ca.Sparsity.diag(2), ca.Sparsity.dense(2, 1))
Sparsity(2x1)
julia> ca.MX.sym("x", 2, 3) * ca.MX.sym("y", 3, 1)
MX(mac(x,y,zeros(2x1)))
julia> try
           A * ca.DM.ones(3, 1)
       catch err
           println(occursin("incompatible dimensions", sprint(showerror, err)))
       end
true

See also
mac, times, norm_0_mul

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

Definition at line 1381 of file sparsity_interface.hpp.

1382  {
1383  return MatType::mtimes(x, y, blas);
1384  }

◆ mtimes [2/2]

template<typename MatType >
MatType mtimes ( const std::vector< MatType > &  args,
const std::string &  blas = "reference" 
)
friend

Multiplies the matrices left to right: mtimes([a, b, c]) == mtimes(mtimes(a, b), c). An empty list raises an error; a single matrix is returned as is. See the 2-arg mtimes for the meaning of blas.

>>> mtimes([DM([[1, 2]]), DM([[1, 0], [0, 2]]), DM([1, 1])])
DM(5)
>>> mtimes([])
Traceback (most recent call last):
  ...
RuntimeError: ...must not be empty...

julia> ca.DM([1 2]) * ca.DM([1 0; 0 2]) * ca.DM([1, 1])
DM(5)
julia> try
           ca.mtimes(ca.DM[])
       catch err
           println(occursin("must not be empty", sprint(showerror, err)))
       end
true

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

Definition at line 1417 of file sparsity_interface.hpp.

1418  {
1419  return MatType::mtimes(args, blas);
1420  }

◆ n_nodes

template<typename MatType >
casadi_int n_nodes ( const MatType &  A)
friend

The number of nodes of the Function that evaluates the expression, which for SX includes symbols and constants and for MX also output nodes. Shared subexpressions are counted once; identical subexpressions built separately are counted separately (use cse to merge them). Not defined for DM.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> n_nodes(x), n_nodes(x + y), n_nodes(vertcat(x + y, x + y))
(1, 3, 4)
>>> n_nodes(SX(0, 1))
0

>> x = SX.sym('x'); y = SX.sym('y');
>> fprintf('%d %d %d\n', n_nodes(x), n_nodes(x + y), n_nodes(vertcat(x + y, x + y)))
1 3 4
>> n_nodes(SX(0, 1))
0

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.n_nodes(x), ca.n_nodes(x + y), ca.n_nodes([x + y; x + y])
(1, 3, 4)
julia> ca.n_nodes(ca.SX(0, 1))
0

See also
cse, Function::n_nodes

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

Definition at line 4445 of file generic_matrix.hpp.

4445  {
4446  return MatType::n_nodes(A);
4447  }

◆ ne

template<typename ExType >
ExType ne ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt and eq. For symbolic operands x != x with the same node simplifies to 0, so it cannot be used to detect NaN symbolically.

>>> ne(DM([1, 2, 3]), 2)
DM([1, 0, 1])
>>> x = MX.sym("x")
>>> x != x
MX(0)

>> ne(DM([1; 2; 3]), 2)
[1, 0, 1]
>> x = MX.sym('x');
>> x ~= x
0

julia> ca.ne(ca.DM([1, 2, 3]), 2)
DM([1, 0, 1])
julia> x = ca.MX.sym("x");
julia> x != x
MX(0)

See also
eq

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

Definition at line 694 of file generic_expression.hpp.

694  {
695  return ExType::ne(x, y);
696  }

◆ no_grad

MX no_grad ( const MX &  expr)
friend

Same as stop_diff(expr, 1): the value is unchanged but all derivatives with respect to the symbols in expr are zero.

>>> x = MX.sym("x")
>>> y = no_grad(x**2)
>>> Function("f", [x], [y])(3), jacobian(y, x).nnz()
(DM(9), 0)
>>> Function("f", [x], [jacobian(x * y, x)])(3)
DM(9)

>> x = MX.sym('x');
>> y = no_grad(x^2); J = jacobian(y, x);
>> f = Function('f', {x}, {y});
>> f(3), J.nnz()
9
0
>> f = Function('f', {x}, {jacobian(x * y, x)});
>> f(3)
9

julia> x = ca.MX.sym("x");
julia> y = ca.no_grad(x^2);
julia> ca.Function("f", [x], [y])(3), ca.nnz(ca.jacobian(y, x))
(9, 0)
julia> ca.Function("f", [x], [ca.jacobian(x * y, x)])(3)
DM(9)

See also
stop_diff, no_hess

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

Definition at line 4544 of file mx.hpp.

4544  {
4545  return MX::stop_diff(expr, 1);
4546  }
static MX stop_diff(const MX &expr, casadi_int order)
Definition: mx.cpp:3020

◆ no_hess

MX no_hess ( const MX &  expr)
friend

Same as stop_diff(expr, 2): first derivatives are exact, the Hessian contribution is zero. Useful for Gauss-Newton-type Hessians.

>>> x = MX.sym("x")
>>> y = no_hess(x**3)
>>> Function("f", [x], [jacobian(y, x)])(2), hessian(y, x)[0].nnz()
(DM(12), 0)

>> x = MX.sym('x');
>> y = no_hess(x^3);
>> f = Function('f', {x}, {jacobian(y, x)});
>> [H, g] = hessian(y, x);
>> f(2), H.nnz()
12
0

julia> x = ca.MX.sym("x");
julia> y = ca.no_hess(x^3);
julia> ca.Function("f", [x], [ca.jacobian(y, x)])(2), ca.nnz(first(ca.hessian(y, x)))
(12, 0)

See also
stop_diff, no_grad

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

Definition at line 4586 of file mx.hpp.

4586  {
4587  return MX::stop_diff(expr, 2);
4588  }

◆ norm_0_mul

template<typename MatType >
casadi_int norm_0_mul ( const MatType &  x,
const MatType &  y 
)
friend

Number of structural nonzeros that mtimes(x, y) would have, computed from the sparsity patterns without forming the product. The inner dimensions must match, otherwise an error is raised.

>>> norm_0_mul(DM.eye(3), DM.ones(3, 2))
6
>>> norm_0_mul(Sparsity.diag(3), Sparsity.diag(3))
3
>>> norm_0_mul(DM.ones(2, 3), DM.ones(2, 3))
Traceback (most recent call last):
  ...
RuntimeError: ...Dimension error...

>> norm_0_mul(DM.eye(3), DM.ones(3, 2))
6
>> norm_0_mul(Sparsity.diag(3), Sparsity.diag(3))
3
>> try, norm_0_mul(DM.ones(2, 3), DM.ones(2, 3)); catch e, disp(e.message); end
...Dimension error...

julia> ca.norm_0_mul(ca.DM.eye(3), ca.DM.ones(3, 2))
6
julia> ca.norm_0_mul(ca.Sparsity.diag(3), ca.Sparsity.diag(3))
3
julia> try
           ca.norm_0_mul(ca.DM.ones(2, 3), ca.DM.ones(2, 3))
       catch err
           println(occursin("Dimension error", sprint(showerror, err)))
       end
true

See also
mtimes

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

Definition at line 1921 of file sparsity_interface.hpp.

1921  {
1922  return MatType::norm_0_mul(x, y);
1923  }

◆ norm_1

template<typename MatType >
MatType norm_1 ( const MatType &  x)
friend

Sum of the absolute values of all entries. For a matrix this is the entrywise norm, not the induced (maximum column sum) matrix norm. Nonsmooth at zero entries.

>>> norm_1(DM([[1, -2], [3, 4]]))
DM(10)
>>> norm_1(SX.sym("x", 2))
SX((fabs(x_0)+fabs(x_1)))

>> norm_1(DM([1 -2; 3 4]))
10
>> norm_1(SX.sym('x', 2))
(fabs(x_0)+fabs(x_1))

julia> ca.norm_1(ca.DM([1 -2; 3 4]))
DM(10)
julia> ca.norm_1(ca.SX.sym("x", 2))
SX((fabs(x_0)+fabs(x_1)))

See also
norm_inf, norm_2, norm_fro

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

Definition at line 2031 of file generic_matrix.hpp.

2031 { return MatType::norm_1(x);}

◆ norm_2

template<typename MatType >
MatType norm_2 ( const MatType &  x)
friend

For a vector (row or column) this is the Euclidean norm, identical to norm_fro. For a matrix argument DM and SX raise an error (use norm_fro if that is what you meant); MX creates a spectral-norm node that cannot be evaluated. Not differentiable at x == 0; use sumsqr in objectives where possible.

>>> norm_2(DM([3, 4]))
DM(5)
>>> norm_2(MX.sym("x", 2))
MX(||x||_F)
>>> try: norm_2(DM([[1, 2], [3, 4]]))
... except Exception as e: print("error")
error

>> norm_2(DM([3; 4]))
5
>> norm_2(MX.sym('x', 2))
||x||_F
>> try, norm_2(DM([1 2; 3 4])); catch e, disp('error'); end
error

julia> ca.norm_2(ca.DM([3, 4]))
DM(5)
julia> ca.norm_2(ca.MX.sym("x", 2))
MX(||x||_F)
julia> try ca.norm_2(ca.DM([1 2; 3 4])) catch e; println("error") end
error

See also
norm_fro, norm_1, norm_inf, sumsqr

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

Definition at line 1994 of file generic_matrix.hpp.

1994 { return MatType::norm_2(x);}

◆ norm_fro

template<typename MatType >
MatType norm_fro ( const MatType &  x)
friend

sqrt(sum_ij x_ij^2) over all entries, for a matrix of any shape. Not differentiable at x == 0 (the derivative evaluates to nan there); use sumsqr in objectives where possible.

>>> norm_fro(DM([[1, -2], [3, 4]]))
DM(5.47723)
>>> norm_fro(SX.sym("x", 2))
SX(sqrt((sq(x_0)+sq(x_1))))

>> norm_fro(DM([1 -2; 3 4]))
5.47723
>> norm_fro(SX.sym('x', 2))
sqrt((sq(x_0)+sq(x_1)))

julia> ca.norm_fro(ca.DM([1 -2; 3 4]))
DM(5.47723)
julia> ca.norm_fro(ca.SX.sym("x", 2))
SX(sqrt((sq(x_0)+sq(x_1))))

See also
norm_2, norm_1, norm_inf, sumsqr

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

Definition at line 1948 of file generic_matrix.hpp.

1948 { return MatType::norm_fro(x);}

◆ norm_inf

template<typename MatType >
MatType norm_inf ( const MatType &  x)
friend

Largest absolute value among all entries. For a matrix this is the entrywise norm, not the induced (maximum row sum) matrix norm. Nonsmooth.

>>> norm_inf(DM([[1, -2], [3, 4]]))
DM(4)
>>> norm_inf(SX.sym("x", 2))
SX(fmax(fmax(0,fabs(x_0)),fabs(x_1)))

>> norm_inf(DM([1 -2; 3 4]))
4
>> norm_inf(SX.sym('x', 2))
fmax(fmax(0,fabs(x_0)),fabs(x_1))

julia> ca.norm_inf(ca.DM([1 -2; 3 4]))
DM(4)
julia> ca.norm_inf(ca.SX.sym("x", 2))
SX(fmax(fmax(0,fabs(x_0)),fabs(x_1)))

See also
norm_1, norm_2, norm_fro, mmax

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

Definition at line 2068 of file generic_matrix.hpp.

2068 { return MatType::norm_inf(x);}

◆ norm_inf_mul

template<typename Scalar >
Matrix<Scalar> norm_inf_mul ( const Matrix< Scalar > &  x,
const Matrix< Scalar > &  y 
)
friend

Equals norm_inf(mtimes(x, y)): the largest absolute entry of x*y. Exploits sparsity; a shape mismatch raises an error. Works symbolically on SX.

>>> A = DM([[1, 2], [3, 4]])
>>> norm_inf_mul(A, A), norm_inf(mtimes(A, A))
(DM(22), DM(22))

>> A = DM([1 2; 3 4]);
>> norm_inf_mul(A, A), norm_inf(A*A)
22
22

julia> A = ca.DM([1 2; 3 4]);
julia> ca.norm_inf_mul(A, A), ca.norm_inf(A * A)
(22, 22)

See also
norm_inf, mtimes

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

Definition at line 1954 of file matrix_decl.hpp.

1954  {
1955  return Matrix<Scalar>::norm_inf_mul(x, y);
1956  }
static Matrix< Scalar > norm_inf_mul(const Matrix< Scalar > &x, const Matrix< Scalar > &y)

◆ nullspace

template<typename MatType >
MatType nullspace ( const MatType &  A)
friend

Finds Z m-by-(m-n) such that AZ = 0 with A n-by-m with m >= n (a matrix with more rows than columns raises an error).

Assumes A is full rank. Computed with Householder reflections, dense; for MX the SX algorithm is embedded as a function call node.

Inspired by Numerical Methods in Scientific Computing by Ake Bjorck

>>> A = DM([[1, 2, 3], [4, 5, 6]])
>>> Z = nullspace(A)
>>> Z.dim(), norm_1(mtimes(A, Z))
('3x1', DM(0))

>> A = DM([1 2 3; 4 5 6]);
>> Z = nullspace(A);
>> Z.dim(), norm_1(A*Z)
3x1
0

julia> A = ca.DM([1 2 3; 4 5 6]);
julia> Z = ca.nullspace(A);
julia> ca.dim(Z), ca.norm_1(A * Z)
("3x1", 0)

See also
qr, pinv

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

Definition at line 2287 of file generic_matrix.hpp.

2287  {
2288  return MatType::nullspace(A);
2289  }

◆ offset

template<typename MatType >
std::vector<casadi_int > offset ( const std::vector< MatType > &  v,
bool  vert = true 
)
friend

Returns the cumulative row counts (or column counts when vert is false) of the matrices in v, starting with 0. The result has one entry more than v and is the offset list that vertsplit (or horzsplit) needs to undo a vertcat (or horzcat) of v.

Parameters
vList of matrices (or Sparsity patterns)
vertTrue for row offsets, false for column offsets
Returns
List of offsets of length len(v)+1

>>> offset([DM(2, 3), DM(4, 3)])
[0, 2, 6]
>>> offset([DM(2, 3), DM(2, 4)], False)
[0, 3, 7]
>>> offset([])
[0]

julia> ca.offset([ca.DM(2, 3), ca.DM(4, 3)])
3-element Vector{Int64}:
 0
 2
 6
julia> ca.offset([ca.DM(2, 3), ca.DM(2, 4)], false)
3-element Vector{Int64}:
 0
 3
 7
julia> ca.offset(ca.DM[])
1-element Vector{Int64}:
 0

See also
vertsplit, horzsplit

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

Definition at line 583 of file sparsity_interface.hpp.

583  {
584  return MatType::offset(v, vert);
585  }

◆ operator!=

template<typename ExType >
ExType operator!= ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt and eq. For symbolic operands x != x with the same node simplifies to 0, so it cannot be used to detect NaN symbolically.

>>> ne(DM([1, 2, 3]), 2)
DM([1, 0, 1])
>>> x = MX.sym("x")
>>> x != x
MX(0)

>> ne(DM([1; 2; 3]), 2)
[1, 0, 1]
>> x = MX.sym('x');
>> x ~= x
0

julia> ca.ne(ca.DM([1, 2, 3]), 2)
DM([1, 0, 1])
julia> x = ca.MX.sym("x");
julia> x != x
MX(0)

See also
eq

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

Definition at line 697 of file generic_expression.hpp.

697  {
698  return ne(x, y);
699  }
static ExType ne(const ExType &x, const ExType &y)
Logical not equal to: (x,y) -> x != y.

◆ operator&&

template<typename ExType >
ExType operator&& ( const ExType &  x,
const ExType &  y 
)
friend

Returns (an expression evaluating to) 1 where both entries are nonzero and 0 otherwise, elementwise

Any nonzero value, including NaN, counts as true. Shape rules as for plus; the result is structurally zero wherever either operand is. The Python keyword and does not work on matrices (it needs a single truth value); use this function.

 >>> logic_and(DM([1, 2, 0]), DM([3, 0, 0]))
 DM([1, 0, 0])
 >>> logic_and(SX.sym("x"), SX.sym("y"))
 SX((x&&y))
 >>> logic_and(DM.eye(2), DM.ones(2, 2))
 DM(
 [[1, 00],
  [00, 1]])

 >> DM([1; 2; 0]) & DM([3; 0; 0])
 [1, 0, 0]
 >> SX.sym('x') & SX.sym('y')
 (x&&y)
 >> and(DM.eye(2), DM.ones(2, 2))
 [[1, 00],
  [00, 1]]

 julia> ca.logic_and(ca.DM([1, 2, 0]), ca.DM([3, 0, 0]))
 DM([1, 0, 0])
 julia> ca.logic_and(ca.SX.sym("x"), ca.SX.sym("y"))
 SX((x&&y))
 julia> ca.logic_and(ca.DM.eye(2), ca.DM.ones(2, 2))
 DM(
 [[1, 00],
  [00, 1]])

See also
logic_or, logic_not

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

Definition at line 759 of file generic_expression.hpp.

759  {
760  return logic_and(x, y);
761  }
static ExType logic_and(const ExType &x, const ExType &y)
Logical and

◆ operator*

template<typename ExType >
ExType operator* ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise (Hadamard) product, not the matrix product; use mtimes for that. Shape rules as for plus. The result is structurally zero wherever either operand is, so the sparsity pattern is the intersection of the patterns.

>>> times(DM([1, 2]), DM([3, 4]))
DM([3, 8])
>>> DM([[1, 2], [3, 4]]) * DM([[1, 2], [3, 4]])
DM(
[[1, 4],
 [9, 16]])
>>> DM.eye(2) * DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

>> times(DM([1; 2]), DM([3; 4]))
[3, 8]
>> DM([1 2; 3 4]) .* DM([1 2; 3 4])
[[1, 4],
 [9, 16]]
>> DM([1 2; 3 4]) * DM([1 2; 3 4])
[[7, 10],
 [15, 22]]
>> DM.eye(2) .* DM.ones(2, 2)
[[1, 00],
 [00, 1]]

julia> ca.times(ca.DM([1, 2]), ca.DM([3, 4]))
DM([3, 8])
julia> ca.DM([1 2; 3 4]) .* ca.DM([1 2; 3 4])
DM(
[[1, 4],
 [9, 16]])
julia> ca.DM.eye(2) .* ca.DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

See also
mtimes

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

Definition at line 295 of file generic_expression.hpp.

295  {
296  return times(x, y);
297  }
static ExType times(const ExType &x, const ExType &y)
Elementwise multiplication: (x,y) -> x .* y.

◆ operator+

template<typename ExType >
ExType operator+ ( const ExType &  x,
const ExType &  y 
)
friend

Definition at line 167 of file generic_expression.hpp.

167  {
168  return plus(x, y);
169  }
static ExType plus(const ExType &x, const ExType &y)
Addition: (x,y) -> x + y.

◆ operator-

template<typename ExType >
ExType operator- ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise difference; shape and sparsity rules as for plus. For symbolic arguments, x - x with the same node simplifies to 0.

>>> minus(DM([5, 6]), DM([1, 2]))
DM([4, 4])
>>> DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
>>> x = SX.sym("x")
>>> x - x
SX(0)

>> minus(DM([5; 6]), DM([1; 2]))
[4, 4]
>> DM.eye(2) - 1
[[0, -1],
 [-1, 0]]
>> x = SX.sym('x');
>> x - x
0

julia> ca.minus(ca.DM([5, 6]), ca.DM([1, 2]))
DM([4, 4])
julia> ca.DM.eye(2) - 1
DM(
[[0, -1],
 [-1, 0]])
julia> x = ca.SX.sym("x");
julia> x - x
SX(0)

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

Definition at line 227 of file generic_expression.hpp.

227  {
228  return minus(x, y);
229  }
static ExType minus(const ExType &x, const ExType &y)
Subtraction: (x,y) -> x - y.

◆ operator/

template<typename ExType >
ExType operator/ ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise quotient, not a linear solve; use solve or mldivide for that. Shape rules as for plus. Division by zero follows IEEE arithmetic (inf or nan), no error is raised. Structural zeros of x stay structural zeros; dividing by a sparse y densifies.

>>> rdivide(DM([1, 2]), 4)
DM([0.25, 0.5])
>>> DM([1, 2]) / DM([4, 8])
DM([0.25, 0.25])
>>> 1 / DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
>>> DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

>> rdivide(DM([1; 2]), 4)
[0.25, 0.5]
>> DM([1; 2]) ./ DM([4; 8])
[0.25, 0.25]
>> 1 ./ DM.eye(2)
[[1, inf],
 [inf, 1]]
>> DM.eye(2) ./ 2
[[0.5, 00],
 [00, 0.5]]

julia> ca.rdivide(ca.DM([1, 2]), 4)
DM([0.25, 0.5])
julia> ca.DM([1, 2]) ./ ca.DM([4, 8])
DM([0.25, 0.25])
julia> 1 ./ ca.DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
julia> ca.DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

See also
mldivide, mrdivide, solve

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

Definition at line 366 of file generic_expression.hpp.

366  {
367  return rdivide(x, y);
368  }
static ExType rdivide(const ExType &x, const ExType &y)
Elementwise division: (x,y) -> x ./ y.

◆ operator<

template<typename ExType >
ExType operator< ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison; shape rules as for plus. Every comparison in this group returns a matrix of the same type as the operands with entries 1 (true) and 0 (false): for DM a numeric 0/1 matrix, not a boolean, and for SX and MX a symbolic expression that is only decided at evaluation time. Comparisons with NaN are false. The result is sparse where both operands are structurally zero (0 < 0 is false).

>>> lt(DM([1, 2, 3]), 2)
DM([1, 0, 0])
>>> DM([1, 2, 3]) < DM([3, 2, 1])
DM([1, 0, 0])
>>> SX.sym("x", 2) < SX.sym("y", 2)
SX([(x_0<y_0), (x_1<y_1)])
>>> DM.eye(2) < 1
DM(
[[0, 1],
 [1, 0]])

>> lt(DM([1; 2; 3]), 2)
[1, 0, 0]
>> DM([1; 2; 3]) < DM([3; 2; 1])
[1, 0, 0]
>> SX.sym('x', 2) < SX.sym('y', 2)
[(x_0<y_0), (x_1<y_1)]
>> DM.eye(2) < 1
[[0, 1],
 [1, 0]]

julia> ca.lt(ca.DM([1, 2, 3]), 2)
DM([1, 0, 0])
julia> ca.DM([1, 2, 3]) < ca.DM([3, 2, 1])
DM([1, 0, 0])
julia> ca.SX.sym("x", 2) < ca.SX.sym("y", 2)
SX([(x_0<y_0), (x_1<y_1)])
julia> ca.DM.eye(2) < 1
DM(
[[0, 1],
 [1, 0]])

See also
le, gt, ge, eq, ne, if_else

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

Definition at line 434 of file generic_expression.hpp.

434  {
435  return lt(x, y);
436  }
static ExType lt(const ExType &x, const ExType &y)
Logical less than: (x,y) -> x < y.

◆ operator<=

template<typename ExType >
ExType operator<= ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt. Chained inequalities such as lb <= (x <= ub) are recognised by Opti as bound constraints.

>>> le(DM([1, 2, 3]), 2)
DM([1, 1, 0])
>>> x = SX.sym("x")
>>> x <= x
SX(1)

>> le(DM([1; 2; 3]), 2)
[1, 1, 0]
>> x = SX.sym('x');
>> x <= x
1

julia> ca.le(ca.DM([1, 2, 3]), 2)
DM([1, 1, 0])
julia> x = ca.SX.sym("x");
julia> x <= x
SX(1)

See also
lt

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

Definition at line 484 of file generic_expression.hpp.

484  {
485  return le(x, y);
486  }
static ExType le(const ExType &x, const ExType &y)
Logical less or equal to: (x,y) -> x <= y.

◆ operator==

template<typename ExType >
ExType operator== ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt: this is value equality of the entries, not identity of expressions; use is_equal to test whether two symbolic expressions are the same. For symbolic operands x == x with the same node simplifies to 1. NaN is not equal to anything, including itself.

>>> eq(DM([1, 2, 3]), 2)
DM([0, 1, 0])
>>> DM.eye(2) == DM([[1, 0], [0, 1]])
DM(
[[1, 1],
 [1, 1]])
>>> x = SX.sym("x")
>>> x == x, x == SX.sym("x")
(SX(1), SX((x==x)))
>>> DM(float("nan")) == DM(float("nan"))
DM(0)

>> eq(DM([1; 2; 3]), 2)
[0, 1, 0]
>> DM.eye(2) == DM([1 0; 0 1])
[[1, 1],
 [1, 1]]
>> x = SX.sym('x');
>> x == x, x == SX.sym('x')
1
(x==x)
>> DM(nan) == DM(nan)
0

julia> ca.eq(ca.DM([1, 2, 3]), 2)
DM([0, 1, 0])
julia> ca.DM.eye(2) == ca.DM([1 0; 0 1])
DM(
[[1, 1],
 [1, 1]])
julia> x = ca.SX.sym("x");
julia> x == x, x == ca.SX.sym("x")
(1, (x==x))
julia> ca.DM(NaN) == ca.DM(NaN)
DM(0)

See also
ne, is_equal

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

Definition at line 647 of file generic_expression.hpp.

647  {
648  return eq(x, y);
649  }
static ExType eq(const ExType &x, const ExType &y)
Logical equal to: (x,y) -> x == y.

◆ operator>

template<typename ExType >
ExType operator> ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt. Implemented as y < x, which is how a symbolic expression prints.

>>> gt(DM([1, 2, 3]), 2)
DM([0, 0, 1])
>>> gt(SX.sym("x"), SX.sym("y"))
SX((y<x))

>> gt(DM([1; 2; 3]), 2)
[0, 0, 1]
>> SX.sym('x') > SX.sym('y')
(y<x)

julia> ca.gt(ca.DM([1, 2, 3]), 2)
DM([0, 0, 1])
julia> ca.gt(ca.SX.sym("x"), ca.SX.sym("y"))
SX((y<x))

See also
lt

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

Definition at line 531 of file generic_expression.hpp.

531  {
532  return gt(x, y);
533  }
static ExType gt(const ExType &x, const ExType &y)
Logical greater than: (x,y) -> x > y.

◆ operator>=

template<typename ExType >
ExType operator>= ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise comparison with 0/1 result, see lt. Implemented as y <= x.

>>> ge(DM([1, 2, 3]), 2)
DM([0, 1, 1])
>>> ge(MX.sym("x"), MX.sym("y"))
MX((y<=x))

>> ge(DM([1; 2; 3]), 2)
[0, 1, 1]
>> MX.sym('x') >= MX.sym('y')
(y<=x)

julia> ca.ge(ca.DM([1, 2, 3]), 2)
DM([0, 1, 1])
julia> ca.ge(ca.MX.sym("x"), ca.MX.sym("y"))
MX((y<=x))

See also
lt

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

Definition at line 577 of file generic_expression.hpp.

577  {
578  return ge(x, y);
579  }
static ExType ge(const ExType &x, const ExType &y)
Logical greater or equal to: (x,y) -> x >= y.

◆ operator||

template<typename ExType >
ExType operator|| ( const ExType &  x,
const ExType &  y 
)
friend

returns (an expression evaluating to) 1 where at least one entry is nonzero and 0 otherwise, elementwise

Shape rules as for plus; the result is structurally zero where both operands are.

 >>> logic_or(DM([1, 0, 0]), DM([0, 0, 2]))
 DM([1, 0, 1])
 >>> logic_or(MX.sym("x"), MX.sym("y"))
 MX((x||y))

 >> DM([1; 0; 0]) | DM([0; 0; 2])
 [1, 0, 1]
 >> or(MX.sym('x'), MX.sym('y'))
 (x||y)

 julia> ca.logic_or(ca.DM([1, 0, 0]), ca.DM([0, 0, 2]))
 DM([1, 0, 1])
 julia> ca.logic_or(ca.MX.sym("x"), ca.MX.sym("y"))
 MX((x||y))

See also
logic_and, logic_not

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

Definition at line 808 of file generic_expression.hpp.

808  {
809  return logic_or(x, y);
810  }
static ExType logic_or(const ExType &x, const ExType &y)
Logical or

◆ pinv [1/2]

template<typename MatType >
MatType pinv ( const MatType &  A)
friend

If the matrix A is fat (size1<size2), mul(A, pinv(A)) is unity.

pinv(A)' = (AA')^(-1) A

If the matrix A is slender (size1>size2), mul(pinv(A), A) is unity.

pinv(A) = (A'A)^(-1) A'

The normal equations are solved with solve, so A is assumed to have full rank. For a square matrix this is the inverse.

>>> A = DM([[1, 0, 0], [0, 2, 0]])
>>> pinv(A)
DM(
[[1, 0],
 [0, 0.5],
 [0, 0]])
>>> mtimes(A, pinv(A))
DM(
[[1, 0],
 [0, 1]])
>>> pinv(SX.sym("A", 1, 2))
SX(@1=(sq(A_0)+sq(A_1)), [(A_0/@1), (A_1/@1)])

>> A = DM([1 0 0; 0 2 0]);
>> pinv(A)
[[1, 0],
 [0, 0.5],
 [0, 0]]
>> A*pinv(A)
[[1, 0],
 [0, 1]]
>> pinv(SX.sym('A', 1, 2))
@1=(sq(A_0)+sq(A_1)), [(A_0/@1), (A_1/@1)]

julia> A = ca.DM([1 0 0; 0 2 0]);
julia> ca.pinv(A)
DM(
[[1, 0],
 [0, 0.5],
 [0, 0]])
julia> A * ca.pinv(A)
DM(
[[1, 0],
 [0, 1]])
julia> ca.pinv(ca.SX.sym("A", 1, 2))
SX(@1=(sq(A_0)+sq(A_1)), [(A_0/@1), (A_1/@1)])

See also
inv, solve, nullspace

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

Definition at line 3364 of file generic_matrix.hpp.

3364  {
3365  return MatType::pinv(A);
3366  }

◆ pinv [2/2]

template<typename MatType >
MatType pinv ( const MatType &  A,
const std::string &  lsolver,
const Dict &  dict = Dict() 
)
friend

Same as pinv(A) but the normal equations are solved with the given Linsol plugin. Available for DM and MX; SX raises an error.

Parameters
lsolverName of a Linsol plugin, e.g. "qr"
dictOptions passed to the linear solver

>>> pinv(DM([[1, 0, 0], [0, 2, 0]]), "qr")
DM(
[[1, 0],
 [0, 0.5],
 [0, 0]])

>> pinv(DM([1 0 0; 0 2 0]), 'qr')
[[1, 0],
 [0, 0.5],
 [0, 0]]

julia> print(ca.pinv(ca.DM([1 0 0; 0 2 0]), "qr"))
[[1, 0],
 [0, 0.5],
 [0, 0]]

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

Definition at line 3405 of file generic_matrix.hpp.

3406  {
3407  return MatType::pinv(A, lsolver, dict);
3408  }

◆ plus

template<typename ExType >
ExType plus ( const ExType &  x,
const ExType &  y 
)
friend

Definition at line 164 of file generic_expression.hpp.

164  {
165  return ExType::plus(x, y);
166  }

◆ poly_coeff

template<typename Scalar >
Matrix<Scalar> poly_coeff ( const Matrix< Scalar > &  f,
const Matrix< Scalar > &  x 
)
friend

Returns the coefficients in decreasing power order (highest degree first), as a column vector; the constant term is last. Coefficients may be symbolic in other variables. Found by repeated differentiation, so an expression that is not polynomial in x raises an error. Only defined for SX.

Parameters
exScalar SX expression that is a polynomial in x
xScalar SX symbol

>>> x = SX.sym("x"); a = SX.sym("a")
>>> poly_coeff(3*x**2 + 2*x + 1, x)
SX([3, 2, 1])
>>> poly_coeff((x - a)*(x + a), x)
SX([1, 0, (-sq(a))])
>>> try: poly_coeff(sin(x), x)
... except Exception as e: print("error")
error

>> x = SX.sym('x'); a = SX.sym('a');
>> poly_coeff(3*x^2 + 2*x + 1, x)
[3, 2, 1]
>> poly_coeff((x - a)*(x + a), x)
[1, 0, (-sq(a))]
>> try, poly_coeff(sin(x), x); catch e, disp('error'); end
error

julia> x = ca.SX.sym("x"); a = ca.SX.sym("a");
julia> ca.poly_coeff(3*x^2 + 2*x + 1, x)
SX([3, 2, 1])
julia> ca.poly_coeff((x - a)*(x + a), x)
SX([1, 0, (-sq(a))])
julia> try ca.poly_coeff(sin(x), x); catch; println("error") end
error

See also
poly_roots, polyval

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

Definition at line 2605 of file matrix_decl.hpp.

2606  {
2607  return Matrix<Scalar>::poly_coeff(f, x);
2608  }
static Matrix< Scalar > poly_coeff(const Matrix< Scalar > &ex, const Matrix< Scalar > &x)

◆ poly_roots

template<typename Scalar >
Matrix<Scalar> poly_roots ( const Matrix< Scalar > &  p)
friend

p is a dense column vector of coefficients in decreasing power order, as returned by poly_coeff. Closed-form formulas are used for degrees 1 to 4; a zero constant term is split off as a root 0 first. Higher degrees raise an error. All roots are assumed real: complex roots give nan. The result stays symbolic for symbolic coefficients. Only defined for SX; pass SX(p) for numeric coefficients.

>>> poly_roots(SX([1, -3, 2]))
SX([1, 2])
>>> evalf(poly_roots(SX([1, -6, 11, -6])))
DM([3, 1, 2])
>>> a = SX.sym("a")
>>> poly_roots(vertcat(a, 2))
SX((-2/a))
>>> try: poly_roots(SX([1, 1, 1, 1, 1, 1]))
... except Exception as e: print("error")
error

>> poly_roots(SX([1; -3; 2]))
[1, 2]
>> evalf(poly_roots(SX([1; -6; 11; -6])))
[3, 1, 2]
>> a = SX.sym('a');
>> poly_roots([a; 2])
(-2/a)
>> try, poly_roots(SX([1; 1; 1; 1; 1; 1])); catch e, disp('error'); end
error

julia> ca.poly_roots(ca.SX([1, -3, 2]))
SX([1, 2])
julia> ca.evalf(ca.poly_roots(ca.SX([1, -6, 11, -6])))
DM([3, 1, 2])
julia> a = ca.SX.sym("a");
julia> ca.poly_roots([a; 2])
SX((-2/a))
julia> try ca.poly_roots(ca.SX([1, 1, 1, 1, 1, 1])); catch; println("error") end
error

See also
poly_coeff, eig_symbolic

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

Definition at line 2664 of file matrix_decl.hpp.

2664  {
2665  return Matrix<Scalar>::poly_roots(p);
2666  }
static Matrix< Scalar > poly_roots(const Matrix< Scalar > &p)

◆ polyval

template<typename MatType >
MatType polyval ( const MatType &  p,
const MatType &  x 
)
friend

Horner evaluation of p[0]*x^(n-1) + ... + p[n-2]*x + p[n-1]: the first coefficient belongs to the highest power, as in MATLAB's polyval. The polynomial is applied to every structurally nonzero entry of x; the result keeps the shape and sparsity of x (structural zeros stay zero even when p[n-1] != 0).

Parameters
pDense column vector of coefficients, at least one entry
xPoint(s) of evaluation, any shape

>>> polyval(DM([1, 2, 3]), 2)
DM(11)
>>> polyval(DM([1, 0, -1]), DM([0, 1, 2]))
DM([-1, 0, 3])
>>> polyval(SX.sym("p", 3), SX.sym("x"))
SX(((((p_0*x)+p_1)*x)+p_2))

>> polyval(DM([1; 2; 3]), 2)
11
>> polyval(DM([1; 0; -1]), DM([0; 1; 2]))
[-1, 0, 3]
>> polyval(SX.sym('p', 3), SX.sym('x'))
((((p_0*x)+p_1)*x)+p_2)

julia> ca.polyval(ca.DM([1, 2, 3]), 2)
DM(11)
julia> ca.polyval(ca.DM([1, 0, -1]), ca.DM([0, 1, 2]))
DM([-1, 0, 3])
julia> ca.polyval(ca.SX.sym("p", 3), ca.SX.sym("x"))
SX(((((p_0*x)+p_1)*x)+p_2))

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

Definition at line 2335 of file generic_matrix.hpp.

2335  {
2336  return MatType::polyval(p, x);
2337  }

◆ pow

template<typename ExType >
ExType pow ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise; shape rules as for plus. Named power in Python, where the ** operator maps to it as well. A constant exponent is simplified symbolically: integer powers become repeated squaring and multiplication, 0.5 becomes sqrt, -1 a reciprocal, other constants a constpow operation. Structural zeros of x stay zero for a nonzero exponent; x**0 is dense (0**0 == 1).

>>> power(DM([1, 2, 3]), 2)
DM([1, 4, 9])
>>> DM([1, 2, 3])**DM([3, 2, 1]), 2**DM([1, 2, 3])
(DM([1, 4, 3]), DM([2, 4, 8]))
>>> x = SX.sym("x")
>>> x**2, x**3, x**-1, x**x
(SX(sq(x)), SX((x*sq(x))), SX((1./x)), SX(pow(x,x)))
>>> x**2.5
SX(pow(x,2.5))

>> power(DM([1; 2; 3]), 2)
[1, 4, 9]
>> DM([1; 2; 3]).^DM([3; 2; 1]), 2.^DM([1; 2; 3])
[1, 4, 3]
[2, 4, 8]
>> x = SX.sym('x');
>> x.^2, x.^3, x.^-1, x.^x
sq(x)
(x*sq(x))
(1./x)
pow(x,x)
>> x.^2.5
pow(x,2.5)

julia> ca.power(ca.DM([1, 2, 3]), 2)
DM([1, 4, 9])
julia> ca.DM([1, 2, 3]) .^ ca.DM([3, 2, 1]), 2 .^ ca.DM([1, 2, 3])
([1, 4, 3], [2, 4, 8])
julia> x = ca.SX.sym("x");
julia> x^2, x^3, x^-1, x^x
(sq(x), (x*sq(x)), (1./x), pow(x,x))
julia> x^2.5
SX(pow(x,2.5))

See also
sq, sqrt, constpow, mpower

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

Definition at line 1981 of file generic_expression.hpp.

1981  {
1982  return ExType::pow(x, y);
1983  }

◆ print_operator

template<typename MatType >
std::string print_operator ( const MatType &  xb,
const std::vector< std::string > &  args 
)
friend

Prints the operation at the root of a scalar expression with the given strings in place of its operands. xb must be a unary or binary operation (a symbol or constant raises an error for SX) and args must have one entry per operand.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> print_operator(x + y, ["a", "b"]), print_operator(sin(x), ["a"])
('(a+b)', 'sin(a)')
>>> X = MX.sym("X"); Y = MX.sym("Y")
>>> print_operator(vertcat(X, Y), ["a", "b"])
'vertcat(a, b)'

>> x = SX.sym('x'); y = SX.sym('y');
>> print_operator(x + y, {'a', 'b'}), print_operator(sin(x), {'a'})
(a+b)
sin(a)
>> X = MX.sym('X'); Y = MX.sym('Y');
>> print_operator(vertcat(X, Y), {'a', 'b'})
vertcat(a, b)

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.print_operator(x + y, ["a", "b"]), ca.print_operator(sin(x), ["a"])
("(a+b)", "sin(a)")
julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
julia> ca.print_operator([X; Y], ["a", "b"])
"vertcat(a, b)"

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

Definition at line 4580 of file generic_matrix.hpp.

4580  {
4581  return MatType::print_operator(xb, args);
4582  }

◆ printme

template<typename ExType >
ExType printme ( const ExType &  x,
const ExType &  y 
)
friend

Identity operation with a side effect: whenever the expression is evaluated inside a Function (and hence inside a solver), every nonzero of x is printed on its own line as "|> y : value" in storage (column-major) order, with y an identifier of choice to tell several printme nodes apart. Available as a method x.printme(y) in Python; the derivative is that of x. Use monitor to print a whole matrix at once.

>>> x = MX.sym("x")
>>> f = Function("f", [x], [x.printme(7) * 2])
>>> f(3)
|> 7 : 3.0000000000000000e+00
DM(6)

>> x = MX.sym('x');
>> f = Function('f', {x}, {x.printme(7) * 2});
>> f(3)
|> 7 : 3.0000000000000000e+00
6

julia> x = ca.MX.sym("x");
julia> f = ca.Function("f", [x], [ca.printme(x, 7) * 2]);
julia> f(3)
|> 7 : 3.0000000000000000e+00
DM(6)

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

Definition at line 2518 of file generic_expression.hpp.

2518  {
2519  return ExType::binary(OP_PRINTME, x, y);
2520  }

◆ project

template<typename MatType >
MatType project ( const MatType &  A,
const Sparsity &  sp,
bool  intersect = false 
)
friend

nonzeros taken from an existing matrix

Entries of A outside sp are dropped; entries of sp not present in A become numerical zeros. The shape of sp must equal that of A. With intersect true, the target pattern is the intersection of sp and the pattern of A, so the result only ever loses entries.

>>> project(DM([[1, 2], [3, 4]]), Sparsity.diag(2))
DM(
[[1, 00],
 [00, 4]])
>>> project(DM.eye(2), Sparsity.dense(2, 2))
DM(
[[1, 0],
 [0, 1]])
>>> project(DM(Sparsity.diag(2), [1, 2]), Sparsity.upper(2), True)
DM(
[[1, 00],
 [00, 2]])

>> project(DM([1 2; 3 4]), Sparsity.diag(2))
[[1, 00],
 [00, 4]]
>> project(DM.eye(2), Sparsity.dense(2, 2))
[[1, 0],
 [0, 1]]
>> project(DM(Sparsity.diag(2), [1; 2]), Sparsity.upper(2), true)
[[1, 00],
 [00, 2]]

julia> ca.project(ca.DM([1 2; 3 4]), ca.Sparsity.diag(2))
DM(
[[1, 00],
 [00, 4]])
julia> ca.project(ca.DM.eye(2), ca.Sparsity.dense(2, 2))
DM(
[[1, 0],
 [0, 1]])
julia> ca.project(ca.DM(ca.Sparsity.diag(2), [1, 2]), ca.Sparsity.upper(2), true)
DM(
[[1, 00],
 [00, 2]])

See also
densify, sparsify, unite

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

Definition at line 2574 of file generic_matrix.hpp.

2575  {
2576  return MatType::project(A, sp, intersect);
2577  }

Referenced by casadi::GenericMatrix< MatType >::tril(), and casadi::GenericMatrix< MatType >::triu().

◆ pw_const

template<typename Scalar >
Matrix<Scalar> pw_const ( const Matrix< Scalar > &  t,
const Matrix< Scalar > &  tval,
const Matrix< Scalar > &  val 
)
friend

Builds an SX expression that equals val[0] for t < tval[0], val[k] for tval[k-1] <= t < tval[k] and val[n-1] for t >= tval[n-2], with n = val.numel(). tval must be increasing. The result is a sum of step functions, so it is not smooth; its derivative with respect to t is zero almost everywhere. Only defined for SX.

Parameters
tScalar SX variable (e.g. time)
tvalBreakpoints, length n-1; a length mismatch raises an error
valFunction value on each interval, length n

>>> t = SX.sym("t")
>>> e = pw_const(t, [0, 2], [7, 1, 3])
>>> f = Function("f", [t], [e])
>>> [float(f(v)) for v in [-1, 0, 1, 2, 5]]
[7.0, 1.0, 1.0, 3.0, 3.0]
>>> try: pw_const(t, [0], [7, 1, 3])
... except Exception as e: print("error")
error

>> t = SX.sym('t');
>> e = pw_const(t, [0 2], [7 1 3]);
>> f = Function('f', {t}, {e});
>> f([-1; 0; 1; 2; 5])
[7, 1, 1, 3, 3]
>> try, pw_const(t, 0, [7 1 3]); catch e, disp('error'); end
error

julia> t = ca.SX.sym("t");
julia> e = ca.pw_const(t, [0, 2], [7, 1, 3]);
julia> f = ca.Function("f", [t], [e]);
julia> [Float64(f(v)) for v in [-1, 0, 1, 2, 5]]
5-element Vector{Float64}:
 7.0
 1.0
 1.0
 3.0
 3.0
julia> try ca.pw_const(t, [0], [7, 1, 3]); catch; println("error") end
error

See also
pw_lin, if_else, interpolant

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

Definition at line 2133 of file matrix_decl.hpp.

2135  {
2136  return Matrix<Scalar>::pw_const(t, tval, val);
2137  }
static Matrix< Scalar > pw_const(const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)

◆ pw_lin

template<typename Scalar >
Matrix<Scalar> pw_lin ( const Matrix< Scalar > &  t,
const Matrix< Scalar > &  tval,
const Matrix< Scalar > &  val 
)
friend

Builds an SX expression that interpolates linearly between the points (tval[k], val[k]). Outside [tval[0], tval[N-1]] the first and last segments are extrapolated. Only defined for SX.

Parameters
tScalar SX variable (e.g. time)
tvalBreakpoints, increasing, at least two
valFunction values at the breakpoints, same length as tval

>>> t = SX.sym("t")
>>> f = Function("f", [t], [pw_lin(t, [0, 1, 3], [0, 2, 1])])
>>> [float(f(v)) for v in [-1, 0, 0.5, 1, 2, 3, 4]]
[-2.0, 0.0, 1.0, 2.0, 1.5, 1.0, 0.5]
>>> try: pw_lin(t, [0], [7])
... except Exception as e: print("error")
error

>> t = SX.sym('t');
>> f = Function('f', {t}, {pw_lin(t, [0 1 3], [0 2 1])});
>> f([-1; 0; 0.5; 1; 2; 3; 4])
[-2, 0, 1, 2, 1.5, 1, 0.5]
>> try, pw_lin(t, 0, 7); catch e, disp('error'); end
error

julia> t = ca.SX.sym("t");
julia> f = ca.Function("f", [t], [ca.pw_lin(t, [0, 1, 3], [0, 2, 1])]);
julia> [Float64(f(v)) for v in [-1, 0, 0.5, 1, 2, 3, 4]]
7-element Vector{Float64}:
 -2.0
  0.0
  1.0
  2.0
  1.5
  1.0
  0.5
julia> try ca.pw_lin(t, [0], [7]); catch; println("error") end
error

See also
pw_const, interpolant

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

Definition at line 2194 of file matrix_decl.hpp.

2195  {
2196  return Matrix<Scalar>::pw_lin(t, tval, val);
2197  }
static Matrix< Scalar > pw_lin(const Matrix< Scalar > &t, const Matrix< Scalar > &tval, const Matrix< Scalar > &val)

◆ qr

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

Factorizes A = Q*R with Q of the same shape as A (orthonormal columns) and R square upper triangular. More stable than classical Gram-Schmidt, but breaks down (nan entries, no error) when the columns of A are (nearly) linearly dependent. A must have at least as many rows as columns. Works symbolically on SX. See J. Demmel: Applied Numerical Linear Algebra (algorithm 3.1). In Python and MATLAB, Q and R are returned as a tuple.

>>> Q, R = qr(DM([[3, 1], [4, 2]]))
>>> Q
DM(
[[0.6, -0.8],
 [0.8, 0.6]])
>>> R
DM(
[[5, 2.2],
 [00, 0.4]])
>>> mtimes(Q, R)
DM(
[[3, 1],
 [4, 2]])
>>> try: qr(DM([[1, 2, 3]]))
... except Exception as e: print("error")
error

>> [Q, R] = qr(DM([3 1; 4 2]));
>> Q
[[0.6, -0.8],
 [0.8, 0.6]]
>> R
[[5, 2.2],
 [00, 0.4]]
>> Q*R
[[3, 1],
 [4, 2]]
>> try, [Q, R] = qr(DM([1 2 3])); catch e, disp('error'); end
error

julia> Q, R = ca.qr(ca.DM([3 1; 4 2]));
julia> Q
DM(
[[0.6, -0.8],
 [0.8, 0.6]])
julia> R
DM(
[[5, 2.2],
 [00, 0.4]])
julia> Q * R
DM(
[[3, 1],
 [4, 2]])
julia> try ca.qr(ca.DM([1 2 3])); catch; println("error") end
error

See also
qr_sparse, chol, ldl

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

Definition at line 1582 of file matrix_decl.hpp.

1582  {
1583  return Matrix<Scalar>::qr(A, Q, R);
1584  }
static void qr(const Matrix< Scalar > &A, Matrix< Scalar > &Q, Matrix< Scalar > &R)

◆ qr_solve

template<typename Scalar >
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 
)
friend
Parameters
bRight-hand side, one column per system; a sparse b is densified
v,r,beta,prinv,pcOutput of qr_sparse(A)
trSolve with the transpose of A instead
Returns
Dense solution with the shape of b

>>> A = DM([[3, 1], [4, 2]])
>>> V, R, beta, prinv, pc = qr_sparse(A)
>>> qr_solve(DM([1, 2]), V, R, beta, prinv, pc, True)
DM([-3, 2.5])
>>> solve(A.T, DM([1, 2]))
DM([-3, 2.5])

>> A = DM([3 1; 4 2]);
>> [V, R, beta, prinv, pc] = qr_sparse(A);
>> qr_solve(DM([1; 2]), V, R, beta, prinv, pc, true)
[-3, 2.5]
>> solve(A', DM([1; 2]))
[-3, 2.5]

julia> A = ca.DM([3 1; 4 2]);
julia> V, R, beta, prinv, pc = ca.qr_sparse(A);
julia> ca.qr_solve(ca.DM([1, 2]), V, R, beta, prinv, pc, true)
DM([-3, 2.5])
julia> A' \ [1, 2]
DM([-3, 2.5])

See also
qr_sparse, solve

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

Definition at line 1688 of file matrix_decl.hpp.

1691  {
1692  return Matrix<Scalar>::qr_solve(b, v, r, beta, prinv, pc, tr);
1693  }
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)

◆ qr_sparse

template<typename Scalar >
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 
)
friend

Computes a compact representation of Q (Householder vectors V and scaling factors beta), the upper triangular factor R and the row and column permutations prinv and pc that were applied, so that the factors can be reused by qr_solve. See T. Davis: Direct Methods for Sparse Linear Systems. Works symbolically on SX.

Parameters
AMatrix to factorize
amdUse an approximate minimum degree column ordering to reduce fill-in
Returns
V, R, beta, prinv, pc (a tuple in Python)

>>> A = DM([[3, 1], [4, 2]])
>>> V, R, beta, prinv, pc = qr_sparse(A)
>>> R
DM(
[[5, 2.2],
 [00, 0.4]])
>>> qr_solve(DM([1, 2]), V, R, beta, prinv, pc)
DM([0, 1])

>> A = DM([3 1; 4 2]);
>> [V, R, beta, prinv, pc] = qr_sparse(A);
>> R
[[5, 2.2],
 [00, 0.4]]
>> qr_solve(DM([1; 2]), V, R, beta, prinv, pc)
[0, 1]

julia> A = ca.DM([3 1; 4 2]);
julia> V, R, beta, prinv, pc = ca.qr_sparse(A);
julia> R
DM(
[[5, 2.2],
 [00, 0.4]])
julia> ca.qr_solve(ca.DM([1, 2]), V, R, beta, prinv, pc)
DM([0, 1])

See also
qr_solve, Sparsity::qr_sparse, qr

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

Definition at line 1638 of file matrix_decl.hpp.

1640  {
1641  return Matrix<Scalar>::qr_sparse(A, V, R, beta, prinv, pc, amd);
1642  }
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)

◆ quadratic_coeff

template<typename MatType >
void quadratic_coeff ( const MatType &  expr,
const MatType &  var,
MatType &  A,
MatType &  b,
MatType &  c,
bool  check = true 
)
friend

1/2*x' A x + b' x + c

e = 0.5*bilin(A,x,x)+dot(b,x)+c

Returns A = hessian(expr, var), b = jacobian(expr, var) at var = 0 as a column, and c = expr at var = 0. A non-scalar expr raises an error.

Parameters
check[in]When true (default), A is checked to be independent of x and an error is raised otherwise ("called on non-quadratic expression"). Provided to deal with false positive dependency checks.

* >>> x = SX.sym("x"); y = SX.sym("y")
* >>> A, b, c = quadratic_coeff(x**2 + 2*x*y + 3*x + 4, vertcat(x, y))
* >>> A
* SX(@1=2,
* [[@1, @1],
*  [@1, 00]])
* >>> b, c
* (SX([3, 0]), SX(4))
* >>> try: quadratic_coeff(x**3, x)
* ... except Exception as e: print("error")
* error
* 

* >> x = SX.sym('x'); y = SX.sym('y');
* >> [A, b, c] = quadratic_coeff(x^2 + 2*x*y + 3*x + 4, vertcat(x, y));
* >> A
* @1=2,
* [[@1, @1],
*  [@1, 00]]
* >> b, c
* [3, 0]
* 4
* >> try, [A, b, c] = quadratic_coeff(x^3, x); catch e, disp('error'); end
* error
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
* julia> A, b, c = ca.quadratic_coeff(x^2 + 2 * x * y + 3 * x + 4, [x; y]);
* julia> A
* SX(@1=2,
* [[@1, @1],
*  [@1, 00]])
* julia> b, c
* ([3, 0], 4)
* julia> try ca.quadratic_coeff(x^3, x) catch e; println("error") end
* error
* 

See also
linear_coeff, is_quadratic, hessian

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

Definition at line 4122 of file generic_matrix.hpp.

4123  {
4124  MatType::quadratic_coeff(expr, var, A, b, c, check);
4125  }

◆ ramp

template<typename Scalar >
Matrix<Scalar> ramp ( const Matrix< Scalar > &  x)
friend

x for x > 0 and 0 for x <= 0, computed as x*heaviside(x). Also called the slope function or relu. Not defined for MX; use fmax(x, 0) for an MX-compatible alternative.

>>> ramp(DM([0, 0.5, 3]))
DM([0, 0.5, 3])
>>> ramp(SX.sym("x"))
SX((x*((1+sign(x))/2)))

>> ramp(DM([0; 0.5; 3]))
[0, 0.5, 3]
>> ramp(SX.sym('x'))
(x*((1+sign(x))/2))

julia> ca.ramp(ca.DM([0, 0.5, 3]))
DM([0, 0.5, 3])
julia> ca.ramp(ca.SX.sym("x"))
SX((x*((1+sign(x))/2)))

See also
heaviside, fmax

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

Definition at line 2338 of file matrix_decl.hpp.

2338  {
2339  return Matrix<Scalar>::ramp(x);
2340  }
static Matrix< Scalar > ramp(const Matrix< Scalar > &x)

◆ rank1

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::rank1 ( const MatType &  A,
const MatType &  alpha,
const MatType &  x,
const MatType &  y 
)
friend

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

Definition at line 1263 of file generic_matrix.hpp.

1264  {
1265  return MatType::rank1(A, alpha, x, y);
1266  }

◆ rdivide

template<typename ExType >
ExType rdivide ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise quotient, not a linear solve; use solve or mldivide for that. Shape rules as for plus. Division by zero follows IEEE arithmetic (inf or nan), no error is raised. Structural zeros of x stay structural zeros; dividing by a sparse y densifies.

>>> rdivide(DM([1, 2]), 4)
DM([0.25, 0.5])
>>> DM([1, 2]) / DM([4, 8])
DM([0.25, 0.25])
>>> 1 / DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
>>> DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

>> rdivide(DM([1; 2]), 4)
[0.25, 0.5]
>> DM([1; 2]) ./ DM([4; 8])
[0.25, 0.25]
>> 1 ./ DM.eye(2)
[[1, inf],
 [inf, 1]]
>> DM.eye(2) ./ 2
[[0.5, 00],
 [00, 0.5]]

julia> ca.rdivide(ca.DM([1, 2]), 4)
DM([0.25, 0.5])
julia> ca.DM([1, 2]) ./ ca.DM([4, 8])
DM([0.25, 0.25])
julia> 1 ./ ca.DM.eye(2)
DM(
[[1, inf],
 [inf, 1]])
julia> ca.DM.eye(2) / 2
DM(
[[0.5, 00],
 [00, 0.5]])

See also
mldivide, mrdivide, solve

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

Definition at line 363 of file generic_expression.hpp.

363  {
364  return ExType::rdivide(x, y);
365  }

◆ rectangle

template<typename Scalar >
Matrix<Scalar> rectangle ( const Matrix< Scalar > &  x)
friend

1 for |x| < 1/2, 1/2 for |x| == 1/2 and 0 for |x| > 1/2, built from sign. Not defined for MX.

>>> rectangle(DM([-1, -0.5, 0, 0.5, 1]))
DM([0, 0.5, 1, 0.5, 0])

>> rectangle(DM([-1; -0.5; 0; 0.5; 1]))
[0, 0.5, 1, 0.5, 0]

julia> ca.rectangle(ca.DM([-1, -0.5, 0, 0.5, 1]))
DM([0, 0.5, 1, 0.5, 0])

See also
triangle, heaviside

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

Definition at line 2267 of file matrix_decl.hpp.

2267  {
2268  return Matrix<Scalar>::rectangle(x);
2269  }
static Matrix< Scalar > rectangle(const Matrix< Scalar > &x)

◆ remainder

template<typename ExType >
ExType remainder ( const ExType &  x,
const ExType &  y 
)
friend

This Function follows the convention of https://en.cppreference.com/w/c/numeric/math/remainder: x - n*y with n the integer nearest to x/y (ties to even), so the result lies in [-|y|/2, |y|/2].

Notably:

  • remainder(5,3) -> -1
  • remainder(5,-3) -> -1
  • remainder(-5,3) -> 1
  • remainder(-5,-3) -> 1

This is equivalent to Python's math.remainder. There is no equivalence in Matlab. Elementwise, shape rules as for plus; structural zeros of x stay zero.

>>> remainder(DM([5, 5, -5, -5]), DM([3, -3, 3, -3]))
DM([-1, -1, 1, 1])
>>> remainder(DM([2.5, 3.5]), 2)
DM([0.5, -0.5])
>>> remainder(MX.sym("x"), 2)
MX(remainder(x,2))

>> remainder(DM([5; 5; -5; -5]), DM([3; -3; 3; -3]))
[-1, -1, 1, 1]
>> remainder(DM([2.5; 3.5]), 2)
[0.5, -0.5]
>> remainder(MX.sym('x'), 2)
remainder(x,2)

julia> ca.remainder(ca.DM([5, 5, -5, -5]), ca.DM([3, -3, 3, -3]))
DM([-1, -1, 1, 1])
julia> ca.remainder(ca.DM([2.5, 3.5]), 2)
DM([0.5, -0.5])
julia> ca.remainder(ca.MX.sym("x"), 2)
MX(remainder(x,2))

See also
fmod
Extra doc: https://github.com/casadi/casadi/wiki/L_24x 

Definition at line 2102 of file generic_expression.hpp.

2102  {
2103  return ExType::remainder(x, y);
2104  }

◆ repmat [1/2]

template<typename MatType >
MatType repmat ( const MatType &  A,
casadi_int  n,
casadi_int  m = 1 
)
friend

Tiles A into an n-by-m grid of copies; the sparsity pattern is tiled along. A zero count gives an empty matrix with the remaining dimension kept. For MX the horizontal repetition is a single node, the vertical one a vertcat of n references.

Parameters
AMatrix (or Sparsity) to tile; any shape
nNumber of vertical repetitions, non-negative
mNumber of horizontal repetitions, non-negative

>>> repmat(DM([[1, 2]]), 2, 2)
DM(
[[1, 2, 1, 2],
 [1, 2, 1, 2]])
>>> repmat(DM.eye(2), 1, 2)
DM(
[[1, 00, 1, 00],
 [00, 1, 00, 1]])
>>> repmat(MX.sym("x", 2), 1, 3)
MX(repmat(x, 3))
>>> repmat(MX.sym("x", 2), 3, 1)
MX(vertcat(x, x, x))
>>> repmat(DM.ones(2, 3), 0, 2).shape
(0, 6)

>> repmat(DM([1 2]), 2, 2)
[[1, 2, 1, 2],
 [1, 2, 1, 2]]
>> repmat(DM.eye(2), 1, 2)
[[1, 00, 1, 00],
 [00, 1, 00, 1]]
>> repmat(MX.sym('x', 2), 1, 3)
repmat(x, 3)
>> repmat(MX.sym('x', 2), 3, 1)
vertcat(x, x, x)
>> size(repmat(DM.ones(2, 3), 0, 2))
0 6

julia> repeat(ca.DM([1 2]), 2, 2)
DM(
[[1, 2, 1, 2],
 [1, 2, 1, 2]])
julia> repeat(ca.DM.eye(2), 1, 2)
DM(
[[1, 00, 1, 00],
 [00, 1, 00, 1]])
julia> repeat(ca.MX.sym("x", 2), 1, 3)
MX(repmat(x, 3))
julia> repeat(ca.MX.sym("x", 2), 3, 1)
MX(vertcat(x, x, x))
julia> size(repeat(ca.DM.ones(2, 3), 0, 2))
(0, 6)

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

Definition at line 2166 of file sparsity_interface.hpp.

2166  {
2167  return MatType::repmat(A, n, m);
2168  }

◆ repmat [2/2]

template<typename MatType >
MatType repmat ( const MatType &  A,
const std::pair< casadi_int, casadi_int > &  rc 
)
friend

Same as repmat(A, rc[0], rc[1]).

>>> repmat(DM(7), (1, 3))
DM([[7, 7, 7]])

>> repmat(DM(7), [1, 3])
[[7, 7, 7]]

julia> ca.repmat(ca.DM(7), 1, 3)
DM([[7, 7, 7]])

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

Definition at line 2196 of file sparsity_interface.hpp.

2196  {
2197  return MatType::repmat(A, rc);
2198  }

◆ repsum

template<typename MatType >
MatType repsum ( const MatType &  A,
casadi_int  n,
casadi_int  m = 1 
)
friend

Splits A into an n-by-m grid of equally sized blocks and returns their sum, a matrix of size A.size1()/n by A.size2()/m. The inverse of repmat(x, n, m) in the sense that repsum(repmat(x, n, m), n, m) == n*m*x. The dimensions of A must be multiples of n and m.

>>> repsum(DM([[1, 2, 3, 4], [5, 6, 7, 8]]), 1, 2)
DM(
[[4, 6],
 [12, 14]])
>>> repsum(SX.sym("x", 4), 2)
SX([(x_0+x_2), (x_1+x_3)])

>> repsum(DM([1 2 3 4; 5 6 7 8]), 1, 2)
[[4, 6],
 [12, 14]]
>> repsum(SX.sym('x', 4), 2)
[(x_0+x_2), (x_1+x_3)]

julia> ca.repsum(ca.DM([1 2 3 4; 5 6 7 8]), 1, 2)
DM(
[[4, 6],
 [12, 14]])
julia> ca.repsum(ca.SX.sym("x", 4), 2)
SX([(x_0+x_2), (x_1+x_3)])

See also
repmat, blocksplit

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

Definition at line 4771 of file generic_matrix.hpp.

4771  {
4772  return MatType::repsum(A, n, m);
4773  }

◆ reshape [1/3]

template<typename MatType >
MatType reshape ( const MatType &  x,
casadi_int  nrow,
casadi_int  ncol 
)
friend

Reinterprets the entries of x in column-major order as a matrix of nrow by ncol, as reshape does in MATLAB and numpy with order='F'. The number of elements must not change, otherwise an error is raised. One of the dimensions can be -1, it is then inferred from the other. The sparsity pattern is reshaped along; no entries are densified. For MX this is a cheap reordering node.

Parameters
xMatrix (or Sparsity) to reshape; any shape
nrowNumber of rows of the result, or -1
ncolNumber of columns of the result, or -1

>>> x = DM([[1, 2, 3], [4, 5, 6]])
>>> reshape(x, 3, 2)
DM(
[[1, 5],
 [4, 3],
 [2, 6]])
>>> reshape(x, -1, 1)
DM([1, 4, 2, 5, 3, 6])
>>> reshape(DM.eye(2), 1, 4)
DM([[1, 00, 00, 1]])
>>> reshape(MX.sym("x", 2, 3), 3, 2)
MX(reshape(x))
>>> reshape(x, 4, 2)
Traceback (most recent call last):
  ...
RuntimeError: ...number of elements must remain the same...

>> x = DM([1 2 3; 4 5 6]);
>> reshape(x, 3, 2)
[[1, 5],
 [4, 3],
 [2, 6]]
>> reshape(x, -1, 1)
[1, 4, 2, 5, 3, 6]
>> reshape(DM.eye(2), 1, 4)
[[1, 00, 00, 1]]
>> reshape(MX.sym('x', 2, 3), 3, 2)
reshape(x)
>> try, reshape(x, 4, 2); catch e, disp(e.message); end
...number of elements must remain the same...

julia> x = ca.DM([1 2 3; 4 5 6]);
julia> reshape(x, 3, 2)
DM(
[[1, 5],
 [4, 3],
 [2, 6]])
julia> reshape(x, :, 1)
DM([1, 4, 2, 5, 3, 6])
julia> reshape(ca.DM.eye(2), 1, 4)
DM([[1, 00, 00, 1]])
julia> reshape(ca.MX.sym("x", 2, 3), 3, 2)
MX(reshape(x))
julia> try
           reshape(x, 4, 2)
       catch err
           println(occursin("number of elements must remain", sprint(showerror, err)))
       end
true

See also
vec, sparsity_cast, transpose

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

Definition at line 1669 of file sparsity_interface.hpp.

1669  {
1670  return MatType::reshape(x, nrow, ncol);
1671  }

◆ reshape [2/3]

template<typename MatType >
MatType reshape ( const MatType &  x,
const Sparsity &  sp 
)
friend

sp must be the reshaped pattern of x.sparsity() (same number of elements and the same nonzeros in column-major order), otherwise an error is raised. Useful when the target pattern is already available, e.g. to undo a vec.

>>> reshape(SX.sym("x", 2, 2), Sparsity.dense(4, 1))
SX([x_0, x_1, x_2, x_3])
>>> reshape(MX.sym("x", Sparsity.diag(2)), Sparsity.dense(4, 1))
Traceback (most recent call last):
  ...
RuntimeError: ...Reshape mismatch...

>> reshape(SX.sym('x', 2, 2), Sparsity.dense(4, 1))
[x_0, x_1, x_2, x_3]
>> x = MX.sym('x', Sparsity.diag(2));
>> try, reshape(x, Sparsity.dense(4, 1)); catch e, disp(e.message); end
...Reshape mismatch...

julia> ca.reshape(ca.SX.sym("x", 2, 2), ca.Sparsity.dense(4, 1))
SX([x_0, x_1, x_2, x_3])
julia> try
           ca.reshape(ca.MX.sym("x", ca.Sparsity.diag(2)), ca.Sparsity.dense(4, 1))
       catch err
           println(occursin("Reshape mismatch", sprint(showerror, err)))
       end
true

See also
sparsity_cast

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

Definition at line 1751 of file sparsity_interface.hpp.

1751  {
1752  return MatType::reshape(x, sp);
1753  }

◆ reshape [3/3]

template<typename MatType >
MatType reshape ( const MatType &  x,
std::pair< casadi_int, casadi_int >  rc 
)
friend

Same as reshape(x, rc[0], rc[1]).

>>> reshape(DM([1, 2, 3, 4]), (2, 2))
DM(
[[1, 3],
 [2, 4]])

>> reshape(DM([1; 2; 3; 4]), [2, 2])
[[1, 3],
 [2, 4]]

julia> reshape(ca.DM([1, 2, 3, 4]), (2, 2))
DM(
[[1, 3],
 [2, 4]])

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

Definition at line 1704 of file sparsity_interface.hpp.

1704  {
1705  return MatType::reshape(x, rc.first, rc.second);
1706  }

◆ reverse

template<typename MatType >
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() 
)
friend

For each adjoint seed v[d] (a list with one seed per entry of ex, with matching shapes), computes the adjoint sensitivities with respect to every entry of arg: the result is a list over directions of lists over arg. Not defined for DM.

Parameters
exList of expressions
argList of symbolic expressions
vList of directions, each a list of seeds matching ex
optsOptions, see jacobian

>>> x = SX.sym("x"); y = SX.sym("y")
>>> reverse([x*y, x**2], [x, y], [[1, 0]])
[[SX(y), SX(x)]]
>>> reverse([x*y, x**2], [x, y], [[1, 1]])
[[SX(((2.*x)+y)), SX(x)]]

>> x = SX.sym('x'); y = SX.sym('y');
>> r = reverse({x*y, x^2}, {x, y}, {{1, 0}});
>> r{1}{1}, r{1}{2}
y
x
>> r = reverse({x*y, x^2}, {x, y}, {{1, 1}});
>> r{1}{1}, r{1}{2}
((2.*x)+y)
x

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.reverse([x*y, x^2], [x, y], [[ca.SX(1), ca.SX(0)]])
1-element Vector{Vector{SX}}:
 SX[y, x]
julia> ca.reverse([x*y, x^2], [x, y], [[ca.SX(1), ca.SX(1)]])
1-element Vector{Vector{SX}}:
 SX[((2.*x)+y), x]

See also
forward, jtimes, gradient, Function::reverse

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

Definition at line 3792 of file generic_matrix.hpp.

3794  {
3795  return MatType::reverse(ex, arg, v, opts);
3796  }

◆ separate_linear [1/2]

template<typename MatType >
void separate_linear ( const MatType &  expr,
const MatType &  sym_lin,
const MatType &  sym_const,
MatType &  expr_const,
MatType &  expr_lin,
MatType &  expr_nonlin 
)
friend
Parameters
expr[in]The expression to be separated
sym_lin[in]The symbolic variables w.r.t. which linearity should be checked
sym_const[in]The symbolic variables that are deemed constant
expr_const[out]The constant part of the expression
expr_lin[out]The linear part of the expression
expr_nonlin[out]The nonlinear part of the expression

Every symbol in expr must be listed in sym_lin or sym_const; otherwise an error about free variables is raised.

A post condition is that the following holds: expr = expr_const + expr_lin + expr_nonlin

Here, expr_const is only dependant on sym_const, expr_lin is linear in sym_lin (with coefficients that may depend on sym_const)

Example:

[expr_const,expr_lin,expr_nonlin] = separate_linear(cos(p)+7*x+x*y, vertcat(x,y), p)

expr_const: cos(p) expr_lin: 7*x expr_nonlin: x*y

sym_lin and sym_const may also be lists of symbols. Not defined for DM.

* >>> x = SX.sym("x"); y = SX.sym("y"); p = SX.sym("p")
* >>> separate_linear(cos(p) + 7*x + x*y, vertcat(x, y), p)
* (SX(cos(p)), SX((7*x)), SX((x*y)))
* >>> separate_linear(p*x, x, p)
* (SX(0), SX((p*x)), SX(0))
* 

* >> x = SX.sym('x'); y = SX.sym('y'); p = SX.sym('p');
* >> [c, l, n] = separate_linear(cos(p) + 7*x + x*y, vertcat(x, y), p);
* >> c, l, n
* cos(p)
* (7*x)
* (x*y)
* >> [c, l, n] = separate_linear(p*x, x, p);
* >> c, l, n
* 0
* (p*x)
* 0
* 

* julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); p = ca.SX.sym("p");
* julia> ca.separate_linear(cos(p) + 7 * x + x * y, [x; y], p)
* (cos(p), (7*x), (x*y))
* julia> ca.separate_linear(p * x, x, p)
* (0, (p*x), 0)
* 

See also
extract_parametric, linear_coeff

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

Definition at line 4392 of file generic_matrix.hpp.

4394  {
4395  MatType::separate_linear(expr, sym_lin, sym_const, expr_const, expr_lin, expr_nonlin);
4396  }

◆ separate_linear [2/2]

template<typename MatType >
void separate_linear ( const MatType &  expr,
const std::vector< MatType > &  sym_lin,
const std::vector< MatType > &  sym_const,
MatType &  expr_const,
MatType &  expr_lin,
MatType &  expr_nonlin 
)
friend

Definition at line 4398 of file generic_matrix.hpp.

4400  {
4401  separate_linear(expr, veccat(sym_lin), veccat(sym_const),
4402  expr_const, expr_lin, expr_nonlin);
4403  }
friend void separate_linear(const MatType &expr, const MatType &sym_lin, const MatType &sym_const, MatType &expr_const, MatType &expr_lin, MatType &expr_nonlin)
Separate an expression into subexpressions that are constant, linear and nonlinear.

◆ shared

template<typename MatType >
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 = "" 
)
friend

Same as extract with the options lift_shared true and lift_calls false: every node that is used more than once is replaced by a new symbol named v_prefix + counter

  • v_suffix. In Python the call is shared(ex, v_prefix, v_suffix) -> (ex, v, vdef).

>>> x = SX.sym("x"); y = SX.sym("y")
>>> e = x + y
>>> shared([sin(e)*cos(e) + e])
([SX(((sin(v_0)*cos(v_0))+v_0))], [SX(v_0)], [SX((x+y))])
>>> shared([sin(e), cos(e)], "w_", "_s")
([SX(sin(w_0_s)), SX(cos(w_0_s))], [SX(w_0_s)], [SX((x+y))])

>> x = SX.sym('x'); y = SX.sym('y');
>> e = x + y;
>> [ex, v, vdef] = shared({sin(e)*cos(e) + e});
>> ex{1}, v{1}, vdef{1}
((sin(v_0)*cos(v_0))+v_0)
v_0
(x+y)
>> [ex, v, vdef] = shared({sin(e), cos(e)}, 'w_', '_s');
>> ex{1}, ex{2}, v{1}
sin(w_0_s)
cos(w_0_s)
w_0_s

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> e = x + y;
julia> ca.shared([sin(e) * cos(e) + e])
(SX[((sin(v_0)*cos(v_0))+v_0)], SX[v_0], SX[(x+y)])
julia> ca.shared([sin(e), cos(e)], "w_", "_s")
(SX[sin(w_0_s), cos(w_0_s)], SX[w_0_s], SX[(x+y)])

See also
extract, cse

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

Definition at line 4721 of file generic_matrix.hpp.

4725  {
4726  return MatType::shared(ex, v, vdef, v_prefix, v_suffix);
4727  }

◆ sign

template<typename ExType >
ExType sign ( const ExType &  x)
friend

sign(x) := -1 for x<0 sign(x) := 1 for x>0, sign(0) := 0 sign(NaN) := NaN

Elementwise; sparsity preserved. The derivative is zero everywhere.

>>> sign(DM([-2, 0, 3]))
DM([-1, 0, 1])
>>> sign(SX.sym("x"))
SX(sign(x))

>> sign(DM([-2; 0; 3]))
[-1, 0, 1]
>> sign(SX.sym('x'))
sign(x)

julia> sign.(ca.DM([-2, 0, 3]))
DM([-1, 0, 1])
julia> sign(ca.SX.sym("x"))
SX(sign(x))

See also
abs, copysign

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

Definition at line 1915 of file generic_expression.hpp.

1915  {
1916  return ExType::sign(x);
1917  }

◆ simplify

template<typename MatType >
MatType simplify ( const MatType &  x)
friend

For SX, applies simplification passes such as constant folding and removal of trivial operations. For MX and DM the expression is returned unchanged.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> simplify(x*2*3), simplify(x*0 + y*1)
(SX((6*x)), SX(y))

>> x = SX.sym('x'); y = SX.sym('y');
>> simplify(x*2*3), simplify(x*0 + y*1)
(6*x)
y

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.simplify(x * 2 * 3), ca.simplify(x * 0 + y * 1)
((6*x), y)

See also
transform, cse

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

Definition at line 4482 of file generic_matrix.hpp.

4482  {
4483  return MatType::simplify(x);
4484  }

◆ sin

template<typename ExType >
ExType sin ( const ExType &  x)
friend

Elementwise; sparsity preserved (sin(0) == 0).

>>> sin(DM.eye(2))
DM(
[[0.841471, 00],
 [00, 0.841471]])
>>> sin(MX.sym("x", 2))
MX(sin(x))

>> sin(DM.eye(2))
[[0.841471, 00],
 [00, 0.841471]]
>> sin(MX.sym('x', 2))
sin(x)

julia> sin.(ca.DM.eye(2))
DM(
[[0.841471, 00],
 [00, 0.841471]])
julia> sin.(ca.MX.sym("x", 2))
MX(sin(x))

See also
cos, tan, asin

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

Definition at line 1061 of file generic_expression.hpp.

1061  {
1062  return ExType::sin(x);
1063  }

◆ sinh

template<typename ExType >
ExType sinh ( const ExType &  x)
friend

Elementwise; sparsity preserved.

>>> sinh(DM([0, 1]))
DM([0, 1.1752])

>> sinh(DM([0; 1]))
[0, 1.1752]

julia> sinh.(ca.DM([0, 1]))
DM([0, 1.1752])

See also
asinh, cosh, tanh

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

Definition at line 1338 of file generic_expression.hpp.

1338  {
1339  return ExType::sinh(x);
1340  }

◆ skew

template<typename MatType >
MatType skew ( const MatType &  a)
friend

skew(a) is the 3-by-3 matrix S with S*b == cross(a, b).

>>> skew(DM([1, 2, 3]))
DM(
[[0, -3, 2],
 [3, 0, -1],
 [-2, 1, 0]])
>>> inv_skew(skew(DM([1, 2, 3])))
DM([1, 2, 3])

>> skew(DM([1; 2; 3]))
[[0, -3, 2],
 [3, 0, -1],
 [-2, 1, 0]]
>> inv_skew(skew(DM([1; 2; 3])))
[1, 2, 3]

julia> ca.skew(ca.DM([1, 2, 3]))
DM(
[[0, -3, 2],
 [3, 0, -1],
 [-2, 1, 0]])
julia> ca.inv_skew(ca.skew(ca.DM([1, 2, 3])))
DM([1, 2, 3])

See also
inv_skew, cross

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

Definition at line 1547 of file generic_matrix.hpp.

1547  {
1548  return MatType::skew(a);
1549  }

◆ soc

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::soc ( const MatType &  x,
const MatType &  y 
)
friend

soc(x, y) computes the (n+1)-by-(n+1) matrix [y*eye(n) x; x' y]. This matrix is positive semidefinite if and only if norm_2(x) <= y, which makes it a way to express a second-order cone constraint as a semidefinite constraint.

Parameters
xVector expression of size n (row or column; sparse entries are densified)
yScalar expression

>>> soc(DM([1, 2]), 3)
DM(
[[3, 00, 1],
 [00, 3, 2],
 [1, 2, 3]])

>> soc(DM([1; 2]), 3)
[[3, 00, 1],
 [00, 3, 2],
 [1, 2, 3]]

julia> ca.soc(ca.DM([1, 2]), 3)
DM(
[[3, 00, 1],
 [00, 3, 2],
 [1, 2, 3]])

See also
norm_2

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

Definition at line 936 of file generic_matrix.hpp.

936  {
937  return MatType::soc(x, y);
938  }

◆ solve [1/2]

template<typename MatType >
MatType solve ( const MatType &  A,
const MatType &  b 
)
friend

The solve routine works similar to Matlab's backslash when A is square and nonsingular. The algorithm used is the following:

  1. A simple forward or backward substitution if A is upper or lower triangular
  2. If the linear system is at most 3-by-3, form the inverse via minor expansion and multiply
  3. Permute the variables and equations as to get a (structurally) nonzero diagonal, then perform a QR factorization without pivoting and solve the factorized system.

Note 1: If there are entries of the linear system known to be zero, these will be removed. Elements that are very small, or will evaluate to be zero, can still cause numerical errors, due to the lack of pivoting (which is not possible since cannot compare the size of entries)

Note 2: When permuting the linear system, a BLT (block lower triangular) transformation is formed. Only the permutation part of this is however used. An improvement would be to solve block-by-block if there are multiple BLT blocks.

The description above applies to DM and SX. For MX, triangular systems give dedicated solve nodes, an orthonormal pattern is inverted by transposition, and everything else falls back to solve(A, b, "qr"). A scalar A gives the elementwise division b/A. b may have several columns (one right-hand side each). A non-square A or a row count mismatch raises an error; a numerically singular DM returns nan or inf.

>>> A = DM([[1, 2], [3, 4]])
>>> solve(A, DM([1, 2]))
DM([0, 0.5])
>>> solve(A, DM.eye(2))
DM(
[[-2, 1],
 [1.5, -0.5]])
>>> solve(SX.sym("A", 2, 2), SX.sym("b", 2))
SX(@1=((A_0*A_3)-(A_2*A_1)), [(((A_3/@1)*b_0)-((A_2/@1)*b_1)),
(((A_0/@1)*b_1)-((A_1/@1)*b_0))])
>>> solve(MX.sym("A", 2, 2), MX.sym("b", 2))
MX((A\b))
>>> solve(DM(2), DM([1, 2]))
DM([0.5, 1])

>> A = DM([1 2; 3 4]);
>> solve(A, DM([1; 2]))
[0, 0.5]
>> solve(A, DM.eye(2))
[[-2, 1],
 [1.5, -0.5]]
>> solve(SX.sym('A', 2, 2), SX.sym('b', 2))
@1=((A_0*A_3)-(A_2*A_1)), [(((A_3/@1)*b_0)-((A_2/@1)*b_1)), (((A_0/@1)*b_1)-((A_1/@1)*b_0))]
>> solve(MX.sym('A', 2, 2), MX.sym('b', 2))
(A\b)
>> solve(DM(2), DM([1; 2]))
[0.5, 1]

julia> A = ca.DM([1 2; 3 4]);
julia> ca.solve(A, ca.DM([1, 2]))
DM([0, 0.5])
julia> ca.solve(A, ca.DM.eye(2))
DM(
[[-2, 1],
 [1.5, -0.5]])
julia> ca.solve(ca.SX.sym("A", 2, 2), ca.SX.sym("b", 2))
SX(@1=((A_0*A_3)-(A_2*A_1)), [(((A_3/@1)*b_0)-((A_2/@1)*b_1)), (((A_0/@1)*b_1)-((A_1/@1)*b_0))])
julia> ca.solve(ca.MX.sym("A", 2, 2), ca.MX.sym("b", 2))
MX((A\b))
julia> ca.solve(ca.DM(2), ca.DM([1, 2]))
DM([0.5, 1])

See also
inv, mldivide, Linsol, qr

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

Definition at line 3197 of file generic_matrix.hpp.

3197  {
3198  // If A is scalar, just divide
3199  if (A.is_scalar()) return b/A;
3200  return MatType::solve(A, b);
3201  }

◆ solve [2/2]

template<typename MatType >
MatType solve ( const MatType &  A,
const MatType &  b,
const std::string &  lsolver,
const Dict &  dict = Dict() 
)
friend

Available for DM and MX (for MX the solve becomes a call to the solver, evaluated numerically); SX raises an error. A scalar A gives the elementwise division b/A.

Parameters
lsolverName of a Linsol plugin, e.g. "qr", "csparse", "lapacklu", "lapackqr", "symbolicqr", "ldl", "ma27"; which ones are available depends on the build
dictOptions passed to the linear solver

>>> A = DM([[1, 2], [3, 4]])
>>> solve(A, DM([1, 2]), "qr")
DM([0, 0.5])
>>> solve(MX.sym("A", 2, 2), MX.sym("b", 2), "qr")
MX((A\b))

>> A = DM([1 2; 3 4]);
>> solve(A, DM([1; 2]), 'qr')
[0, 0.5]
>> solve(MX.sym('A', 2, 2), MX.sym('b', 2), 'qr')
(A\b)

julia> A = ca.DM([1 2; 3 4]);
julia> ca.solve(A, ca.DM([1, 2]), "qr")
DM([0, 0.5])
julia> ca.solve(ca.MX.sym("A", 2, 2), ca.MX.sym("b", 2), "qr")
MX((A\b))

See also
Linsol, has_linsol

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

Definition at line 3245 of file generic_matrix.hpp.

3247  {
3248  // If A is scalar, just divide
3249  if (A.is_scalar()) return b/A;
3250  return MatType::solve(A, b, lsolver, dict);
3251  }

◆ sparsify

template<typename Scalar >
Matrix<Scalar> sparsify ( const Matrix< Scalar > &  A,
double  tol = 0 
)
friend

Every stored entry with absolute value at most tol is dropped from the sparsity pattern. With the default tol=0 only exact zeros are removed; SX entries that the symbolic simplifications already reduced to the constant 0 count as zeros. Returns a new matrix; the input is unchanged.

Parameters
AMatrix to compact
tolAbsolute threshold below which an entry counts as zero

>>> sparsify(DM([[1, 0], [0, 2]]))
DM(
[[1, 00],
 [00, 2]])
>>> A = DM([[1, 0], [1e-9, 2]])
>>> sparsify(A).nnz(), sparsify(A, 1e-6).nnz()
(3, 2)
>>> x = SX.sym("x")
>>> sparsify(vertcat(x, 0, x * 0))
SX([x, 00, 00])

>> sparsify(DM([1 0; 0 2]))
[[1, 00],
 [00, 2]]
>> A = DM([1 0; 1e-9 2]);
>> fprintf('%d %d\n', sparsify(A).nnz(), sparsify(A, 1e-6).nnz())
3 2
>> x = SX.sym('x');
>> sparsify([x; 0; x * 0])
[x, 00, 00]

julia> ca.sparsify(ca.DM([1 0; 0 2]))
DM(
[[1, 00],
 [00, 2]])
julia> A = ca.DM([1 0; 1e-9 2]);
julia> ca.nnz(ca.sparsify(A)), ca.nnz(ca.sparsify(A, 1e-6))
(3, 2)
julia> x = ca.SX.sym("x");
julia> ca.sparsify([x; 0; x * 0])
SX([x, 00, 00])

See also
densify, has_zeros

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

Definition at line 2016 of file matrix_decl.hpp.

2016  {
2017  return Matrix<Scalar>::sparsify(A, tol);
2018  }
static Matrix< Scalar > sparsify(const Matrix< Scalar > &x, double tol=0)

◆ sparsity_cast

template<typename MatType >
MatType sparsity_cast ( const MatType &  x,
const Sparsity &  sp 
)
friend

Keeps the nonzero values of x in storage order and gives them the pattern sp, which must have the same number of nonzeros (an error is raised otherwise). Unlike reshape, the dimensions and the positions of the nonzeros can change freely; only the nonzero count is preserved. This is how the nonzeros of a sparse matrix are extracted as a dense vector.

Parameters
xMatrix (or Sparsity) whose nonzeros are reused
spTarget pattern with x.nnz() nonzeros
Returns
Matrix of the same type as x with pattern sp

>>> sparsity_cast(DM([[1, 2], [3, 4]]), Sparsity.dense(1, 4))
DM([[1, 3, 2, 4]])
>>> sparsity_cast(DM.eye(3), Sparsity.dense(3, 1))
DM([1, 1, 1])
>>> sparsity_cast(MX.sym("x", Sparsity.diag(3)), Sparsity.dense(3, 1))
MX(nonzeros(x))
>>> sparsity_cast(MX.sym("x", 2, 2), Sparsity.dense(3, 1))
Traceback (most recent call last):
  ...
RuntimeError: ...Mismatching nonzero count...

>> sparsity_cast(DM([1 2; 3 4]), Sparsity.dense(1, 4))
[[1, 3, 2, 4]]
>> sparsity_cast(DM.eye(3), Sparsity.dense(3, 1))
[1, 1, 1]
>> sparsity_cast(MX.sym('x', Sparsity.diag(3)), Sparsity.dense(3, 1))
nonzeros(x)
>> x = MX.sym('x', 2, 2);
>> try, sparsity_cast(x, Sparsity.dense(3, 1)); catch e, disp(e.message); end
...Mismatching nonzero count...

julia> ca.sparsity_cast(ca.DM([1 2; 3 4]), ca.Sparsity.dense(1, 4))
DM([[1, 3, 2, 4]])
julia> ca.sparsity_cast(ca.DM.eye(3), ca.Sparsity.dense(3, 1))
DM([1, 1, 1])
julia> ca.sparsity_cast(ca.MX.sym("x", ca.Sparsity.diag(3)), ca.Sparsity.dense(3, 1))
MX(nonzeros(x))
julia> try
           ca.sparsity_cast(ca.MX.sym("x", 2, 2), ca.Sparsity.dense(3, 1))
       catch err
           println(occursin("Mismatching nonzero count", sprint(showerror, err)))
       end
true

See also
reshape

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

Definition at line 1815 of file sparsity_interface.hpp.

1815  {
1816  return MatType::sparsity_cast(x, sp);
1817  }

◆ sprank

template<typename MatType >
casadi_int sprank ( const MatType &  x)
friend

The structural rank is the size of a maximum matching between rows and columns of the pattern (via the Dulmage-Mendelsohn decomposition): an upper bound on the numerical rank that depends only on which entries are structurally nonzero, not on their values. A dense matrix always has full structural rank, even when it is numerically singular.

Parameters
xMatrix (or Sparsity); only x.sparsity() is used
Returns
Structural rank, at most min(size1, size2)

>>> sprank(Sparsity.diag(3))
3
>>> sprank(Sparsity.triplet(3, 3, [0, 1, 2], [0, 0, 0]))
1
>>> sprank(DM([[1, 2], [2, 4]]))
2
>>> sprank(DM(2, 3))
0

>> sprank(Sparsity.diag(3))
3
>> sprank(Sparsity.triplet(3, 3, [0, 1, 2], [0, 0, 0]))
1
>> sprank(DM([1 2; 2 4]))
2
>> sprank(DM(2, 3))
0

julia> ca.sprank(ca.Sparsity.diag(3))
3
julia> ca.sprank(ca.Sparsity.triplet(3, 3, [0, 1, 2], [0, 0, 0]))
1
julia> ca.sprank(ca.DM([1 2; 2 4]))
2
julia> ca.sprank(ca.DM(2, 3))
0

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

Definition at line 1869 of file sparsity_interface.hpp.

1869  {
1870  return MatType::sprank(x);
1871  }

◆ sq

template<typename ExType >
ExType sq ( const ExType &  x)
friend

Elementwise; sparsity preserved. Not exposed as a function in Python; a symbolic power with exponent 2 is turned into this operation.

>>> SX.sym("x")**2
SX(sq(x))

>> SX.sym('x').^2
sq(x)

julia> ca.SX.sym("x")^2
SX(sq(x))

See also
sqrt, pow

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

Definition at line 1013 of file generic_expression.hpp.

1013  {
1014  return ExType::sq(x);
1015  }

◆ sqrt

template<typename ExType >
ExType sqrt ( const ExType &  x)
friend

Elementwise; sparsity preserved. A negative argument gives NaN, no error is raised. A symbolic power x**0.5 simplifies to sqrt(x).

>>> sqrt(DM([4, 9]))
DM([2, 3])
>>> SX.sym("x")**0.5
SX(sqrt(x))

>> sqrt(DM([4; 9]))
[2, 3]
>> SX.sym('x').^0.5
sqrt(x)

julia> sqrt.(ca.DM([4, 9]))
DM([2, 3])
julia> ca.SX.sym("x")^0.5
SX(sqrt(x))

See also
sq, pow

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

Definition at line 975 of file generic_expression.hpp.

975  {
976  return ExType::sqrt(x);
977  }

◆ stop_diff [1/2]

MX stop_diff ( const MX &  expr,
casadi_int  order 
)
friend

Wraps expr in a non-inlined Function call whose derivatives of the given order are declared zero. The value is unchanged. With order 1 the expression is treated as a constant by all differentiation; with order 2 first derivatives are propagated normally but second derivatives are zero. Other orders raise an error.

>>> x = MX.sym("x")
>>> e = x * stop_diff(x**2, 1)
>>> Function("f", [x], [e, jacobian(e, x)])(3)
(DM(27), DM(9))

>> x = MX.sym('x');
>> e = x * stop_diff(x^2, 1);
>> f = Function('f', {x}, {e, jacobian(e, x)});
>> [v, J] = f(3);
>> v, J
27
9

julia> x = ca.MX.sym("x");
julia> e = x * ca.stop_diff(x^2, 1);
julia> ca.Function("f", [x], [e, ca.jacobian(e, x)])(3)
(27, 9)

See also
no_grad, no_hess

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

Definition at line 4496 of file mx.hpp.

4496  {
4497  return MX::stop_diff(expr, order);
4498  }

◆ stop_diff [2/2]

MX stop_diff ( const MX &  expr,
const MX &  var,
casadi_int  order 
)
friend

Like stop_diff(expr, order), but only the derivatives with respect to the symbols occurring in var are stopped; derivatives with respect to the other symbols are propagated. Emits a warning that this variant is not well tested.

>>> x = MX.sym("x")
>>> y = MX.sym("y")
>>> e = stop_diff(x * y, x, 1)
>>> jacobian(e, x).nnz(), Function("f", [x, y], [jacobian(e, y)])(2, 3)
(0, DM(2))

>> x = MX.sym('x'); y = MX.sym('y');
>> e = stop_diff(x * y, x, 1);
...
>> J = jacobian(e, x);
>> f = Function('f', {x, y}, {jacobian(e, y)});
>> J.nnz(), f(2, 3)
0
2

julia> x = ca.MX.sym("x"); y = ca.MX.sym("y");
julia> e = ca.stop_diff(x * y, x, 1);
julia> ca.nnz(ca.jacobian(e, x)), ca.Function("f", [x, y], [ca.jacobian(e, y)])(2, 3)
(0, 2)

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

Definition at line 4630 of file mx.hpp.

4630  {
4631  return MX::stop_diff(expr, var, order);
4632  }

◆ substitute [1/2]

template<typename MatType >
MatType substitute ( const MatType &  ex,
const MatType &  v,
const MatType &  vdef 
)
friend

v must be symbolic and have the same shape as vdef (for SX also the same sparsity). For MX, v must be a symbolic primitive as created by MX.sym; an element of such a primitive is rejected. Substituting a symbol that does not occur in ex is a no-op. Not defined for DM.

>>> x = SX.sym("x"); y = SX.sym("y"); z = SX.sym("z")
>>> substitute(x*y + x, x, 2)
SX(((2.*y)+2))
>>> substitute(x*y + x, x, z**2)
SX(@1=sq(z), ((@1*y)+@1))
>>> p = SX.sym("p", 2)
>>> substitute(sumsqr(p), p, DM([1, 2]))
SX(5)
>>> X = MX.sym("X"); Y = MX.sym("Y")
>>> substitute(X*Y + X, X, Y)
MX((sq(Y)+Y))

>> x = SX.sym('x'); y = SX.sym('y'); z = SX.sym('z');
>> substitute(x*y + x, x, 2)
((2.*y)+2)
>> substitute(x*y + x, x, z^2)
@1=sq(z), ((@1*y)+@1)
>> p = SX.sym('p', 2);
>> substitute(sumsqr(p), p, DM([1; 2]))
5
>> X = MX.sym('X'); Y = MX.sym('Y');
>> substitute(X*Y + X, X, Y)
(sq(Y)+Y)

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); z = ca.SX.sym("z");
julia> ca.substitute(x * y + x, x, 2)
SX(((2.*y)+2))
julia> ca.substitute(x * y + x, x, z^2)
SX(@1=sq(z), ((@1*y)+@1))
julia> p = ca.SX.sym("p", 2);
julia> ca.substitute(ca.sumsqr(p), p, ca.DM([1, 2]))
SX(5)
julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
julia> ca.substitute(X * Y + X, X, Y)
MX((sq(Y)+Y))

See also
substitute_inplace, graph_substitute, Function

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

Definition at line 2917 of file generic_matrix.hpp.

2918  {
2919  return MatType::substitute(ex, v, vdef);
2920  }

◆ substitute [2/2]

template<typename MatType >
std::vector<MatType> substitute ( const std::vector< MatType > &  ex,
const std::vector< MatType > &  v,
const std::vector< MatType > &  vdef 
)
friend

All substitutions happen simultaneously: vdef[k] is not itself subject to the other substitutions, so swapping two variables works as expected.

>>> x = SX.sym("x"); y = SX.sym("y")
>>> substitute([x*y, x + y], [x, y], [1, 2])
[SX(2), SX(3)]
>>> substitute([x + y], [x, y], [y, x])
[SX((x+y))]

>> x = SX.sym('x'); y = SX.sym('y');
>> r = substitute({x*y, x + y}, {x, y}, {1, 2});
>> r{1}, r{2}
2
3
>> r = substitute({x + y}, {x, y}, {y, x});
>> r{1}
(x+y)

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.substitute([x * y, x + y], [x, y], [1, 2])
2-element Vector{SX}:
 SX(2)
 SX(3)
julia> ca.substitute([x + y], [x, y], [y, x])
1-element Vector{SX}:
 SX((x+y))

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

Definition at line 2965 of file generic_matrix.hpp.

2966  {
2967  return MatType::substitute(ex, v, vdef);
2968  }

◆ substitute_inplace

template<typename MatType >
void substitute_inplace ( const std::vector< MatType > &  v,
std::vector< MatType > &  inout_vdef,
std::vector< MatType > &  inout_ex,
bool  reverse = false 
)
friend

Substitute variables v out of the expressions vdef sequentially, as well as out of a number of other expressions piggyback

Interprets vdef[k] as the definition of the symbol v[k] and resolves the definitions into each other, so that afterwards neither vdef nor ex contain any of the symbols v. Both vdef and ex are modified in place (and returned, in Python). v must consist of symbolic primitives with the shapes of vdef. reverse is only accepted for SX; MX raises "Not implemented".

>>> x = SX.sym("x"); y = SX.sym("y")
>>> substitute_inplace([x, y], [y + 1, 3], [x*y])
([SX(4), SX(3)], [SX(12)])
>>> X = MX.sym("X"); Y = MX.sym("Y")
>>> substitute_inplace([X, Y], [Y + 1, 3], [X*Y])
([MX(1), MX(3)], [MX(00)])

>> x = SX.sym('x'); y = SX.sym('y');
>> [vdef, ex] = substitute_inplace({x, y}, {y + 1, 3}, {x*y});
>> vdef{1}, vdef{2}, ex{1}
4
3
12
>> X = MX.sym('X'); Y = MX.sym('Y');
>> [vdef, ex] = substitute_inplace({X, Y}, {Y + 1, 3}, {X*Y});
>> vdef{1}, vdef{2}, ex{1}
1
3
00

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> ca.substitute_inplace([x, y], [y + 1, ca.SX(3)], [x*y])
(SX[4, 3], SX[12])
julia> X = ca.MX.sym("X"); Y = ca.MX.sym("Y");
julia> ca.substitute_inplace([X, Y], [Y + 1, ca.MX(3)], [X*Y])
(MX[1, 3], MX[00])

See also
substitute

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

Definition at line 3024 of file generic_matrix.hpp.

3026  {
3027  return MatType::substitute_inplace(v, inout_vdef, inout_ex, reverse);
3028  }
friend 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.

◆ sum

template<typename MatType >
MatType sum ( const MatType &  x)
friend

Returns a 1-by-1 matrix with the sum of all elements, combining sum1 and sum2. An empty matrix sums to a structural zero.

sum also accepts an axis argument: sum(x, 0) is sum1(x) and sum(x, 1) is sum2(x).

>>> sum(DM([[1, 2, 3], [4, 5, 6]]))
DM(21)
>>> sum(DM([[1, 2, 3], [4, 5, 6]]), 0)
DM([[5, 7, 9]])
>>> sum(SX.sym("x", 3))
SX(((x_0+x_1)+x_2))
>>> sum(DM(3, 0))
DM(00)

>> sum(DM([1 2 3; 4 5 6]), 'all')
21
>> sum(DM([1 2 3; 4 5 6]))
[[5, 7, 9]]
>> sum(SX.sym('x', 3))
((x_0+x_1)+x_2)
>> sum(DM(3, 0), 'all')
00

julia> sum(ca.DM([1 2 3; 4 5 6]))
DM(21)
julia> sum(ca.DM([1 2 3; 4 5 6]), dims=1)
DM([[5, 7, 9]])
julia> sum(ca.SX.sym("x", 3))
SX(((x_0+x_1)+x_2))
julia> sum(ca.DM(3, 0))
DM(00)

See also
sum1, sum2

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

Definition at line 2490 of file sparsity_interface.hpp.

2490  {
2491  if (x.is_vector()) {
2492  if (x.is_column()) {
2493  return MatType::sum1(x);
2494  } else {
2495  return MatType::sum2(x);
2496  }
2497  }
2498  if (x.size1()>x.size2()) {
2499  return MatType::sum2(MatType::sum1(x));
2500  } else {
2501  return MatType::sum1(MatType::sum2(x));
2502  }
2503  }

◆ sum1

template<typename MatType >
MatType sum1 ( const MatType &  x)
friend

Returns a row vector with the sum over the rows, as MATLAB sum(x, 1); implemented as the product ones(1, size1) * x, so it is a mtimes node for MX. A column of structural zeros sums to a structural zero. For Sparsity only the pattern is computed.

>>> sum1(DM([[1, 2, 3], [4, 5, 6]]))
DM([[5, 7, 9]])
>>> sum1(SX.sym("x", 2, 2))
SX([[(x_0+x_1), (x_2+x_3)]])
>>> sum1(MX.sym("x", 2, 3))
MX(mac(ones(1x2),x,zeros(1x3)))
>>> sum1(DM(0, 3))
DM([[00, 00, 00]])

>> sum1(DM([1 2 3; 4 5 6]))
[[5, 7, 9]]
>> sum(DM([1 2 3; 4 5 6]), 1)
[[5, 7, 9]]
>> sum1(SX.sym('x', 2, 2))
[[(x_0+x_1), (x_2+x_3)]]
>> sum1(MX.sym('x', 2, 3))
mac(ones(1x2),x,zeros(1x3))
>> sum1(DM(0, 3))
[[00, 00, 00]]

julia> ca.sum1(ca.DM([1 2 3; 4 5 6]))
DM([[5, 7, 9]])
julia> ca.sum1(ca.SX.sym("x", 2, 2))
SX([[(x_0+x_1), (x_2+x_3)]])
julia> ca.sum1(ca.MX.sym("x", 2, 3))
MX(mac(ones(1x2),x,zeros(1x3)))
julia> ca.sum1(ca.DM(0, 3))
DM([[00, 00, 00]])

See also
sum2, sum

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

Definition at line 2394 of file sparsity_interface.hpp.

2394 { return MatType::sum1(x);}

◆ sum2

template<typename MatType >
MatType sum2 ( const MatType &  x)
friend

Returns a column vector with the sum over the columns, as MATLAB sum(x, 2); implemented as the product x * ones(size2, 1). A row of structural zeros sums to a structural zero.

>>> sum2(DM([[1, 2, 3], [4, 5, 6]]))
DM([6, 15])
>>> sum2(SX.sym("x", 2, 2))
SX([(x_0+x_2), (x_1+x_3)])
>>> sum2(DM(3, 3))
DM([00, 00, 00])

>> sum2(DM([1 2 3; 4 5 6]))
[6, 15]
>> sum(DM([1 2 3; 4 5 6]), 2)
[6, 15]
>> sum2(SX.sym('x', 2, 2))
[(x_0+x_2), (x_1+x_3)]
>> sum2(DM(3, 3))
[00, 00, 00]

julia> ca.sum2(ca.DM([1 2 3; 4 5 6]))
DM([6, 15])
julia> ca.sum2(ca.SX.sym("x", 2, 2))
SX([(x_0+x_2), (x_1+x_3)])
julia> ca.sum2(ca.DM(3, 3))
DM([00, 00, 00])

See also
sum1, sum

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

Definition at line 2439 of file sparsity_interface.hpp.

2439 { return MatType::sum2(x);}

◆ sumsqr

template<typename MatType >
MatType sumsqr ( const MatType &  x)
friend

Defined as dot(x, x) for a matrix of any shape; structural zeros contribute nothing. Prefer this over norm_2(x)**2 in objectives: it is smooth everywhere.

>>> sumsqr(DM([[1, 2], [3, 4]]))
DM(30)
>>> sumsqr(SX.sym("x", 2))
SX((sq(x_0)+sq(x_1)))
>>> sumsqr(MX.sym("x", 2))
MX(dot(x, x))

>> sumsqr(DM([1 2; 3 4]))
30
>> sumsqr(SX.sym('x', 2))
(sq(x_0)+sq(x_1))
>> sumsqr(MX.sym('x', 2))
dot(x, x)

julia> ca.sumsqr(ca.DM([1 2; 3 4]))
DM(30)
julia> ca.sumsqr(ca.SX.sym("x", 2))
SX((sq(x_0)+sq(x_1)))
julia> ca.sumsqr(ca.MX.sym("x", 2))
MX(dot(x, x))

See also
dot, norm_2, norm_fro

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

Definition at line 1312 of file generic_matrix.hpp.

1312  {
1313  return MatType::sumsqr(x);
1314  }

◆ symvar

template<typename MatType >
std::vector<MatType> symvar ( const MatType &  x)
friend

Returns the symbols the expression was built from, in order of first appearance, each once. For SX every scalar symbol is a separate entry; for MX the entries are the symbolic matrices as created by MX.sym, even if only part of such a matrix is used. Presence in the graph is a structural notion; an expression like 0*x still lists x. Use depends_on for mathematical dependency. Not defined for DM.

>>> x = SX.sym("x"); y = SX.sym("y"); z = SX.sym("z")
>>> symvar(vertcat(z*x, y))
[SX(z), SX(x), SX(y)]
>>> symvar(SX.sym("v", 2)*3)
[SX(v_0), SX(v_1)]
>>> v = MX.sym("v", 2)
>>> symvar(v[0]*3)
[MX(v)]
>>> symvar(SX(3))
[]

>> x = SX.sym('x'); y = SX.sym('y'); z = SX.sym('z');
>> v = symvar(vertcat(z*x, y));
>> numel(v), vertcat(v{:})
3
[z, x, y]
>> v = symvar(SX.sym('v', 2)*3); disp(vertcat(v{:}))
[v_0, v_1]
>> v = MX.sym('v', 2);
>> s = symvar(v(1)*3); disp(numel(s)), disp(s{1})
1
v
>> numel(symvar(SX(3)))
0

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y"); z = ca.SX.sym("z");
julia> ca.symvar([z*x; y])
3-element Vector{SX}:
 SX(z)
 SX(x)
 SX(y)
julia> ca.symvar(ca.SX.sym("v", 2) * 3)
2-element Vector{SX}:
 SX(v_0)
 SX(v_1)
julia> v = ca.MX.sym("v", 2);
julia> ca.symvar(v[1] * 3)
1-element Vector{MX}:
 MX(v)
julia> ca.symvar(ca.SX(3))
SX[]

See also
depends_on, contains

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

Definition at line 1157 of file generic_matrix.hpp.

1157  {
1158  return MatType::symvar(x);
1159  }

◆ tan

template<typename ExType >
ExType tan ( const ExType &  x)
friend

Elementwise; sparsity preserved.

>>> tan(DM([0, 1]))
DM([0, 1.55741])

>> tan(DM([0; 1]))
[0, 1.55741]

julia> tan.(ca.DM([0, 1]))
DM([0, 1.55741])

See also
atan, sin, cos

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

Definition at line 1152 of file generic_expression.hpp.

1152  {
1153  return ExType::tan(x);
1154  }

◆ tangent

template<typename MatType >
MatType casadi::GenericMatrix< MatType >::tangent ( const MatType &  ex,
const MatType &  arg,
const Dict &  opts = Dict() 
)
friend

Derivative of ex with respect to a scalar arg, computed with one forward-mode sweep and projected onto the sparsity of ex: the result has the shape and pattern of ex. A non-scalar arg raises an error (use jacobian).

>>> x = SX.sym("x")
>>> tangent(vertcat(x**2, 3*x), x)
SX([(2.*x), 3])

>> x = SX.sym('x');
>> tangent(vertcat(x^2, 3*x), x)
[(2.*x), 3]

julia> x = ca.SX.sym("x");
julia> ca.tangent([x^2; 3 * x], x)
SX([(2.*x), 3])

See also
gradient, jacobian, jtimes

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

Definition at line 3606 of file generic_matrix.hpp.

3606  {
3607  return MatType::tangent(ex, arg, opts);
3608  }

◆ tanh

template<typename ExType >
ExType tanh ( const ExType &  x)
friend

Elementwise; sparsity preserved.

>>> tanh(DM([0, 1]))
DM([0, 0.761594])

>> tanh(DM([0; 1]))
[0, 0.761594]

julia> tanh.(ca.DM([0, 1]))
DM([0, 0.761594])

See also
atanh, sinh, cosh

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

Definition at line 1301 of file generic_expression.hpp.

1301  {
1302  return ExType::tanh(x);
1303  }

◆ taylor [1/2]

template<typename Scalar >
Matrix<Scalar> taylor ( const Matrix< Scalar > &  ex,
const Matrix< Scalar > &  x 
)
friend

Expands ex around a with respect to the scalar symbol x up to the given order: ex(a) + ex'(a)*(x-a) + ex''(a)*(x-a)^2/2! + ... + ex^(order)(a)*(x-a)^order/order! The expansion is done entrywise for a dense matrix ex; sparse ex raises an error. a may itself be symbolic. The two-argument form expands around 0 to first order. Only defined for SX.

Parameters
exDense SX expression to expand
xScalar SX symbol
aScalar expansion point, default 0
orderHighest power of (x-a) kept, default 1; 0 gives ex(a)

>>> x = SX.sym("x"); a = SX.sym("a")
>>> taylor(sin(x), x)
SX(x)
>>> taylor(sin(x), x, a, 1)
SX((sin(a)+(cos(a)*(x-a))))
>>> taylor(exp(x), x, 0, 3)
SX(@1=sq(x), (((1+x)+(0.5*@1))+(0.166667*(@1*x))))
>>> taylor(vertcat(sin(x), cos(x)), x, 0, 2)
SX([x, (1+(-0.5*sq(x)))])

>> x = SX.sym('x'); a = SX.sym('a');
>> taylor(sin(x), x)
x
>> taylor(sin(x), x, a, 1)
(sin(a)+(cos(a)*(x-a)))
>> taylor(exp(x), x, 0, 3)
@1=sq(x), (((1+x)+(0.5*@1))+(0.166667*(@1*x)))
>> taylor([sin(x); cos(x)], x, 0, 2)
[x, (1+(-0.5*sq(x)))]

julia> x = ca.SX.sym("x"); a = ca.SX.sym("a");
julia> ca.taylor(sin(x), x)
SX(x)
julia> ca.taylor(sin(x), x, a, 1)
SX((sin(a)+(cos(a)*(x-a))))
julia> ca.taylor(exp(x), x, 0, 3)
SX(@1=sq(x), (((1+x)+(0.5*@1))+(0.166667*(@1*x))))
julia> ca.taylor([sin(x); cos(x)], x, 0, 2)
SX([x, (1+(-0.5*sq(x)))])

See also
mtaylor, linearize, linear_coeff

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

Definition at line 2461 of file matrix_decl.hpp.

2461  {
2462  return Matrix<Scalar>::taylor(ex, x, 0, 1);
2463  }
static Matrix< Scalar > taylor(const Matrix< Scalar > &ex, const Matrix< Scalar > &x, const Matrix< Scalar > &a, casadi_int order)

◆ taylor [2/2]

template<typename Scalar >
Matrix<Scalar> taylor ( const Matrix< Scalar > &  ex,
const Matrix< Scalar > &  x,
const Matrix< Scalar > &  a,
casadi_int  order = 1 
)
friend

Expands ex around a with respect to the scalar symbol x up to the given order: ex(a) + ex'(a)*(x-a) + ex''(a)*(x-a)^2/2! + ... + ex^(order)(a)*(x-a)^order/order! The expansion is done entrywise for a dense matrix ex; sparse ex raises an error. a may itself be symbolic. The two-argument form expands around 0 to first order. Only defined for SX.

Parameters
exDense SX expression to expand
xScalar SX symbol
aScalar expansion point, default 0
orderHighest power of (x-a) kept, default 1; 0 gives ex(a)

>>> x = SX.sym("x"); a = SX.sym("a")
>>> taylor(sin(x), x)
SX(x)
>>> taylor(sin(x), x, a, 1)
SX((sin(a)+(cos(a)*(x-a))))
>>> taylor(exp(x), x, 0, 3)
SX(@1=sq(x), (((1+x)+(0.5*@1))+(0.166667*(@1*x))))
>>> taylor(vertcat(sin(x), cos(x)), x, 0, 2)
SX([x, (1+(-0.5*sq(x)))])

>> x = SX.sym('x'); a = SX.sym('a');
>> taylor(sin(x), x)
x
>> taylor(sin(x), x, a, 1)
(sin(a)+(cos(a)*(x-a)))
>> taylor(exp(x), x, 0, 3)
@1=sq(x), (((1+x)+(0.5*@1))+(0.166667*(@1*x)))
>> taylor([sin(x); cos(x)], x, 0, 2)
[x, (1+(-0.5*sq(x)))]

julia> x = ca.SX.sym("x"); a = ca.SX.sym("a");
julia> ca.taylor(sin(x), x)
SX(x)
julia> ca.taylor(sin(x), x, a, 1)
SX((sin(a)+(cos(a)*(x-a))))
julia> ca.taylor(exp(x), x, 0, 3)
SX(@1=sq(x), (((1+x)+(0.5*@1))+(0.166667*(@1*x))))
julia> ca.taylor([sin(x); cos(x)], x, 0, 2)
SX([x, (1+(-0.5*sq(x)))])

See also
mtaylor, linearize, linear_coeff

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

Definition at line 2457 of file matrix_decl.hpp.

2458  {
2459  return Matrix<Scalar>::taylor(ex, x, a, order);
2460  }

◆ times

template<typename ExType >
ExType times ( const ExType &  x,
const ExType &  y 
)
friend

Elementwise (Hadamard) product, not the matrix product; use mtimes for that. Shape rules as for plus. The result is structurally zero wherever either operand is, so the sparsity pattern is the intersection of the patterns.

>>> times(DM([1, 2]), DM([3, 4]))
DM([3, 8])
>>> DM([[1, 2], [3, 4]]) * DM([[1, 2], [3, 4]])
DM(
[[1, 4],
 [9, 16]])
>>> DM.eye(2) * DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

>> times(DM([1; 2]), DM([3; 4]))
[3, 8]
>> DM([1 2; 3 4]) .* DM([1 2; 3 4])
[[1, 4],
 [9, 16]]
>> DM([1 2; 3 4]) * DM([1 2; 3 4])
[[7, 10],
 [15, 22]]
>> DM.eye(2) .* DM.ones(2, 2)
[[1, 00],
 [00, 1]]

julia> ca.times(ca.DM([1, 2]), ca.DM([3, 4]))
DM([3, 8])
julia> ca.DM([1 2; 3 4]) .* ca.DM([1 2; 3 4])
DM(
[[1, 4],
 [9, 16]])
julia> ca.DM.eye(2) .* ca.DM.ones(2, 2)
DM(
[[1, 00],
 [00, 1]])

See also
mtimes

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

Definition at line 292 of file generic_expression.hpp.

292  {
293  return ExType::times(x, y);
294  }

◆ trace

template<typename MatType >
MatType trace ( const MatType &  x)
friend

Sum of the diagonal entries of a square matrix (structural zeros contribute nothing). A non-square argument raises an error.

>>> trace(DM([[1, 2], [3, 4]]))
DM(5)
>>> trace(SX.sym("A", 2, 2))
SX((A_0+A_3))

>> trace(DM([1 2; 3 4]))
5
>> trace(SX.sym('A', 2, 2))
(A_0+A_3)

julia> ca.tr(ca.DM([1 2; 3 4]))
DM(5)
julia> ca.tr(ca.SX.sym("A", 2, 2))
SX((A_0+A_3))

See also
diag

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

Definition at line 1817 of file generic_matrix.hpp.

1817 { return MatType::trace(x);}

◆ transform [1/4]

template<typename MatType >
MatType transform ( const MatType &  x,
const Dict &  opts = Dict() 
)
friend

Wraps the expression in a Function of its free symbols, applies Function::transform and re-evaluates it on the original symbols. Each pass is a list starting with a verb: "simplify" (followed by task names), "expand" or "external"; an unknown verb raises an error. Without passes a default set of simplification passes is applied.

>>> x = SX.sym("x")
>>> transform(x*2*3)
SX((6*x))

>> x = SX.sym('x');
>> transform(x*2*3)
(6*x)

julia> x = ca.SX.sym("x");
julia> ca.transform(x * 2 * 3)
SX((6*x))

See also
simplify, Function::transform

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

Definition at line 4521 of file generic_matrix.hpp.

4521  {
4522  return MatType::transform(x, opts);
4523  }

◆ transform [2/4]

template<typename MatType >
MatType transform ( const MatType &  x,
const std::vector< std::vector< GenericType > > &  passes,
const Dict &  opts = Dict() 
)
friend

Wraps the expression in a Function of its free symbols, applies Function::transform and re-evaluates it on the original symbols. Each pass is a list starting with a verb: "simplify" (followed by task names), "expand" or "external"; an unknown verb raises an error. Without passes a default set of simplification passes is applied.

>>> x = SX.sym("x")
>>> transform(x*2*3)
SX((6*x))

>> x = SX.sym('x');
>> transform(x*2*3)
(6*x)

julia> x = ca.SX.sym("x");
julia> ca.transform(x * 2 * 3)
SX((6*x))

See also
simplify, Function::transform

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

Definition at line 4524 of file generic_matrix.hpp.

4525  {
4526  return MatType::transform(x, passes, opts);
4527  }

◆ transform [3/4]

template<typename MatType >
std::vector<MatType> transform ( const std::vector< MatType > &  x,
const Dict &  opts = Dict() 
)
friend

Wraps the expression in a Function of its free symbols, applies Function::transform and re-evaluates it on the original symbols. Each pass is a list starting with a verb: "simplify" (followed by task names), "expand" or "external"; an unknown verb raises an error. Without passes a default set of simplification passes is applied.

>>> x = SX.sym("x")
>>> transform(x*2*3)
SX((6*x))

>> x = SX.sym('x');
>> transform(x*2*3)
(6*x)

julia> x = ca.SX.sym("x");
julia> ca.transform(x * 2 * 3)
SX((6*x))

See also
simplify, Function::transform

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

Definition at line 4528 of file generic_matrix.hpp.

4529  {
4530  return MatType::transform(x, opts);
4531  }

◆ transform [4/4]

template<typename MatType >
std::vector<MatType> transform ( const std::vector< MatType > &  x,
const std::vector< std::vector< GenericType > > &  passes,
const Dict &  opts = Dict() 
)
friend

Wraps the expression in a Function of its free symbols, applies Function::transform and re-evaluates it on the original symbols. Each pass is a list starting with a verb: "simplify" (followed by task names), "expand" or "external"; an unknown verb raises an error. Without passes a default set of simplification passes is applied.

>>> x = SX.sym("x")
>>> transform(x*2*3)
SX((6*x))

>> x = SX.sym('x');
>> transform(x*2*3)
(6*x)

julia> x = ca.SX.sym("x");
julia> ca.transform(x * 2 * 3)
SX((6*x))

See also
simplify, Function::transform

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

Definition at line 4532 of file generic_matrix.hpp.

4533  {
4534  return MatType::transform(x, passes, opts);
4535  }

◆ transpose

template<typename MatType >
MatType transpose ( const MatType &  X)
friend

Returns the transpose of X; the sparsity pattern is transposed along. Same as X.T().

>>> transpose(DM([[1, 2, 3]]))
DM([1, 2, 3])
>>> transpose(MX.sym("x", 2, 3))
MX(x')
>>> transpose(Sparsity.lower(2)) == Sparsity.upper(2)
True

>> DM([1 2 3])'
[1, 2, 3]
>> MX.sym('x', 2, 3)'
x'
>> transpose(Sparsity.lower(2)) == Sparsity.upper(2)
1

julia> ca.DM([1 2 3])'
DM([1, 2, 3])
julia> ca.MX.sym("x", 2, 3)'
MX(x')
julia> ca.is_equal(ca.transpose(ca.Sparsity.lower(2)), ca.Sparsity.upper(2))
true

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

Definition at line 1532 of file sparsity_interface.hpp.

1532  {
1533  return X.T();
1534  }

◆ triangle

template<typename Scalar >
Matrix<Scalar> triangle ( const Matrix< Scalar > &  x)
friend

1 - |x| for |x| < 1 and 0 otherwise, built from rectangle and fabs. Not defined for MX.

>>> triangle(DM([-1, -0.5, 0, 0.5, 1]))
DM([0, 0.5, 1, 0.5, 0])

>> triangle(DM([-1; -0.5; 0; 0.5; 1]))
[0, 0.5, 1, 0.5, 0]

julia> ca.triangle(ca.DM([-1, -0.5, 0, 0.5, 1]))
DM([0, 0.5, 1, 0.5, 0])

See also
rectangle, ramp

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

Definition at line 2299 of file matrix_decl.hpp.

2299  {
2300  return Matrix<Scalar>::triangle(x);
2301  }
static Matrix< Scalar > triangle(const Matrix< Scalar > &x)

◆ tril

template<typename MatType >
MatType tril ( const MatType &  x,
bool  includeDiagonal = true 
)
friend

Entries above the diagonal become structural zeros; the matrix need not be square.

Parameters
xMatrix (or Sparsity); any shape
includeDiagonalFalse to drop the diagonal as well

>>> x = DM([[1, 2, 3], [4, 5, 6]])
>>> tril(x)
DM(
[[1, 00, 00],
 [4, 5, 00]])
>>> tril(x, False)
DM(
[[00, 00, 00],
 [4, 00, 00]])

>> x = DM([1 2 3; 4 5 6]);
>> tril(x)
[[1, 00, 00],
 [4, 5, 00]]
>> tril(x, false)
[[00, 00, 00],
 [4, 00, 00]]

julia> x = ca.DM([1 2 3; 4 5 6]);
julia> ca.tril(x)
DM(
[[1, 00, 00],
 [4, 5, 00]])
julia> ca.tril(x, false)
DM(
[[00, 00, 00],
 [4, 00, 00]])

See also
triu

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

Definition at line 2036 of file sparsity_interface.hpp.

2036  {
2037  return MatType::tril(x, includeDiagonal);
2038  }

◆ tril2symm

template<typename MatType >
MatType tril2symm ( const MatType &  a)
friend

Returns a + a' - diag(diag(a)), i.e. mirrors the strictly lower part to the upper part. The argument must be square and have no structural nonzeros above the diagonal (a dense matrix is rejected even if numerically lower triangular).

>>> L = DM(Sparsity.lower(3), [1, 2, 3, 4, 5, 6])
>>> tril2symm(L)
DM(
[[1, 2, 3],
 [2, 4, 5],
 [3, 5, 6]])
>>> tril2symm(SX.sym("L", Sparsity.lower(2)))
SX(
[[L_0, L_1],
 [L_1, L_2]])

>> L = DM(Sparsity.lower(3), [1; 2; 3; 4; 5; 6]);
>> tril2symm(L)
[[1, 2, 3],
 [2, 4, 5],
 [3, 5, 6]]
>> tril2symm(SX.sym('L', Sparsity.lower(2)))
[[L_0, L_1],
 [L_1, L_2]]

julia> L = ca.DM(ca.Sparsity.lower(3), [1, 2, 3, 4, 5, 6]);
julia> ca.tril2symm(L)
DM(
[[1, 2, 3],
 [2, 4, 5],
 [3, 5, 6]])
julia> ca.tril2symm(ca.SX.sym("L", ca.Sparsity.lower(2)))
SX(
[[L_0, L_1],
 [L_1, L_2]])

See also
triu2symm, tril, is_tril

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

Definition at line 1871 of file generic_matrix.hpp.

1871 { return MatType::tril2symm(a);}

◆ triu

template<typename MatType >
MatType triu ( const MatType &  x,
bool  includeDiagonal = true 
)
friend

Entries below the diagonal become structural zeros; the matrix need not be square. For MX this is a projection node.

Parameters
xMatrix (or Sparsity); any shape
includeDiagonalFalse to drop the diagonal as well

>>> x = DM([[1, 2, 3], [4, 5, 6]])
>>> triu(x)
DM(
[[1, 2, 3],
 [00, 5, 6]])
>>> triu(x, False)
DM(
[[00, 2, 3],
 [00, 00, 6]])
>>> triu(MX.sym("x", 2, 2))
MX(project(x))

>> x = DM([1 2 3; 4 5 6]);
>> triu(x)
[[1, 2, 3],
 [00, 5, 6]]
>> triu(x, false)
[[00, 2, 3],
 [00, 00, 6]]
>> triu(MX.sym('x', 2, 2))
project(x)

julia> x = ca.DM([1 2 3; 4 5 6]);
julia> ca.triu(x)
DM(
[[1, 2, 3],
 [00, 5, 6]])
julia> ca.triu(x, false)
DM(
[[00, 2, 3],
 [00, 00, 6]])
julia> ca.triu(ca.MX.sym("x", 2, 2))
MX(project(x))

See also
tril

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

Definition at line 1982 of file sparsity_interface.hpp.

1982  {
1983  return MatType::triu(x, includeDiagonal);
1984  }

◆ triu2symm

template<typename MatType >
MatType triu2symm ( const MatType &  a)
friend

Mirror image of tril2symm: the argument must be square without structural nonzeros below the diagonal.

>>> U = DM(Sparsity.upper(2), [1, 2, 3])
>>> triu2symm(U)
DM(
[[1, 2],
 [2, 3]])

>> U = DM(Sparsity.upper(2), [1; 2; 3]);
>> triu2symm(U)
[[1, 2],
 [2, 3]]

julia> U = ca.DM(ca.Sparsity.upper(2), [1, 2, 3]);
julia> ca.triu2symm(U)
DM(
[[1, 2],
 [2, 3]])

See also
tril2symm, triu, is_triu

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

Definition at line 1910 of file generic_matrix.hpp.

1910 { return MatType::triu2symm(a);}

◆ unite

template<typename MatType >
MatType unite ( const MatType &  A,
const MatType &  B 
)
friend

The result has the union of both patterns with the nonzeros of A and B at their positions. Both must have the same shape; a shared structural nonzero raises "Pattern intersection not empty".

>>> A = DM(Sparsity.upper(2), [1, 2, 3])
>>> B = DM(Sparsity.triplet(2, 2, [1], [0]), [4])
>>> unite(A, B)
DM(
[[1, 2],
 [4, 3]])
>>> try: unite(DM.eye(2), DM.eye(2))
... except Exception as e: print("error")
error

>> A = DM(Sparsity.upper(2), [1; 2; 3]);
>> B = DM(Sparsity.triplet(2, 2, 1, 0), 4);
>> unite(A, B)
[[1, 2],
 [4, 3]]
>> try, unite(DM.eye(2), DM.eye(2)); catch e, disp('error'); end
error

julia> A = ca.DM(ca.Sparsity.upper(2), [1, 2, 3]);
julia> B = ca.DM(ca.Sparsity.triplet(2, 2, [1], [0]), [4]);
julia> ca.unite(A, B)
DM(
[[1, 2],
 [4, 3]])
julia> try ca.unite(ca.DM.eye(2), ca.DM.eye(2)) catch e; println("error") end
error

See also
project, Sparsity::unite

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

Definition at line 2445 of file generic_matrix.hpp.

2445  {
2446  return MatType::unite(A, B);
2447  }

◆ vec

template<typename MatType >
MatType vec ( const MatType &  x)
friend

Stacks the columns of x on top of each other (column-major order), giving a column of length x.numel(). Same as reshape(x, x.numel(), 1); a column is returned unchanged. The sparsity pattern is carried along: no entries are densified.

a c
b d
turns into

a
b
c
d

>>> vec(DM([[1, 2], [3, 4]]))
DM([1, 3, 2, 4])
>>> vec(DM.eye(2))
DM([1, 00, 00, 1])
>>> vec(MX.sym("x", 1, 3))
MX(x')

>> vec(DM([1 2; 3 4]))
[1, 3, 2, 4]
>> vec(DM.eye(2))
[1, 00, 00, 1]
>> vec(MX.sym('x', 1, 3))
x'

julia> vec(ca.DM([1 2; 3 4]))
DM([1, 3, 2, 4])
julia> vec(ca.DM.eye(2))
DM([1, 00, 00, 1])
julia> vec(ca.MX.sym("x", 1, 3))
MX(x')

See also
veccat, reshape

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

Definition at line 1588 of file sparsity_interface.hpp.

1588  {
1589  return MatType::vec(x);
1590  }

◆ veccat

template<typename MatType >
MatType veccat ( const std::vector< MatType > &  x)
friend

Every argument is first turned into a column with vec (column-major), then the columns are stacked. Arguments can have any shape, so this is the common way to collect decision variables into one vector. With no arguments the result is a 0-by-1 matrix.

Parameters
xList of matrices of any shape
Returns
Column vector of the same type as the arguments

>>> veccat(DM([[1, 2], [3, 4]]), 5)
DM([1, 3, 2, 4, 5])
>>> veccat(MX.sym("x", 2, 2), MX.sym("y", 1, 3))
MX(vertcat(vec(x), y'))
>>> veccat().shape
(0, 1)

>> veccat(DM([1 2; 3 4]), 5)
[1, 3, 2, 4, 5]
>> veccat(MX.sym('x', 2, 2), MX.sym('y', 1, 3))
vertcat(vec(x), y')

julia> ca.veccat([ca.MX.sym("x", 2, 2), ca.MX.sym("y", 1, 3)])
MX(vertcat(vec(x), y'))
julia> [vec(ca.DM([1 2; 3 4])); 5]
DM([1, 3, 2, 4, 5])

See also
vec, vertcat

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

Definition at line 1287 of file sparsity_interface.hpp.

1287  {
1288  return MatType::veccat(x);
1289  }

◆ vertcat [1/6]

template<typename MatType >
MatType vertcat ( const MatType &  x,
const MatType &  y 
)
friend

Same as the list form of vertcat.

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

Definition at line 2253 of file sparsity_interface.hpp.

2253  {
2254  return vertcat(std::vector<MatType>{x, y});
2255  }

◆ vertcat [2/6]

template<typename MatType >
MatType vertcat ( const MatType &  x,
const MatType &  y,
const MatType &  z 
)
friend

Same as the list form of vertcat.

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

Definition at line 2262 of file sparsity_interface.hpp.

2262  {
2263  return vertcat(std::vector<MatType>{x, y, z});
2264  }

◆ vertcat [3/6]

template<typename MatType >
MatType vertcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w 
)
friend

Same as the list form of vertcat.

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

Definition at line 2271 of file sparsity_interface.hpp.

2272  {
2273  return vertcat(std::vector<MatType>{x, y, z, w});
2274  }

◆ vertcat [4/6]

template<typename MatType >
MatType vertcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w,
const MatType &  v 
)
friend

Same as the list form of vertcat.

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

Definition at line 2281 of file sparsity_interface.hpp.

2282  {
2283  return vertcat(std::vector<MatType>{x, y, z, w, v});
2284  }

◆ vertcat [5/6]

template<typename MatType >
MatType vertcat ( const MatType &  x,
const MatType &  y,
const MatType &  z,
const MatType &  w,
const MatType &  v,
const MatType &  u 
)
friend

Same as the list form of vertcat.

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

Definition at line 2291 of file sparsity_interface.hpp.

2292  {
2293  return vertcat(std::vector<MatType>{x, y, z, w, v, u});
2294  }

◆ vertcat [6/6]

template<typename MatType >
MatType vertcat ( const std::vector< MatType > &  v)
friend

Stacks the matrices on top of each other, in order: the result has the column count of the inputs and a row count equal to the sum of the row counts. All inputs must have the same number of columns; a 0-by-0 matrix is ignored. Type and sparsity rules are those of horzcat. With no arguments the result is a 0-by-1 matrix.

Column vectors stack cheaply; for MX, stacking matrices with more than one column is rewritten as a horizontal concatenation of the transposes.

Alternative terminology: vertical stack, vstack, vertical append, [a;b]. Inverse of vertsplit: vertcat(vertsplit(x, ...)) == x.

Parameters
vList of matrices with matching column count
Returns
Matrix with the blocks of v stacked vertically

>>> vertcat(SX.sym("x", 2), 3)
SX([x_0, x_1, 3])
>>> vertcat(DM([[1, 2]]), DM.eye(2))
DM(
[[1, 2],
 [1, 00],
 [00, 1]])
>>> vertcat(MX.sym("a", 2, 3), MX.sym("b", 1, 3))
MX(horzcat(a', b')')
>>> vertcat().shape
(0, 1)
>>> vertcat(MX.sym("a", 2, 2), MX.sym("b", 2, 3))
Traceback (most recent call last):
  ...
RuntimeError: ...vertcat dimension mismatch...

>> [SX.sym('x', 2); 3]
[x_0, x_1, 3]
>> vertcat(DM([1 2]), DM.eye(2))
[[1, 2],
 [1, 00],
 [00, 1]]
>> [MX.sym('a', 2, 3); MX.sym('b', 1, 3)]
horzcat(a', b')'
>> try, [MX.sym('a', 2, 2); MX.sym('b', 2, 3)]; catch e, disp(e.message); end
...vertcat dimension mismatch...

julia> [ca.SX.sym("x", 2); 3]
SX([x_0, x_1, 3])
julia> [ca.DM([1 2]); ca.DM.eye(2)]
DM(
[[1, 2],
 [1, 00],
 [00, 1]])
julia> print([ca.MX.sym("a", 2, 3); ca.MX.sym("b", 1, 3)])
horzcat(a', b')'
julia> try
           [ca.MX.sym("a", 2, 2); ca.MX.sym("b", 2, 3)]
       catch err
           println(occursin("vertcat dimension mismatch", sprint(showerror, err)))
       end
false

See also
horzcat, diagcat, veccat, vertsplit

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

Definition at line 249 of file sparsity_interface.hpp.

249  {
250  return MatType::vertcat(v);
251  }

◆ vertsplit [1/2]

template<typename MatType >
std::vector<MatType > vertsplit ( const MatType &  x,
casadi_int  incr = 1 
)
friend

Returns the consecutive row blocks of x, each with incr rows, in order. The last block is smaller when the row count is not a multiple of incr. A matrix without rows gives an empty list. Each block carries the corresponding part of the sparsity pattern. Inverse of vertcat: vertcat(vertsplit(x, incr)) == x.

Parameters
xMatrix (or Sparsity) to split; any shape
incrNumber of rows per block, must be positive
Returns
List of matrices of the same type as x

>>> vertsplit(SX.sym("a", 4))
[SX(a_0), SX(a_1), SX(a_2), SX(a_3)]
>>> vertsplit(SX.sym("a", 4), 2)
[SX([a_0, a_1]), SX([a_2, a_3])]
>>> vertsplit(DM([0, 1, 2, 3, 4]), 2)
[DM([0, 1]), DM([2, 3]), DM(4)]
>>> vertsplit(MX.sym("x", 4), 2)
[MX(vertsplit(x){0}), MX(vertsplit(x){1})]
>>> vertsplit(MX.sym("x", 0, 2))
[]

>> v = vertsplit(SX.sym('a', 4)); disp(numel(v)), disp(v{1}), disp(v{4})
4
a_0
a_3
>> v = vertsplit(SX.sym('a', 4), 2); disp(v{1}), disp(v{2})
[a_0, a_1]
[a_2, a_3]
>> v = vertsplit(DM([0; 1; 2; 3; 4]), 2); disp(numel(v)), disp(v{3})
3
4
>> v = vertsplit(MX.sym('x', 4), 2); disp(v{1}), disp(v{2})
vertsplit(x){0}
vertsplit(x){1}
>> v = vertsplit(MX.sym('x', 0, 2)); disp(numel(v))
0

julia> ca.vertsplit(ca.SX.sym("a", 4))
4-element Vector{SX}:
 SX(a_0)
 SX(a_1)
 SX(a_2)
 SX(a_3)
julia> ca.vertsplit(ca.SX.sym("a", 4), 2)
2-element Vector{SX}:
 SX([a_0, a_1])
 SX([a_2, a_3])
julia> ca.vertsplit(ca.DM([0, 1, 2, 3, 4]), 2)
3-element Vector{DM}:
 DM([0, 1])
 DM([2, 3])
 DM(4)
julia> ca.vertsplit(ca.MX.sym("x", 4), 2)
2-element Vector{MX}:
 MX(vertsplit(x){0})
 MX(vertsplit(x){1})
julia> ca.vertsplit(ca.MX.sym("x", 0, 2))
MX[]

See also
vertsplit_n, horzsplit

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

Definition at line 663 of file sparsity_interface.hpp.

663  {
664  return MatType::vertsplit(x, incr);
665  }

◆ vertsplit [2/2]

template<typename MatType >
std::vector<MatType > vertsplit ( const MatType &  x,
const std::vector< casadi_int > &  offset 
)
friend

Returns the row blocks x[offset[i]:offset[i+1], :], in order. The offsets must start at 0, be non-decreasing and end at the number of rows of x, otherwise an error is raised. Each block carries the corresponding part of the sparsity pattern. The offsets of a list of matrices that were stacked with vertcat can be recovered with offset. Inverse of vertcat: vertcat(vertsplit(x, offset)) == x.

Parameters
xMatrix (or Sparsity) to split; any shape
offsetStart row of every block followed by the row count of x
Returns
List of matrices of the same type as x, one per block

>>> x = SX.sym("x", 5)
>>> vertsplit(x, [0, 2, 5])
[SX([x_0, x_1]), SX([x_2, x_3, x_4])]
>>> parts = [DM.ones(2, 1), DM.ones(3, 1)]
>>> vertsplit(x, offset(parts))
[SX([x_0, x_1]), SX([x_2, x_3, x_4])]
>>> vertsplit(DM([[1, 2], [3, 4]]), [0, 1, 2])
[DM([[1, 2]]), DM([[3, 4]])]

>> x = SX.sym('x', 5);
>> v = vertsplit(x, [0, 2, 5]); disp(numel(v)), disp(v{1}), disp(v{2})
2
[x_0, x_1]
[x_2, x_3, x_4]
>> v = vertsplit(DM([1 2; 3 4]), [0, 1, 2]); disp(v{1}), disp(v{2})
[[1, 2]]
[[3, 4]]

julia> x = ca.SX.sym("x", 5);
julia> ca.vertsplit(x, [0, 2, 5])
2-element Vector{SX}:
 SX([x_0, x_1])
 SX([x_2, x_3, x_4])
julia> parts = [ca.DM.ones(2, 1), ca.DM.ones(3, 1)];
julia> ca.vertsplit(x, ca.offset(parts))
2-element Vector{SX}:
 SX([x_0, x_1])
 SX([x_2, x_3, x_4])
julia> ca.vertsplit(ca.DM([1 2; 3 4]), [0, 1, 2])
2-element Vector{DM}:
 DM([[1, 2]])
 DM([[3, 4]])

See also
vertsplit_n, horzsplit, offset

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

Definition at line 537 of file sparsity_interface.hpp.

537  {
538  return MatType::vertsplit(x, offset);
539  }

◆ vertsplit_n

template<typename MatType >
std::vector<MatType > vertsplit_n ( const MatType &  x,
casadi_int  n 
)
friend

Returns n row blocks of equal height, in order. An error is raised when the row count of x is not a multiple of n or n is negative. A matrix without rows gives n copies of itself. Inverse of vertcat: vertcat(vertsplit_n(x, n)) == x.

Parameters
xMatrix (or Sparsity) to split; any shape
nNumber of blocks
Returns
List of n matrices of the same type as x

>>> vertsplit_n(DM([1, 2, 3, 4]), 2)
[DM([1, 2]), DM([3, 4])]
>>> vertsplit_n(DM([1, 2, 3]), 2)
Traceback (most recent call last):
  ...
RuntimeError: ...must be a multiple of n...

>> v = vertsplit_n(DM([1; 2; 3; 4]), 2); disp(v{1}), disp(v{2})
[1, 2]
[3, 4]
>> try, vertsplit_n(DM([1; 2; 3]), 2); catch e, disp(e.message); end
...must be a multiple of n...

julia> ca.vertsplit_n(ca.DM([1, 2, 3, 4]), 2)
2-element Vector{DM}:
 DM([1, 2])
 DM([3, 4])
julia> try
           ca.vertsplit_n(ca.DM([1, 2, 3]), 2)
       catch err
           println(occursin("must be a multiple of n", sprint(showerror, err)))
       end
true

See also
vertsplit, horzsplit_n

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

Definition at line 717 of file sparsity_interface.hpp.

717  {
718  return MatType::vertsplit_n(x, n);
719  }

◆ which_depends

template<typename MatType >
std::vector<bool> which_depends ( const MatType &  expr,
const MatType &  var,
casadi_int  order,
bool  tr 
)
friend

Structural dependency check by sparsity propagation, without forming derivatives. With order 1, an entry depends if the corresponding Jacobian entry is structurally nonzero; with order 2, if it enters nonlinearly (second derivative structurally nonzero). Operations whose derivative is structurally zero, such as floor or sign, count as not depending.

Parameters
exprExpression
varSymbolic expression
order1 or 2; other values raise an error
trWhen false, the result has one entry per element of var: does any element of expr depend on it. When true, one entry per element of expr: does it depend on any element of var.
Returns
List of booleans

>>> x = SX.sym("x"); y = SX.sym("y")
>>> e = vertcat(x*y, x**2, 3*x)
>>> which_depends(e, vertcat(x, y), 1, False)
[True, True]
>>> which_depends(e, vertcat(x, y), 1, True)
[True, True, True]
>>> which_depends(e, vertcat(x, y), 2, True)
[True, True, False]

>> x = SX.sym('x'); y = SX.sym('y');
>> e = vertcat(x*y, x^2, 3*x);
>> which_depends(e, vertcat(x, y), 1, false)
1 1
>> which_depends(e, vertcat(x, y), 1, true)
1 1 1
>> which_depends(e, vertcat(x, y), 2, true)
1 1 0

julia> x = ca.SX.sym("x"); y = ca.SX.sym("y");
julia> e = [x * y; x^2; 3 * x];
julia> ca.which_depends(e, [x; y], 1, false)
2-element Vector{Bool}:
 1
 1
julia> ca.which_depends(e, [x; y], 1, true)
3-element Vector{Bool}:
 1
 1
 1
julia> ca.which_depends(e, [x; y], 2, true)
3-element Vector{Bool}:
 1
 1
 0

See also
depends_on, is_linear, jacobian_sparsity

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

Definition at line 3927 of file generic_matrix.hpp.

3928  {
3929  return MatType::which_depends(expr, var, order, tr);
3930  }