Algorithm to partition a complex equation into 2 master equations

AI Thread Summary
The discussion revolves around developing a helper function for a Calculus tool, specifically the partitionEquation function, which aims to split a mathematical equation into two parts based on the operator with the lowest precedence. The function should handle various operators defined in an enum, including addition, subtraction, multiplication, and division. Key points include how to manage equations with multiple instances of the same operator, such as "x + x + x," where the output should correctly identify the first operand and the remaining expression. The conversation also touches on the complexities of handling non-associative operators like subtraction, emphasizing the need for careful parsing to ensure accurate partitioning. Participants suggest exploring resources on parsers and lexical analyzers to enhance understanding and implementation of the function.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
4K
Replies
3
Views
2K
Replies
13
Views
3K
Back
Top