tags:

views:

219

answers:

4

I have derived and simplified an equation in Matlab and want to use it in a c++ program. Matlab likes to use powers, the ^ sign but c++ doesn't like it one bit. How can i get Matlab to rewrite the equation so that it outputs a c++ friendly equation?

+1  A: 

You can use the pow family.

Matthew Flaschen
Well, all my powers are to the power of 2 or 3, whole integers. The pow() function is expensive and i need to do a lot of calculations. The equation itself is very long and i really don't want to manually go in and clean it up.
17mm
So you want to automatically parse your Matlab code and output C code (e.g. x * x or y * y * y)?
Matthew Flaschen
yes, that's exactly what im looking for
17mm
+1  A: 

In C++ the pow() function is overloaded for integer powers - it uses a faster algorithm for ^2,3 etc

Martin Beckett
I just tested that a few seconds ago. I used the overloaded version with integers only and it seems that using simply x*x is still faster than the integer pow() function...by a factor of 20 times. The equations are literally an entire 8.5x11 sheet of paper long. I want to know if theirs a command in matlab to rewrite it whout the caret because doing it by hand is going to suck.
17mm
It shouldn't be - there will be a small overhead from the function call. Are you sure the second argument is declared as an 'int', just passing a double with the value 2 isn't enough.Are you using release build?
Martin Beckett
No, im simply using a timer running in debug mode. Is there really no way to get matlab to output some c++ friendly code?
17mm
@17mm - not that I know of. There are matlab functions to call external C++ dlls, but it's not really in their interest to let you replace expensive copies of Matlab with free C++ programs is it?
Martin Beckett
There have been Matlab compile-to-C products, IIRC, but they rely heavily on a Mathworks runtime library -- so they're not really letting people replace their copies of Matlab with free C++ programs! In any case, doing benchmarks in debug mode is unlikely to give representative numbers for things like pow() overloads.
Brooks Moses
A: 

There is a Matlab Clone Octave which uses the same Syntax as Matlab (I don't know how much of the syntax is supported though). Since it is Open Source, maybe you can reuse the parser (I read something about it from the author in that "a bit old" thread as well).

Afterwards you can create C++ Code from the syntax tree.

And then there is also a tool for converting Matlab into C code. I havent used it yet and it is not available for free.

Rupert Jones
There's a DDJ article about that tool, as well: http://www.drdobbs.com/cpp/196901296.
Brooks Moses
+2  A: 

If the equation is really so long that you don't want to go through by hand, one option you might consider for reformatting the equation to make it C++ friendly is to parse the text of the MATLAB code for the equation using the REGEXPREP function in MATLAB. Here's an example of how you could replace expressions of the form x^2 or y.^3 with pow(x,2) or pow(y,3):

eqStr = 'the text of your equation code';  %# Put your equation in a string
expr = '(\w+)\.?\^(\d+)';                  %# The pattern to match
repStr = 'pow($1,$2)';                     %# The replacement string
newStr = regexprep(eqStr,expr,repStr);     %# The new equation string

You would just have to take the code for your MATLAB equation and put it in a string variable eqStr first. The output from REGEXPREP will then be the text for your new C++ friendly equation newStr.

You could also change the replacement string to give you results of the form x*x or y*y*y using dynamic operators. For example:

eqStr = 'the text of your equation code';  %# Put your equation in a string
expr = '(\w+)\.?\^(\d+)';                  %# The pattern to match
repStr = '${repmat([$1,''*''],1,$2-''0''-1)}$1';  %# The replacement string
newStr = regexprep(eqStr,expr,repStr);            %# The new equation string
gnovice
Perfect! Thank you very much this is exactly what I'm looking for. To be honest, I'm a little surprised that matlab doesn't already have something like this implemented directly into a simple to use function. A lot of people working with me derive and simplify their equations in matlab first and then implement it into their c++ programs (most of the time the algebraic matlab equations can simply be pasted into the code without modifications, but in this case, sometimes not). Thanks, you've saved me hours of work!
17mm
@17mm: Glad to help. Just one head's up... the above examples won't match things like `(x+y)^2`. They will only match single variables raised to a power. You will have to modify the match expression to work for other cases, which may end up being more complicated.
gnovice