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

In summary, the conversation discusses converting mathematical expressions to C++ code and the use of the pow() function. The individual asking the question is seeking help with replacing the Power() function with pow() and converting variables x and y to pos().x and pos().y. The conversation also touches on the complexity of the expression and offers a tip to break it up into smaller expressions for easier debugging.
  • #1
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
  • #2
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.
 
  • #3
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.
 
  • #4
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
  • #5
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.
 
  • #6
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().
 
  • #7
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.
 
  • #8
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.
 
  • #9
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

1. How do I change the output format to fit C++?

To change the output format to fit C++, you can use the setw() function from the iomanip library. This function allows you to set the width of the output and align it to the left or right. You can also use the setprecision() function to set the number of decimal places for floating-point numbers.

2. Can I use the cout statement to output multiple variables in C++?

Yes, you can use the cout statement to output multiple variables in C++. To do this, you can use the << operator to separate each variable. For example, cout << x << " " << y; will output the values of variables x and y with a space in between.

3. How do I change the precision of floating-point numbers in C++?

To change the precision of floating-point numbers in C++, you can use the setprecision() function from the iomanip library. This function takes in an integer value as a parameter, which represents the number of decimal places you want to display for floating-point numbers.

4. Is there a way to align the output to the right in C++?

Yes, you can align the output to the right in C++ by using the setw() function from the iomanip library and passing in a negative value as the parameter. For example, cout << setw(-10) << x; will align the output of variable x to the right with a width of 10 spaces.

5. Can I change the output format for specific data types in C++?

Yes, you can change the output format for specific data types in C++ by using the setprecision() function for floating-point numbers and the setw() function for other data types. You can also use the setfill() function to fill the empty spaces in the output with a specific character. For example, cout << setfill('*') << setw(10) << x; will fill the empty spaces in the output of variable x with asterisks and align it to the right with a width of 10 spaces.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
6K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
59
Views
9K
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top