This library provides vector and matrix maths with templated precision for numerical/simulation programs written in C++20. It is intended to help you to write maths into your C++ in a simple, readable and modern idiom, with the ability to switch machine precision easily.
It provides:
- Static and dynamically sized vector templates that are
constexprcapable - Matrix template classes (one with static memory allocation, the other dynamic) that provide comprehensive matrix operations for real and complex values
- A quaternion template class
- Scaling and mathematical interval templates
- Random number, and string generation
- Classes for working with 2D grids of data (Cartesian and hexagonal)
- Bezier curves and cubic spline fits
- A variety of algorithms (Nelder-Mead, simulated annealing, winding numbers, box filter)
- Basic statistics including a histo class, a number of bootstrap methods and principal component analysis
- A set of
constexprmaths methods, derived from Keith O'Hara's GCEM project
The vector classes are compatible with the standard library's C++ algorithms, and have their own built-in methods.
The vector, quaternion, matrix and interval classes are constexpr capable and can be used to build functions that generate lookup tables at compile time.
You can stream objects to debug or observe their values.
You can visualize the data in mathplot (this repo was developed alongside mathplot when they were both 'morphologica').
The namespace is just sm (I like short namespaces for frequently used types).
Here's an example
#include <iostream> // possibly import std; in the future.
import sm.mathconst;
import sm.vec;
import sm.quaternion;
import sm.mat;
int main()
{
// Mathematical constants are provided by mathconst
using mc = sm::mathconst<float>;
// Create a fixed-size mathematical 3D vector object using single precision elements
sm::vec<float, 3> v1 = { 1, 2, 3 };
// Create and intialize a quaternion rotation about the x axis
sm::quaternion<float> q1 (sm::vec<float, 3>::ux(), mc::pi_over_2);
// Rotate the vector with the quaternion
sm::vec<float, 3> v1_rotated = q1 * v1;
std::cout << v1 << " rotated pi/2 about x-axis is " << v1_rotated << "\n";
// Make a rotation matrix that encodes the rotation from the quaternion
sm::mat<float, 4, 4> rmat (q1);
std::cout << "Rotation matrix:\n" << rmat << "\n";
}You could replace use of GLM with this library; in many ways it's like GLM, but with templated precision.
For example, rather than glm::vec2 whose dimensionality is fixed to 2 and precision to single, you use sm::vec<float, 2>.
Because all of the class templates take precision as a template argument, you can build your algorithms using a typedef or using-defined type, leaving the choice of precision as one which can be modified at any stage of development.
sebsjames/maths also provides some of the general matrix manipulation functionality offered by Armadillo and Eigen.
It does not pretend to be a comprehensive linear-algebra library, but its static matrix class (sm::mat<>) supports the operations for use as transformation matrices, the dynamic class (sm::vmat<>) also provides a number of additional linear algebra algorithms.
It was developed to build the 3D plotting library mathplot.
I decided to go fully modules-native with this code. However, there is a header-only branch that contains the code in its last header-only form.
The code depends only on the standard library.
Minimum compilers: g++-15, clang++-18, Visual Studio 2022. Recommended compilers: g++-16, clang++-20. You will need cmake version 3.28.5 as a minimum. Ninja is required as a partner for cmake because GNU make does not yet support C++-20 modules.
gcc 15 compiles (almost) all the unit tests in this repository, but its modules support is considered experimental, and so gcc 16 is recommended. If you wish to visualize with mathplot, you'll certainly need gcc 16 (or clang-20 and up) for that code.
Clang-18, Clang-19 and Clang-20 are available in the default repositories:
sudo apt install clang-18 clang-tools-18 # need both.To install gcc 15, you can add an extra repository:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test # provides recent gcc versions
sudo apt install gcc-15 g++-15At time of writing, gcc-16 is just released. I have been compiling it from source. It may be ok to install from the ubuntu-toolchain repo, though right now, the version there is slightly behind the released gcc-16 and doesn't complete the build.
mkdir build
cd build
# A cmake call something like (if you want to use clang and your default libc++/libstdc++):
CC=clang-20 CXX=clang++-20 cmake .. -GNinja
# Optionally add -DCMAKE_CXX_FLAGS="-stdlib=libc++" to compile with libc++ if it is not your default
# possibly also: -DCMAKE_EXE_LINKER_FLAGS="-stdlib=libc++ -lc++abi"
ninjaFor a correctly installed GCC, you can build like this:
mkdir build
cd build
# A cmake call something like
CC=gcc-15 CXX=g++-15 cmake .. -GNinja
ninjaIf you're building with a from-source compiled version of gcc, then you have to take a little care to ensure that you get the right associated c++ runtime library rather than the possibly out-of-date one that's installed on your system. One way to do this is to ask the compiler to link libstdc++ statically. This seems to ensure that you get the exact libstdc++ that's associated with your compiler. I installed gcc 16 in /opt/gcc-16 so my build was:
mkdir build
cd build
CC=/opt/gcc-16/bin/gcc CXX=/opt/gcc-16/bin/g++ cmake .. -GNinja -DCMAKE_CXX_FLAGS="-static-libstdc++"
ninjaI have wrapped and modified third party code in some of these modules.
The Voronoi (and Delaunay) code jc_voronoi.cppm is based on Mathias Westerdahl's jcvoronoi.
I wrapped the HEALPix implementation from sourceforge to create healpix_bare.cppm.
I copied code that computes the cylindrical Bessel function of order 0 from the Boost library into bessel_i0.cppm. This code was written by Xiaogang Zhang and John Maddock.
I thank the authors of all of these codes.
The vast majority of the code here was human-written and is human maintained. There are a couple of exceptions: the polysolve code was initially coded with Claude Code, although it took extensive rewriting by humans (Alex and Seb) before it was acceptable. The hexfft code was a second experiment that began as a query to Claude Code to see how well it would work. Again, it took a big rewrite before it was correct.