r/cpp • u/human_or_coffee • Jun 04 '26
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 :).
13
u/summetdev Jun 04 '26
Interesting project! I remember running Fast-Fourier transform fully at compile-time using c++' constexpr. How do you compute sines and cosines, is there a table with a bunch of values?
4
u/WatercressNo1384 Jun 04 '26
It is an impressive feat! I am however, wondering what are practical applications of compile time Eigen solver? If matrix is constant at compile time, we can find Eigen values from any other Eigen solver.
2
u/Cautious-Ad-6535 Jun 05 '26
Is there limit how much math can be done if data is available inncompile time? I once wrote neural network that, after learning can be generated to constexpr C++ so that the feed network is a build time constant. To compile even quite simple network (just few hidden layers) I had to increase my Linux virtual memory as compile took some time and 60GB of ram 😳 ... just for fun, but I was thinking to apply to a some embedded system where dynamic allocations are not ok, or at least slow - its in github
31
u/inequity AAA Games Dev Jun 04 '26
Good to see people are still doing real programming
1
u/bill_klondike Jun 04 '26
This is fascinating! How large of a system have you tried? Any interest in adding sparse solvers in the future?
1
u/no_overplay_no_fun Jun 04 '26
This looks fun but looking at the compile time and memory scaling with respect to the matrix size it seems like doing the computation in python would be more efficient. It would be "build time" instead of compile time. :) I'd suggest including your typical/indented matrix size to the docs.
Little bit of a math nitpick, as you already document, Laplace formula is problematic. But not only complexity-wise, computing determinant via Laplace formula is known to have numerical issues, namely potential for catastrophic cancellation. Better approach would definitely be to compute determinant via LU decomposition.
2
u/Ok_Path_4731 Jun 04 '26
Have a look at the "Const expression template library" https://github.com/zokrezyl/ycetl you can bring your compile time calculation structured results into runtime.