A comprehensive C++ template-based linear algebra library implementing vectors, matrices, and essential mathematical operations for computational applications.
- Overview
- Features
- Project Structure
- Building the Project
- Usage Examples
- Exercise Breakdown
- API Reference
- Testing
- Requirements
This library provides a robust implementation of linear algebra operations in C++ using template programming for type flexibility. It supports both vectors and matrices with a wide range of mathematical operations commonly used in graphics programming, machine learning, and scientific computing.
- Template-based: Works with any numeric type (
int,float,double, etc.) - Memory efficient: Uses
std::vectorfor dynamic storage - Exception safe: Proper error handling with descriptive messages
- Operator overloading: Natural mathematical syntax
- Header-only: Easy integration into existing projects
- โ Basic arithmetic (addition, subtraction, scalar multiplication)
- โ Dot product and cross product (3D)
- โ Vector norms (L1, L2, infinity)
- โ Normalization and angle calculations
- โ Linear interpolation (lerp)
- โ Matrix arithmetic (addition, subtraction, scalar multiplication)
- โ Matrix-vector multiplication
- โ Matrix-matrix multiplication
- โ Transpose and inverse operations
- โ Determinant calculation (1x1 to 4x4)
- โ Row echelon form transformation
- โ Trace and rank calculations
- โ Projection Matrix
- โ Linear combinations of vectors
- โ Perspective projection matrix generation
- โ Comprehensive unit testing framework
- โ Clean operator overloading
linear-algebra-lib/
โโโ Makefile # Build system
โโโ includes/
โ โโโ vector.hpp # Vector class and operations
โ โโโ matrix.hpp # Matrix class and operations
โ โโโ utils.hpp # Utility functions and algorithms
โโโ ex00/ to ex14/ # Exercise implementations and tests
- C++11 compatible compiler (GCC, Clang, MSVC)
- Make build system
# Build all exercises
make all
# Build specific exercise (replace XX with exercise number)
make -C exXX
# Clean build files
make clean
# Clean all generated files
make fclean
# Rebuild everything
make re
# Run all unit tests
make test#include "includes/vector.hpp"
#include "includes/utils.hpp"
// Create vectors
Vector<float> v1({1.0f, 2.0f, 3.0f});
Vector<float> v2({4.0f, 5.0f, 6.0f});
// Basic operations
Vector<float> sum = v1 + v2; // [5, 7, 9]
Vector<float> scaled = v1 * 2.0f; // [2, 4, 6]
// Advanced operations
float dotProduct = dot(v1, v2); // 32.0
Vector<float> normalized = normalize(v1);
float magnitude = norm(v1); // โ14 โ 3.74
// Cross product (3D only)
Vector<float> cross = cross_product(v1, v2); // [-3, 6, -3]#include "includes/matrix.hpp"
// Create matrices
Matrix<double> m1({{1, 2}, {3, 4}});
Matrix<double> m2({{5, 6}, {7, 8}});
// Basic operations
Matrix<double> sum = m1 + m2;
Matrix<double> product = mul_mat(m1, m2);
// Advanced operations
double det = m1.determinant(); // -2.0
Matrix<double> inv = inverse(m1); // Inverse matrix
Matrix<double> transposed = transpose(m1);
// Matrix-vector multiplication
Vector<double> v({1, 2});
Vector<double> result = mul_vec(m1, v); // [5, 11]#include "includes/utils.hpp"
// Linear combination: 2*v1 + 3*v2 - v3
std::vector<Vector<float>> vectors = {v1, v2, v3};
std::vector<float> coefficients = {2.0f, 3.0f, -1.0f};
Vector<float> combination = linear_combination(vectors, coefficients);| Exercise | Focus | Key Functions |
|---|---|---|
| ex00 | Basic Operations | add(), sub(), scl() |
| ex01 | Linear Combination | linear_combination() |
| ex02 | Linear Interpolation | lerp() |
| ex03 | Dot Product | dot() |
| ex04 | Vector Norms | norm(), norm_1(), norm_inf() |
| ex05 | Angle & Normalize | angle_cos(), normalize() |
| ex06 | Cross Product | cross_product() |
| ex07 | Matrix Multiplication | mul_vec(), mul_mat() |
| ex08 | Matrix Trace | trace() |
| ex09 | Matrix Transpose | transpose() |
| ex10 | Row Echelon Form | row_echelon() |
| ex11 | Determinant | determinant(), det2(), det3(), det4() |
| ex12 | Matrix Inverse | inverse() |
| ex13 | Matrix Rank | rank() |
| ex14 | Projection Matrix | projection() |
Vector() // Empty vector
Vector(std::vector<K> data) // From std::vector
Vector(const Vector<K>& other) // Copy constructorsize_t getSize() const // Get vector size
void append(K value) // Add element
K& operator[](size_t index) // Access element
void print() const // Print to consolevoid add(const Vector<K>& v) // In-place addition
void sub(const Vector<K>& v) // In-place subtraction
void scl(const K& scalar) // In-place scaling
K dot(const Vector<K>& v) // Dot product
void normalize() // Normalize to unit vector
K max() const // Maximum elementMatrix() // Empty matrix
Matrix(std::vector<std::vector<K>> data) // From 2D vector
Matrix(std::vector<K> data, size_t rows, size_t cols) // From flat vectorsize_t getRows() const // Get row count
size_t getCols() const // Get column count
std::vector<K>& operator[](size_t row) // Access row
void print() const // Print to consolevoid add(const Matrix<K>& M) // In-place addition
void sub(const Matrix<K>& M) // In-place subtraction
void scl(const K& scalar) // In-place scaling
void mul_vec(const Vector<K>& u) // Matrix-vector multiplication
void mul_mat(const Matrix<K>& A) // Matrix-matrix multiplication
K trace() // Matrix trace
void transpose() // In-place transpose
K determinant() // Calculate determinant
void inverse() // In-place inverse
void row_echelon() // Convert to row echelon form
K rank() // Calculate matrix ranktemplate<typename K>
Vector<K> linear_combination(std::vector<Vector<K>> vectors, std::vector<K> coefs);
Vector<K> lerp(const Vector<K>& v, const Vector<K>& u, const K& t);
K angle_cos(const Vector<K>& v, const Vector<K>& u);
Vector<K> normalize(const Vector<K>& v);
Vector<K> cross_product(const Vector<K>& v, const Vector<K>& u);
K norm(const Vector<K>& v);
K norm_1(const Vector<K>& v);
K norm_inf(const Vector<K>& v);template<typename K>
Matrix<K> add(const Matrix<K>& M, const Matrix<K>& N);
Matrix<K> sub(const Matrix<K>& M, const Matrix<K>& N);
Matrix<K> scl(const Matrix<K>& M, const K& scalar);
Vector<K> mul_vec(const Matrix<K>& M, const Vector<K>& u);
Matrix<K> mul_mat(Matrix<K> A, Matrix<K> B);
Matrix<K> transpose(const Matrix<K>& A);
Matrix<K> inverse(const Matrix<K>& A);
Matrix<K> row_echelon(const Matrix<K>& A);
K det2(const Matrix<K>& A);
K det3(const Matrix<K>& A);
K det4(const Matrix<K>& A);
Matrix<K> projection(K fov, K ratio, K near, K far);The project includes comprehensive unit tests for each exercise:
# Run all tests
make test
# Expected output for successful tests
=== Running unit tests ===
Testing ex00...
โ
ex00: OK
Testing ex01...
โ
ex01: OK
...
=== ALL TESTS PASSED ===Each exercise directory (ex00/ to ex13/) contains:
- Implementation source files
- Unit test cases
- Makefile for individual building
- Expected output validation
- C++ Standard: C++11 or later
- Compiler: GCC 4.8+, Clang 3.3+, or MSVC 2013+
- Build System: GNU Make
- Platform: Cross-platform (Linux, macOS, Windows)
The project uses standard compilation flags for optimal performance and debugging:
CXXFLAGS = -Wall -Wextra -Werror -std=c++11 -gThis project follows educational exercise structure. Each exercise builds upon previous concepts:
- Start with basics (ex00-02): Vector operations and linear combinations
- Progress to products (ex03-06): Dot product, norms, and cross product
- Advance to matrices (ex07-09): Matrix operations and transformations
- Master advanced topics (ex10-14): Echelon form, determinants, rank, projection matrix
This project is part of an educational linear algebra curriculum. Please refer to your institution's guidelines for usage and distribution.
Happy Computing! ๐งฎโจ