Functions | Friends
Expression_tools

Functions

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

Friends

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::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...
 
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 >::bilin (const MatType &A, const MatType &x, const MatType &y)
 Calculate bilinear/quadratic form x^T A y. 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 >::rank1 (const MatType &A, const MatType &alpha, const MatType &x, const MatType &y)
 Make a rank-1 update to a matrix A. 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...
 
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)
 

Detailed Description

Function Documentation

◆ 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

◆ 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

◆ 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

◆ 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

◆ logsumexp()

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

◆ mpower()

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

Functions called by friend functions defined here

◆ 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

◆ soc()

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

Functions called by friend functions defined here

◆ 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

Friends

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

◆ cross

template<typename MatType >
MatType casadi::GenericMatrix< 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.

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

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

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

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

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

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

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

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

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

◆ diff

template<typename MatType >
MatType casadi::GenericMatrix< 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

◆ interp1d

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

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

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

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

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

◆ inv_skew

template<typename MatType >
MatType casadi::GenericMatrix< 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.

◆ is_linear

template<typename MatType >
bool casadi::GenericMatrix< MatType >::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.

◆ is_quadratic

template<typename MatType >
bool casadi::GenericMatrix< MatType >::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.

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

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

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

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

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

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

◆ linear_coeff

template<typename MatType >
void casadi::GenericMatrix< MatType >::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.

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

◆ linspace

template<typename MatType >
MatType casadi::GenericMatrix< 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

◆ quadratic_coeff

template<typename MatType >
void casadi::GenericMatrix< MatType >::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.

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

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

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

◆ repsum

template<typename MatType >
MatType casadi::GenericMatrix< 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.

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

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

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

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

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

◆ skew

template<typename MatType >
MatType casadi::GenericMatrix< 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

◆ tril2symm

template<typename MatType >
MatType casadi::GenericMatrix< 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.

◆ triu2symm

template<typename MatType >
MatType casadi::GenericMatrix< 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.

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

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