Lab_1 0.1.1
Matrix Library
Loading...
Searching...
No Matches
linalg::Matrix Class Reference

Public Member Functions

 Matrix (size_t rows)
 Construct a matrix with a given number of rows.
 
 Matrix (size_t rows, size_t columns)
 Construct a matrix with a given number of rows and columns.
 
 Matrix (const Matrix &m) noexcept
 Copy constructor.
 
 Matrix (Matrix &&m) noexcept
 Move constructor.
 
 Matrix (std::initializer_list< double > lst) noexcept
 Construct a matrix from a list of doubles.
 
 Matrix (std::initializer_list< std::initializer_list< double > > lst)
 Construct a matrix from a list of lists of doubles.
 
size_t rows () const noexcept
 
size_t columns () const noexcept
 
bool empty () const noexcept
 
void reshape (size_t new_rows, size_t new_columns)
 
double & operator() (size_t row, size_t column)
 Accesses the element at the specified position in the matrix.
 
const double & operator() (size_t row, size_t column) const
 Accesses the element at the specified position in the matrix (const version)
 
Matrixoperator= (Matrix m) noexcept
 Copy assignment operator.
 
Matrixoperator+= (const Matrix &m)
 Adds another matrix to the current matrix.
 
Matrixoperator-= (const Matrix &m)
 Subtracts one matrix from another.
 
Matrixoperator*= (const double c) noexcept
 Scales a matrix by a scalar.
 
Matrixoperator*= (const Matrix &m)
 Multiplies the matrix by another matrix.
 
double trace () const
 Calculates the trace of the matrix.
 
double norm () const
 Calculates the norm of the matrix.
 
Matrix Gauss (bool rref) const
 Gaussian elimination.
 
double det () const
 Calculates the determinant of the matrix.
 
double minor (size_t rows, size_t columns) const
 Calculates the minor of the matrix at the given position.
 

Constructor & Destructor Documentation

◆ Matrix() [1/6]

linalg::Matrix::Matrix ( size_t rows)

Construct a matrix with a given number of rows.

Parameters
[in]rowsThe number of rows
Exceptions
std::runtime_errorIf the number of rows is negative

This constructor initializes a matrix with the specified number of rows. It allocates memory for the matrix elements and sets the internal row and column counters. The number of columns is set to 1. If the given number of rows is negative, an exception is thrown.

◆ Matrix() [2/6]

linalg::Matrix::Matrix ( size_t rows,
size_t columns )

Construct a matrix with a given number of rows and columns.

Parameters
[in]rowsThe number of rows
[in]columnsThe number of columns
Exceptions
std::runtime_errorIf the number of rows or columns is negative

This constructor initializes a matrix with the specified number of rows and columns. It allocates memory for the matrix elements and sets the internal row and column counters. If the given number of rows or columns is negative, an exception is thrown.

◆ Matrix() [3/6]

linalg::Matrix::Matrix ( const Matrix & m)
noexcept

Copy constructor.

Parameters
[in]mThe matrix to be copied

This constructor creates a new matrix object as a copy of the given matrix. It allocates memory for the matrix elements and copies the data from the source matrix. The constructor is marked as noexcept, indicating that no exceptions will be thrown during the copy operation.

◆ Matrix() [4/6]

linalg::Matrix::Matrix ( Matrix && m)
noexcept

Move constructor.

Parameters
[in]mThe matrix to be moved

This constructor is used to move the contents of another matrix to the current one. The contents of the other matrix are swapped with the contents of the current matrix. The move constructor is marked as noexcept, which means that no exceptions are thrown during the move operation.

◆ Matrix() [5/6]

linalg::Matrix::Matrix ( std::initializer_list< double > lst)
noexcept

Construct a matrix from a list of doubles.

Parameters
[in]lstThe list of doubles

This constructor initializes a matrix with the number of rows equal to the size of the list and a single column. Each element of the list becomes an element in the matrix. The matrix is constructed in a way that each element of the list corresponds to a row in the matrix.

◆ Matrix() [6/6]

linalg::Matrix::Matrix ( std::initializer_list< std::initializer_list< double > > lst)

Construct a matrix from a list of lists of doubles.

Parameters
[in]lstThe list of lists of doubles
Exceptions
std::runtime_errorIf the lists of the list are not of equal size

The function iterates over all sublists of the given list and checks if they are all of the same size. If not, it throws an exception. Otherwise, it allocates memory for the matrix and copies the elements of the sublists to the matrix.

Member Function Documentation

◆ det()

double linalg::Matrix::det ( ) const

Calculates the determinant of the matrix.

Exceptions
std::runtime_errorIf the matrix is empty or not square
Returns
The determinant of the matrix

