I have several expressions I have generated in Mathematica that I would like to export into the source code of an external C program. “CForm” almost does what I want except that exponentiation is expressed as calls to Power(). My expressions involves only small powers so I would prefer that the expression in C use inlined multiplication rather than a call to Power().
For example CForm[2 hgt^2 k1inv^3 mx0 wid^2 + hgt^2 k1inv^3 wid^3] yields
2*Power(hgt,2)*Power(k1inv,3)*mx0*Power(wid,2) +
Power(hgt,2)*Power(k1inv,3)*Power(wid,3)
..whereas what I would like to generate is:
2*hgt*hgt*k1inv*k1inv*k1inv*mx0*wid*wid +
hgt*hgt*k1inv*k1inv*k1inv*wid*wid*wid
My initial attempts to pick out the internal Power[..] parts of the expression and remap it to a multiplication using x_Symbol^y_Integer /; y > 1 :> Fold[Times, 1, Table[#1, {#2}]] have been stymied by mathematica immediately converting my carefully generated sub expressions a*a*a right back into Power[a,3] 😉 I know it is only trying to help but I can’t figure out how to ask it to stop, in this case…
As I have written this question it has occurred to me that I could capture the output of CForm into a string and then perform string pattern matching and manipulation on that, but is that a good way to go? I think I prefer to work on it as a Mathematica expression as I do my remapping and then output..?
For the case at hand, you may use something like the following:
For example,
The result is wrapped in
Hold, to prevent its evaluation back toPower-s. You can convert it toa string at any time, using something like
ToString[HoldForm@@result]. Or you can manipulate is further.Edit:
as an alternative, you can do this:
which will also keep the original order of your terms and will get rid of unnecessary parentheses, so this one seem to correspond precisely to your specs.
Generally, you may want to have a look at the new “symbolic C generation” capabilities of the version 8. Mapping your code to symbolic C expressions may be a more robust approach. In that way, you don’t have to worry about evaluation all the time, and you may use the new functionality to generate entire C programs at the end.
Edit 2:
To illustrate how the problem can be solved with the SymbolicC:
This method IMO has several advantages. Perhaps the two main ones being composability (one can nest such expressions in their symbolic form, building larger blocks of code from smaller ones), and the fact that one does not have to think much about evaluation (I didn’t need any tricks with
Holdhere).