5

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.

jdaw1
  • 499
  • 2
  • 9
  • 1
    maybe this isn't what you want,but how about this ? https://github.com/njpipeorgan/MathCompile – AsukaMinato Jan 19 '20 at 16:26
  • Useful to some, so worth the link — thank you — but not for this question. The question isn’t how to take a mass of Mathematica and convert or compile it. Instead it asks how to get Mathematica to Simplify things into a form that is sensible in an imperative language. – jdaw1 Jan 19 '20 at 18:22
  • 1
    Mathematica generally reduces expressions to a standard form intended for computer algebra. If you're exporting to a compiled language, optimizing procedural code is your compiler's responsibility. – John Doty Jan 19 '20 at 23:56
  • But is it easy to have Mathematica simplify expressions for a different purpose? I’ve failed to do so, and was hoping that a generous respondent might see a clean way through the problem. Even if, or because, it isn’t Mathematica’s default behaviour. – jdaw1 Jan 20 '20 at 09:24
  • The reason Mathematica behaves the way it does is because Power automatically expands over Times. It has nothing to do with Simplify. Therefore, your best bet is, in your code, avoid using Power, and opt for your own power function–call it pow–which does not automatically expand. Then, add a CForm definition to pow that makes the appropriate transcription to C code. – QuantumDot Jan 22 '20 at 17:56
  • The target here is not that any function does or doesn’t expand over another. Please allow the target to be more general, and therefore of broader use. Can Mathematica (which might mean Simplify, but I’ll happily step back from that) arrange an expression for something like CPU efficiency? – jdaw1 Jan 23 '20 at 10:35
  • Can you provide any larger examples? It would be easier to test approaches if we didn't have to craft cases ourselves. – Mr.Wizard Jan 28 '20 at 22:43
  • Not readily. I’d need to carefully count computations and minimise them. This example, as-is except for a change of parameter names, was enough for me to know that Mathematica’s Simplify wasn’t doing anything like what CpuEfficiencify should. (I’m trying to avoid doing the hard work by hand.) – jdaw1 Jan 28 '20 at 22:47

0 Answers0