Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Algorithm to partition a complex equation into 2 master equations

  1. May 16, 2014 #1
    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 (Text):

    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 (Text):

    enum eqOps { ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, COMPOSITION, NONE };
     
    defined in

    Code (Text):

    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?
     
  2. jcsd
  3. May 16, 2014 #2
    Welcome to the fascinating world of parsers and lexical analysers.

    What should your function do if the equation looks like x + x + x?
     
  4. May 16, 2014 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

  5. May 23, 2014 #4
    In that case,

    eq1 = x,
    eq2 = x + x
     
  6. May 23, 2014 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    + 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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Algorithm to partition a complex equation into 2 master equations
Loading...