Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i just started learning perl a little while ago, and i have been spending ages trying to write this script to do my algebra homework for me since i am too lazy to do it myself lol. it has to solve multistep equations such as this: 197 = 5(4a + 10) + 4a. and i totally cannot get it to work and it is driving me mad!! i came up with a basic algorithm for how it should work, it takes one argument, the equation, formatted so that it contains no spaces and the variable is called x, so that 197 = 5(4a + 10) + 4a would be 197=5(4x+10)+4x. then it splits the equation into two halves around the equals sign so that it becomes my $left = 197 and my $right = 5(4x+10)+4x. and that is where i get stuck, i want it to substring 5(4x+10) out of $right and then distribute it, but i cant figure out how to get it to do that. does anyone have any idea how i would write this script?? thanks in advance

Replies are listed 'Best First'.
Re: solving multistep equations
by GrandFather (Saint) on Sep 14, 2014 at 20:48 UTC

    Think about the steps you take to solve it yourself. Write the steps down one below the other and write a comment beside each one to say what your did to get from the previous step to the current step. Think about how you could write some code to perform the step, then think about the pattern you recognized in the first step and how you could write code to recognize that pattern.

    When you have code that tries to do all that, come back to us for help getting the code to work. We will want to see the code and the test cases you are using.

    Perl is the programming world's equivalent of English
Re: solving multistep equations
by LanX (Saint) on Sep 14, 2014 at 20:53 UTC
    You could try to 1. parse all and 2. transform it into a Horner scheme.

    The second step would imply multiplying all parts out especially the parens and summing up all parts where x has the same power.

    At the end you should have something of the form of a sum 0 = a + bx + cx² + dx³ + ...

    (I suppose you don't expect any exponents bigger or equal 2, but the approach is the same)

    Already the first parsing step isn't easy and since you didn't provide any code I dare to doubt that you could master to understand the full algorithm.

    So better try doing your homework or just acquire a symbolic calculator. ;-)

    HTH! :)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: solving multistep equations
by Anonymous Monk on Sep 14, 2014 at 21:02 UTC

    Welcome to PerlMonks! Writing an equation solver is a great way to learn (Perl) programming.

    To get help with your code, please post it! See this node for information on how to help us help you.

    How you approach the problem in general will depend on which level you want to begin.

    The basic approach to this kind of problem is to parse the expression using a parser such as a Recursive descent parser to translate the string "197 = 5(4a + 10) + 4a" into a representation that the software can more easily understand and manipulate. The Perl module Parse::RecDescent can help you write such a parser; this might help get you started.

    If you don't want to go to that low of a level, it looks like someone else has already written a parser, Math::Expr (haven't used this myself, YMMV). You could use this module to parse the equation, and then manipulate the resulting tree to solve your equation.

    Of course, this wheel has already been invented too - a quick search brings up Math::Algebra::Symbols, among others, which can solve equations for you.

    And going even further than that, there are full-featured math software packages available for free online, such as Sage online at https://cloud.sagemath.com/ , see in particular this tutorial: http://www.sagemath.org/doc/tutorial/tour_algebra.html . There's also Wolfram Alpha, see e.g. http://www.wolframalpha.com/examples/Algebra.html . If you want a software you can download and install, there's Maxima (I've found wxMaxima a useful interface).

    See also a few other places this kind of question has been asked before: Parsing Math Strings, Perl and maths.

Re: solving multistep equations
by Laurent_R (Canon) on Sep 15, 2014 at 06:33 UTC
    Symbolic calculation is not an easy task, but if you succeed, you'll have learnt a lot about programming. Another possible approach is an iterative numerical approximation. Just google "newton raphson" (for example) to get an idea on how it might work.