# 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 – 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::t Matrix::antidiag(shared_ptr<ndarray<double,1>> d)
Matrix::t Matrix::antidiag(shared_ptr<ndarray<double,1>> d, int k)
Matrix::t Matrix::antidiag(int n, double val)
Matrix::t Matrix::antidiag(int n, double val, int k)


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

Parameters:
• d (double[]) – 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 (double) – Use this value for all anti-diagonal elements.

Return:
Matrix.dense
Matrix::t Matrix::dense(shared_ptr<ndarray<double,2>> data)
Matrix::t Matrix::dense(int dimi, int dimj, shared_ptr<ndarray<double,1>> data)
Matrix::t Matrix::dense(int dimi, int dimj, double value)
Matrix::t Matrix::dense(Matrix::t other)


Create a dense matrix from the given data.

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

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

• dimi (int) – Number of rows.

• dimj (int) – Number of columns.

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

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

Return:
Matrix.diag
Matrix::t Matrix::diag(shared_ptr<ndarray<double,1>> d)
Matrix::t Matrix::diag(shared_ptr<ndarray<double,1>> d, int k)
Matrix::t Matrix::diag(int n, double val)
Matrix::t Matrix::diag(int n, double val, int k)
Matrix::t Matrix::diag(shared_ptr<ndarray<Matrix::t,1>> md)
Matrix::t Matrix::diag(int num, Matrix::t mv)


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

Parameters:
• d (double[]) – 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 (double) – 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::t Matrix::eye(int n)


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

Parameters:

n (int) – The dimension of the matrix.

Return:
Matrix.get
double get(int i, int j)


Get a single entry.

Parameters:
• i (int) – Row index.

• j (int) – Column index.

Return:

(double)

Matrix.getDataAsArray
shared_ptr<ndarray<double,1>> getDataAsArray()


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

Return:

(double[])

Matrix.getDataAsTriplets
void getDataAsTriplets(shared_ptr<ndarray<int,1>> subi, shared_ptr<ndarray<int,1>> subj, shared_ptr<ndarray<double,1>> 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 (double[]) – Coefficient values are returned in this array.

Matrix.isSparse
bool isSparse()


Returns true if the matrix is sparse.

Return:

(bool)

Matrix.numColumns
int numColumns()


Returns the number of columns in the matrix.

Return:

(int)

Matrix.numNonzeros
long long numNonzeros()


Returns the number of non-zeros in the matrix.

Return:

(long long)

Matrix.numRows
int numRows()


Returns the number of rows in the matrix.

Return:

(int)

Matrix.ones
Matrix::t Matrix::ones(int n, int m)


Construct a matrix filled with ones.

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

• m (int) – Number of columns.

Return:
Matrix.sparse
Matrix::t Matrix::sparse(int nrow, int ncol, shared_ptr<ndarray<int,1>> subi, shared_ptr<ndarray<int,1>> subj, shared_ptr<ndarray<double,1>> val)
Matrix::t Matrix::sparse(shared_ptr<ndarray<int,1>> subi, shared_ptr<ndarray<int,1>> subj, shared_ptr<ndarray<double,1>> val)
Matrix::t Matrix::sparse(shared_ptr<ndarray<int,1>> subi, shared_ptr<ndarray<int,1>> subj, double val)
Matrix::t Matrix::sparse(int nrow, int ncol, shared_ptr<ndarray<int,1>> subi, shared_ptr<ndarray<int,1>> subj, double val)
Matrix::t Matrix::sparse(int nrow, int ncol)
Matrix::t Matrix::sparse(shared_ptr<ndarray<double,2>> data)
Matrix::t Matrix::sparse(shared_ptr<ndarray<Matrix::t,2>> blocks)
Matrix::t Matrix::sparse(Matrix::t mx)


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 (double[]) – Coefficients of non-zero elements.

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

• data (double[][]) – 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 nullptr 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 nullptr entries will have height 0, and any column that contains only nullptr entries will have width 0.

• mx (Matrix) – A Matrix object.

Return:
Matrix.toString
string toString()


Get a string representation of the matrix.

Return:

(string)

Matrix.transpose
Matrix::t transpose()


Transpose the matrix.

Return: