matlab math to c++ math conversion

matlab math to c++ math conversion

Post by Joseph Dju » Sat, 17 Apr 2010 06:21:04

I was wondering if anyone has run into an easy way to convert a math expression in matlab to a c++ expression. For example:

In MATLAB you have a symbolic expression like:

In C++ this would be something like:
"pow((x+y),2.0) + 1/pow((2*x+y),1.0/2.0)"
(this assumes of course the user is smart enough to include math.h and define the variable x and y)

I am faced with converting several dozen math expressions that span several lines each into a c++ code and was wondering if others have found an easy solution for this other than to manually switch out expressions like "^" and transform them to "pow(,)".
Thanks in advance for any help with this.

PS: If it wasn't obvious from the question, I am trying to re-write a matlab code in c++ and was faced with converting lots of math equations. I could use the mcc function but I would like a more clean and perhaps more optimized code, hence the reason for no using mcc or other such converters.

matlab math to c++ math conversion

Post by Steven Lor » Sat, 17 Apr 2010 23:48:23

If you really mean you have an expression created using sym objects from
Symbolic Math Toolbox when you say "a symbolic expression" then look here:

If you mean that you have a string that you'd like to convert, I don't
believe there's a ready-made function to do what you want, though creating
the correct patterns [the hard part] and applying REGEXP to your expression
should do the trick.

Steve Lord
comp.soft-sys.matlab (CSSM) FAQ:


matlab math to c++ math conversion

Post by Joseph Dju » Tue, 20 Apr 2010 23:43:21

Wow never knew that command existed! THANKS A BUNCH STEVE! This is exactly what I needed! You saved me hours upon hours of work. :D