# 14.2.21 Class Matrix¶

mosek.fusion.Matrix

Base class for all matrix objects. It can be used to create and manipulate matrices of constant coefficients both in dense and sparse format. To operate with matrices containing variables and linear expressions use the classes Expr and Variable.

Members:

Matrix.get – Get a single entry.

Matrix.getDataAsArray – Return a dense array of values.

Matrix.getDataAsTriplets – Return the matrix data in sparse triplet format.

Matrix.isSparse – Returns true if the matrix is sparse.

Matrix.numColumns – Returns the number of columns in the matrix.

Matrix.numNonzeros – Returns the number of non-zeros in the matrix.

Matrix.numRows – Returns the number of rows in the matrix.

Matrix.toString – Get a string representation of the matrix.

Matrix.transpose (.T) – Transpose the matrix.

Static members:

Matrix.antidiag – Create a sparse square matrix with a given vector as anti-diagonal.

Matrix.dense – Create a dense matrix from the given data.

Matrix.diag – Create a sparse square matrix with a given vector as diagonal.

Matrix.eye – Create the identity matrix.

Matrix.ones – Create a matrix filled with all ones.

Matrix.sparse – Create a sparse matrix from the given data.

Matrix.antidiag
Matrix.antidiag(float[] d) -> Matrix
Matrix.antidiag(float[] d, int k) -> Matrix
Matrix.antidiag(int n, float val) -> Matrix
Matrix.antidiag(int n, float val, int k) -> Matrix


Create a sparse square matrix with a given vector as anti-diagonal.

Parameters:
• d (float[]) – The anti-diagonal vector.

• k (int) – The anti-diagonal index. $$k=0$$ is the default and means the main anti-diagonal. $$k>0$$ means above, and $$k<0$$ means below the main anti-diagonal.

• n (int) – The dimension of the matrix.

• val (float) – Use this value for all anti-diagonal elements.

Return:
Matrix.dense
Matrix.dense(float[][] data) -> Matrix
Matrix.dense(int dimi, int dimj, float[] data) -> Matrix
Matrix.dense(int dimi, int dimj, float value) -> Matrix
Matrix.dense(Matrix other) -> Matrix


Create a dense matrix from the given data.

Parameters:
• data (float[][]) – A one- or two-dimensional array of matrix coefficients.

• data (float[]) – A one- or two-dimensional array of matrix coefficients.

• dimi (int) – Number of rows.

• dimj (int) – Number of columns.

• value (float) – Use this value for all elements.

• other (Matrix) – Create a dense matrix from another matrix.

Return:
Matrix.diag
Matrix.diag(float[] d) -> Matrix
Matrix.diag(float[] d, int k) -> Matrix
Matrix.diag(int n, float val) -> Matrix
Matrix.diag(int n, float val, int k) -> Matrix
Matrix.diag(Matrix[] md) -> Matrix
Matrix.diag(int num, Matrix mv) -> Matrix


Create a sparse square matrix with a given vector as diagonal.

Parameters:
• d (float[]) – The diagonal vector.

• k (int) – The diagonal index. $$k=0$$ is the default and means the main diagonal. $$k>0$$ means above, and $$k<0$$ means below the main diagonal.

• n (int) – The dimension of the matrix.

• val (float) – Use this value for all diagonal elements.

• md (Matrix[]) – A list of square matrices that are used to create a block-diagonal square matrix.

• num (int) – Number of times to repeat the mv matrix.

• mv (Matrix) – A matrix to be repeated in all blocks of a block-diagonal square matrix.

Return:
Matrix.eye
Matrix.eye(int n) -> Matrix


Construct the identity matrix of size $$n$$.

Parameters:

n (int) – The dimension of the matrix.

Return:
Matrix.get
get(int i, int j) -> float


Get a single entry.

Parameters:
• i (int) – Row index.

• j (int) – Column index.

Return:

(float)

Matrix.getDataAsArray
getDataAsArray() -> float[]


Return the matrix elements as a dense array in row-major format.

Return:

(float[])

Matrix.getDataAsTriplets
getDataAsTriplets(int[] subi, int[] subj, float[] val)


Return the matrix data in sparse triplet format. Data is copied to the arrays subi, subj and val which must be pre-allocated to hold at least the number of non-zeros in the matrix.

The data returned must be ordered with subi as primary key and subj as secondary key.

Parameters:
• subi (int[]) – Row subscripts are returned in this array.

• subj (int[]) – Column subscripts are returned in this array.

• val (float[]) – Coefficient values are returned in this array.

Matrix.isSparse
isSparse() -> bool


Returns true if the matrix is sparse.

Return:

(bool)

Matrix.numColumns
numColumns() -> int


Returns the number of columns in the matrix.

Return:

(int)

Matrix.numNonzeros
numNonzeros() -> int


Returns the number of non-zeros in the matrix.

Return:

(int)

Matrix.numRows
numRows() -> int


Returns the number of rows in the matrix.

Return:

(int)

Matrix.ones
Matrix.ones(int n, int m) -> Matrix


Construct a matrix filled with ones.

Parameters:
• n (int) – Number of rows.

• m (int) – Number of columns.

Return:
Matrix.sparse
Matrix.sparse(int nrow, int ncol, int[] subi, int[] subj, float[] val) -> Matrix
Matrix.sparse(int[] subi, int[] subj, float[] val) -> Matrix
Matrix.sparse(int[] subi, int[] subj, float val) -> Matrix
Matrix.sparse(int nrow, int ncol, int[] subi, int[] subj, float val) -> Matrix
Matrix.sparse(int nrow, int ncol) -> Matrix
Matrix.sparse(float[][] data) -> Matrix
Matrix.sparse(Matrix[][] blocks) -> Matrix
Matrix.sparse(Matrix mx) -> Matrix


Create a sparse matrix from the given data.

Parameters:
• nrow (int) – Number of rows.

• ncol (int) – Number of columns.

• subi (int[]) – Row subscripts of non-zero elements.

• subj (int[]) – Column subscripts of non-zero elements.

• val (float[]) – Coefficients of non-zero elements.

• val (float) – Coefficients of non-zero elements.

• data (float[][]) – Dense data array.

• blocks (Matrix[][]) – The matrix data in block format. All elements in a row must have the same height, and all elements in a column must have the same width. Entries that are None will be interpreted as a block of zeros whose height and width are deduced from the other elements in the same row and column. Any row that contains only None entries will have height 0, and any column that contains only None entries will have width 0.

• mx (Matrix) – A Matrix object.

Return:
Matrix.toString
toString() -> str


Get a string representation of the matrix.

Return:

(str)

Matrix.transpose (.T)
transpose() -> Matrix


Transpose the matrix.

The standard Python operator .T can also be used for this purpose. See Sec. 14.1.3 (Pythonic extensions) for details.

Return: