6
$\begingroup$

Is there a function which turns optimized expression to C code?

Calling

Experimental`OptimizeExpression[ Sin[x] + Cos[Sin[x]]]

returns

Experimental`OptimizedExpression[Block[{Compile`$1},
          Compile`$1 = Sin[x];
          Cos[Compile`$1] + Compile`$1]]

I would like to turn this to C code

float $1 = sin(x);
return cos($1) + $1;

Is there an easy way to do this? Or do I have to write the conversion myself?

$\endgroup$
3
$\begingroup$

So since I need it, I wrote the function myself. It is a little bit hackish approach, but it solves my problem. So any comment and suggestions for improvement are welcome.

OptimizeExpressionToC[expr_] := 
  Module[ {optimizedExpr, mainExpr, n, m, defs, output},
   optimizedExpr = Experimental`OptimizeExpression[expr];
   n = Length[optimizedExpr[[1, 1]]];
   mainExpr = Flatten@{optimizedExpr[[1, 2, n + 1]]};
   m = Length[mainExpr];

   defs  = 
    Table[ "Real " <>   
            ToString@CForm@optimizedExpr[[1, 2, i, 1]]  <>  
           " = " <>
            ToString@CForm@optimizedExpr[[1, 2, i, 2]] <>
           ";",
            {i, 1, n}];

   output = 
    Table[ "out[" <>
            ToString[i - 1] <>              
           "] = " <>
            ToString@CForm@mainExpr[[i]] <>     
           ";",
           {i, 1, m}];
   Join[defs, output]
   ]; 

Test

OptimizeExpressionToC[{Sin[x] + Cos[Sin[x]] + x y, x y}] 

outputs

{"Real Compile_$32 = Sin(x);",
 "Real Compile_$31 = x*y;", 
 "out[0] = Compile_$31 + Compile_$32 + Cos(Compile_$32);",
 "out[1] = Compile_$31;"}
| improve this answer | |
$\endgroup$
  • $\begingroup$ There's the SymbolicC package. You can construct a symbolic representation of the C code and convert it to a C code string in one go. The advantage is that the symbolic representation is a Mathematica expression, so you can use all the standard expression manipulation techniques up to the point where you decide to convert to a string. Will it be easier here than your approach? I do not know. It might be, but I'm not sure. Just wanted to let you know about this. $\endgroup$ – Szabolcs Oct 20 '16 at 22:12
  • $\begingroup$ I strongly suspect that there is some mechanism in Mathematica that already does the conversion to symbolic C automatically. But I don't know where it is and how to use it. It is internal stuff. $\endgroup$ – Szabolcs Oct 20 '16 at 22:14
  • $\begingroup$ I'm aware of SumbolicC package, but I could not figure out how it can help me in this situation. $\endgroup$ – tom Oct 22 '16 at 15:06
2
$\begingroup$

Not an answer but possibly a starting point.

You could try the CCodeGenerator package along the lines of:

Needs["CCodeGenerator`"]
out = Compile[{{x, _Real}}, Sin[x] + Cos[Sin[x]], 
              CompilationOptions -> {"ExpressionOptimization" -> True}]

then

CCodeStringGenerate[out , "test"]

will produce C code.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Yeah, In this example the generated C code is quite nice, but take for example a simple expression x^3 and generate C code for it and you get something completely unreadable. $\endgroup$ – tom Oct 20 '16 at 9:59
  • $\begingroup$ @tom Try with out = Compile[{{x, _Real}}, x x x, CompilationOptions -> {"ExpressionOptimization" -> True}]; the output looks very reasonable. $\endgroup$ – b.gates.you.know.what Oct 20 '16 at 13:17
  • $\begingroup$ x^3 was just a toy example. I have an expression which is long over 9000 of lines of code which I need to convert to C code. I do not have the time to change the form of this expression such that CCodeStringGenerate would give me easily modifiable code. $\endgroup$ – tom Oct 20 '16 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.