14.2.11 Class Expr

mosek::fusion::Expr

It represents an expression of the form \(Ax+b\), where \(A\) is a matrix on sparse form, \(x\) is a variable vector and \(b\) is a vector of scalars.

Additionally, the class defines a set of static methods for constructing and manipulating various expressions.

Members:
 

Expr.eval – Evaluate the expression info to a simple array-based form.

Expr.getModel – Return the model to which the expression belongs

Expr.getShape – Return the shape of the expression

Expr.index – Return a specific term of the expression.

Expr.numNonzeros – Return the number of non-zero elements in the expression.

Expr.pick – Pick elements from this expression.

Expr.shape – Get the shape of the expression.

Expr.size – Return the expression size.

Expr.slice – Return a slice of the expression.

Expr.toString – Create a human readable string representation of the expression.

Expr.transpose – Transpose the expression.

Static members:
 

Expr.add – Compute the sum of expressions.

Expr.constTerm – Create an expression consisting of a constant vector of values.

Expr.dot – Return a scalar expression object representing the dot-product of two items.

Expr.flatten – Reshape the expression into a vector.

Expr.hstack – Stack a list of expressions horizontally (i.e. along the second dimension).

Expr.mul – Multiply two items.

Expr.mulDiag – Compute the diagonal of the product of two matrices.

Expr.mulElm – Element-wise product of two items.

Expr.neg – Change the sign of an expression

Expr.ones – Create a vector of ones as an expression.

Expr.outer – Return the outer-product of two vectors.

Expr.repeat – Repeat an expression a number of times in the given dimension.

Expr.reshape – Reshape the expression into a different shape with the same number of elements.

Expr.stack – Stack a list of expressions in an arbitrary dimension.

Expr.sub – Compute the difference of two expressions.

Expr.sum – Sum the elements of an expression.

Expr.vstack – Stack a list of expressions vertically (i.e. along the first dimension).

Expr.zeros – Create a vector of zeros as an expression.

Expr.add
Expression::t Expr::add(Expression::t e1, Expression::t e2)
Expression::t Expr::add(Expression::t e1, Variable::t v2)
Expression::t Expr::add(Variable::t v1, Expression::t e2)
Expression::t Expr::add(Expression::t e1, shared_ptr<ndarray<double,1>> a1)
Expression::t Expr::add(Expression::t e1, shared_ptr<ndarray<double,2>> a2)
Expression::t Expr::add(shared_ptr<ndarray<double,1>> a1, Expression::t e2)
Expression::t Expr::add(shared_ptr<ndarray<double,2>> a2, Expression::t e2)
Expression::t Expr::add(Expression::t e1, double c)
Expression::t Expr::add(double c, Expression::t e2)
Expression::t Expr::add(Expression::t e1, Matrix::t m)
Expression::t Expr::add(Matrix::t m, Expression::t e2)
Expression::t Expr::add(Expression::t e1, NDSparseArray::t n)
Expression::t Expr::add(NDSparseArray::t n, Expression::t e2)
Expression::t Expr::add(Variable::t v1, Variable::t v2)
Expression::t Expr::add(Variable::t v1, shared_ptr<ndarray<double,1>> a1)
Expression::t Expr::add(Variable::t v1, shared_ptr<ndarray<double,2>> a2)
Expression::t Expr::add(shared_ptr<ndarray<double,1>> a1, Variable::t v2)
Expression::t Expr::add(shared_ptr<ndarray<double,2>> a2, Variable::t v2)
Expression::t Expr::add(Variable::t v1, double c)
Expression::t Expr::add(double c, Variable::t v2)
Expression::t Expr::add(Variable::t v1, Matrix::t m)
Expression::t Expr::add(Matrix::t m, Variable::t v2)
Expression::t Expr::add(Variable::t v1, NDSparseArray::t n)
Expression::t Expr::add(NDSparseArray::t n, Variable::t v2)
Expression::t Expr::add(shared_ptr<ndarray<Variable::t,1>> vs)
Expression::t Expr::add(shared_ptr<ndarray<Expression::t,1>> exps)

Computes the sum of two or more expressions or variables. The following types of arguments are allowed:

A B
Variable Variable
Expression Expression
double  
double[]  
double[,]  
Matrix  
NDSparseArray  

By symmetry both add(A,B) and add(B,A) are available.

The arguments must have the same shapes and the returned expression also has that shape. If one of the arguments is a single scalar, it is promoted to the shape that matches the shape of the other argument, i.e. the scalar is added to all entries of the other argument.

Parameters:
 
  • e1 (Expression) – An expression.
  • e2 (Expression) – An expression.
  • v2 (Variable) – A variable.
  • v1 (Variable) – A variable.
  • a1 (double[]) – A one-dimensional array of constants.
  • a2 (double[][]) – A two-dimensional array of constants.
  • c (double) – A constant.
  • m (Matrix) – A Matrix object.
  • n (NDSparseArray) – An NDSparseArray object.
  • vs (Variable[]) – A list of variables. All variables in the array must have the same shape. The list must contain at least one element.
  • exps (Expression[]) – A list of expressions. All expressions in the array must have the same shape. The list must contain at least one element.
Return:
 

(Expression)

Expr.constTerm
Expression::t Expr::constTerm(shared_ptr<ndarray<double,1>> vals1)
Expression::t Expr::constTerm(shared_ptr<ndarray<double,2>> vals2)
Expression::t Expr::constTerm(int size, double val)
Expression::t Expr::constTerm(Set::t shp, double val)
Expression::t Expr::constTerm(double val)
Expression::t Expr::constTerm(Matrix::t m)
Expression::t Expr::constTerm(NDSparseArray::t nda)

Create an expression consisting of a constant vector of values.

Parameters:
 
  • vals1 (double[]) – A vector initializing the expression.
  • vals2 (double[][]) – A two-dimensional array initializing the expression.
  • size (int) – Length of the vector to be constructed.
  • val (double) – A scalar value to be repeated in all entries of the expression.
  • shp (Set) – Defines the shape of the expression.
  • m (Matrix) – A Matrix of values initializing the expression.
  • nda (NDSparseArray) – An multi-dimensional sparse array initializing the expression.
Return:
 

(Expression)

Expr.dot
Expression::t Expr::dot(Variable::t v, shared_ptr<ndarray<double,1>> a1)
Expression::t Expr::dot(Variable::t v, shared_ptr<ndarray<double,2>> a2)
Expression::t Expr::dot(Variable::t v, Matrix::t m)
Expression::t Expr::dot(Variable::t v, NDSparseArray::t spm)
Expression::t Expr::dot(Expression::t expr, NDSparseArray::t spm)
Expression::t Expr::dot(Expression::t expr, shared_ptr<ndarray<double,1>> a1)
Expression::t Expr::dot(Expression::t expr, shared_ptr<ndarray<double,2>> a2)
Expression::t Expr::dot(Expression::t expr, Matrix::t m)
Expression::t Expr::dot(shared_ptr<ndarray<double,1>> a1, Expression::t expr)
Expression::t Expr::dot(shared_ptr<ndarray<double,1>> a1, Variable::t v)
Expression::t Expr::dot(shared_ptr<ndarray<double,2>> a2, Expression::t expr)
Expression::t Expr::dot(shared_ptr<ndarray<double,2>> a2, Variable::t v)
Expression::t Expr::dot(NDSparseArray::t spm, Expression::t expr)
Expression::t Expr::dot(NDSparseArray::t spm, Variable::t v)
Expression::t Expr::dot(Matrix::t m, Variable::t v)
Expression::t Expr::dot(Matrix::t m, Expression::t expr)

Return an object representing the inner product (dot product) \(x^Ty = \sum_{i=1}^nx_iy_i\) of two objects \(x,y\) of size \(n\).

Both arguments must have the same length when flattened. In particular, they can be two vectors of the same length or two matrices of the same shape.

Parameters:
 
  • v (Variable) – A variable object.
  • a1 (double[]) – A one-dimensional coefficient array.
  • a2 (double[][]) – A two-dimensional coefficient array.
  • m (Matrix) – A matrix object.
  • spm (NDSparseArray) – A multidimensional sparse array object.
  • expr (Expression) – An expression object.
Return:
 

(Expression)

Expr.eval
FlatExpr::t eval()

Evaluate the expression info to a simple array-based form.

Return:
  (FlatExpr)
Expr.flatten
Expression::t Expr::flatten(Expression::t e)

Reshape the expression into a vector.

Parameters:
 e (Expression)
Return:
  (Expression)
Expr.getModel
Model::t getModel()

