Math-Expression-Evaluator

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

          my $m = Math::Expression::Evaluator->new();
          $m->parse('1 + a');
          my $callback = sub { ord($_[0]) };
          $m->set_var_callback($callback);
          print $m->val();    # calls $callback, which returns 97
                              # so $m->val() return 98

      The callback will be called every time the variable is accessed, so if
      it requires expensive calculations, you are encouraged to cache it
      either yourself our automatically with Memoize.

    set_function
      Allows to add a user-defined function, or to override a built-in
      function.

          my $m = Math::Expression::Evaluator->new();
          $m->set_function('abs', sub { abs($_[0]) });
          $m->parse('abs(10.6)');
          print $m->val();

      If you first compile the expression to a perl closure and then call
      "<$m-"set_function>> again, the compiled function stays unaffected, so

          $m->set_function('f', sub { 42 });
          my $compiled = $m->parse('f')->compiled;
          $m->set_function('f', sub { -23 });
          print $compiled->();

      print out 42, not -23.

    ast_size
      "ast_size" returns an integer which gives a crude measure of the
      logical size of the expression. Note that this value isn't guarantueed
      to be stable across multiple versions of this module. It is mainly
      intended for testing.

SPEED
    MEE isn't as fast as perl, because it is built on top of perl.

    If you execute an expression multiple times, it pays off to either
    optimize it first, or (even better) compile it to a pure perl function.

                       Rate  no_optimize     optimize opt_compiled     compiled
        no_optimize  83.9/s           --         -44%         -82%         -83%
        optimize      150/s          78%           --         -68%         -69%
        opt_compiled  472/s         463%         215%           --          -4%
        compiled      490/s         485%         227%           4%           --

    This shows the time for 200 evaluations of "2+a+5+(3+4)" (with MEE
    0.0.5). As you can see, the non-optimized version is painfully slow,
    optimization nearly doubles the execution speed. The compiled and the
    optimized-and-then-compiled versions are both much faster.

    With this example expression the optimization prior to compilation pays
    off if you evaluate it more than 1000 times. But even if you call it
    "10**5" times the optimized and compiled version is only 3% faster than
    the directly compiled one (mostly due to perl's overhead for method
    calls).

    So to summarize you should compile your expresions, and if you have
    really many iterations it might pay off to optimize it first (or to
    write your program in C instead ;-).

BUGS AND LIMITATIONS
    *   Modulo operator produces an unnecessary big AST, making it
        relatively slow

INTERNALS
    The AST can be accessed as "$obj-"{ast}>. Its structure is described in
    Math::Expression::Evaluator::Parser (or you can use Data::Dumper to
    figure it out for yourself). Note that the exact form of the AST is
    considered to be an implementation detail, and subject to change.

SEE ALSO
    Math::Expression also evaluates mathematical expressions, but also
    handles string operations.

    If you want to do symbolic (aka algebraic) transformations,
    Math::Symbolic will fit your needs.

LICENSE
    This module is free software. You may use, redistribute and modify it
    under the same terms as perl itself.

COPYRIGHT
    Copyright (C) 2007 - 2009 Moritz Lenz, <http://perlgeek.de/>,
    moritz@faui2k3.org

DEVELOPMENT
    You can obtain the latest development version from github
    <http://github.com/moritz/math-expression-evaluator>.

        git clone git://github.com/moritz/math-expression-evaluator.git

    If you want to contribute something to this module, please ask me for a
    commit bit to the github repository, I'm giving them out freely.

ACKNOWLEDGEMENTS
    The following people have contributed to this module, in no particular
    order:

    Leonardo Herrera
        Initial patch for "set_function"

    Tina Müller
        Helpful feedback



( run in 0.996 second using v1.01-cache-2.11-cpan-71847e10f99 )