Lab_1 0.1.1
Matrix Library
|
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) | |
Matrix & | operator= (Matrix m) noexcept |
Copy assignment operator. | |
Matrix & | operator+= (const Matrix &m) |
Adds another matrix to the current matrix. | |
Matrix & | operator-= (const Matrix &m) |
Subtracts one matrix from another. | |
Matrix & | operator*= (const double c) noexcept |
Scales a matrix by a scalar. | |
Matrix & | operator*= (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. | |
linalg::Matrix::Matrix | ( | size_t | rows | ) |
Construct a matrix with a given number of rows.
[in] | rows | The number of rows |
std::runtime_error | If 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.
linalg::Matrix::Matrix | ( | size_t | rows, |
size_t | columns ) |
Construct a matrix with a given number of rows and columns.
[in] | rows | The number of rows |
[in] | columns | The number of columns |
std::runtime_error | If 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.
|
noexcept |
Copy constructor.
[in] | m | The 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.
|
noexcept |
Move constructor.
[in] | m | The 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.
|
noexcept |
Construct a matrix from a list of doubles.
[in] | lst | The 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.
linalg::Matrix::Matrix | ( | std::initializer_list< std::initializer_list< double > > | lst | ) |
Construct a matrix from a list of lists of doubles.
[in] | lst | The list of lists of doubles |
std::runtime_error | If 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.
double linalg::Matrix::det | ( | ) | const |
Calculates the determinant of the matrix.
std::runtime_error | If the matrix is empty or not square |
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.
Matrix linalg::Matrix::Gauss | ( | bool | rref | ) | const |
Gaussian elimination.
rref | If true, the result will be in reduced row echelon form (RREF) |
The function performs Gaussian elimination on the matrix. If the parameter rref is true, the result will be in reduced row echelon form (RREF).
double linalg::Matrix::minor | ( | size_t | rows, |
size_t | columns ) const |
Calculates the minor of the matrix at the given position.
[in] | rows | The row of the minor |
[in] | columns | The column of the minor |
std::runtime_error | If the position is outside 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.
double linalg::Matrix::norm | ( | ) | const |
Calculates the norm of the matrix.
std::runtime_error | If 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.
double & linalg::Matrix::operator() | ( | size_t | row, |
size_t | column ) |
Accesses the element at the specified position in the matrix.
[in] | row | The row index |
[in] | column | The column index |
std::out_of_range | If 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.
const double & linalg::Matrix::operator() | ( | size_t | row, |
size_t | column ) const |
Accesses the element at the specified position in the matrix (const version)
[in] | row | The row index |
[in] | column | The column index |
std::out_of_range | If 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.
|
noexcept |
Scales a matrix by a scalar.
[in] | c | The scalar to scale by |
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.
Multiplies the matrix by another matrix.
[in] | m | The matrix to multiply with |
std::runtime_error | If 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.
Adds another matrix to the current matrix.
[in] | m | The matrix to add |
std::runtime_error | If 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.
Subtracts one matrix from another.
[in] | m | The matrix to subtract from the current matrix |
std::runtime_error | If 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.
Copy assignment operator.
[in] | other | The matrix to copy |
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.
double linalg::Matrix::trace | ( | ) | const |
Calculates the trace of the matrix.
std::runtime_error | If 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.