Return the model to which the expression belongs

Return:
  (Model)
Expr.getShape
Set::t getShape()

Return the shape of the expression

Return:
  (Set)
Expr.hstack
Expression::t Expr::hstack(shared_ptr<ndarray<Expression::t,1>> exprs)
Expression::t Expr::hstack(Expression::t e1, Expression::t e2)
Expression::t Expr::hstack(Expression::t e1, double a2)
Expression::t Expr::hstack(Expression::t e1, Variable::t v2)
Expression::t Expr::hstack(double a1, Variable::t v2)
Expression::t Expr::hstack(double a1, Expression::t e2)
Expression::t Expr::hstack(Variable::t v1, double a2)
Expression::t Expr::hstack(Variable::t v1, Variable::t v2)
Expression::t Expr::hstack(Variable::t v1, Expression::t e2)
Expression::t Expr::hstack(double a1, double a2, Variable::t v3)
Expression::t Expr::hstack(double a1, double a2, Expression::t e3)
Expression::t Expr::hstack(double a1, Variable::t v2, double a3)
Expression::t Expr::hstack(double a1, Variable::t v2, Variable::t v3)
Expression::t Expr::hstack(double a1, Variable::t v2, Expression::t e3)
Expression::t Expr::hstack(double a1, Expression::t e2, double a3)
Expression::t Expr::hstack(double a1, Expression::t e2, Variable::t v3)
Expression::t Expr::hstack(double a1, Expression::t e2, Expression::t e3)
Expression::t Expr::hstack(Variable::t v1, double a2, double a3)
Expression::t Expr::hstack(Variable::t v1, double a2, Variable::t v3)
Expression::t Expr::hstack(Variable::t v1, double a2, Expression::t e3)
Expression::t Expr::hstack(Variable::t v1, Variable::t v2, double a3)
Expression::t Expr::hstack(Variable::t v1, Variable::t v2, Variable::t v3)
Expression::t Expr::hstack(Variable::t v1, Variable::t v2, Expression::t e3)
Expression::t Expr::hstack(Variable::t v1, Expression::t e2, double a3)
Expression::t Expr::hstack(Variable::t v1, Expression::t e2, Variable::t v3)
Expression::t Expr::hstack(Variable::t v1, Expression::t e2, Expression::t e3)
Expression::t Expr::hstack(Expression::t e1, double a2, double a3)
Expression::t Expr::hstack(Expression::t e1, double a2, Variable::t v3)
Expression::t Expr::hstack(Expression::t e1, double a2, Expression::t e3)
Expression::t Expr::hstack(Expression::t e1, Variable::t v2, double a3)
Expression::t Expr::hstack(Expression::t e1, Variable::t v2, Variable::t v3)
Expression::t Expr::hstack(Expression::t e1, Variable::t v2, Expression::t e3)
Expression::t Expr::hstack(Expression::t e1, Expression::t e2, double a3)
Expression::t Expr::hstack(Expression::t e1, Expression::t e2, Variable::t v3)
Expression::t Expr::hstack(Expression::t e1, Expression::t e2, Expression::t e3)

Stack a list of expressions horizontally (i.e. along the second dimension). The expressions must have the same shape, except for the second dimension. The arguments may be any combination of expressions, scalar constants and variables.

For example, if \(x^1,x^2,x^3\) are three vectors of length \(n\) then their horizontal stack is the matrix

\[\begin{split}\left[ \begin{array}{ccc} | & | & | \\ x^1 & x^2 & x^3 \\ | & | & | \end{array}\right]\end{split}\]

of shape (n,3).

Parameters:
 
  • exprs (Expression[]) – A list of expressions.
  • e1 (Expression) – An expression.
  • e2 (Expression) – An expression.
  • a2 (double) – A scalar constant.
  • v2 (Variable) – A variable.
  • a1 (double) – A scalar constant.
  • v1 (Variable) – A variable.
  • v3 (Variable) – A variable.
  • e3 (Expression) – An expression.
  • a3 (double) – A scalar constant.
Return:
 

(Expression)

Expr.index
Expression::t index(int first)
Expression::t index(shared_ptr<ndarray<int,1>> firsta)

Return a specific term of the expression.

Parameters:
 
  • first (int) – The index of the term in a one-dimensional expression.
  • firsta (int[]) – The indices of the term in a multi-dimensional expression.
