Simplifying expressions for use in C programs

*To*: mathgroup at smc.vnet.net*Subject*: [mg22051] Simplifying expressions for use in C programs*From*: Wagner Truppel <wtruppel at uci.edu>*Date*: Fri, 11 Feb 2000 02:38:32 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

Hi, I'm currently working with very large expressions that are going to be hard-coded in a C program, and I'd like to pre-process them using Mathematica so as to accomplish the following tasks: - minimize the number of multiplications - re-define common sub-expressions as new variables For instance, if d = - x * z^2 * y^2 - (z^5 - x * y^2)*y^3 + (x^3 - y^5)^2 + (z^5 - x * y^2)^3 which is much much simpler than the simplest expression I actually have to deal with, I'd like to have Mathematica pre-process it and output something like this (sans the comments, added here to clarify my notation for powers): x3 = x * x * x; y2 = y * y; y3 = y * y2; y5 = y2 * y3; z2 = z * z; z5 = z * z2 * z2; aux1 = (x^3 - y^5); aux1p2 = aux1 * aux1; /* p2 stands for power 2 */ aux2 = (z^5 - x * y^2); aux2p3 = aux2 * aux2 * aux2; /* p3 stands for power 3 */ d = - x + z2 + y2 - aux2 * y3 + aux1p2 + aux2p3; (variable type declarations can easily be added, of course) Naturally, I'd start with calling Simplify[], whose default behavior tries to minimize the number of terms in the input expression. Figuring out all the powers appearing in the expression doesn't seem hard (something like Cases[d, _^_, {0,Infinity}] ought to do it), and figuring out how to break those powers in a way that minimizes the number of multiplications also isn't too difficult. For instance, since x already appears in d, but x^2 does not, x3 is defined as x*x*x rather than x*x2, which would require another variable to be defined (although the number of multiplications remains two in both cases, since x2 = x*x). Similarly, since y3 has to be defined anyway, y5 can be defined as y2*y3, but z5 = z*z2*z2 since z2 does not have to be defined. I've already written a function to break powers in this fashion: given a list of positive integers, choose one at a time and try to write it as the sum of either a pair or a triplet of the smaller ones. If such pair or triplet cannot be found for a given number, insert half the troubled number (or both the Ceiling[] and the Floor[] of half of the number, if it's odd) into the list and start all over again, with the larger list. My biggest problem is actually recognizing sub-expressions that appear frequently. Performing that simplification would save a substantial amount of computation given the expressions I'm dealing with. I'm wondering if anyone has had to do things like these or if there are already Mathematica notebooks freely available that can perform this sort of pre-processing. Thanks for any and all help. Wagner Truppel wtruppel at uci.edu

**Follow-Ups**:**Re: Simplifying expressions for use in C programs***From:*Wagner Truppel <wtruppel@uci.edu>

**Re: global real variables**

**Finding parts of Equations...**

**Re: Dealing with irregular shaped arrays**

**Re: Simplifying expressions for use in C programs**