This guide explains how to use the C++ Matrix Library in your own projects.
The Matrix Library provides the following functionality:
- LU Decomposition: Decompose a matrix A into L and U matrices where A = L × U
- QR Decomposition: Decompose a matrix A into Q and R matrices where A = Q × R
- SVD Decomposition: Decompose a matrix A into U, S, and V^T matrices where A = U × S × V^T
- Eigenvalue Computation: Find the dominant eigenvalue and eigenvector using power iteration
- Sparse Matrix Operations: Efficient operations on sparse matrices including addition, multiplication, transpose, and determinant calculation
- Include the library as a subdirectory in your project's
CMakeLists.txt:
cmake_minimum_required(VERSION 3.31)
project(YourProject)
set(CMAKE_CXX_STANDARD 20)
# Add the MatrixLib as a subdirectory
add_subdirectory(../MatrixLib MatrixLib)
# Create your executable
add_executable(your_app main.cpp)
# Link against the MatrixLib
target_link_libraries(your_app PRIVATE MatrixLib)
# Include directories for MatrixLib headers
target_include_directories(your_app PRIVATE ../MatrixLib/include)- Include the headers in your C++ code:
#include "SparseMatrix.h"
#include "LUDecomposition.h"
#include "QRDecomposition.h"
#include "SVDDecomposition.h"
#include "EigenSolver.h"If you prefer not to use CMake, you can compile directly:
g++ -std=c++20 -I../MatrixLib/include your_file.cpp ../MatrixLib/src/*.cpp -o your_program#include "LUDecomposition.h"
std::vector<std::vector<double>> A = {
{2, 1, 1},
{1, 3, 2},
{1, 0, 0}
};
std::vector<std::vector<double>> L, U;
luDecomposition(A, L, U);
// Now L and U contain the LU decomposition of A#include "QRDecomposition.h"
std::vector<std::vector<double>> A = {
{2, 1, 1},
{1, 3, 2},
{1, 0, 0}
};
std::vector<std::vector<double>> Q, R;
qrDecomposition(A, Q, R);
// Now Q and R contain the QR decomposition of A#include "SVDDecomposition.h"
std::vector<std::vector<double>> A = {
{2, 1, 1},
{1, 3, 2},
{1, 0, 0}
};
std::vector<std::vector<double>> U, S, Vt;
svdDecomposition(A, U, S, Vt);
// Now U, S, and Vt contain the SVD decomposition of A#include "EigenSolver.h"
std::vector<std::vector<double>> A = {
{2, 1, 1},
{1, 3, 2},
{1, 0, 0}
};
auto [eigenvalue, eigenvector] = powerIteration(A);
// eigenvalue contains the dominant eigenvalue
// eigenvector contains the corresponding eigenvector#include "SparseMatrix.h"
// Create a sparse matrix
SparseMatrix sparse(3, 3);
sparse.set(0, 0, 2.0);
sparse.set(0, 1, 1.0);
sparse.set(1, 1, 3.0);
sparse.set(2, 2, 1.0);
// Get a value
double value = sparse.get(0, 0); // Returns 2.0
// Convert to dense matrix
auto dense = sparse.toDense();
// Transpose
SparseMatrix transposed = sparse.transpose();
// Determinant
double det = sparse.determinant();
// Addition
SparseMatrix sparse2(3, 3);
sparse2.set(0, 0, 1.0);
SparseMatrix sum = sparse.add(sparse2);void luDecomposition(const std::vector<std::vector<double>>& A, std::vector<std::vector<double>>& L, std::vector<std::vector<double>>& U)
void qrDecomposition(const std::vector<std::vector<double>>& A, std::vector<std::vector<double>>& Q, std::vector<std::vector<double>>& R)
void svdDecomposition(const std::vector<std::vector<double>>& A, std::vector<std::vector<double>>& U, std::vector<std::vector<double>>& S, std::vector<std::vector<double>>& Vt)
std::pair<double, std::vector<double>> powerIteration(const std::vector<std::vector<double>>& matrix, int maxIterations = 1000, double tolerance = 1e-6)
- Constructor:
SparseMatrix(int rows, int cols) void set(int i, int j, double val)- Set value at position (i,j)double get(int i, int j) const- Get value at position (i,j)void print() const- Print the matrixdouble determinant() const- Calculate determinantSparseMatrix add(const SparseMatrix& other) const- Add two sparse matricesSparseMatrix transpose() const- Transpose the matrixSparseMatrix multiply(const SparseMatrix& other) const- Multiply two sparse matricesstd::vector<std::vector<double>> toDense() const- Convert to dense matrixint getRows() const- Get number of rowsint getCols() const- Get number of columns
-
Build the examples:
./build_examples.sh
-
Run the comprehensive example:
cd build/example_usage && ./example_app
-
Run the simple example:
cd build/simple_example && ./simple_example
- All matrix operations expect
std::vector<std::vector<double>>for dense matrices - Sparse matrices use the
SparseMatrixclass for memory efficiency - The library uses C++20 standard
- All decomposition functions modify the output parameters passed by reference
- The power iteration method returns a pair containing the eigenvalue and eigenvector