The function creates a temporary matrix to avoid modifying the original matrix. The determinant is calculated by multiplying the elements on the main diagonal of the matrix after Gaussian elimination.

◆ Gauss()

Matrix linalg::Matrix::Gauss ( bool rref) const

Gaussian elimination.

Parameters
rrefIf true, the result will be in reduced row echelon form (RREF)
Returns
The result of Gaussian elimination

The function performs Gaussian elimination on the matrix. If the parameter rref is true, the result will be in reduced row echelon form (RREF).

◆ minor()

double linalg::Matrix::minor ( size_t rows,
size_t columns ) const

Calculates the minor of the matrix at the given position.

Parameters
[in]rowsThe row of the minor
[in]columnsThe column of the minor
Exceptions
std::runtime_errorIf the position is outside of the matrix
Returns
The minor of the matrix

The minor is calculated by creating a temporary matrix that excludes the row and column of the given position. The determinant of this temporary matrix is then calculated and returned.

◆ norm()

double linalg::Matrix::norm ( ) const

Calculates the norm of the matrix.

Returns
The norm of the matrix
Exceptions
std::runtime_errorIf the matrix is empty

The norm of a matrix is the square root of the sum of the squares of all elements. This method is const, as it does not modify the matrix.

◆ operator()() [1/2]

double & linalg::Matrix::operator() ( size_t row,
size_t column )

Accesses the element at the specified position in the matrix.

Parameters
[in]rowThe row index
[in]columnThe column index
Returns
A reference to the element at the specified position
Exceptions
std::out_of_rangeIf the specified position is out of the matrix bounds

This function provides direct access to the element at the given row and column indices. It checks if the indices are within the matrix bounds and throws an exception if they are out of range. The function returns a reference, which allows modification of the element at the specified position.

◆ operator()() [2/2]

const double & linalg::Matrix::operator() ( size_t row,
size_t column ) const

Accesses the element at the specified position in the matrix (const version)

Parameters
[in]rowThe row index
[in]columnThe column index
Returns
A const reference to the element at the specified position
Exceptions
std::out_of_rangeIf the specified position is out of the matrix bounds

This function provides read-only access to the element at the given row and column indices. It checks if the indices are within bounds and throws an exception if not.

◆ operator*=() [1/2]

Matrix & linalg::Matrix::operator*= ( const double c)
noexcept

Scales a matrix by a scalar.

Parameters
[in]cThe scalar to scale by
Returns
The scaled matrix

The result is a new matrix that is the result of scaling the input matrix by the given scalar. The original matrix is not modified.

◆ operator*=() [2/2]

Matrix & linalg::Matrix::operator*= ( const Matrix & m)

Multiplies the matrix by another matrix.

Parameters
[in]mThe matrix to multiply with
Returns
A reference to the resulting matrix
Exceptions
std::runtime_errorIf the number of columns of the current matrix is not equal to the number of rows of the matrix to multiply with

This function performs matrix multiplication. The resulting matrix will have the same number of rows as the current matrix and the same number of columns as the matrix being multiplied. The original matrix is replaced with the result.

◆ operator+=()

Matrix & linalg::Matrix::operator+= ( const Matrix & m)

Adds another matrix to the current matrix.

Parameters
[in]mThe matrix to add
Returns
A reference to the current matrix after addition
Exceptions
std::runtime_errorIf the sizes of the matrices are not equal

This function performs element-wise addition of another matrix to the current matrix. It checks if the sizes of the matrices are equal, and throws an exception if not. The result replaces the original matrix.

◆ operator-=()

Matrix & linalg::Matrix::operator-= ( const Matrix & m)

Subtracts one matrix from another.

Parameters
[in]mThe matrix to subtract from the current matrix
Returns
The result of the subtraction
Exceptions
std::runtime_errorIf the sizes of the matrices are not equal

This method subtracts the matrix m from the current matrix. It checks if the sizes of the matrices are equal, and throws an exception if not. Then it subtracts the elements of the matrix m from the current matrix.

◆ operator=()

Matrix & linalg::Matrix::operator= ( Matrix other)
noexcept

Copy assignment operator.

Parameters
[in]otherThe matrix to copy
Returns
A reference to the current matrix after assignment

This method implements the copy-swap idiom, which allows to use this operator as a move assignment operator as well. It swaps the contents of the current matrix with the contents of the other matrix, and then returns a reference to the current matrix.

◆ trace()

double linalg::Matrix::trace ( ) const

Calculates the trace of the matrix.

Returns
The trace of the matrix
Exceptions
std::runtime_errorIf the matrix is empty or not square

The trace of a matrix is the sum of the elements on the main diagonal. This method is const, as it does not modify the matrix.


The documentation for this class was generated from the following files: