# So i was doing some algebra today....

• 09-26-2004
misplaced
So i was doing some algebra today....
:o
...and thought about how much faster I could do it if i had some sort of tool to automate the BS of simplifying and solving equations...hmmmmmmm........DING!.....I have decided to take on the most complex computer program i have ever written (probably not real complex to any of you real coders though). After thinking about it all day, it turns out it will logically be a little harder than i thought.

I was wondering if anyopne had any advie on how to arrange and store my data for example:

Code:

2-x                                  50x^3
------  + 8xyz^2 = 2(5 + x) -  ----------
5*2                                    9

I'm having trouble trying to figure out how to arrange that into manageable data structures. One thing i know for sure is i would like to break the equation up into terms. The pitfall is in a fraction there may be one or more terms for either numerator and/or divisor....however a fraction IS a term... any advice on this ?

any advice on the project in general?
• 09-26-2004
misplaced
ok....i just remembered that you can split terms in a numerator up into seperate terms over a common divisor 4-1 / 6 = 4/6 -1/6 ....... but what if it's like 10 / 25 - 5? how do i break that down? or can i?
• 09-26-2004
manofsteel972
A few suggestions. Define exactly what you want your program to accomplish in simple english terms. Then describe the process involved in psuedo code then decide what data structures would best be used in those processes then write some code and test it. Without a clear plan and a clear idea of the program you are going to get lost pretty quickly.
• 09-26-2004
Sang-drax
First, you'll have to write an expression parser, which takes a string and computes the value of the expression contained.

Then, you'll need an algorithm to solve the system of equations you have.
To solve a system of non-linear equations, the Newton Raphson method can be used:

Equation system:
f1(x1, x2, ..., xn) = 0
f2(x1, x2, ..., xn) = 0
...
fn(x1, x2, ..., xn) = 0

X is a column-matrix with the initial solution guess.
X[i,1] = x[i ]

F is a column-matrix with the function values for X
F[i,1] = f[i ] (x1, x2, ..., xn)

J is a n*n matrix called the Jacobian matrix which looks like this:
J[i,j] = df[i ] / dx[j]
(all the partial derivates)

The solution to the equation system is found by iterating the following formula:
X = X - inv(J) * F;

If it converges, it may converge towards the solution,
otherwise a new initial guess will be chosen.

I cannot prove this or anything (haven't read calculus yet). But it seems to work okay. If you know about Newton Raphson's method for a single variable, you'll notice that the algorithms remind of each other.

I have implemented this algorithm in my language.
You can try it out here:
http://www.strandmark.com/omicroncompiler.shtml
choose example "Equation solving".

The above algorithm only works when the number of unknowns is equal to the number of equations, but I think that can be fixed by adding some extra "0 == 0" equations to make the matrices square.

Conclusion: Yes, this is a quite complicated task. :)
• 09-26-2004
The Brain
Sang-drax = C++ GOD
• 09-27-2004
manofsteel972
Granted Sang_drax is a very accomplished programmer. But he has a clear plan and he understands the problem. Anyone who knows something that you don't i guess would seem to be godlike. Hmm I think that is how magic was born to explain the unexplained. I think Sang_drax is more of a C++ magician then a god. Because once you know the trick it no longer seems magical.
• 09-27-2004
misplaced
i haven't ran it yet or anything, but the idea is to take a string and convert it into type int, float, long, or long double.

would anyone implement this differently?...

Code:

template<class NumberType> NumberType parse(const char &stringOfNumbers)
{
//some parsing algorithm

//isn't there a header with these defintions?
NumberType convertedNumber;
int  sizeFloat = sizeof(float);
int longSize = sizeof(long);
int longdoubleSize = sizeof(long double);

switch(sizeof(NumberType))
{
case floatSize:
convertedNumber = atof(stringOfNumber, 10);  //stringOfNumbers = "1234.54"
break;
case longSize:
convertedNumber = atol(stringOfNumbers, 10);
break;
//.....................
}

}

• 09-27-2004
misplaced
...oh yea....thanks for the previous advice
• 09-29-2004
misplaced
if anyone came to this thread looking for the same thing i did, then go to this url. this is exactly what i was looking for.

http://www.brpreiss.com/books/opus4/...00000000000000