2

Here is my Mathematica code:

f[x_] := x^2 + 4 x + 4; Solve[f[x] == 9, x]

I tried to convert it to C but failed.

Here is the code for conversion:

CForm[f[x_] := x^2 + 4 x + 4]

and this

CForm[f[x_] := x^2 + 4 x + 4; Solve[f[x] == 9, x]]

The former one just outputs Null, the latter one just converts the final result in the below

List(List(Rule(x,-5)),
   List(Rule(x,1)))
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
kile
  • 1,671
  • 5
  • 10
  • 3
    From help it says Standard arithmetic functions and certain control structures are translated. and No declarations are generated.. So CForm is limited to basic expressions that have direct C mapping. Also, What would CForm translate Solve command to in C? Since C do not have build in Solve command. If you do CForm[x^2 + 4 x + 4] then it translate it. – Nasser Dec 25 '19 at 02:40
  • I can't understand why you want to do what you asking for. What would you expect the C code to look like? How would use translation in C program if you had it? – m_goldberg Dec 25 '19 at 05:07

1 Answers1

10

Mathematica does not currently have any facilities to expose complex symbolic functionality, such as Solve, to C. To be precise, this is not possible without having access to a full Mathematica installation and explicitly calling it e.g. through MathLink (see chapter 2).

As for CForm, its purpose is to convert simple arithmetic expressions to C syntax, not to convert full programs.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263