Skip to content

Axiaaa/ft_matrix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

29 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Linear Algebra Library

A comprehensive C++ template-based linear algebra library implementing vectors, matrices, and essential mathematical operations for computational applications.

๐Ÿ“‹ Table of Contents

๐Ÿ” Overview

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.

Key Design Principles

  • Template-based: Works with any numeric type (int, float, double, etc.)
  • Memory efficient: Uses std::vector for dynamic storage
  • Exception safe: Proper error handling with descriptive messages
  • Operator overloading: Natural mathematical syntax
  • Header-only: Easy integration into existing projects

โœจ Features

Vector Operations

  • โœ… 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 Operations

  • โœ… 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

Advanced Features

  • โœ… Linear combinations of vectors
  • โœ… Perspective projection matrix generation
  • โœ… Comprehensive unit testing framework
  • โœ… Clean operator overloading

๐Ÿ“ Project Structure

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

๐Ÿ”ง Building the Project

Prerequisites

  • C++11 compatible compiler (GCC, Clang, MSVC)
  • Make build system

Build Commands

# 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

๐Ÿš€ Usage Examples

Vector Operations

#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]

Matrix Operations

#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]

Linear Combinations

#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 Breakdown

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()

๐Ÿ“– API Reference

Vector Class

Constructors

Vector()                           // Empty vector
Vector(std::vector<K> data)        // From std::vector
Vector(const Vector<K>& other)     // Copy constructor

Core Methods

size_t getSize() const             // Get vector size
void append(K value)               // Add element
K& operator[](size_t index)        // Access element
void print() const                 // Print to console

Mathematical Operations

void 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 element

Matrix Class

Constructors

Matrix()                                    // 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 vector

Core Methods

size_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 console

Mathematical Operations

void 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 rank

Utility Functions

Vector Utilities

template<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);

Matrix Utilities

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);

๐Ÿงช Testing

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 ===

Test Structure

Each exercise directory (ex00/ to ex13/) contains:

  • Implementation source files
  • Unit test cases
  • Makefile for individual building
  • Expected output validation

โš™๏ธ Requirements

  • 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)

Compiler Flags

The project uses standard compilation flags for optimal performance and debugging:

CXXFLAGS = -Wall -Wextra -Werror -std=c++11 -g

๐Ÿค Contributing

This project follows educational exercise structure. Each exercise builds upon previous concepts:

  1. Start with basics (ex00-02): Vector operations and linear combinations
  2. Progress to products (ex03-06): Dot product, norms, and cross product
  3. Advance to matrices (ex07-09): Matrix operations and transformations
  4. Master advanced topics (ex10-14): Echelon form, determinants, rank, projection matrix

๐Ÿ“„ License

This project is part of an educational linear algebra curriculum. Please refer to your institution's guidelines for usage and distribution.


Happy Computing! ๐Ÿงฎโœจ

About

42 Project | ft_matrix is a linear algebra project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •