++ed by:

2 PAUSE users

Kevin Ryde


Math::NumSeq::Expression -- mathematical expression values


 use Math::NumSeq::Expression;
 my $seq = Math::NumSeq::Expression->new (expression => '2*i+1');
 my ($i, $value) = $seq->next;


A string expression evaluated at i=0, 1, 2, etc, by Perl or a choice of evaluator modules.

This is designed to take expression strings from user input though could be used for something quick from program code too. The expression syntax in the evaluator modules varies in subtle ways.


The default expression_evaluator => 'Perl' evaluates with Perl itself. This is always available. Expressions are run with the Safe module to restrict to arithmetic (see Safe).

The i index is in a $i variable and an i() function. The i() function is prototyped like a constant.

    2*$i - 2

The functions made available include

    atan2 sin cos exp log                 \    Perl builtins
      sqrt rand                           /
    min max                               List::Util
    floor ceil                            POSIX module
    cbrt hypot erf erfc expm1             \
      j0 j1 jn lgamma_r log10              |  Math::Libm
      log1p pow rint y0 y1 yn             /
    tan asin acos atan                    \
      csc cosec sec cot cotan              |  Math::Trig
      acsc acosec asec acot acotan         |
      sinh cosh tanh                       |
      csch cosech sech coth cotanh         |
      asinh acosh atanh                    |
      acsch acosech asech acoth acotanh   /


expression_evaluator => 'MS' selects the Math::Symbolic module, if available.

The expression is parsed with Math::Symbolic->parse_from_string() and should use a single variable for the i index in the sequence. The variable can be any name, not just "i"

    x^2 + x + 1           # any single variable

The usual $ms->simplify() is applied to perhaps reduce the expression a bit, then to_sub() for actual evaluation.


expression_evaluator => 'MEE' selects the Math::Expression::Evaluator module, if available.

The expression should use a single input variable, which can be any name, and takes the i index in the sequence. Temporary variables can be used by assigning to them,

    x^2 + x + 1      # any single variable
    t=2*i; t^2       # temporary variables assigned

The expression is run with $mee->compiled(). It turns the expression into a Perl subr for actual evaluation.


expression_evaluator => 'LE' selects the Language::Expr module, if available.

The expression should use a single variable, of any name, which will be the i index in the sequence. See Language::Expr::Manual::Syntax for the expression syntax.

    $x*$x + $x + 1

The expression is compiled with Language::Expr::Compiler::perl for evaluation.


See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.

$seq = Math::NumSeq::Expression->new (expression => $str)

Create and return a new sequence object.

Random Access

$value = $seq->ith($i)

Return the expression evaluated at $i.


Safe.pm seems a bit of a slowdown. Is that right or is it supposed to validate ops during the eval which compiles a subr?


Math::NumSeq, Safe Math::Symbolic, Math::Expression::Evaluator, Language::Expr




Copyright 2010, 2011, 2012, 2013, 2014, 2016 Kevin Ryde

Math-NumSeq is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Math-NumSeq is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.