Matrix

template<typename ElemType>
class Matrix : public mocca::Array<ElemType>, public mocca::MatrixBase<Matrix<ElemType>>

One of the basic types within MOCCA. The Matrix class represents a Dense Matrix in Linear Algebra.

All elements in the Matrix class are stored in a plain contiguous Array, following a row-major format for indexing the elements in a 2D space. All Matrix objects are dynamically sized.

See Matrix in Basic Datatypes for more information.

Template Parameters:

  • ElemType - Numeric type of the elements in the matrix (i.e., any integral, floating-point or std::complex types).

Constructors

Matrix()

Default Constructor. Creates an empty Matrix (i.e., with size 0). Complexity: Constant

Matrix(index_t rows, index_t cols)

Creates a Matrix and fills with zeros.

Parameters:
  • cols[in] number of columns

  • rows[in] number of rows Complexity: Constant

Matrix(index_t rows, index_t cols, ElemType val)

Creates a Matrix and fills with copies of val.

Parameters:
  • cols[in] number of columns

  • rows[in] number of rows

  • val[in] value to initialize the matrix with Complexity: Linear in cols * rows

template<typename InIter, internal::require_input_iterator<InIter> = true>
Matrix(index_t rows, index_t cols, InIter first, InIter last)

Creates a Matrix and then copies the content from the range [first, last[.

If std::distance(first, last) is less than matrix size, the remaining elements are left uninitialised.

Parameters:
  • cols[in] number of columns

  • rows[in] number of rows

  • first[in] iterator to the first element in the range

  • last[in] iterator to the “past-the-end” element in the range

Throws:

std::length_error – if `std::distance(first, last) > cols * rows` Complexity: Linear in distance between first and last

Matrix(std::initializer_list<std::initializer_list<ElemType>> ilist)

Creates a Matrix from a initializer list.

Parameters:

ilist[in] initializer list to use as data source Complexity: Linear in ilist size

template<typename T>
Matrix(const Matrix<T> &other)

Copy construct. Creates a Matrix and then copy the content from an other matrix.

Parameters:

other[in] another Matrix to use as data source Complexity: Linear in other.size()

Matrix(const Matrix &other)

Copy construct. Creates a Matrix and then copy the content from an other Matrix.

Parameters:

other[in] another Matrix to use as data source Complexity: Linear in other.size()

Matrix(Matrix &&other) noexcept

Move Constructor. Creates a Matrix and then copy the content from an other Matrix, following the move semantics (i.e., the data is moved from other to this container). other is valid but left in a unspecified state afterwards.

Parameters:

other[in] another Matrix to use as data source Complexity: Constant

template<typename E>
Matrix(const MatrixBase<E> &expr)

Creates a Matrix from an expression.

Parameters:

expr[in] an expression

Throws:

std::length_error – if the dimensions of the operands in``expr`` do not match and cannot be broadcasted. Complexity: Linear in this->size()

Destructor

virtual ~Matrix() = default

Default destructor.

Assignment

template<typename T>
Matrix &operator=(const Matrix<T> &other)

Copy Assignment Operator. Replaces Matrix content with the contents of other Matrix. This routine will automatically reallocate the container if necessary.

Parameters:

other[in] another Matrix to use as data source

Returns:

*this Complexity: Linear in other.size()

Matrix &operator=(const Matrix &other)

Copy Assignment Operator. Replaces Matrix content with the contents of other Matrix. This routine will automatically reallocate the container if necessary.

Parameters:

other[in] another Matrix to use as data source

Returns:

*this Complexity: Linear in other.size()

Matrix &operator=(Matrix &&other) noexcept

Move Assignment Operator. Replaces Matrix content with a the contents of other Matrix, following the move semantics (i.e., the data is moved from other to this container). other is valid but left in a unspecified state afterwards.

Parameters:

other[in] another Matrix to use as data source

Returns:

*this Complexity: Constant

Matrix &operator=(std::initializer_list<std::initializer_list<ElemType>> ilist)

Replaces the Matrix content with the values defined in the initializer list.

Parameters:

ilist[in] initializer list to use as data source

Returns:

*this Complexity: Linear in ilist.size()

template<typename E>
Matrix &operator=(const MatrixBase<E> &expr)

Evaluates the expression and assign the result to the Matrix. This routine automatically resizes the Matrix to match the expression dimensions (after broadcasting, if applicable). The resizing can be disabled by setting MOCCA_AUTO_RESIZE to 0.

Parameters:

expr[in] a matrix or vector expression

Throws:

std::length_error – if the dimensions of the operands in``expr`` do not match and cannot be broadcasted. Complexity: Linear in expr.size()

Returns:

*this

void fill(ElemType val)

Fills the Matrix with copies of val.

Parameters:

val[in] value to replace the matrix content with Complexity: Linear in this->size()

template<typename InIter, internal::require_input_iterator<InIter> = true>
void fill(InIter first, InIter last)

Replaces the content of a Matrix with values defined in [first, last[.

Parameters:
  • first[in] iterator to the first element in the range

  • last[in] iterator to the “past-the-end” element in the range

Throws:

std::length_error – if `std::distance(first, last) > size()` Complexity: Linear in distance between first and last

template<typename E>
void set_diagonal(const MatrixBase<E> &expr)

Evaluates the vector expression and stores the results into the diagonal of the Matrix.

Parameters:

expr[in] a vector expression to use as data source Complexity: Linear in expr.size()

Element Access

ElemType &operator()(index_t row, index_t col)

Accesses the matrix element at (row, col).

Parameters:
  • row[in] row of the requested element

  • col[in] column of the requested element

Returns:

the reference to requested element Complexity: Constant

ElemType operator()(index_t row, index_t col) const

Accesses the matrix element at (row, col).

Parameters:
  • row[in] row of the requested element

  • col[in] column of the requested element

Returns:

the requested element Complexity: Constant

Capacity

constexpr index_t rows() const noexcept
Returns:

the number of rows in the Matrix Complexity: Constant

constexpr index_t cols() const noexcept
Returns:

the number of columns in the Matrix Complexity: Constant

constexpr index_t size() const noexcept
Returns:

the number of entries in the Matrix Complexity: Constant

Matrix Block

MatrixView1D<Matrix<ElemType>, internal::kRowMajor> row(index_t r)
Parameters:

r[in] the index of the desired row Complexity: Constant

Returns:

the row r of the matrix

MatrixView1D<const Matrix<ElemType>, internal::kRowMajor> row(index_t r) const
Parameters:

r[in] the index of the desired row Complexity: Constant

Returns:

the row r of the matrix

MatrixView1D<Matrix<ElemType>, internal::kColMajor> col(index_t c)
Parameters:

c[in] the index of the desired column Complexity: Constant

Returns:

the column c of the matrix

MatrixView1D<const Matrix<ElemType>, internal::kColMajor> col(index_t c) const
Parameters:

c[in] the index of the desired column Complexity: Constant

Returns:

the column c of the matrix

Modifiers

void deallocate() noexcept

Deallocates the container from memory.

Complexity: Constant

void clear()

Clear the content from the Matrix. This routine do not deallocate the container. Complexity: Constant

void transpose_inplace()

Calculates the transpose of the Matrix in-place. Use this routine to avoid aliasing.

If the matrix is not square, this routine will use additional memory. Complexity: Linear in this->size()

void resize(index_t nrows, index_t ncols)

Resizes the Matrix, changing the number of rows and columns to nrows and ncols, respectively. This routine will clear the previous content from the matrix.

Replaces Array::resize().

Parameters:
  • nrows[in] new number of rows

  • ncols[in] new number of columns Complexity: Constant.

void resize(index_t nrows, index_t ncols, ElemType val)

Resizes the Matrix, changing the number of rows and columns to nrows and ncols, respectively. Then fills the matrix with copies of val. This routine will clear the previous content from the matrix.

Replaces Array::resize().

Parameters:
  • nrows[in] new number of rows

  • ncols[in] new number of columns

  • val[in] value to initialise matrix with Complexity: Constant.

void safe_resize(index_t nrows, index_t ncols)

Resizes the Matrix, changing the number of rows and columns to nrows and ncols, respectively. This routine preserves the original content of the Matrix

Replaces Array::resize().

Parameters:
  • nrows[in] new number of rows

  • ncols[in] new number of columns Complexity: Linear in ncols * nrows.

void safe_resize(index_t nrows, index_t ncols, ElemType val)

Resizes the Matrix, changing the number of rows and columns to nrows and ncols, respectively. This routine preserves the original content of the Matrix The newly created entries are set to val.

Replaces Array::resize().

Parameters:
  • nrows[in] new number of rows

  • ncols[in] new number of columns Complexity: Linear in ncols * nrows.

void reshape(index_t nrows, index_t ncols)

Reshape the Matrix without changing its content.

Warning

The new shape of the matrix must not exceed the allocated space.

Parameters:
  • nrows[in] new number of rows

  • ncols[in] new number of columns

Throws:

std::length_error – if the ``nrows`` or ``ncols`` are less than 1 or ``nrows * ncols > capacity()``. Complexity: Constant

void swap(Matrix &other) noexcept

Swaps the Matrix content with the other Matrix, including any memory allocation.

Parameters:

other[in] another matrix to exchange content with Complexity: Constant

Friends

template<typename T>
friend std::ostream &operator<<(std::ostream &stream, const Matrix<T> &mat)

Stream Operator. Writes the content of the Matrix on a given stream.

Complexity: Linear in this->size()

Parameters:
  • stream[in] output stream (e.g., std::cout)

  • expr[in] CSRMatrix

Returns:

stream