Math-Expression-Evaluator

 view release on metacpan or  search on metacpan

lib/Math/Expression/Evaluator.pm  view on Meta::CPAN

    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 L<Memoize>.

=item 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
C<<$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 C<42>, not C<-23>.

=item ast_size

C<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.

=back

=head1 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 C<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 C<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 ;-).

=head1 BUGS AND LIMITATIONS

=over 4

=item *

Modulo operator produces an unnecessary big AST, making it relatively slow

=back

=head1 INTERNALS

The AST can be accessed as C<$obj->{ast}>. Its structure is described in 
L<Math::Expression::Evaluator::Parser> (or you can use L<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.

=head1 SEE ALSO

L<Math::Expression> also evaluates mathematical expressions, but also handles
string operations.

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

=head1 LICENSE

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

=head1 COPYRIGHT

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

=head1 DEVELOPMENT

You can obtain the latest development version from github
L<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.

=head1 ACKNOWLEDGEMENTS

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

=over

=item Leonardo Herrera

Initial patch for C<set_function>

=item Tina Müller

Helpful feedback



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