Return:
 

(Expression)

Expr.mul
Expression::t Expr::mul(Matrix::t mx, Variable::t v)
Expression::t Expr::mul(Variable::t v, Matrix::t mx)
Expression::t Expr::mul(Variable::t v, shared_ptr<ndarray<double,1>> vals)
Expression::t Expr::mul(shared_ptr<ndarray<double,1>> vals, Variable::t v)
Expression::t Expr::mul(double val, Variable::t v)
Expression::t Expr::mul(Variable::t v, double val)
Expression::t Expr::mul(shared_ptr<ndarray<double,2>> vals2, Variable::t v)
Expression::t Expr::mul(Variable::t v, shared_ptr<ndarray<double,2>> vals2)
Expression::t Expr::mul(Expression::t expr, double val)
Expression::t Expr::mul(double val, Expression::t expr)
Expression::t Expr::mul(shared_ptr<ndarray<double,1>> vals, Expression::t expr)
Expression::t Expr::mul(Expression::t expr, shared_ptr<ndarray<double,1>> vals)
Expression::t Expr::mul(Expression::t expr, Matrix::t mx)
Expression::t Expr::mul(Matrix::t mx, Expression::t expr)

Compute the product (in the sense of matrix multiplication or scalar-by-matrix multiplication) of two arguments.

The operands must be at most two-dimensional. One of the arguments must be a constant, a vector of constants or a matrix of constants. The other argument can be a variable or expression. This allows to produce matrix expressions where the entries are linear combinations of variables.

The size and shape of the arguments must adhere to the rules of linear algebra.

Parameters:
 
  • mx (Matrix) – A matrix.
  • v (Variable) – A variable.
  • vals (double[]) – A vector of scalars.
  • val (double) – A scalar value.
  • vals2 (double[][]) – An array of scalars.
  • expr (Expression) – An expression.
Return:
 

(Expression)

Expr.mulDiag
Expression::t Expr::mulDiag(shared_ptr<ndarray<double,2>> a, Expression::t expr)
Expression::t Expr::mulDiag(Expression::t expr, shared_ptr<ndarray<double,2>> a)
Expression::t Expr::mulDiag(shared_ptr<ndarray<double,2>> a, Variable::t v)
Expression::t Expr::mulDiag(Variable::t v, shared_ptr<ndarray<double,2>> a)
Expression::t Expr::mulDiag(Matrix::t mx, Expression::t expr)
Expression::t Expr::mulDiag(Expression::t expr, Matrix::t mx)
Expression::t Expr::mulDiag(Matrix::t mx, Variable::t v)
Expression::t Expr::mulDiag(Variable::t v, Matrix::t mx)

Compute the diagonal of the product of two matrices. If \(A\in\mathbb{M}(m,n)\) and \(B\in\mathbb{M}(n,p)\), the result is a vector expression of length \(n\) equal to \(\diag(AB)\).

Parameters:
 
  • a (double[][]) – A constant matrix.
  • expr (Expression) – An expression object.
  • v (Variable) – A variable object.
  • mx (Matrix) – A matrix object.
Return:
 

(Expression)

Expr.mulElm
Expression::t Expr::mulElm(Variable::t v, shared_ptr<ndarray<double,1>> a1)
Expression::t Expr::mulElm(Variable::t v, shared_ptr<ndarray<double,2>> a2)
Expression::t Expr::mulElm(Variable::t v, NDSparseArray::t spm)
Expression::t Expr::mulElm(Variable::t v, Matrix::t m)
Expression::t Expr::mulElm(Expression::t expr, NDSparseArray::t spm)
Expression::t Expr::mulElm(Expression::t expr, shared_ptr<ndarray<double,1>> a1)
Expression::t Expr::mulElm(Expression::t expr, shared_ptr<ndarray<double,2>> a2)
Expression::t Expr::mulElm(Expression::t expr, Matrix::t m)
Expression::t Expr::mulElm(shared_ptr<ndarray<double,1>> a1, Expression::t expr)
Expression::t Expr::mulElm(shared_ptr<ndarray<double,1>> a1, Variable::t v)
Expression::t Expr::mulElm(shared_ptr<ndarray<double,2>> a2, Expression::t expr)
Expression::t Expr::mulElm(shared_ptr<ndarray<double,2>> a2, Variable::t v)
Expression::t Expr::mulElm(NDSparseArray::t spm, Expression::t expr)
Expression::t Expr::mulElm(NDSparseArray::t spm, Variable::t v)
Expression::t Expr::mulElm(Matrix::t m, Variable::t v)
Expression::t Expr::mulElm(Matrix::t m, Expression::t expr)

