C++ library for working with matrices.
More...
#include "matrix.h"
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
|
size_t | linalg::max_length_first (const Matrix &m, const std::ostream &out) |
| Returns the maximum number of digits in the first column of the matrix.
|
|
size_t | linalg::max_length_not_first (const Matrix &m, const std::ostream &out) |
| Returns the maximum number of digits in the second and later columns of the matrix.
|
|
bool | linalg::equal (const double &a, const double &b) |
| Compares two double values for equality.
|
|
std::ostream & | linalg::operator<< (std::ostream &out, const Matrix &m) |
| Prints the matrix to the output stream.
|
|
Matrix | linalg::operator+ (const Matrix &m1, const Matrix &m2) |
| Sums two matrices.
|
|
Matrix | linalg::operator* (const Matrix &m, const double c) noexcept |
| Scales a matrix by a scalar.
|
|
Matrix | linalg::operator* (const double c, const Matrix &m) noexcept |
| Scales a matrix by a scalar.
|
|
Matrix | linalg::operator- (const Matrix &m1, const Matrix &m2) |
| Subtracts one matrix from another.
|
|
Matrix | linalg::operator* (const Matrix &m1, const Matrix &m2) |
| Multiply two matrices.
|
|
bool | linalg::operator== (const Matrix &m1, const Matrix &m2) |
| delat
|
|
bool | linalg::operator!= (const Matrix &m1, const Matrix &m2) |
| Checks if two matrices are not equal.
|
|
Matrix | linalg::concatenate (const Matrix &m1, const Matrix &m2) |
| Concatenates two matrices horizontally.
|
|
Matrix | linalg::transpose (const Matrix &m) |
| Returns the transposed matrix.
|
|
Matrix | linalg::uni (size_t n) |
| Creates a unit matrix of size n x n.
|
|
const Matrix | linalg::invert_old (const Matrix &m) |
| Calculates the inverse matrix.
|
|
Matrix | linalg::invert (const Matrix &m) |
| Inverts a square matrix.
|
|
Matrix | linalg::power (const Matrix &m, int p) |
| Calculates the power of the matrix.
|
|
C++ library for working with matrices.
- Version
- 0.1.0
- Date
- 2024-10-21
- Authors
- vmarkov20
◆ concatenate()
Matrix linalg::concatenate |
( |
const Matrix & | m1, |
|
|
const Matrix & | m2 ) |
Concatenates two matrices horizontally.
- Parameters
-
[in] | m1 | the first matrix |
[in] | m2 | the second matrix |
- Exceptions
-
std::runtime_error | if the number of rows of matrices are not equal |
- Returns
- the resulting matrix
If both matrices are empty, the result is an empty matrix. If only one of the matrices is empty, the result is the other matrix.
◆ equal()
bool linalg::equal |
( |
const double & | a, |
|
|
const double & | b ) |
Compares two double values for equality.
- Parameters
-
[in] | a | The first number |
[in] | b | The second number |
- Returns
- true if the difference between the two numbers is less than EPS
EPS is a small value used to compare floating point numbers. It is set to 1e-10
◆ invert()
Matrix linalg::invert |
( |
const Matrix & | m | ) |
|
Inverts a square matrix.
- Parameters
-
[in] | m | The matrix to be inverted. Must be square and non-degenerate. |
- Returns
- A new matrix that is the inverse of the input matrix.
- Exceptions
-
std::runtime_error | If the matrix is degenerate, empty, or not square. |
- This function takes a square matrix and returns its inverse. If the matrix is degenerate (i.e., its determinant is zero), an exception is thrown. Similarly, if the matrix is empty or not square, the function throws an exception.
The function internally uses Gaussian elimination to compute the inverse. It concatenates the input matrix with an identity matrix, applies Gaussian elimination, and extracts the inverse matrix from the result.
◆ invert_old()
const Matrix linalg::invert_old |
( |
const Matrix & | m | ) |
|
Calculates the inverse matrix.
- Parameters
-
- Returns
- The inverse matrix
- Exceptions
-
std::runtime_error | If the matrix is empty, not square, or determinant is zero |
- Deprecated
- This implementation uses an older method of matrix inversion. Consider using the new method
invert
for better performance.
This function computes the inverse of a given square matrix. It first checks if the matrix is square and has a non-zero determinant. If these conditions are met, it calculates the inverse using the formula involving minors, cofactors, and the determinant.
◆ max_length_first()
size_t linalg::max_length_first |
( |
const Matrix & | m, |
|
|
const std::ostream & | out ) |
Returns the maximum number of digits in the first column of the matrix.
- Parameters
-
[in] | m | The matrix |
[in] | out | The output stream |
- Returns
- The maximum number of digits
The function iterates over all elements of the first column of the matrix and measures the length of each element when it is printed to the given output stream. The maximum length of all elements is returned as the result.
◆ max_length_not_first()
size_t linalg::max_length_not_first |
( |
const Matrix & | m, |
|
|
const std::ostream & | out ) |
Returns the maximum number of digits in the second and later columns of the matrix.
- Parameters
-
[in] | m | The matrix |
[in] | out | The output stream |
- Returns
- The maximum number of digits
The function iterates over all elements of the matrix except the first column and measures the length of each element when it is printed to the given output stream. The maximum length of all elements is returned as the result.
◆ operator!=()
bool linalg::operator!= |
( |
const Matrix & | m1, |
|
|
const Matrix & | m2 ) |
Checks if two matrices are not equal.
- Parameters
-
[in] | m1 | The first matrix |
[in] | m2 | The second matrix |
- Returns
- true if the matrices are not equal, false otherwise
The function checks if the two matrices are not equal. This function is the opposite of the operator==. If the operator== returns false, this function returns true and vice versa.
◆ operator*() [1/3]
Matrix linalg::operator* |
( |
const double | c, |
|
|
const Matrix & | m ) |
|
noexcept |
Scales a matrix by a scalar.
- Parameters
-
[in] | c | The scalar to scale by |
[in] | m | The matrix to scale |
- Returns
- The scaled matrix
This function multiplies the matrix m by the scalar c. It is equivalent to calling m * c.
◆ operator*() [2/3]
Matrix linalg::operator* |
( |
const Matrix & | m, |
|
|
const double | c ) |
|
noexcept |
Scales a matrix by a scalar.
- Parameters
-
[in] | c | The 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*() [3/3]
Matrix linalg::operator* |
( |
const Matrix & | m1, |
|
|
const Matrix & | m2 ) |
Multiply two matrices.
- Parameters
-
[in] | m1 | The first matrix |
[in] | m2 | The second matrix |
- Returns
- The result of the multiplication
- Exceptions
-
std::runtime_error | If the number of columns of the first matrix is not equal to the number of rows of the second matrix |
The function multiplies two matrices. The function first checks if the number of columns of the first matrix is equal to the number of rows of the second matrix. If the sizes are different, the function throws an exception. Otherwise, the function multiplies the matrices and returns the result.
◆ operator+()
Matrix linalg::operator+ |
( |
const Matrix & | m1, |
|
|
const Matrix & | m2 ) |
Sums two matrices.
- Parameters
-
[in] | m1 | The first matrix |
[in] | m2 | The second matrix |
- Exceptions
-
std::runtime_error | If the matrices have different sizes |
- Returns
- The sum of the two matrices
This function adds two matrices element-wise. The result is a new matrix, the original matrices are unchanged.
◆ operator-()
Matrix linalg::operator- |
( |
const Matrix & | m1, |
|
|
const Matrix & | m2 ) |
Subtracts one matrix from another.
- Parameters
-
[in] | m1 | The first matrix |
[in] | m2 | The second matrix |
- Returns
- The result of the subtraction
- Exceptions
-
std::runtime_error | If the sizes of the matrices are not equal |
The function subtracts the second matrix from the first matrix and returns the resulting matrix. It first checks if the sizes of the matrices are equal. If they are not, it throws an exception.
◆ operator<<()
std::ostream & linalg::operator<< |
( |
std::ostream & | out, |
|
|
const Matrix & | m ) |
Prints the matrix to the output stream.
- Parameters
-
[in] | out | The output stream |
[in] | m | The matrix to print |
- Returns
- The output stream after printing the matrix
This function prints the matrix in a human-readable format to the output stream. It first checks if the matrix is empty and prints "|empty|". Otherwise, it prints the matrix row-by-row, with elements separated by spaces and rows separated by newline characters. The elements are formatted according to the settings of the output stream. If an element is equal to zero, it is printed as "0". The width of the first element is determined by the maximum number of digits in the first column, and the width of other elements is determined by the maximum number of digits in the other columns. The elements are aligned to the right.
◆ operator==()
bool linalg::operator== |
( |
const Matrix & | m1, |
|
|
const Matrix & | m2 ) |
delat
Checks if two matrices are equal.
- Parameters
-
[in] | m1 | The first matrix |
[in] | m2 | The second matrix |
- Returns
- true if the matrices are equal, false otherwise
The function checks if the two matrices are equal. If the number of rows or columns of the matrices is different, the function returns false. Otherwise, the function compares each element of the matrices. If any of the elements are not equal, the function returns false. If all the elements are equal, the function returns true.
◆ power()
Matrix linalg::power |
( |
const Matrix & | m, |
|
|
int | p ) |
Calculates the power of the matrix.
- Parameters
-
[in] | m | Input matrix |
[in] | deg | Degree of the power |
- Returns
- The result of the power of the matrix
- Exceptions
-
std::runtime_error | If the matrix is empty or the dimensions are different |
This function computes the power of a given matrix. It first checks if the matrix is empty or if the dimensions are different, and if so, throws an exception. If the degree of the power is 0, it returns the unit matrix. If the degree is negative, it returns the inverse of the matrix. Otherwise, it multiplies the matrix by itself deg times.
Calculates the power of the matrix
- Parameters
-
[in] | m | The matrix to be raised to a power. Must be square. |
[in] | p | The exponent. Can be a positive or negative integer. |
- Returns
- A new matrix that is the result of raising the input matrix to the power
p
.
- Exceptions
-
std::runtime_error | If the matrix is non-invertible when p is negative. |
This function raises a matrix m
to an integer power p
. If p
is zero, the function returns the identity matrix. For positive powers, the matrix is multiplied by itself p
times. For negative powers, the function returns the inverse of the matrix raised to the absolute value of p
.
◆ transpose()
Matrix linalg::transpose |
( |
const Matrix & | m | ) |
|
Returns the transposed matrix.
- Parameters
-
- Returns
- The transposed matrix
The transposed matrix is a matrix that has the same elements as the original one, but the elements are rearranged so that the rows of the original matrix become the columns of the transposed matrix, and the columns of the original matrix become the rows of the transposed matrix.
◆ uni()
Matrix linalg::uni |
( |
size_t | n | ) |
|
Creates a unit matrix of size n x n.
- Parameters
-
[in] | n | The size of the unit matrix |
- Returns
- A unit matrix with ones on the diagonal and zeros elsewhere
A unit matrix, also known as an identity matrix, is a square matrix in which all the elements of the principal diagonal are ones and all other elements are zeros.