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

Is there any way to answer simple math equations? I looked around for modules, but I didn't find any. Let me define simple equation like this:

- only 1 variable

- solutions would only be 2 decimal places

- no cos or sin

An example equation would be 2x(-5*10x) = 100

If there isn't a module made for this, could someone point me in the correct direction on how to make one?

Replies are listed 'Best First'.
Re: Solving Math Equations
by artist (Parson) on Jan 28, 2003 at 03:43 UTC
    Hi
    You may have to reformat your equation:

    From the document..

    # Find the roots of ax + b my @x1 = linear_roots($a, $b); # Find the roots of ax**2 + bx +c my @x2 = quadratic_roots($a, $b, $c);

    Artist

Re: Solving Math Equations
by gjb (Vicar) on Jan 28, 2003 at 08:23 UTC

    As an alternative to what has already been mentioned above, Math::GSL is also an option. It's a Perl API for calling the Gnu Scientific Library, an effort to provide a number of good-quality algorithms for numerical applications. Currently the Perl interface is far from complete in the sense that only a fraction of the GSL is accessible from Perl, but a polynomial solver happens to be available.

    Hope this helps, -gjb-

      Of course, some enterprising young programmer could (and should!) extend the interface and contribute back to the community. (Hint, hint!)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Solving Math Equations
by toma (Vicar) on Jan 28, 2003 at 07:12 UTC
    It would be greate to write a module to encapsulate maxima, which is the old DOE MACSYMA. See the most excellent Maxima home page. It is a great program and runs on many platforms. It is maintained by William F. Schelter, and has source code, a nice install for windows, and rpms for linux. I have had success with all of these.

    Your sample equation is nifty since the solution is a complex number. Maxima program:

    solve([2*x*(-5*10*x)=100],[x])
    yields:
    [x = - %I, x = %I]
    Where %I is maxima's notation for sqrt(-1) .

    Maxima is massive overkill for your requirements, since it can also solve nasty problems such as the indefinite integral:

    integrate(sin(x)*exp(x^2),x);
    or a relatively difficult problem in two variables:
    solve([2*x^2+14*y+y^2=5,x*y+14*x+x^2=0],[x,y])

    Even though it is overkill, it is a good open-source problem-solver for easy algebra problems.

    I have used Maxima for many real-world problems and also for teaching algebra. It is much better at algebra than I am, so I use it because I don't have the answer key for my textbook!

    It should work perfectly the first time! - toma