# 14.2.40 Class Var¶

- mosek::fusion::Var¶
Contains several static methods for manipulating variable objects and creating new variables from old ones.

Primal and dual solution values and additional operations on variables are available from the

`Variable`

class.- Static members
Var.compress – Reshape a variable object by removing all dimensions of size 1.

Var.empty – Produce a new empty variable of the given shape.

Var.flatten – Create a one-dimensional logical view of a variable object.

Var.hrepeat – Repeat a variable a number of times in the second dimension.

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

Var.promote – Pad variable shape.

Var.repeat – Repeat a variable a number of times in the given dimension.

Var.reshape – Create a reshaped view of the given variable.

Var.stack – Stack a list of variables in an arbitrary dimension.

Var.vrepeat – Repeat a variable a number of times in the first dimension.

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

- Var.compress¶
Variable::t Var::compress(Variable::t v)

Reshape a variable object by removing all dimensions of size 1. The result contains the same number of elements, but all dimensions are larger than 1 (except if the original variable contains exactly one element).

- Var.empty¶
Variable::t Var::empty(shared_ptr<ndarray<int,1>> shape)

Produce a new empty variable of the given shape.

- Parameters
`shape`

(`int`

[]) – The shape of a variable.- Return
(

`Variable`

)

- Var.flatten¶
Variable::t Var::flatten(Variable::t v)

Create a one-dimensional

`Variable`

object that represents a*logical*view of the \(k-\)dimensional variable \(V\). The \(V\) matrix is traversed starting from the innermost dimension. For a two-dimensional matrix this means it is traversed row after row.The returned view is a one-dimensional array of size equal to the product of dimensions of \(V\).

- Var.hrepeat¶
Variable::t Var::hrepeat(Variable::t v, int n)

Repeat a variable a number of times in the second dimension. This is equivalent to horizontal stacking of \(n\) copies of the variable; see

`Var.hstack`

.

- Var.hstack¶
Variable::t Var::hstack(shared_ptr<ndarray<Variable::t,1>> v) Variable::t Var::hstack(Variable::t v1, Variable::t v2) Variable::t Var::hstack(Variable::t v1, Variable::t v2, Variable::t v3)

Stack a list of variables horizontally (i.e. along the second dimension). The variables must have the same shape, except for the second dimension.

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

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

`(n,3)`

.

- Var.promote¶
Variable::t Var::promote(Variable::t v, int nd)

Pad the variable shape up to

`nd`

dimensions.

- Var.repeat¶
Variable::t Var::repeat(Variable::t v, int dim, int n) Variable::t Var::repeat(Variable::t v, int n)

Repeat a variable a number of times in the given dimension. If

`dim`

is non-negative this is equivalent to stacking \(n\) copies of the variable in dimension`dim`

; see`Var.stack`

.If

`dim`

is negative then a new dimension is added in front, so the new variable has shape`(n, v.shape())`

.The default is repeating in the first dimension as in

`Var.vrepeat`

.

- Var.reshape¶
Variable::t Var::reshape(Variable::t v, shared_ptr<ndarray<int,1>> shape) Variable::t Var::reshape(Variable::t v, int d1, int d2) Variable::t Var::reshape(Variable::t v, int d1)

Create a reshaped view of the given variable.

- Var.stack¶
Variable::t Var::stack(int dim, Variable::t v1, Variable::t v2) Variable::t Var::stack(int dim, Variable::t v1, Variable::t v2, Variable::t v3) Variable::t Var::stack(int dim, shared_ptr<ndarray<Variable::t,1>> v) Variable::t Var::stack(Variable::t v1, Variable::t v2, int dim) Variable::t Var::stack(Variable::t v1, Variable::t v2, Variable::t v3, int dim) Variable::t Var::stack(shared_ptr<ndarray<Variable::t,1>> v, int dim) Variable::t Var::stack(shared_ptr<ndarray<Variable::t,2>> vlist)

Stack a list of variables along an arbitrary dimension. All variables must have the same shape, except for dimension

`dim`

.For example, suppose \(x,y\) are two matrix variables of shape \(n\times m\). Then stacking them in the first dimension produces a matrix variable of shape

`(2n,m)`

:\[\begin{split}\left[\begin{array}{c}x\\ y\end{array}\right],\end{split}\]stacking them in the second dimension produces a matrix variable of shape

`(n,2m)`

:\[\left[\begin{array}{cc}x & y\end{array}\right],\]and stacking them in the third dimension produces a three-dimensional variable of shape

`(n,m,2)`

.The version which takes a two-dimensional array of variables constructs a block matrix variable with the given variables as blocks. The dimensions of the blocks must be suitably compatible. The variables may be more than two-dimensional, if they have the same size in the remaining dimensions; the block stacking still takes place in the first and second dimension.

- Parameters
- Return
(

`Variable`

)

- Var.vrepeat¶
Variable::t Var::vrepeat(Variable::t v, int n)

Repeat a variable a number of times in the first dimension. This is equivalent to vertically stacking of \(n\) copies of the variable; see

`Var.vstack`

.

- Var.vstack¶
Variable::t Var::vstack(shared_ptr<ndarray<Variable::t,1>> v) Variable::t Var::vstack(Variable::t v1, Variable::t v2) Variable::t Var::vstack(Variable::t v1, Variable::t v2, Variable::t v3)

Stack a list of variables vertically (i.e. along the first dimension). The variables must have the same shape, except for the first dimension.

For example, if \(y^1,y^2,y^3\) are three horizontal vector variables of length \(n\) (and shape

`(1,n)`

) then their vertical stack is the matrix variable\[\begin{split}\left[ \begin{array}{c}- y^1 - \\ - y^2 - \\ - y^3 - \end{array}\right]\end{split}\]of shape

`(3,n)`

.