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

Implements

BaseExpression

Members

BaseExpression.GetDim – Return the d’th dimension in the expression.

BaseExpression.GetND – Return the number of dimensions in the expression.

BaseExpression.GetShape – Get the shape of the expression.

BaseExpression.GetSize – Return the total number of elements in the expression (the product of the dimensions).

BaseExpression.Index – Get a single element in the expression.

BaseExpression.Pick – Pick a number of elements from the expression.

BaseExpression.Slice – Get a slice of the expression.

BaseExpression.ToString – Return a string representation of the expression object.

Expr.Eval – Evaluate the expression and push the result onto the work stack.

Static members

Expr.Add – Compute the sum of expressions.

Expr.Condense – Flatten expression and remove all structural zeros.

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 an expression consisting of ones.

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.Transpose – Transpose a two-dimensional expression.

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

Expr.Zeros – Create an expression consisting of zeros.

Expr.Add
Expression Expr.Add(Expression e1, Expression e2)
Expression Expr.Add(Expression e1, double[] a1)
Expression Expr.Add(Expression e1, double[,] a2)
Expression Expr.Add(double[] a1, Expression e2)
Expression Expr.Add(double[,] a2, Expression e2)
Expression Expr.Add(Expression e1, double c)
Expression Expr.Add(double c, Expression e2)
Expression Expr.Add(Expression e1, Matrix m)
Expression Expr.Add(Matrix m, Expression e2)
Expression Expr.Add(Expression e1, NDSparseArray n)
Expression Expr.Add(NDSparseArray n, Expression e2)
Expression Expr.Add(Variable[] vs)
Expression Expr.Add(Expression[] 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.

  • 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.Condense
Expression Expr.Condense(Expression e)

Flatten expression and remove all structural zeros.

Parameters

e (Expression) – Expression to be condensed.

Return

(Expression)

Expr.ConstTerm
Expression Expr.ConstTerm(double[] vals1)
Expression Expr.ConstTerm(double[,] vals2)
Expression Expr.ConstTerm(int size, double val)
Expression Expr.ConstTerm(int[] shp, double val)
Expression Expr.ConstTerm(int[] shp, int[,] sparsity, double[] vals1)
Expression Expr.ConstTerm(int[] shp, int[,] sparsity, double val)
Expression Expr.ConstTerm(double val)
Expression Expr.ConstTerm(Matrix m)
Expression Expr.ConstTerm(NDSparseArray nda)

Create an expression consisting of a constant vector of values.

Parameters
  • vals1 (double[]) – A vector initializing the expression.

  • vals2 (double[,]) – An 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 (int[]) – Defines the shape of the expression.

  • sparsity (int[,]) – Sparsity pattern.

  • m (Matrix) – A matrix of values initializing the expression.

  • nda (NDSparseArray) – An multi-dimensional sparse array initializing the expression.

Return

(Expression)

Expr.Dot
Expression Expr.Dot(Parameter p, Expression e)
Expression Expr.Dot(Expression e, Parameter p)
Expression Expr.Dot(double[] c1, Expression e)
Expression Expr.Dot(double[,] c2, Expression e)
Expression Expr.Dot(NDSparseArray nda, Expression e)
Expression Expr.Dot(Matrix m, Expression e)
Expression Expr.Dot(Expression e, double[] c1)
Expression Expr.Dot(Expression e, NDSparseArray nda)
Expression Expr.Dot(Expression e, double[,] c2)
Expression Expr.Dot(Expression e, Matrix m)

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
  • p (Parameter) – A parameter.

  • e (Expression) – An expression object.

  • c1 (double[]) – A one-dimensional coefficient vector.

  • c2 (double[,]) – A two-dimensional coefficient array.

  • nda (NDSparseArray) – A multi-dimensional sparse array.

  • m (Matrix) – A matrix object.

Return

(Expression)

Expr.Eval
void Eval(WorkStack rs, WorkStack ws, WorkStack xs)

Evaluate the expression and push the result onto the rs work stack.

Parameters
  • rs (WorkStack) – The stack where the result of the evaluation is stored.

  • ws (WorkStack) – The stack used by evaluation to perform intermediate computations. It will be returned in the same state as when the function is called.

  • xs (WorkStack) – An auxiliary stack.

Expr.Flatten
Expression Expr.Flatten(Expression e)

Reshape the expression into a vector.

Parameters

e (Expression) – The expression to be flattened.

Return

(Expression)

Expr.Hstack
Expression Expr.Hstack(Expression[] exprs)
Expression Expr.Hstack(Expression e1, Expression e2)
Expression Expr.Hstack(Expression e1, double a2)
Expression Expr.Hstack(double a1, Expression e2)
Expression Expr.Hstack(double a1, double a2, Expression e3)
Expression Expr.Hstack(double a1, Expression e2, double a3)
Expression Expr.Hstack(double a1, Expression e2, Expression e3)
Expression Expr.Hstack(Expression e1, double a2, double a3)
Expression Expr.Hstack(Expression e1, double a2, Expression e3)
Expression Expr.Hstack(Expression e1, Expression e2, double a3)
Expression Expr.Hstack(Expression e1, Expression e2, Expression 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.

  • a1 (double) – A scalar constant.

  • e3 (Expression) – An expression.

  • a3 (double) – A scalar constant.

Return

(Expression)

Expr.Mul
Expression Expr.Mul(Parameter p, Expression expr)
Expression Expr.Mul(Expression expr, Parameter p)
Expression Expr.Mul(Matrix mx, Variable v)
Expression Expr.Mul(Variable v, Matrix mx)
Expression Expr.Mul(double[,] mx, Variable v)
Expression Expr.Mul(Variable v, double[,] mx)
Expression Expr.Mul(Matrix mx, Expression expr)
Expression Expr.Mul(Expression expr, Matrix mx)
Expression Expr.Mul(double[,] a, Expression expr)
Expression Expr.Mul(Expression expr, double[,] a)
Expression Expr.Mul(double[] a, Expression expr)
Expression Expr.Mul(Expression expr, double[] a)
Expression Expr.Mul(double c, Expression expr)
Expression Expr.Mul(Expression expr, double c)

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
  • p (Parameter) – A parameter object.

  • expr (Expression) – An expression.

  • mx (Matrix) – A matrix.

  • mx (double[,]) – A matrix.

  • v (Variable) – A variable.

  • a (double[,]) – Scalar data.

  • a (double[]) – Scalar data.

  • c (double) – A scalar value.

Return

(Expression)

Expr.MulDiag
Expression Expr.MulDiag(double[,] a, Expression expr)
Expression Expr.MulDiag(double[,] a, Variable v)
Expression Expr.MulDiag(Expression expr, double[,] a)
Expression Expr.MulDiag(Variable v, double[,] a)
Expression Expr.MulDiag(Matrix mx, Expression expr)
Expression Expr.MulDiag(Expression expr, Matrix mx)
Expression Expr.MulDiag(Matrix mx, Variable v)
Expression Expr.MulDiag(Variable v, Matrix mx)
Expression Expr.MulDiag(Parameter p, Expression expr)
Expression Expr.MulDiag(Expression expr, Parameter p)
Expression Expr.MulDiag(Parameter p, Variable v)
Expression Expr.MulDiag(Variable v, Parameter p)

Compute the diagonal of the product of two matrices. If \(A\in\mathbb{M}(m,n)\) and \(B\in\mathbb{M}(n,m)\), the result is a vector expression of length \(m\) 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.

  • p (Parameter) – A parameter object.

Return

(Expression)

Expr.MulElm
Expression Expr.MulElm(Expression expr, Parameter p)
Expression Expr.MulElm(Parameter p, Expression expr)
Expression Expr.MulElm(Expression expr, NDSparseArray spm)
Expression Expr.MulElm(Expression expr, double[] a1)
Expression Expr.MulElm(Expression expr, double[,] a2)
Expression Expr.MulElm(Expression expr, Matrix m)
Expression Expr.MulElm(double[] a1, Expression expr)
Expression Expr.MulElm(double[,] a2, Expression expr)
Expression Expr.MulElm(NDSparseArray spm, Expression expr)
Expression Expr.MulElm(Matrix m, Expression 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
  • expr (Expression) – An expression object.

  • p (Parameter) – A parameter object.

  • spm (NDSparseArray) – A multidimensional sparse array object.

  • a1 (double[]) – A one-dimensional coefficient array.

  • a2 (double[,]) – A two-dimensional coefficient array.

  • m (Matrix) – A matrix object.

Return

(Expression)

Expr.Neg
Expression Expr.Neg(Expression e)

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

Parameters

e (Expression) – An expression object.

Return

(Expression)

Expr.Ones
Expression Expr.Ones(int size)
Expression Expr.Ones(int[] shp)
Expression Expr.Ones(int[] shp, int[,] sparsity)
Expression Expr.Ones()

Create an expression consisting of ones.

Parameters
  • size (int) – Length of the vector to be constructed.

  • shp (int[]) – Defines the shape of the expression.

  • sparsity (int[,]) – Defines the sparsity pattern of the expression - everything outside the sparsitry patterm will be zero.

Return

(Expression)

Expr.Outer
Expression Expr.Outer(Expression e, double[] a)
Expression Expr.Outer(double[] a, Expression e)
Expression Expr.Outer(Expression e, Matrix m)
Expression Expr.Outer(Matrix m, Expression e)
Expression Expr.Outer(Expression e, Parameter p)
Expression Expr.Outer(Parameter p, Expression 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
  • e (Expression) – A vector expression.

  • a (double[]) – A vector of constants.

  • m (Matrix) – A one-dimensional matrix.

  • p (Parameter) – A vector parameter.

Return

(Expression)

Expr.Repeat
Expression Expr.Repeat(Expression e, int n, int d)
Expression Expr.Repeat(Variable x, 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.

  • x (Variable) – The variable to repeat.

Return

(Expression)

Expr.Reshape
Expression Expr.Reshape(Expression e, int[] newshape)
Expression Expr.Reshape(Expression e, int size)
Expression Expr.Reshape(Expression 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.

  • newshape (int[]) – Reshape into an expression of this 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.Stack
Expression Expr.Stack(int dim, Expression[] exprs)
Expression Expr.Stack(int dim, Expression e1, Expression e2)
Expression Expr.Stack(int dim, Expression e1, double a2)
Expression Expr.Stack(int dim, double a1, Expression e2)
Expression Expr.Stack(int dim, double a1, double a2, Expression e1)
Expression Expr.Stack(int dim, double a1, Expression e2, double a3)
Expression Expr.Stack(int dim, double a1, Expression e2, Expression e3)
Expression Expr.Stack(int dim, Expression e1, double a2, double a3)
Expression Expr.Stack(int dim, Expression e1, double a2, Expression e3)
Expression Expr.Stack(int dim, Expression e1, Expression e2, double a3)
Expression Expr.Stack(int dim, Expression e1, Expression e2, Expression e3)
Expression Expr.Stack(Expression[][] 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.

  • a1 (double) – A scalar constant.

  • a3 (double) – A scalar constant.

  • e3 (Expression) – An expression.

Return

(Expression)

Expr.Sub
Expression Expr.Sub(Expression e1, Expression e2)
Expression Expr.Sub(Expression e1, double[] a1)
Expression Expr.Sub(Expression e1, double[,] a2)
Expression Expr.Sub(double[] a1, Expression e2)
Expression Expr.Sub(double[,] a2, Expression e2)
Expression Expr.Sub(Expression e1, double c)
Expression Expr.Sub(double c, Expression e2)
Expression Expr.Sub(Expression e1, Matrix m)
Expression Expr.Sub(Matrix m, Expression e2)
Expression Expr.Sub(Expression e1, NDSparseArray n)
Expression Expr.Sub(NDSparseArray n, Expression e2)

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.

  • 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 Expr.Sum(Expression expr)
Expression Expr.Sum(Expression expr, int dim)

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

With argument dim, elements are summed along a specific dimension, 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 \(0\)-th dimension computes the \(1\times m\) vector of column sums, and summing along the \(1\)-st dimension computes the \(n\times 1\) vector of row sums.

Parameters
  • expr (Expression) – An expression object.

  • dim (int) – The dimension along which to sum.

Return

(Expression)

Expr.Transpose
Expression Expr.Transpose(Expression e)

Transpose a two-dimensional expression.

Parameters

e (Expression) – Expression to transpose.

Return

(Expression)

Expr.Vstack
Expression Expr.Vstack(Expression[] exprs)
Expression Expr.Vstack(Expression e1, Expression e2)
Expression Expr.Vstack(Expression e1, double a2)
Expression Expr.Vstack(double a1, Expression e2)
Expression Expr.Vstack(Expression e1, Expression e2, Expression e3)
Expression Expr.Vstack(Expression e1, Expression e2, double a3)
Expression Expr.Vstack(Expression e1, double a2, Expression e3)
Expression Expr.Vstack(Expression e1, double a2, double a3)
Expression Expr.Vstack(double a1, Expression e2, Expression e3)
Expression Expr.Vstack(double a1, Expression e2, double a3)
Expression Expr.Vstack(double a1, double a2, Expression e3)
Expression 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.

  • a2 (double) – A scalar constant.

  • a1 (double) – A scalar constant.

  • e3 (Expression) – An expression.

  • a3 (double) – A scalar constant.

Return

(Expression)

Expr.Zeros
Expression Expr.Zeros(int size)
Expression Expr.Zeros(int[] shp)

Create an expression consisting of zeros.

Parameters
  • size (int) – Length of the vector to be constructed.

  • shp (int[]) – Defines the shape of the expression.

Return

(Expression)