Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exporting expressions to C, part 1 [wolfram-mathematica]

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..?

like image 910
Daniel Chisholm Avatar asked Mar 25 '11 14:03

Daniel Chisholm


1 Answers

For the case at hand, you may use something like the following:

Clear[getCFormNoPowers];
getCFormNoPowers[expr_] :=
  Module[{times},      
   Apply[Function[code, Hold[CForm[code]], HoldAll],
     Hold[#] &[expr /. x_Symbol^y_Integer /; y > 1 :> 
         times @@ Table[x, {y}]] /. times -> Times]];

For example,

In[52]:= getCFormNoPowers[2 hgt^2 k1inv^3 mx0 wid^2+hgt^2 k1inv^3 wid^3]

Out[52]= Hold[2*mx0*(hgt*hgt)*(wid*wid)*(k1inv*k1inv*k1inv) + 
hgt*hgt*(k1inv*k1inv*k1inv)* (wid*wid*wid)]

The result is wrapped in Hold, to prevent its evaluation back to Power-s. You can convert it to a string at any time, using something like ToString[HoldForm@@result]. Or you can manipulate is further.

Edit:

as an alternative, you can do this:

Clear[getCFormNoPowers];
getCFormNoPowers[expr_] :=
 Block[{Times},
   SetAttributes[Times, {Flat, OneIdentity}];
   Apply[Function[code, Hold[CForm[code]], HoldAll],
   Hold[#] &[expr /. x_Symbol^y_Integer /; y > 1 :> Times @@ Table[x, {y}]]]];

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:

Needs["SymbolicC`"];

Clear[getCFormNoPowersSymC];
getCFormNoPowersSymC[expr_] :=
  Block[{Times},
   SetAttributes[Times, {Flat, Orderless}];
   ToCCodeString[
     expr /. x_Symbol^y_Integer /; y > 1 :> Times @@ Table[x, {y}] //.     
       HoldPattern[(op : (Times | Plus))[args__]] :>  COperator[op, {args}]]];

In[53]:= getCFormNoPowersSymC[2 hgt^2 k1inv^3 mx0 wid^2+hgt^2 k1inv^3 wid^3]

Out[53]= 2 * hgt * hgt * k1inv * k1inv * k1inv * mx0 * wid * wid + 
    hgt * hgt * k1inv * k1inv * k1inv * wid * wid * wid

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 Hold here).

like image 150
Leonid Shifrin Avatar answered Oct 02 '22 16:10

Leonid Shifrin