Author: Amin Tahmasebi
This project is a comprehensive matrix manipulation library implemented in C++23, designed to perform a wide range of matrix operations efficiently and intuitively. The project is built using CMake, ensuring easy build and integration into other projects.
- Arithmetic Operations: Perform basic arithmetic operations such as addition, subtraction, and multiplication on matrices.
- Decompositions: Includes methods for LU and QR decompositions.
- Determinant and Inverse: Compute the determinant and inverse of square matrices.
- Norms and Other Properties: Calculate various matrix norms and check matrix properties like symmetry, orthogonality, etc.
- Transformation and Decomposition: Transform matrices and decompose them into simpler forms.
- Utility Functions: Various utility functions like transpose, trace, and submatrix creation are included for convenience.
To use the Matrix library in your project, include the Matrix.hpp
header file and link against the compiled library. Example usage:
Matrix<int> mat1(2, 3, {1, 2, 3, 4, 5, 6});
std::cout << "Matrix 1:\n" << mat1 << std::endl;
Matrix<int> mat2(2, 3, {6, 5, 4, 3, 2, 1});
auto result = mat1 + mat2;
std::cout << "Matrix Addition Result:\n" << result << std::endl;
auto transposed = mat1.transpose();
std::cout << "Transposed Matrix:\n" << transposed << std::endl;
Matrix<double> squareMat(3, 3, {1, 2, 3, 0, 4, 5, 1, 0, 6});
double det = squareMat.determinant();
std::cout << "Determinant: " << det << std::endl;
auto inverseMat = squareMat.inverse();
std::cout << "Inverse Matrix:\n" << inverseMat << std::endl;
bool isSymmetric = squareMat.isSymmetric();
std::cout << "Is Symmetric? " << std::boolalpha << isSymmetric << std::endl;
auto [L, U] = squareMat.luDecomposition();
std::cout << "L Matrix:\n" << L << "\nU Matrix:\n" << U << std::endl;
Matrix<double> matQR(3, 2, {12, -51, 4, 6, 167, -68});
auto [Q, R] = matQR.qrDecomposition();
std::cout << "Q Matrix:\n" << Q << "\nR Matrix:\n" << R << std::endl;
auto randomMat = Matrix<int>::createRandomMatrix(3, 3, 1, 10);
std::cout << "Random Matrix:\n" << randomMat << std::endl;
Matrix<int> matA(2, 2, {1, 2, 3, 4});
Matrix<int> matB(2, 2, {0, 5, 6, 7});
auto kronecker = matA.kroneckerProduct(matB);
std::cout << "Kronecker Product:\n" << kronecker << std::endl;
Each method in the Matrix
class is briefly explained below:
- Matrix(): Default constructor, creates an empty matrix.
- Matrix(size_t row, size_t col): Constructs a matrix of given dimensions, uninitialized data.
- Matrix(size_t row, size_t col, std::vector& data): Constructs a matrix with data.
- Matrix(size_t row, size_t col, std::initializer_list data): Constructs a matrix from an initializer list.
- Matrix(const Matrix& other): Copy constructor.
- Matrix(Matrix&& other): Move constructor.
- operator=(): Copy and move assignment operators.
- set(): Sets the value at a specific row and column.
- get(): Retrieves the value at a specific row and column.
- operator(): Access or modify elements via (row, col) syntax.
- isSquare(): Checks if the matrix is square.
- isSymmetric(): Checks if the matrix is symmetric.
- ... (Include a brief description for each method similarly.)
- transpose(): Returns the transpose of the matrix.
- determinant(): Computes the determinant of the matrix.
- inverse(): Calculates the inverse of the matrix.
- luDecomposition(): Performs LU decomposition.
- qrDecomposition(): Performs QR decomposition.
- sizeRow() / sizeCol(): Returns the number of rows/columns.
- fill(): Fills the matrix with a specific value.
- swapRows() / swapCols(): Swaps two rows/columns.
- kroneckerProduct(): Computes the Kronecker product with another matrix.
- cofactor(): Calculates the cofactor matrix.
For detailed examples and more complex operations, refer to the included tests or example programs.
Contributions are welcome. Please open an issue or pull request to suggest improvements or add new features.
This project is open-source and available under the MIT License.