Matrix class

Square matrix

Constructors, destructors, conversion operators

Matrix(size_t ndim, double init = 0.0) explicit

Public functions

void clear(double value = 0.0)
auto operator()(size_t row, size_t col) -> double&
auto operator()(size_t row, size_t col) const -> const double&
auto operator*=(double alpha) -> Matrix&
auto operator*(double alpha) const -> Matrix
void identity()
auto diagonal() -> std::slice_array<double>
auto secondary_diagonal() -> std::slice_array<double>
auto row(std::size_t row) -> std::slice_array<double>
auto column(std::size_t col) -> std::slice_array<double>
auto trace() const -> double

Function documentation

Matrix::Matrix(size_t ndim, double init = 0.0) explicit

Parameters
ndim in - The dimension of the matrix (number of rows/columns)
init in - Optional initial value for all elements. Default is 0.0.

Constructor to create a new Matrix object.

Example: Matrix A(2, 2);

void Matrix::clear(double value = 0.0)

Parameters
value in - The value to set all elements to. Defaults to 0.0.

Sets all elements of the matrix to the given value.

Example:

Matrix A(2, 2);
A.clear(1.0);

double& Matrix::operator()(size_t row, size_t col)

Parameters
row in - Row index to access
col in - Column index to access
Returns Reference to the element at the given row and column.

Operator overload to access elements using 2D array syntax.

Allows modifying elements using matrix(row, col) = value.

Example:

Matrix A(2, 2);
A(0, 0) = 1.0;
A(0, 1) = 2.0;
A(1, 0) = 3.0;
A(1, 1) = 4.0;

const double& Matrix::operator()(size_t row, size_t col) const

Parameters
row in - Row index
col in - Column index
Returns Constant reference to element at (row, col)

Read-only accessor for matrix elements using 2D array syntax.

Allows accessing elements using matrix(row, col).

Example:

Matrix A(2, 2);
A(0, 0) = 1.0;
A(0, 1) = 2.0;
A(1, 0) = 3.0;
A(1, 1) = 4.0;

Matrix& Matrix::operator*=(double alpha)

Parameters
alpha in - The scalar value to multiply each element by.
Returns Reference to this matrix after multiplication.

Multiplies each element of the matrix by the given scalar value.

Example:

Matrix A(2, 2);
A *= 2.0;

Matrix Matrix::operator*(double alpha) const

Parameters
alpha in - The scalar value to multiply the matrix by.
Returns A new Matrix object containing the result of the multiplication.

Overloads the multiplication operator (*) to multiply the matrix by a scalar value.

This performs an element-wise multiplication of the matrix by the scalar value alpha.

Example:

matrix = Matrix(3, 0.0) matrix *= 2.0 matrix

valarray([0., 0., 0., 0., 0., 0., 0., 0., 0.], [3])

void Matrix::identity()

Sets the matrix to be an identity matrix by clearing it and then setting all diagonal elements to 1.

Example:

matrix = Matrix(3, 0.0) matrix.identity() matrix

valarray([1., 0., 0., 0., 1., 0., 0., 0., 1.], [3])

std::slice_array<double> Matrix::diagonal()

Returns View of the diagonal elements as a slice_array.

Returns a view representing the diagonal elements of the matrix.

This slices the underlying data array to extract the elements where row index equals column index, corresponding to the diagonal.

Example:

matrix = Matrix(3, 0.0) matrix(0, 0) = 1.0 matrix(1, 1) = 2.0 matrix(2, 2) = 3.0 matrix.diagonal()

valarray([1., 2., 3.], 3)

std::slice_array<double> Matrix::secondary_diagonal()

Returns View of the secondary diagonal elements as a slice_array.

Returns a view representing the secondary diagonal elements of the matrix.

This slices the underlying data array to extract the elements where row index + column index equals ndim-1, corresponding to the secondary diagonal.

Example:

matrix = Matrix(3, 0.0) matrix(0, 0) = 1.0 matrix(1, 1) = 2.0 matrix(2, 2) = 3.0 matrix.secondary_diagonal()

valarray([0., 0.], 2)

std::slice_array<double> Matrix::row(std::size_t row)

Parameters
row in - The index of the row to extract.
Returns View of the row elements as a slice_array.

Returns a view representing a row of the matrix.

This slices the underlying data array to extract the elements of the specified row index.

Example:

matrix = Matrix(3, 0.0) matrix(0, 0) = 1.0 matrix(1, 1) = 2.0 matrix(2, 2) = 3.0 matrix.row(0)

valarray([1., 0., 0.], 3)

matrix.row(1)

valarray([0., 2., 0.], 3)

matrix.row(2)

valarray([0., 0., 3.], 3)

std::slice_array<double> Matrix::column(std::size_t col)

Parameters
col in - The index of the column to extract.
Returns View of the column elements as a slice_array.

Returns a view representing a column of the matrix.

This slices the underlying data array to extract the elements of the specified column index.

Example:

matrix = Matrix(3, 0.0) matrix(0, 0) = 1.0 matrix(1, 1) = 2.0 matrix(2, 2) = 3.0 matrix.column(0)

valarray([1., 0., 0.], 3)

matrix.column(1)

valarray([0., 2., 0.], 3)

matrix.column(2)

valarray([0., 0., 3.], 3)

double Matrix::trace() const

Returns The trace of the matrix as a double.

Calculates the trace of the matrix, which is the sum of the diagonal elements.

The trace is computed by slicing the data array to extract just the diagonal elements, and then summing those elements.

Example:

matrix = Matrix(3, 0.0) matrix(0, 0) = 1.0 matrix(1, 1) = 2.0 matrix(2, 2) = 3.0 matrix.trace()

6.0