https://github.com/usercrixus/42matrix
https://github.com/usercrixus/42matrix
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/usercrixus/42matrix
- Owner: usercrixus
- Created: 2025-07-28T23:59:30.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-10T19:53:27.000Z (11 months ago)
- Last Synced: 2025-08-10T21:20:04.202Z (11 months ago)
- Language: C++
- Size: 7.2 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Matrix & VectorAlgebra — Modern C++ Linear Algebra (Header-only)
A small, header-only linear algebra toolkit featuring:
- **`VectorAlgebra`** — a `std::vector` with element-wise ops, dot/cross products, norms, interpolation, and linear combinations.
- **`Matrix`** — a matrix built on `std::vector>` with transpose, row-echelon, rank, determinant, inverse, multiplication, trace, interpolation, and a perspective projection helper.
Works with real types (`int`, `float`, `double`) and complex (`std::complex`, `std::complex`). Complex support follows standard linear algebra conventions (Hermitian dot, real-valued norms).
---
## Table of contents
- [Install & Build](#install--build)
- [Hello world](#hello-world)
- [Design notes](#design-notes)
- [VectorAlgebra API](#vectoralgebra-api)
- [Matrix API](#matrix-api)
- [Complex numbers](#complex-numbers)
- [Errors & exceptions](#errors--exceptions)
- [FAQ & tips](#faq--tips)
- [License](#license)
---
## Install & Build
This library is **header-only**. Just add the two headers to your project:
```
Matrix/
├─ VectorAlgebra.hpp
└─ Matrix.hpp
```
Include them:
```cpp
#include "Matrix/VectorAlgebra.hpp"
#include "Matrix/Matrix.hpp"
```
### Build example (g++)
```bash
g++ -std=c++20 -Wall -Wextra -Werror main.cpp -o demo
./demo
```
---
## Hello world
```cpp
#include "Matrix/VectorAlgebra.hpp"
#include "Matrix/Matrix.hpp"
#include
int main() {
VectorAlgebra a = {1, 2, 3};
VectorAlgebra b = {4, 5, 6};
std::cout << "a + b = " << (a + b) << "\n";
std::cout << "a · b = " << a.dotProduct(b) << "\n";
std::cout << "||a||2 = " << a.normEuclidean() << "\n";
Matrix M = Matrix::from({{1, 2}, {3, 4}});
VectorAlgebra x = {1, 1};
std::cout << "M * x = " << (M * x) << "\n";
}
```
Example output:
```
a + b = [5, 7, 9]
a · b = 32
||a||2 = 3.74166
M * x = [3, 7]
```
---
## Design notes
- `VectorAlgebra` **inherits publicly** from `std::vector` to keep standard vector behaviors (iterators, indexing, etc.) and add math methods/operators.
- `Matrix` **inherits privately** from `std::vector>` to control the public surface (exposes `operator[]`, `begin`, `end`).
- All operations do **dimension checks** and throw exceptions on mismatch.
---
## VectorAlgebra API
### Type & construction
```cpp
template
class VectorAlgebra : public std::vector {
public:
using std::vector::vector; // inherit constructors
using ScalarType = decltype(std::abs(T{})); // real scalar type for norms
};
```
Create like a normal vector:
```cpp
VectorAlgebra v = {1, 2, 3};
VectorAlgebra u(5, 0); // [0,0,0,0,0]
```
### Static utilities
```cpp
static VectorAlgebra linearCombinaison(const std::vector>& vects, const VectorAlgebra& coef);
static VectorAlgebra linearInterpolation(const VectorAlgebra& a, const VectorAlgebra& b, float ratio);
static float angleCos(const VectorAlgebra& a, const VectorAlgebra& b);
```
### Vector metrics
```cpp
ScalarType normManhattan() const; // ∑ |x_i|
ScalarType normEuclidean() const; // sqrt(∑ |x_i|^2)
ScalarType normSupremum() const; // max_i |x_i|
```
### Reductions & products
```cpp
T sum();
T dotProduct(const VectorAlgebra& v) const;
```
### Element-wise operators
```cpp
VectorAlgebra operator+(const VectorAlgebra&) const;
VectorAlgebra operator-(const VectorAlgebra&) const;
VectorAlgebra operator*(const VectorAlgebra&) const;
VectorAlgebra operator/(const VectorAlgebra&) const;
template
VectorAlgebra operator*(Scalar s) const;
template
VectorAlgebra operator/(Scalar s) const;
template
VectorAlgebra operator*(Scalar s, const VectorAlgebra& v);
template
VectorAlgebra operator/(Scalar s, const VectorAlgebra& v);
```
### Cross product (3D only)
```cpp
static VectorAlgebra crossProduct(const VectorAlgebra& a, const VectorAlgebra& b);
```
---
## Matrix API
### Construction
```cpp
static Matrix from(const std::vector>& rows);
bool isSquare() const;
```
### Basic ops
```cpp
Matrix transpose() const;
VectorAlgebra operator*(const VectorAlgebra& x) const;
Matrix operator*(const Matrix& B) const;
```
### Row-echelon & rank
```cpp
Matrix rowEchelon() const;
unsigned int rank() const;
Matrix rowEchelonNormalize(int& swapCount) const;
```
### Determinant, trace, inverse
```cpp
T determinant() const;
T trace();
Matrix invert() const;
```
### Interpolation
```cpp
static Matrix linearInterpolation(const Matrix& A, const Matrix& B, float ratio);
```
### Perspective projection helper
```cpp
static Matrix projection(float fov_deg, float aspect_ratio, float near_plane, float far_plane);
```
---
## Complex numbers
- **Dot product:** `a.dotProduct(b)` computes `∑ conj(a_i) * b_i`.
- **Norms:** return real scalar types (`ScalarType`) via `std::abs`/`std::norm`.
- **Angle cosine:** intentionally **throws** for complex vectors.
- All matrix routines work with `std::complex`.
---
## Errors & exceptions
Most precondition failures throw `std::invalid_argument`.
`std::logic_error` is thrown for conceptual errors.
---
## FAQ & tips
- **Why inherit from `std::vector`?** Convenience & zero-overhead reuse.
- **Performance:** Not optimized for heavy workloads; prefer Eigen/Blaze for that.
- **Numerical stability:** Only basic pivoting implemented.
- **Printing:** `operator<<` defined for both vectors and matrices.
---
## License
MIT License