First, I am not completely sure this is the right stackexchange community. Just tell me if you think I should post on another one. I ask this here as I think this is not a problem about programming, but about understanding what is the right tool / algorithm / mathematical structure to use.
I have a series of formula (many of them, typically 100) that I obtain from computations with a CAS (Python Sympy). Those formula will be translated into C++, and they should be evaluated efficiently on a micro controller. Many of the computations to perform to evaluate each formula are redundant between different formula. Is there a way to find, with an algorithm, an 'optimal', or at least 'reasonable', way to group the computations so as to reduce the number of operations to perform, and to do it in a systematic way?
For example, if many formula include the term $\cos\left(\frac{dt}{2} \sqrt{w_x^2 + w_y^2 + w_z^2}\right)$, I should calculate it first and store it in a variable, let us say $cn$, and use $cn$ for later evaluations. Of course, I can go through the code and do this euristicaly 'by hand' but it is not really scalable, definitely 'ugly' and probably non optimal. I guess, in theory, a 'perfect' C++ compilator should be able to do this task, but I guess if the equations are many and complex enough it will fail to come close to an optimum.
Edit: I am of course aware of simple simplifications using sympy, for example http://docs.sympy.org/latest/modules/simplify/simplify.html#module-sympy.simplify.cse_main or factorize, simplify etc. The difficulty here is, I want to 'cross' the simplifications between several equations.
There's a nice Mathematica code for this purpose, written ca. 1994 by Mark Sofroniou for his Ph.D. thesis, and available here. You give it (the Optimize[] command) a list of expressions, and it returns a list of named rules defining dummy variables and a list of expressions in terms of those variables, by which the original expressions can be evaluated efficiently by isolating and pre-computing common subexpressions, etc. (To get the actual C or Fortran code, one then applies the built-in CForm[] or FortranForm[] function.) I've used Sofroniou's package, on occasion, since the 1990s (and it still works). (Maple apparently has a similar capability, I see.)
I'm not regular Python/sympy user, but I searched the Web a bit to see whether such a code is available for sympy. I didn't find anything, but I'd guess a such capability might be hidden `under the hood' within the Cython package (which is used for integrating Python/sympy with optimized C code).