Mathematica How to Break Up and Simplify Complicated Expressions in C++?

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    C++ Fit Output
AI Thread Summary
The discussion focuses on simplifying and converting mathematical expressions in C++ from a specific output format to a desired format. The user seeks to replace "Power" with "pow" and variables "x" and "y" with "pos().x" and "pos().y". There are suggestions regarding the inefficiency of using the pow() function for small integer powers, advocating for direct multiplication instead. The conversation also touches on the complexity of the expressions being handled and the potential use of macros to streamline the process. Overall, the thread emphasizes the importance of breaking down complex expressions for easier debugging and manipulation in C++.
member 428835
Hi PF!

My question is easiest to show via the following input, the output, and my desired output.

input: x^2 + y^2 // CForm
output: Power(x,2) + Power(y,2)
desired output: pow(pos().x,2) + pow(pos().y,2)

I appreciate any help you can offer. Thanks so much!

Edit: for what it's worth, I'm just going to copy-paste the output into a different program, if that matters?
 
Last edited by a moderator:
Physics news on Phys.org
Are you saying you want to convert math expressions to c++ code?

That is a non trivial exercise not unlike what a compiler does.

Some languages have an eval function that compiles expressions on the fly but c++ doesn't have that out of the box.
 
jedishrfu said:
Are you saying you want to convert math expressions to c++ code?
No, but admittedly my title is misleading. I just want to replace the output Power with pow and each variable x with pos().x

I've tried doing this as a string but I fail. See, I'm simply copy-pasting the result. I can actually do this using the find keyboard shortcut and the selecting the replace button, but I figure there has to be a better way.
 
I never use the C/C++ pow() function for small integer powers. Instead of pow(x, 2), I use just x * x. The reason is that the pow() function is very costly in terms of CPU cycles -- whereas multiplication is much less costly.

It's still not clear what you are trying to do with pos().x and pos().y. Apparently pos() is a function that returns a struct or class that has members named x and y.

Would something like this work?
C:
result = pos().x * pos().x +  pos().y * pos().y;

BTW, does your question have anything to do with Matlab, Mathematica, or LaTeX? If not, I'll move it to the Computing section.
 
  • Like
Likes jedishrfu
Mark44 said:
It's still not clear what you are trying to do with pos().x and pos().y. Apparently pos() is a function that returns a struct or class that has members named x and y.
See, what I'm actually doing (before the line "input") is a bunch of trig reductions and simplifications. I do this in Mathematica and then copy-paste into OpenFOAM, which kind of uses this C++ library.

Mark44 said:
Would something like this work?
C:
result = pos().x * pos().x +  pos().y * pos().y;
Not really, since sometimes x,y are lifted to non-integer powers.

Mark44 said:
BTW, does your question have anything to do with Matlab, Mathematica, or LaTeX? If not, I'll move it to the Computing section.
Yep, but I think my first response in this post answers/elaborates on this.
 
Power is itself a macro
C:
#define Power(x, y)    (pow((ldouble)(x), (ldouble)(y)))
It isn't hard to modify it to include pos().
 
DrClaude said:
Power is itself a macro
C:
#define Power(x, y)    (pow((ldouble)(x), (ldouble)(y)))
It isn't hard to modify it to include pos().
Sorry, there's just a ton of x's and y's in the actual post (out of simplicity didn't want to post the real equation). And I actually don't think I can use the #define command, but I'll check tomorrow (having real problems even defining variables. OpenFOAM uses a weird C++ - like language.
 
What editor are you using? I liked the gvim editor because I could use small Perl scripts for text manipulation. I would send lines of the edited file (or the entire file) to the Perl script and the printed results would be inserted back in the file. Most editors have some regular expression parsing/substitution capability that can do limited editing.
 
joshmccraney said:
Sorry, there's just a ton of x's and y's in the actual post (out of simplicity didn't want to post the real equation).
It sounds like you have a very complicated expression to be evaluated. As a tip, instead of packing all that stuff into a single expression that is assigned to some variable, break the expression up into several intermediate assignment statements, something like this:
C:
expr1 = <complicated expression>;
expr2 = <another complicated expression>;
result = pow(expr1, 2.5) + pow(expr2, 2.5);

This is the kind of thing that real programmers do to make debugging a lot simpler. It's also the kind of thing that novice programmers don't usually do, and then they struggle at trying to find why their program is producing incorrect results.
 
  • Like
Likes jedishrfu and FactChecker

Similar threads

Replies
1
Views
2K
Replies
2
Views
2K
Replies
2
Views
3K
Replies
1
Views
6K
Replies
5
Views
3K
Replies
4
Views
12K
Back
Top