Algorithm to partition a complex equation into 2 master equations

Click For Summary

Discussion Overview

The discussion revolves around developing a function to partition a complex equation into two master equations based on operator precedence. The focus is on programming techniques related to parsing mathematical expressions, particularly in the context of a calculus tool.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes a function intended to partition an equation into two parts based on the operator with the lowest precedence.
  • Another participant questions how the function should handle equations with repeated operators, such as "x + x + x".
  • A third participant references a resource on recursive descent parsing, suggesting it may be relevant to the implementation.
  • Further clarification is provided regarding the handling of associative operators, specifically mentioning that the treatment of subtraction may differ from addition.
  • There is a suggestion that in the case of subtraction, the first part of the equation should be adjusted to reflect the correct operator precedence.

Areas of Agreement / Disagreement

Participants express differing views on how to handle specific cases of operator precedence and the implications of associativity, indicating that there is no consensus on the best approach to partitioning equations.

Contextual Notes

Participants have not fully resolved how to handle equations with multiple identical operators or the implications of operator associativity on the partitioning logic.

Jamin2112
Messages
973
Reaction score
12
My Calculus tool is coming along. The only thing left is to write some it's helper functions, such as the one described below:

Code:
void CalculusWizard::partitionEquation(const std::string & eq, std::string & eq1, std::string & eq2, CalcWizConsts::eqOps & oper)
{
	/* Given an equation eq, partion eq into 
	   eq = eq1 oper eq2
	   where oper is the operator with the lowest precedence, 
	   e.g. eq = "x*sin(x)+x^2" --> eq1 = "x*sin(x)", oper = ADDITION, eq2 = "x^2".
	   If there is no operator, e.g. eq = "x", then oper = NONE.
	*/
}

which uses

Code:
enum eqOps { ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, COMPOSITION, NONE };

defined in

Code:
namespace CalcWizConsts.

Does anyone have advice for how I should start out on this function? Or is there any resource you could direct me to where I could learn this type of thing?
 
Technology news on Phys.org
Welcome to the fascinating world of parsers and lexical analysers.

What should your function do if the equation looks like x + x + x?
 
voko said:
Welcome to the fascinating world of parsers and lexical analysers.

What should your function do if the equation looks like x + x + x?

In that case,

eq1 = x,
eq2 = x + x
 
Jamin2112 said:
In that case,

eq1 = x,
eq2 = x + x

+ is too easy, because it is associative. Be careful when you have x - y - z.

I think in your notation eq2 would then become y ##+## z. Or better, eq1 should have been x - y.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K