[Mathematica] : Method to import C-Code to Expressions

Click For Summary
SUMMARY

The discussion focuses on importing C code generated by Mathematica back into Mathematica expressions. The user, Hepth, seeks an automated method to convert complex C code, specifically involving the use of the PolyLog and pow functions, into Mathematica's syntax. A suggested solution involves using the ToExpression function combined with string replacement to convert pow to Power. This method effectively addresses the issue of manual conversion errors.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of C programming concepts, particularly function syntax
  • Basic knowledge of string manipulation in Mathematica
  • Experience with mathematical functions such as PolyLog and Power
NEXT STEPS
  • Research the ToExpression function in Mathematica for converting strings to expressions
  • Explore string manipulation techniques in Mathematica for more complex replacements
  • Investigate existing Mathematica packages that facilitate C code integration
  • Learn about error handling in Mathematica to manage conversion issues effectively
USEFUL FOR

Mathematica users, programmers converting C code to Mathematica, and mathematicians dealing with complex expressions in computational environments.

Hepth
Science Advisor
Gold Member
Messages
458
Reaction score
40
I have some ugly c-code that was generated from Mathematica in years past with CForm, and looks like :

Code:
(PolyLog (2,(-1 + t)*pow (-1 + q2h,-1))*pow (q2h - t,-1)*pow (-1 + \
t,-1)*
     (-8*pow (q2h,3)*pow (t,2) + pow (q2h,4)*(1 + pow (t,2)) - 
       8*q2h*pow (t,3)*(-3 + 8*t - 5*pow (t,2) + pow (t,3)) + 
       pow (q2h,2)*pow (t,2)*(-13 + 40*t - 16*pow (t,2) + pow (t,4)) + 
       pow (t,4)*(-10 + 24*t - 13*pow (t,2) + pow (t,4)))*
     pow (q2h - pow (t,2),-3))/8. - 
  (PolyLog (2,-((q2h - t)*(q2h + (-1 + 2*ctf)*t)*pow (ctf,-1)*pow \
(t,-2))/2.)*
     pow (q2h - t,-1)*pow (-1 + t,-1)*
     (-8*pow (q2h,3)*pow (t,2) + pow (q2h,4)*(1 + pow (t,2)) - 
       8*q2h*pow (t,3)*(-3 + 8*t - 5*pow (t,2) + pow (t,3)) + 
       pow (q2h,2)*pow (t,2)*(-13 + 40*t - 16*pow (t,2) + pow (t,4)) + 
       pow (t,4)*(-10 + 24*t - 13*pow (t,2) + pow (t,4)))*
     pow (q2h - pow (t,2),-3))/8.

I would like to import this back into Mathematica in an automatic way, as the true expressions are many pages long.

I tried by hand with things like : StringReplace[%, {"pow(t," ~~ Shortest[b__] ~~ ")" :> "Power[t," ~~ b ~~ "]"}];

but it gets ugly, and sometimes makes mistakes.

Is there a package or method that exists that would let me do this? Can Mathematica import C code at all, or convert it if its simple (like this)?

Thanks!
-Hepth
 
Physics news on Phys.org
Your string has issues; check out for backslashes (and maybe dots, if you don't want inexact numerics), for example

Once done, try this:

ToExpression[string, TraditionalForm, HoldComplete] /. pow -> Power

Worked OK for me.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K