active library

elementa

Pedagogical C++20 linear algebra library

Started 2026 C++

Resources & Distribution

Source Code

Package Registries

elementa

A pedagogical C++20 linear algebra library designed to teach dense matrix operations through clean, readable code.

Features

  • Generic matrix type: matrix<T> works with any arithmetic type
  • Matrix concept: Enables generic programming over matrix types
  • Core operations:
    • Arithmetic: addition, subtraction, scalar multiplication, negation
    • Matrix multiplication (standard O(n³) algorithm)
    • Transpose, Hadamard (element-wise) product
  • Decompositions:
    • LU decomposition with partial pivoting
  • Derived operations:
    • Determinant, log-determinant
    • Matrix inverse
    • Linear system solve (Ax = b)
  • Reductions: trace, sum, mean
  • Norms: Frobenius, L1, L∞
  • Element-wise functions: exp, log, sqrt, pow, abs

Requirements

  • C++20 compiler (GCC 12+, Clang 15+)
  • CMake 3.20+

Usage

elementa is a header-only library. Simply include elementa.hpp:

#include <elementa.hpp>

using namespace elementa;

int main() {
    // Create matrices
    matrix<double> A{{1, 2}, {3, 4}};
    matrix<double> B{{5, 6}, {7, 8}};

    // Arithmetic
    auto C = A + B;
    auto D = A * B;  // Matrix multiplication

    // Linear algebra
    auto det_A = det(A);
    auto A_inv = inverse(A);

    // Solve Ax = b
    matrix<double> b{{1}, {2}};
    auto x = solve(A, b);

    return 0;
}

Building

mkdir build && cd build
cmake ..
cmake --build .
ctest  # Run tests

Design Philosophy

  • Pedagogical clarity: Code is written to be read and understood
  • Mathematical transparency: Comments explain the underlying mathematics
  • Generic programming: Uses C++20 concepts for type-safe genericity
  • Zero dependencies: Pure C++ standard library

Matrix Concept

The Matrix concept enables writing generic algorithms:

template <Matrix M>
auto my_algorithm(const M& A) {
    // Works with any type satisfying Matrix concept
    return trace(A) + det(A);
}

License

MIT

Discussion