Returns the element-wise product of two items. The two operands must have the same shape and the returned expression also has this shape.

Parameters:
 
  • v (Variable) – A variable object.
  • a1 (double[]) – A one-dimensional coefficient array.
  • a2 (double[][]) – A two-dimensional coefficient array.
  • spm (NDSparseArray) – A multidimensional sparse array object.
  • m (Matrix) – A matrix object.
  • expr (Expression) – An expression object.
Return:
 

(Expression)

Expr.neg
Expression::t Expr::neg(Expression::t e)
Expression::t Expr::neg(Variable::t v)

Return a new expression object representing the given one with opposite sign.

Parameters:
 
Return:
 

(Expression)

Expr.numNonzeros
long long numNonzeros()

Return the number of non-zero elements in the expression.

Return:
  (long long)
Expr.ones
Expression::t Expr::ones(int num)

Create a vector of ones as an expression.

Parameters:
 num (int) – The size of the expression.
Return:
  (Expression)
Expr.outer
Expression::t Expr::outer(Variable::t v, shared_ptr<ndarray<double,1>> a)
Expression::t Expr::outer(shared_ptr<ndarray<double,1>> a, Variable::t v)
Expression::t Expr::outer(Variable::t v, Matrix::t m)
Expression::t Expr::outer(Matrix::t m, Variable::t v)
Expression::t Expr::outer(Expression::t e, shared_ptr<ndarray<double,1>> a)
Expression::t Expr::outer(shared_ptr<ndarray<double,1>> a, Expression::t e)

Return an expression representing the outer product \(xy^T\) of two vectors \(x,y\). If \(x\) has length k and \(y\) has length n then the result is of shape (k,n).

Parameters:
 
  • v (Variable) – A vector variable.
  • a (double[]) – A vector of constants.
  • m (Matrix) – A one-dimensional matrix.
  • e (Expression) – A vector expression.
Return:
 

(Expression)

Expr.pick
Expression::t pick(shared_ptr<ndarray<int,1>> indexes)
Expression::t pick(shared_ptr<ndarray<int,2>> indexrows)

Creates a vector expression by picking elements from the current expression.

Parameters:
 
  • indexes (int[]) – A list of indices specifying positions in a one-dimensional expression.
  • indexrows (int[][]) – A \(n\times m\) array of integers where each row specifies an \(m\)-dimensional index to pick from an \(m\)-dimensional expression.
Return:
 

(Expression)

Expr.repeat
Expression::t Expr::repeat(Expression::t e, int n, int d)

Repeat an expression a number of times in the given dimension. This is equivalent to stacking \(n\) copies of the expression in dimension \(d\); see Expr.stack.

Parameters:
 
  • e (Expression) – The expression to repeat.
  • n (int) – Number of times to repeat. Must be strictly positive.
  • d (int) – The dimension in which to repeat. Must define a valid dimension index.
Return:
 

(Expression)

Expr.reshape
Expression::t Expr::reshape(Expression::t e, Set::t shp)
Expression::t Expr::reshape(Expression::t e, int size)
Expression::t Expr::reshape(Expression::t e, int dimi, int dimj)

Reshape the expression into a different shape with the same number of elements.

Parameters:
 
  • e (Expression) – The expression to reshape.
  • shp (Set) – The new shape of the expression; this must have the same total size as the old shape.
  • size (int) – Reshape into a one-dimensional expression of this size.
  • dimi (int) – The first dimension size.
  • dimj (int) – The second dimension size.
Return:
 

(Expression)

Expr.shape
Set::t shape()

Get the shape of the expression.

Return:
  (Set)
Expr.size
long long size()

Return the expression size, i.e. the product of the lengths along each dimension.

Return:
  (long long)
Expr.slice
Expression::t slice(int first, int last)
Expression::t slice(shared_ptr<ndarray<int,1>> firsta, shared_ptr<ndarray<int,1>> lasta)

Return a slice of the expression.

Parameters:
 
  • first (int) – The index from which the slice begins.
  • last (int) – The index after the last element of the slice.
  • firsta (int[]) – The indices from which the slice of a multidimensional expression begins.
  • lasta (int[]) – The indices after the last element of slice of a multidimensional expression.
Return:
 

(Expression)

Expr.stack
Expression::t Expr::stack(int dim, shared_ptr<ndarray<Expression::t,1>> exprs)
Expression::t Expr::stack(int dim, Expression::t e1, Expression::t e2)
Expression::t Expr::stack(int dim, Expression::t e1, double a2)
Expression::t Expr::stack(int dim, Expression::t e1, Variable::t v2)
Expression::t Expr::stack(int dim, double a1, Variable::t v2)
Expression::t Expr::stack(int dim, double a1, Expression::t e2)
Expression::t Expr::stack(int dim, Variable::t v1, double a2)
Expression::t Expr::stack(int dim, Variable::t v1, Variable::t v2)
Expression::t Expr::stack(int dim, Variable::t v1, Expression::t e2)
Expression::t Expr::stack(int dim, double a1, double a2, Variable::t v1)
Expression::t Expr::stack(int dim, double a1, double a2, Expression::t e1)
Expression::t Expr::stack(int dim, double a1, Variable::t v2, double a3)
Expression::t Expr::stack(int dim, double a1, Variable::t v2, Variable::t v3)
Expression::t Expr::stack(int dim, double a1, Variable::t v2, Expression::t e3)
Expression::t Expr::stack(int dim, double a1, Expression::t e2, double a3)
Expression::t Expr::stack(int dim, double a1, Expression::t e2, Variable::t v3)
Expression::t Expr::stack(int dim, double a1, Expression::t e2, Expression::t e3)
Expression::t Expr::stack(int dim, Variable::t v1, double a2, double a3)
Expression::t Expr::stack(int dim, Variable::t v1, double a2, Variable::t v3)
Expression::t Expr::stack(int dim, Variable::t v1, double a2, Expression::t e3)
Expression::t Expr::stack(int dim, Variable::t v1, Variable::t v2, double a3)
Expression::t Expr::stack(int dim, Variable::t v1, Variable::t v2, Variable::t v3)
Expression::t Expr::stack(int dim, Variable::t v1, Variable::t v2, Expression::t e3)
Expression::t Expr::stack(int dim, Variable::t v1, Expression::t e2, double a3)
Expression::t Expr::stack(int dim, Variable::t v1, Expression::t e2, Variable::t v3)
Expression::t Expr::stack(int dim, Variable::t v1, Expression::t e2, Expression::t e3)
Expression::t Expr::stack(int dim, Expression::t e1, double a2, double a3)
Expression::t Expr::stack(int dim, Expression::t e1, double a2, Variable::t v3)
Expression::t Expr::stack(int dim, Expression::t e1, double a2, Expression::t e3)
Expression::t Expr::stack(int dim, Expression::t e1, Variable::t v2, double a3)
Expression::t Expr::stack(int dim, Expression::t e1, Variable::t v2, Variable::t v3)
Expression::t Expr::stack(int dim, Expression::t e1, Variable::t v2, Expression::t e3)
Expression::t Expr::stack(int dim, Expression::t e1, Expression::t e2, double a3)
Expression::t Expr::stack(int dim, Expression::t e1, Expression::t e2, Variable::t v3)
Expression::t Expr::stack(int dim, Expression::t e1, Expression::t e2, Expression::t e3)
Expression::t Expr::stack(shared_ptr<ndarray<Expression::t,2>> exprs)

Stack a list of expressions along an arbitrary dimension. All expressions must have the same shape, except for dimension dim. The arguments may be any combination of expressions, scalar constants and variables.

For example, suppose \(A,B\) are two \(n\times m\) matrices. Then stacking them in the first dimension produces a matrix of shape (2n,m):

\[\begin{split}\left[\begin{array}{c}A\\ B\end{array}\right],\end{split}\]

stacking them in the second dimension produces a matrix of shape (n,2m):

\[\left[\begin{array}{cc}A & B\end{array}\right],\]

and stacking them in the third dimension produces a three-dimensional array of shape (n,m,2).

The version which takes a two-dimensional array of expressions constructs a block matrix with the given expressions as blocks. The dimensions of the blocks must be suitably compatible.

