Two-mensional matrix

class CMatrix2D

This class describes a two-dimensional matrix in dense format.

Public Functions

CMatrix2D()

Basic constructor.

Creates empty matrix with zero in all rows and columns.

CMatrix2D(size_t _rows, size_t _cols)

Creates a matrix with the specified dimensions and sets all cells to zero.

Parameters
  • _rows – Number of rows.

  • _cols – Number of columns.

void Resize(size_t _rows, size_t _cols)

Sets new dimensions of the matrix.

Parameters
  • _rows – Number of rows.

  • _cols – Number of columns.

void Assign(size_t _rows, size_t _cols, double _val)

Sets new dimensions of the matrix and fills it with the specified value.

Parameters
  • _rows – Number of rows.

  • _cols – Number of columns.

  • _val – Target value.

size_t Rows() const

Returns the current number of rows.

Returns

Number of rows.

size_t Cols() const

Returns the current number of columns.

Returns

Number of columns.

d_vect_t &operator[](size_t _row)

Returns reference to the vector of values of the specified row.

Parameters

_row – Index of row.

Returns

Reference to the vector of values.

const d_vect_t &operator[](size_t _row) const

Returns const reference to the vector of values of the specified row.

Parameters

_row – Index of row.

Returns

Target reference to the vector of values.

d_vect_t GetRow(size_t _row) const

Returns the vector of values for the specified row.

If there is no such row, returns an empty vector.

Parameters

_row – Index of row.

Returns

Vector of values.

d_vect_t GetCol(size_t _col) const

Returns the vector of values for the specified column.

If there is no such column, returns an empty vector.

Parameters

_col – Index of column.

Returns

Vector of values.

d_matr_t GetMatrix() const

Returns all values in the vector-of-vectors form.

Returns

All values in the vector-of-vectors form.

void SetRow(size_t _row, const d_vect_t &_vector)

Sets data to the existing row.

If dimensions do not match, does nothing.

Parameters
  • _row – Index of row.

  • _vector – Vector of values.

void SetCol(size_t _col, const d_vect_t &_vector)

Sets data to the existing column.

If dimensions do not match, does nothing.

Parameters
  • _col – Index of column.

  • _vector – Vector of values.

void SetMatrix(const d_matr_t &_matrix)

Sets all values to the matrix in the vector-of-vectors form.

If dimensions do not match, does nothing.

Parameters

_matrix – All values in the vector-of-vectors form.

void Fill(double _val)

Fills the matrix with the specified value.

Parameters

_val – Target value.

void Clear()

Removes all data and information about dimensions from the matrix.

d_vect_t ToVector() const

Returns a vector with all values joining all rows one after another.

Returns

Vector of all data.

void Normalize()

Normalizes values of the matrix.

CMatrix2D &operator+=(double _val)

Adds a value to all elements of the matrix.

Parameters

_val – Value.

Returns

Reference to this matrix.

CMatrix2D operator+(double _val) const

Adds a value to all elements of the matrix.

Parameters

_val – Value.

Returns

Copy of this matrix.

CMatrix2D &operator-=(double _val)

Subtracts a value from all elements of the matrix.

Parameters

_val – Value.

Returns

Reference to this matrix.

CMatrix2D operator-(double _val) const

Subtracts a value from all elements of the matrix.

Parameters

_val – Value.

Returns

Copy of this matrix.

CMatrix2D &operator*=(double _val)

Multiplies a value with all elements of the matrix.

Parameters

_val – Value.

Returns

Reference to this matrix.

CMatrix2D operator*(double _val) const

Multiplies a value with all elements of the matrix.

Parameters

_val – Value.

Returns

Copy of this matrix.

CMatrix2D &operator/=(double _val)

Divides all elements of the matrix by a value .

Parameters

_val – Value.

Returns

Reference to this matrix.

CMatrix2D operator/(double _val) const

Divides all elements of the matrix by a value .

Parameters

_val – Value.

Returns

Copy of this matrix.

CMatrix2D &operator+=(const CMatrix2D &_matrix)

Adds two matrices element-wise.

If dimensions of the matrices do not match, std::runtime_error exception is thrown.

Parameters

_matrix – Other matrix.

Returns

Reference to this matrix.

CMatrix2D operator+(const CMatrix2D &_matrix) const

Adds two matrices element-wise.

If dimensions of the matrices do not match, std::runtime_error exception is thrown.

Parameters

_matrix – Other matrix.

Returns

Copy of this matrix.

CMatrix2D &operator-=(const CMatrix2D &_matrix)

Subtracts two matrices element-wise.

If dimensions of the matrices do not match, std::runtime_error exception is thrown.

Parameters

_matrix – Other matrix.

Returns

Reference to this matrix.

CMatrix2D operator-(const CMatrix2D &_matrix) const

Subtracts two matrices element-wise.

If dimensions of the matrices do not match, std::runtime_error exception is thrown.

Parameters

_matrix – Other matrix.

Returns

Copy of this matrix.

CMatrix2D operator*(const CMatrix2D &_matrix) const

Multiplies two matrices element-wise.

If dimensions of the matrices do not match, std::runtime_error exception is thrown.

Parameters

_matrix – Other matrix.

Returns

Copy of this matrix.

Public Static Functions

static CMatrix2D Identity(size_t _size)

Returns identity matrix with the specified dimensions.

Parameters

_size – Size of the square matrix.

Returns

Identity matrix.