Thread: Manipulation and analysis of Mathematical Equations

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    110

    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.
    WebmasterMattD
    WebmasterMattD.NET

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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();
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Thanks for your reply.
    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,
    WebmasterMattD
    WebmasterMattD.NET

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    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

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Had a look at your PolyNom class.
    It has given some good ideas as to tackling the problems I will undoutably encounter, and has also given me a starting point.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Thanks for your help.

    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.
    WebmasterMattD
    WebmasterMattD.NET

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Check out ygf's expression manipulator. He posted the .exe and source on the General Discussions Board.

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >>...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.

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by WebmasterMattD
    Thanks for your help.

    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.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Much apretiated advice.

    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,
    WebmasterMattD
    WebmasterMattD.NET

  12. #12
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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"

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    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,
    WebmasterMattD
    WebmasterMattD.NET

  14. #14
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    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).

Popular pages Recent additions subscribe to a feed