Parameters:
 
  • dim (int) – The dimension in which to stack.
  • exprs (Expression[]) – A list of expressions.
  • exprs (Expression[][]) – A list of expressions.
  • e1 (Expression) – An expression.
  • e2 (Expression) – An expression.
  • a2 (double) – A scalar constant.
  • v2 (Variable) – A variable.
  • a1 (double) – A scalar constant.
  • v1 (Variable) – A variable.
  • a3 (double) – A scalar constant.
  • v3 (Variable) – A variable.
  • e3 (Expression) – An expression.
Return:
 

(Expression)

Expr.sub
Expression::t Expr::sub(Expression::t e1, Expression::t e2)
Expression::t Expr::sub(Expression::t e1, Variable::t v2)
Expression::t Expr::sub(Variable::t v1, Expression::t e2)
Expression::t Expr::sub(Expression::t e1, shared_ptr<ndarray<double,1>> a1)
Expression::t Expr::sub(Expression::t e1, shared_ptr<ndarray<double,2>> a2)
Expression::t Expr::sub(shared_ptr<ndarray<double,1>> a1, Expression::t e2)
Expression::t Expr::sub(shared_ptr<ndarray<double,2>> a2, Expression::t e2)
Expression::t Expr::sub(Expression::t e1, double c)
Expression::t Expr::sub(double c, Expression::t e2)
Expression::t Expr::sub(Expression::t e1, Matrix::t m)
Expression::t Expr::sub(Matrix::t m, Expression::t e2)
Expression::t Expr::sub(Expression::t e1, NDSparseArray::t n)
Expression::t Expr::sub(NDSparseArray::t n, Expression::t e2)
Expression::t Expr::sub(Variable::t v1, Variable::t v2)
Expression::t Expr::sub(Variable::t v1, shared_ptr<ndarray<double,1>> a1)
Expression::t Expr::sub(Variable::t v1, shared_ptr<ndarray<double,2>> a2)
Expression::t Expr::sub(shared_ptr<ndarray<double,1>> a1, Variable::t v2)
Expression::t Expr::sub(shared_ptr<ndarray<double,2>> a2, Variable::t v2)
Expression::t Expr::sub(Variable::t v1, double c)
Expression::t Expr::sub(double c, Variable::t v2)
Expression::t Expr::sub(Variable::t v1, Matrix::t m)
Expression::t Expr::sub(Matrix::t m, Variable::t v2)
Expression::t Expr::sub(Variable::t v1, NDSparseArray::t n)
Expression::t Expr::sub(NDSparseArray::t n, Variable::t v2)

Computes the difference of two expressions. The expressions must have the same shape and the result will be also an expression of that shape. The allowed combinations of arguments are the same as for Expr.add.

Parameters:
 
  • e1 (Expression) – An expression.
  • e2 (Expression) – An expression.
  • v2 (Variable) – A variable.
  • v1 (Variable) – A variable.
  • a1 (double[]) – An array of constants.
  • a2 (double[][]) – An array of constants.
  • c (double) – A constant.
  • m (Matrix) – A Matrix object.
  • n (NDSparseArray) – An NDSparseArray object.
Return:
 

(Expression)

Expr.sum
Expression::t Expr::sum(Expression::t expr)
Expression::t Expr::sum(Variable::t v)
Expression::t Expr::sum(Variable::t v, int d)
Expression::t Expr::sum(Variable::t v, int dfirst, int dlast)
Expression::t Expr::sum(Expression::t expr, int d)
Expression::t Expr::sum(Expression::t expr, int dfirst, int dlast)

Sum the elements of an expression. Without extra arguments, all elements are summed into a scalar expression of size 1.

With arguments dfirst, dlast or d, elements are summed along a specific dimension or a range of dimensions, resulting in an expression of reduced dimension. Note that the result of summing over a dimension of size 0 is 0.0. This means that for an expression of shape (2,0,2), summing over the second dimension yields an expression of shape (2,2) of zeros.

For example, if the argument is an \(n\times m\) matrix then summing along the first dimension computes the \(1\times m\) vector of column sums, and summing over the second dimension computes the \(n\times 1\) vector of row sums.

Parameters:
 
  • expr (Expression) – An expression object.
  • v (Variable) – A variable.
  • d (int) – The dimension in which to sum.
  • dfirst (int) – The first dimension to sum.
  • dlast (int) – The last-plus-one dimension to sum.
Return:
 

(Expression)

Expr.toString
string toString()

