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

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    C++ Fit Output
Click For Summary

Discussion Overview

The discussion revolves around converting mathematical expressions into C++ code, specifically focusing on simplifying and modifying the output of mathematical functions for use in programming. Participants explore various methods for achieving this transformation, including string manipulation and the use of macros.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to replace the output of mathematical expressions from "Power" to "pow" and variables "x" and "y" with "pos().x" and "pos().y".
  • Another participant notes that converting math expressions to C++ code is complex, akin to compiler functions, and mentions that C++ lacks an eval function for dynamic expression compilation.
  • A participant suggests using direct multiplication (e.g., "x * x") instead of the "pow" function for small integer powers due to performance considerations.
  • There is a discussion about the nature of "pos()" as a function returning a struct or class with members "x" and "y", with some uncertainty about its application in the context of the original question.
  • One participant mentions that "Power" is a macro and suggests modifying it to include "pos()", while another expresses difficulty in using the #define command due to the complexity of their actual equations.
  • A suggestion is made to use text editors with scripting capabilities for text manipulation, highlighting the utility of regular expressions for editing complex expressions.
  • Another participant advises breaking down complicated expressions into intermediate assignments to simplify debugging, contrasting this with common practices among novice programmers.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to modify and simplify mathematical expressions in C++. There is no consensus on a single method, and the discussion remains unresolved regarding the most effective solution.

Contextual Notes

Some participants mention the use of Mathematica and OpenFOAM, indicating that the context may involve specific programming environments that influence the discussion. There is also mention of potential limitations in using certain C++ features due to the peculiarities of OpenFOAM's language.

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   Reactions: 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   Reactions: jedishrfu and FactChecker

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 52 ·
2
Replies
52
Views
13K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K