So i was doing some algebra today....

This is a discussion on So i was doing some algebra today.... within the C++ Programming forums, part of the General Programming Boards category; ...and thought about how much faster I could do it if i had some sort of tool to automate the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    So i was doing some algebra today....


    ...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?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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?

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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.
    Last edited by Sang-drax; 09-26-2004 at 11:35 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Sang-drax = C++ GOD
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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;
               //.....................
         }
    
            
    
    }

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ...oh yea....thanks for the previous advice

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yesterday, Today and Tomorrow
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-29-2009, 07:36 AM
  2. what happened to the function today
    By yes in forum C++ Programming
    Replies: 6
    Last Post: 02-03-2006, 07:05 AM
  3. algebra solver
    By treenef in forum C++ Programming
    Replies: 20
    Last Post: 03-18-2005, 11:12 AM
  4. Linear Algebra API
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-13-2005, 04:34 PM
  5. Looking for a Boolean Algebra site
    By Majin_Flack38 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-21-2002, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21