Create a human readable string representation of the expression.

Return:
  (string)
Expr.transpose
Expression::t transpose()

Transpose the expression. The expression must have at most two dimensions.

Return:
  (Expression)
Expr.vstack
Expression::t Expr::vstack(shared_ptr<ndarray<Expression::t,1>> exprs)
Expression::t Expr::vstack(Expression::t e1, Expression::t e2)
Expression::t Expr::vstack(Expression::t e1, Variable::t v2)
Expression::t Expr::vstack(Expression::t e1, double a2)
Expression::t Expr::vstack(Variable::t v1, Expression::t e2)
Expression::t Expr::vstack(Variable::t v1, Variable::t v2)
Expression::t Expr::vstack(Variable::t v1, double a2)
Expression::t Expr::vstack(double a1, Expression::t e2)
Expression::t Expr::vstack(double a1, Variable::t v2)
Expression::t Expr::vstack(Expression::t e1, Expression::t e2, Expression::t e3)
Expression::t Expr::vstack(Expression::t e1, Expression::t e2, Variable::t v3)
Expression::t Expr::vstack(Expression::t e1, Expression::t e2, double a3)
Expression::t Expr::vstack(Expression::t e1, Variable::t v2, Expression::t e3)
Expression::t Expr::vstack(Expression::t e1, Variable::t v2, Variable::t v3)
Expression::t Expr::vstack(Expression::t e1, Variable::t v2, double a3)
Expression::t Expr::vstack(Expression::t e1, double a2, Expression::t e3)
Expression::t Expr::vstack(Expression::t e1, double a2, Variable::t v3)
Expression::t Expr::vstack(Expression::t e1, double a2, double a3)
Expression::t Expr::vstack(Variable::t v1, Expression::t e2, Expression::t e3)
Expression::t Expr::vstack(Variable::t v1, Expression::t e2, Variable::t v3)
Expression::t Expr::vstack(Variable::t v1, Expression::t e2, double a3)
Expression::t Expr::vstack(Variable::t v1, Variable::t v2, Expression::t e3)
Expression::t Expr::vstack(Variable::t v1, Variable::t v2, Variable::t v3)
Expression::t Expr::vstack(Variable::t v1, Variable::t v2, double a3)
Expression::t Expr::vstack(Variable::t v1, double a2, Expression::t e3)
Expression::t Expr::vstack(Variable::t v1, double a2, Variable::t v3)
Expression::t Expr::vstack(Variable::t v1, double a2, double a3)
Expression::t Expr::vstack(double a1, Expression::t e2, Expression::t e3)
Expression::t Expr::vstack(double a1, Expression::t e2, Variable::t v3)
Expression::t Expr::vstack(double a1, Expression::t e2, double a3)
Expression::t Expr::vstack(double a1, Variable::t v2, Expression::t e3)
Expression::t Expr::vstack(double a1, Variable::t v2, Variable::t v3)
Expression::t Expr::vstack(double a1, Variable::t v2, double a3)
Expression::t Expr::vstack(double a1, double a2, Expression::t e3)
Expression::t Expr::vstack(double a1, double a2, Variable::t v3)
Expression::t Expr::vstack(double a1, double a2, double a3)

Stack a list of expressions vertically (i.e. along the first dimension). The expressions must have the same shape, except for the first dimension. The arguments may be any combination of expressions, scalar constants and variables.

For example, if \(y^1,y^2,y^3\) are three horizontal vectors of length \(n\) (and shape (1,n)) then their vertical stack is the matrix

\[\begin{split}\left[ \begin{array}{c}- y^1 - \\ - y^2 - \\ - y^3 - \end{array}\right]\end{split}\]

of shape (3,n).

Parameters:
 
  • exprs (Expression[]) – A list of expressions.
  • e1 (Expression) – An expression.
  • e2 (Expression) – An expression.
  • v2 (Variable) – A variable.
  • a2 (double) – A scalar constant.
  • v1 (Variable) – A variable.
  • a1 (double) – A scalar constant.
  • e3 (Expression) – An expression.
  • v3 (Variable) – A variable.
  • a3 (double) – A scalar constant.
Return:
 

(Expression)

Expr.zeros
Expression::t Expr::zeros(int num)

Create a vector of zeros as an expression.

Parameters:
 num (int) – The size of the expression.
Return:
  (Expression)