# Manipulation and analysis of Mathematical Equations

• 05-24-2003
WebmasterMattD
Manipulation and analysis of Mathematical Equations
The title is a bit long, but esentialy it is too the point.

Anyway, I at the begining of the day I was completely out of ideas of what to program, so I decided to try my luck at using C++ to do some mathematical equations.

The problem that I am having is that once I have got the equation in as a string (got a list of variables and their multipliers / dividers) I am stuck as too how to go about manipulating this data.

What is the best way of doing this. For the basics I just have a whole group of switch and ifelse statements and for loops, but this is getting way out of control.

Any help would be greately apreciated.

What I am planning to do with the equations is find such things as their derivatives and also their solutions for a given set of variables.

Thanks heaps.
• 05-24-2003
Sebastiani
Kind of a broad question there. What do you have so far? Post some code so we can see what approach you've taken. For simple arithmatic, just push all of the numbers onto one stack, ops onto another. Then evaluate them like this:

Code:

``` while(!ops.empty() && stack.count()>1) { left = stack.pop(); right = stack.pop(); op = ops.pop(); switch(op) { case '+' : stack.push(left+right); break; // etc. } } total = stack.pop();```
• 05-24-2003
WebmasterMattD
That seems like a viable solution for non-algebraic equations.

One program that I have completed uses the Simplex Method for finding solutions to maximisation problems.
The source code is avaliable here
and this is what I am going to be using for the retrieval of the string and looking at it.

Though this is only one of the programs that I am working on at the moment.

To try and slim down the question I am interested in working with algebraic mathematic equations.

Later,
• 05-24-2003
roktsyntst
Quote:

Originally posted by WebmasterMattD
To try and slim down the question I am interested in working with algebraic mathematic equations.
What kind of algebraic equations? And are they equations to be solved (i.e. something = something_else, both sides of which may include variables ), expressions to be evaluated, or functions? I ask because I once wrote a Polynomial class that might help you out, although its input is kind of specific, and the code is ugly because at the time I didn't know STL and the string class. Still, it may provide some of the functionality you want, or at least some ideas.

Let me know how it works out. I was thinking of writing a set of classes to model math functions, but I thought it would be more trouble than worth (since I had no real use for it, I just thought it was interesting). Good luck, HTH
• 05-24-2003
WebmasterMattD
It has given some good ideas as to tackling the problems I will undoutably encounter, and has also given me a starting point.

Later,
• 05-24-2003
Zach L.
Actually, using a stack to evaluate the expression fails to account for one detail... order of operations. A standard solution is to use a tree where each terminal node must be a number or an identifier (i.e. variable placeholder), and all of the non-terminal nodes are operations (with the number of children corresponding to the number of input variables). This way, the operations with the highest order can be placed closest to the operands. An example:

The expression:

3*4 + 5*4/5

Has a representation of in the tree of:
Code:

```    +  *      * 3 4    5  /           4 5```
If you just place it in a stack though, it evaluates as:

(3*4 + 5) * 4/5

For symbolic algebra, you can use a placeholder class for the leaves of the tree. The placeholder can either be a number or variable, based on the internal state, and then you can manipulate the rest of the tree to solve for the variable.

Cheers
• 05-26-2003
WebmasterMattD

One prolem that I am running into is when I try to overload the '-' operator.

What I am confused about is which of the classes in the expression
class1 = class2 - class3
does the program use for the pointer 'this'?

I know that it uses class3 as the argument passed to the function when declared as
class operator -( class asdf );

Any help on this would be greately apreciated.
• 05-26-2003
golfinguy4
Check out ygf's expression manipulator. He posted the .exe and source on the General Discussions Board.
• 05-26-2003
The Dog
>>...which of the classes in the expression
class1 = class2 - class3
does the program use for the pointer 'this'?

There could be two overloaded operators in that statement ( '=' & '-' ). For the '-' operator, class2 would be answer, and for the '=' operator, class1 would be the answer.
• 05-28-2003
roktsyntst
Quote:

Originally posted by WebmasterMattD

One prolem that I am running into is when I try to overload the '-' operator.

What I am confused about is which of the classes in the expression
class1 = class2 - class3
does the program use for the pointer 'this'?

Like the Dog said, there are two possibilities, and you will have to cover both. As I understand it, that's basically saying
Code:

`class1.operator=( class2.operator-( class3 )  )`
You'll have to define an operator= and an operator- (or something that allows the object to be converted to something that has those defined).

Sometimes it helps to name the parameter for +,-, etc. "rhs" for "right-hand-side", and think of "this" as the left hand side. Then code the function in terms of the lhs and rhs. HTH.
• 06-07-2003
WebmasterMattD

I have got the class to the stage where it can do addition and subtraction as well as factorising of quadratics.

What I am stuck on for ideas is how to do long division (in order to do factorising of cubics, quartics and higher degrees. Any other methods would be greatly apreciated if you feel I am going about this the wrong way.) and how to determine the type of equation that has been entered (currently handles ax^2 + bx + c = 0 style equations) so that it can handle any form of polynomail equation.

If anyone is interested in looking at the code it is avaliable
here.

Thanks for all of your help so far.

Later,
• 06-07-2003
ygfperson
Quote:

What I am stuck on for ideas is how to do long division (in order to do factorising of cubics, quartics and higher degrees. Any other methods would be greatly apreciated if you feel I am going about this the wrong way.) and how to determine the type of equation that has been entered (currently handles ax^2 + bx + c = 0 style equations) so that it can handle any form of polynomail equation.
I'm using linux. When I type stuff in, it forces me to press enter twice, and it doesn't accept my input: "3x^2+5x+3=0"
• 06-07-2003
WebmasterMattD
Code still verry alpha stage of development.

It will accept the input if you provide spaces as per this example
Code:

`  3x^2 + 2x + 1 = 0`
I shall have to go about fixing that part of it up. Thanks for pointing it out.

Later,
• 06-07-2003
roktsyntst
If your divisor is linear, you can use synthetic division. I believe there are techniques for nonlinear divisors (i.e. for a quadratic, divide once each by two linear factors). The algorithm isn't too difficult (though it's kinda hard to explain without showing you an example). If you want more info, let me know (or just google it).