Re: Optimize evaluation of symbolic expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg102017] Re: Optimize evaluation of symbolic expressions
- From: Stonewall Ballard <stoneyb at gmail.com>
- Date: Sun, 26 Jul 2009 03:57:35 -0400 (EDT)
- References: <h4ef4m$t1b$1@smc.vnet.net>
On Jul 25, 4:18 am, Harrie Kraai <hakr... at xs4all.nl> wrote: > ... > If I would program this myself, I would, of course, evaluate the > subexpression (1 + x^2 + y^2) first and use this result 4 times in the > subsequent calculations. > > It looks to me like Mathematica is not able to or at least does not make > this optimization. (Find out using 'Trace'). Perhaps this is difficult > to do in a general sense because any subexpression may have > side-effects. However, in this case it should be possible to instruct > Mathematica to collect common subexpressions first. It would surely make > calculations much faster. > The symbolic calculations have led to an expression that is > "Simplify"-ed in terms of reading (perhaps) but not in terms of > evaluation. Is there, or should there be a function that translates > expressions to a form that is optimized for evaluation? >... I have read that internally, Mathematica recognizes common subexpressions and evaluates them only once. That doesn't help me when I use Mathematica to create C code, so I wrote an explicit common subexpression hoister that rewrites an expression into one with variables holding multiply-used parts. It's not perfect, but it works for me. <http://stoney.sb.org/wordpress/2009/06/converting-symbolic- mathematica-expressions-to-c-code/> Maybe this would be useful to you too. - Stoney