Math::N...ression(3)

๐Ÿ“ฆ
version 75 ยท 2 versions
, in fedora 43

โœจ NAME โœจ

Math::NumSeq::Expression โ€” mathematical expression values ๐Ÿ’…

(ngl bestie, this is a User Contributed Perl Documentation page, and the manual name is literally squished down to Math::N...ression because the column budget said no ๐Ÿ˜ญ)


๐ŸŒธ SYNOPSIS ๐ŸŒธ

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

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So the whole vibe is: it's a string expression, and it gets evaluated at i=0, 1, 2, etc โ€” by Perl or by whichever evaluator module you pick. โœจ

It's literally designed to take expression strings straight from user input, though you could also use it for something quick from program code. Just know that the expression syntax across the evaluator modules varies in subtle ways โ€” so like, pick one and stay loyal to it fr fr. ๐Ÿ™ˆ

๐Ÿช Perl

The default expression_evaluator => 'Perl' evaluates with Perl itself. This one is always available โ€” it's the ride-or-die default. Expressions are run with the Safe module to restrict to arithmetic (see Safe).

The i index lives in a $i variable and also in an i() function. And the i() function is prototyped like a constant, which is very iconic of it.

i+1
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   /

๐Ÿ”ฎ Math-Symbolic

expression_evaluator => 'MS' selects the Math::Symbolic module โ€” if it's available, bestie, that's the catch. ๐Ÿ’œ

The expression gets parsed with Math::Symbolic->parse_from_string() and should use a single variable for the i index in the sequence. And plot twist: the variable can be any name, not just i. ๐Ÿ‘‘

2*i+1
x^2 + x + 1           # any single variable

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

๐Ÿงฎ Math-Expression-Evaluator

expression_evaluator => 'MEE' selects the Math::Expression::Evaluator module โ€” again, if available. ๐Ÿซถ

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

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. โœจ

๐ŸŽ€ Language-Expr

expression_evaluator => 'LE' selects the Language::Expr module โ€” yep, if available. ๐ŸŒˆ

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

$x*$x + $x + 1

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


๐Ÿ’… FUNCTIONS ๐Ÿ’…

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. Brand new, fresh out the box. ๐ŸŽ

๐ŸŽฒ Random Access

  • $value = $seq->ith($i) โ€” Return the expression evaluated at $i. That's it, that's the whole job. ๐Ÿ’–

๐Ÿ’€ BUGS ๐Ÿ’€

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? Genuinely asking, bestie, nobody knows. ๐Ÿ˜ญ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

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


๐Ÿ  HOME PAGE ๐Ÿ 

<http://user42.tuxfamily.org/math-numseq/index.html>


๐Ÿ“œ LICENSE ๐Ÿ“œ

Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 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/>.


okay that's the whole page, you ate that ๐Ÿ’…โœจ this masterpiece is brought to you by perl v5.42.0, dated 2025-07-25 โ€” periodt, no cap, go forth and evaluate some expressions bestie ๐Ÿฆ‹๐Ÿ’–

๐Ÿ“ฆ where this page comes from