Context: Mathematica is being used to simplify expressions. These expressions are then exported to be used in a C-family language. Naturally enough, desiderata include execution speed, and avoidance of overflow.
As a simple example, consider z = ((a b)/c)^2. As entered, this is one multiply, one division, and one square.
Mathematica rearranges this to three squarings, one division into 1, and then two multiplies: Times[Power[a,2],Power[b,2],Power[c,-2]]. If output in CForm this is three squarings, a multiply and a division. So Mathematica’s form would be more CPU work. Mathematica’s form also has more risk of an overflow, such as when a = c = 10^200 and b ≈ 1.
The following doesn’t help.
FullSimplify[
z
, ComplexityFunction -> (1000 Count[#, Power, Infinity]
+ LeafCount[#] &)
, Assumptions -> c != 0
]
Please, what’s the best way to instruct ‘simplify for something like CPU efficiency in a compiled language’?
Relevant link (even though I was unable to apply its lessons): Advice for Mathematica as Mathematician's Aid
Thank you.
Simplifythings into a form that is sensible in an imperative language. – jdaw1 Jan 19 '20 at 18:22Powerautomatically expands overTimes. It has nothing to do withSimplify. Therefore, your best bet is, in your code, avoid usingPower, and opt for your own power function–call itpow–which does not automatically expand. Then, add aCFormdefinition topowthat makes the appropriate transcription to C code. – QuantumDot Jan 22 '20 at 17:56Simplify, but I’ll happily step back from that) arrange an expression for something like CPU efficiency? – jdaw1 Jan 23 '20 at 10:35Simplifywasn’t doing anything like whatCpuEfficiencifyshould. (I’m trying to avoid doing the hard work by hand.) – jdaw1 Jan 28 '20 at 22:47