
consteig. How much math can you force the compiler to do at compile time? (a lot)
Presented here is a header-only C++ compile-time eigenvalue and eigenvector solver with no dependencies beyond a C++17 compatible compiler (so no stdlib dependency, no .cpp files). I started on this project 6 years ago and only got back into finishing it recently.
Technically this is a "personal project" I suppose but I intend it to be used by other C++ programmers (or math nerds) and I'd consider it "production-quality". So I think a formal post is acceptable.
If you don’t remember (or haven’t encountered) eigenvalues/vectors, eigenvectors are vectors whose directions are unchanged when linear transforms are applied to the system (which makes them special). Eigenvalues are the factors by which an eigenvector is stretched or shrunk (but whose direction remains unchanged); usually this is expressed as matrices in linear algebra. They’re useful for lots of engineering problems.
For a certain class of problems the matrix for which you want to find the eigenvalues/vectors doesn’t change, effectively making the eigenvalues/vectors constants. These are things like state space matrices for LTI systems, roots of a polynomial, structural dynamics, and some graph/network problems. I’ve got some examples in my docs. If you need the eigenvalues/vectors for those in a C++ program, what you do today is either (1) calculate them at run-time using something like Eigen or (2) calculate them in matlab/python and hard-code them into your program. I’ve pushed all of the math for doing that into compile-time using the compiler itself. This means you can define static matrices at compile time, and save the eigenvalues/vectors off as constants in memory without needing to spend any run-time cycles nor to independently track/calculate them with another tool.
Again; I’ve got examples above, but you can use this to do something like specify filter characteristics (sample rate, cut-off frequency, Order, etc...) and at compile time calculate all the digital filter coefficients. So you can end up doing something like:
// 3rd order butterworth with 100Hz cut-off and 1kHz sample rate
static constexpr constfilt::Butterworth<double, 3> b(100.0, 1000.0);
//Call at 1kHz at run-time
b(new_sample);
And you never need to use python nor matlab to figure out what those coefficients are. I’ve also got another less-polished / less-tested / less-complete compile-time library called constfilt now that does exactly that. consteig available on GitHub and in vcpkg; I’m working on Conan :).