# OptimizeExpression to C code

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

Calling

ExperimentalOptimizeExpression[ Sin[x] + Cos[Sin[x]]]


returns

ExperimentalOptimizedExpression[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? ## 2 Answers 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 = ExperimentalOptimizeExpression[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 = Compile_$31 + Compile_$32 + Cos(Compile_$32);",
"out = Compile_\$31;"}

• 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. – Szabolcs Oct 20 '16 at 22:12
• 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. – Szabolcs Oct 20 '16 at 22:14
• I'm aware of SumbolicC package, but I could not figure out how it can help me in this situation. – tom Oct 22 '16 at 15:06

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.

• 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. – tom Oct 20 '16 at 9:59
• @tom Try with out = Compile[{{x, _Real}}, x x x, CompilationOptions -> {"ExpressionOptimization" -> True}]; the output looks very reasonable. – b.gates.you.know.what Oct 20 '16 at 13:17
• 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. – tom Oct 20 '16 at 13:57