8.4.2. MDOMatrix

class MDOMatrix

Represent a matrix with double values. Usually used to interact with matrix attributes

Methods

coo

Construct a sparse matrix with indices and values

coo

Construct a sparse matrix with row indices, column indices and values

csc

Construct a Compressed Sparse Column (CSC) formatted matrix

csr

Construct a Compressed Sparse Row (CSR) formatted matrix

dense

Construct a dense matrix

dense

Construct a dense matrix with std::initializer_list

full

Construct a matrix containing only `value`s

identity

Construct a square matrix whose main diagonal elements are all ones

ones

Construct a matrix containing only ones

sparse

Construct a sparse matrix with std::initializer_list

zero

Construct a matrix containing only zeros

cols

The number of columns contained in this matrix

data

Retrieve the values of all non-zero elements in this matrix

indices

Retrieve the indices of all non-zero elements in this matrix

nonzeros

Retrieve the number of non-zero elements in this matrix

rows

The number of rows contained in this matrix

static MDOMatrix coo(int rows, int cols, int nonzeros, int *ind, double *data)

Construct a sparse matrix with indices and values.

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

  • int nonzeros – The number of non-zero elements.

  • int* ind – Indices of all non-zero elements.

  • double* data – Values for all non-zero elements.

Returns

The newly created matrix.

static MDOMatrix coo(int rows, int cols, int nonzeros, int *row, int *col, double *data)

Construct a sparse matrix with row indices, column indices and values.

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

  • int nonzeros – The number of non-zero elements.

  • int* row – Row indices of all non-zero elements.

  • int* col – Column indices of all non-zero elements.

  • double* data – Values for all non-zero elements.

Returns

The newly created matrix.

static MDOMatrix csc(int rows, int cols, int nonzeros, int *ptr, int *ind, double *data)

Construct a Compressed Sparse Column (CSC) formatted matrix.

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

  • int nonzeros – The number of non-zero elements.

  • int* ptr – The begin indices in ind of columns contained in matrix.

  • int* ind – The row indices of all non-zero elements.

  • double* data – Values for all non-zero elements.

Returns

The newly created matrix.

static MDOMatrix csr(int rows, int cols, int nonzeros, int *ptr, int *ind, double *data)

Construct a Compressed Sparse Row (CSR) formatted matrix.

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

  • int nonzeros – The number of non-zero elements.

  • int* ptr – The begin indices in ind of rows contained in matrix.

  • int* ind – The column indices of all non-zero elements.

  • double* data – Values for all non-zero elements.

Returns

The newly created matrix.

static MDOMatrix dense(int rows, int cols, double *data)

Construct a dense matrix.

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

  • double* data – Values for all elements. It should have at least rows * cols elements.

Returns

The newly created matrix.

static MDOMatrix dense(int rows, int cols, std::initializer_list<double> data)

Construct a dense matrix with std::initializer_list .

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

  • std::initializer_list<double> data – Values for all elements. It should have at least rows * cols elements.

Returns

The newly created matrix.

static MDOMatrix full(int rows, int cols, double value)

Construct a matrix containing only value s.

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

  • double value – The value of all matrix elements.

Returns

The newly created matrix.

static MDOMatrix identity(int n)

Construct a square matrix whose main diagonal elements are all ones

Parameters

int n – The dimension of new matrix, that is, the number of rows or columns, for a square matrix these two are equal.

Returns

The newly created matrix.

static MDOMatrix ones(int rows, int cols)

Construct a matrix containing only ones.

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

Returns

The newly created matrix.

static MDOMatrix sparse(int rows, int cols, std::initializer_list<int> i, std::initializer_list<int> j, std::initializer_list<double> data)

Construct a sparse matrix with std::initializer_list .

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

  • std::initializer_list<int> i – Row indices of all non-zero elements. It should have the same length as j and data .

  • std::initializer_list<int> j – Column indices of all non-zero elements. It should have the same length as i and data .

  • std::initializer_list<double> data – Values for all elements. It should have the same length as i and j .

Returns

The newly created matrix.

static MDOMatrix zero(int rows, int cols)

Construct a matrix containing only zeros.

Parameters
  • int rows – The number of rows.

  • int cols – The number of columns.

Returns

The newly created matrix.

int cols()

The number of columns contained in this matrix.

Returns

The number of columns contained in this matrix.

const double *data()

Retrieve the values of all non-zero elements in this matrix.

Returns

The values of all non-zero elements in this matrix.

const int *indices()

Retrieve the indices of all non-zero elements in this matrix.

Returns

The indices of all non-zero elements, its length can be known by nonzeros() . Coordinates of i-th element can by calculated by:

row[i] = indices[i] / cols();
col[i] = indices[i] % cols();

Note

The order of the returned array may not be guaranteed.

int nonzeros()

Retrieve the number of non-zero elements in this matrix.

Returns

The total number of non-zero elements.

int rows()

The number of rows contained in this matrix.

Returns

The number of rows contained in this matrix.