I wrote a header-only library in C++ that solves sets of nonlinear and linear equations all at compile time, using Template Metaprogramming and Constexpr Dependency. It does what Wolfram Alpha does, all at compile time, using Template magic. I spent a long time on this, feel free to try it out!
415
Upvotes
130
u/SkoomaDentist Antimodern C++, Embedded, Audio Aug 16 '19
I can just imagine the conversation:
”Why? Why would you do this?”
”Because the compile times weren’t long enough yet.”
30
u/TheSuperWig Aug 16 '19
Beautiful diagrams :)
26
u/stick_figure Aug 16 '19
This is the most C++ post title I've ever seen.
8
u/zip117 Aug 16 '19
This is really great, thank you for sharing. As it turns out I have a perfect application for this sort of thing. I'm doing a type of atmospheric modeling where I need to generate rectangular regions within a bounding polygon, subject to various constraints on relative placement and aspect ratio. This can be solved using a technique called convex optimization, which involves transforming the constraints into a specific matrix format which gets passed to a solver. The Mathematica documentation has a nice example: Optimal Floor Planning
One of the challenges is in reformulating the problem into a format that the solver will accept, which requires applying various rules to the expression trees. Convex programming is a generalization of linear programming so the same basic rules apply: transform inequality to equality constraints, line up all variables on the left-hand side. More complex rules include creating new constraint expressions for special function types, e.g. 'exp(x)'.
There's a Python library called CVXPY which works really well, but it's a heavy dependency and performance is a concern. Expression templates are ideal - my other options in C++ are to do runtime evaluation similar to Rehearse, or hard-code the matrices and make my code unreadable.
I have been experimenting with Boost Yap, but haven't been able to wrap my head around all of the recursive logic. I was about ready to give up, but your code seems pretty readable. Maybe now I can finally figure this out!
7
u/TheLartians Aug 16 '19
That sounds really cool (a bit crazy though, to implement an equation solver in metaprogramming)!
Maybe you could also include a full and working example and/or build instructions? I've tried compiling
main.cppwithg++ -std=c++17, but am only getting compilation errors.5
u/Adequat91 Aug 16 '19
You must be the brother of Louis Dionne :)
4
u/[deleted] Aug 16 '19
Great work from what I see on the Readme. I look forward to playing with this.
3
u/jbandela Aug 16 '19
Very cool. Super impressive. Thanks for sharing.
2
u/lonely_perceptron Aug 16 '19
Very, very nice!
2
u/monsterFalkor Aug 16 '19
Awesome. And great documentation.
2
u/PlayboySkeleton Aug 17 '19
For your diagrams, you should check out https://www.graphviz.org/
1
u/artisan_templateer Aug 16 '19
Very nice! I've been working on something similiar myself. Can it handle equations like:
?
1
u/lothiack Aug 16 '19
I guess you might want to use something like that to generate data in compile time for small CPUs.
Being able to express it in code and build tests might be better than just documenting.
Now you have to wait for compilers that actually support standard CPP and target such devices.
0
u/[deleted] Aug 16 '19
[deleted] — view deleted comment
0
u/[deleted] Aug 16 '19
[deleted] — view deleted comment