| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | # This file is part of Math-NumSeq. | ||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||
| 5 | # Math-NumSeq is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||
| 6 | # it under the terms of the GNU General Public License as published by the | ||||||||||||||||||||||||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||||||||||||||||||||||||
| 8 | # version. | ||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||
| 10 | # Math-NumSeq is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||||||||||||||||||||||||
| 13 | # for more details. | ||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||||
| 16 | # with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||
| 18 | package Math::NumSeq::Expression; | ||||||||||||||||||||||||
| 19 | 1 | ▶ | use 5.004; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 20 | 1 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 21 | 1 | ▶ | use Carp; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 22 | 1 | ▶ | use List::Util; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 23 | 1 | ▶ | use Math::Libm; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 24 | 1 | ▶ | use Module::Util; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||
| 26 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 27 | 1 | $VERSION = 75; | |||||||||||||||||||||||
| 28 | 1 | ▶ | use Math::NumSeq; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 29 | 1 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||
| 31 | # uncomment this to run the ### lines | ||||||||||||||||||||||||
| 32 | # use Smart::Comments; | ||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||
| 35 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 36 | 1 | my ($have_MS, $have_MEE, $have_LE, @evaluators, @evaluators_display); | |||||||||||||||||||||||
| 37 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=7 SCAR=22.1
| |||||||||||||||||||||||||
| 38 | 1 | $have_MS | |||||||||||||||||||||||
| 39 | = defined(Module::Util::find_installed('Math::Symbolic')); | ||||||||||||||||||||||||
| 40 | 1 | $have_MEE | |||||||||||||||||||||||
| 41 | = defined(Module::Util::find_installed('Math::Expression::Evaluator')); | ||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||
| 43 | # lower case Compiler::perl is the incompatible change in 0.24, required | ||||||||||||||||||||||||
| 44 | # by the code here | ||||||||||||||||||||||||
| 45 | 1 | $have_LE | |||||||||||||||||||||||
| 46 | = defined(Module::Util::find_installed('Language::Expr::Compiler::perl')); | ||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||
| 48 | ### $have_MS | ||||||||||||||||||||||||
| 49 | ### $have_MEE | ||||||||||||||||||||||||
| 50 | ### $have_LE | ||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | 1 | ▶ | @evaluators = ('Perl', | ||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||
| 53 | ($have_MS ? 'MS' : ()), | ||||||||||||||||||||||||
| 54 | ($have_MEE ? 'MEE' : ()), | ||||||||||||||||||||||||
| 55 | ($have_LE ? 'LE' : ())); | ||||||||||||||||||||||||
| 56 | 1 | ▶ | @evaluators_display = (Math::NumSeq::__('Perl'), | ||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||
| 57 | ($have_MS ? Math::NumSeq::__('MS') : ()), | ||||||||||||||||||||||||
| 58 | ($have_MEE ? Math::NumSeq::__('MEE') : ()), | ||||||||||||||||||||||||
| 59 | ($have_LE ? Math::NumSeq::__('LE') : ())); | ||||||||||||||||||||||||
| 60 | ### @evaluators | ||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||
| 63 | # use constant name => Math::NumSeq::__('Arbitrary Expression'); | ||||||||||||||||||||||||
| 64 | 1 | ▶ | use constant description => | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=15.2
Branch: 3 branches
| |||||||||||||||||||||||||
| 65 | join ("\n", | ||||||||||||||||||||||||
| 66 | Math::NumSeq::__('An arbitrary expression. It should be a function of \"i\" at 0,1,2, etc. For example (2*i)^2 would give the even perfect squares. | ||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||
| 68 | Syntax is per the chosen evaluator, an invalid expression displays an error message. | ||||||||||||||||||||||||
| 69 | Perl (the default) is either 2*i+1 or 2*$i+1.'), | ||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||
| 71 | ($have_MS ? | ||||||||||||||||||||||||
| 72 | Math::NumSeq::__('Math::Symbolic is like 2*i^2.') | ||||||||||||||||||||||||
| 73 | : ()), | ||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||
| 75 | ($have_MEE ? | ||||||||||||||||||||||||
| 76 | Math::NumSeq::__('Math::Expression::Evaluator is like t=2*i;t^2') | ||||||||||||||||||||||||
| 77 | : ()), | ||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | ($have_LE ? | ||||||||||||||||||||||||
| 80 | Math::NumSeq::__('Language::Expr is like $k**2 + $k - 1.') | ||||||||||||||||||||||||
| 81 | 1 | : ())); | |||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||
| 83 | 1 | ▶ | use constant i_start => 0; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 84 | 1 | ▶ | use constant parameter_info_array => | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 85 | [ | ||||||||||||||||||||||||
| 86 | { name => 'expression', | ||||||||||||||||||||||||
| 87 | display => Math::NumSeq::__('Expression'), | ||||||||||||||||||||||||
| 88 | type => 'string', | ||||||||||||||||||||||||
| 89 | default => '3*i*i + i + 2', | ||||||||||||||||||||||||
| 90 | width => 30, | ||||||||||||||||||||||||
| 91 | description => Math::NumSeq::__('A mathematical expression giving values to display, for example x^2+x+41. Only one variable is allowed, see the chosen evaluator Math::Symbolic or Math::Expression::Evaluator for possible operators and function.'), | ||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||
| 93 | { name => 'expression_evaluator', | ||||||||||||||||||||||||
| 94 | display => Math::NumSeq::__('Evaluator'), | ||||||||||||||||||||||||
| 95 | type => 'enum', | ||||||||||||||||||||||||
| 96 | default => $evaluators[0], | ||||||||||||||||||||||||
| 97 | choices => \@evaluators, | ||||||||||||||||||||||||
| 98 | choices_display => \@evaluators_display, | ||||||||||||||||||||||||
| 99 | description => Math::NumSeq::__('The expression evaluator module, Perl for Perl itself, MS for Math::Symbolic, MEE for Math::Expression::Evaluator, LE for Language::Expr.'), | ||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||
| 101 | 1 | ]; | |||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||
| 103 | ### parameter_info_array: parameter_info_array() | ||||||||||||||||||||||||
| 104 | ### parameter_info_hash: __PACKAGE__->parameter_info_hash | ||||||||||||||||||||||||
| 105 | ### evaluator default: __PACKAGE__->parameter_default('expression_evaluator') | ||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||
| 107 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||
| 108 | 1 | my %oeis_anum; | |||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||
| 110 | # some experimental A-number generators for easy expressions not with their | ||||||||||||||||||||||||
| 111 | # own module | ||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||
| 113 | # but A008865 starts from i=1 | ||||||||||||||||||||||||
| 114 | # $oeis_anum{'i*i-2'} = 'A008865'; | ||||||||||||||||||||||||
| 115 | # # OEIS-Catalogue: A008865 expression=i*i-2 | ||||||||||||||||||||||||
| 116 | # | ||||||||||||||||||||||||
| 117 | # A162395 start i=1 | ||||||||||||||||||||||||
| 118 | # $oeis_anum{'i*i*(-1)**(i+1)'} = 'A162395'; | ||||||||||||||||||||||||
| 119 | # # OEIS-Catalogue: A162395 expression=i*i*(-1)**(i+1) | ||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||
| 121 | 1 | $oeis_anum{'i*(i+2)'} = 'A005563'; | |||||||||||||||||||||||
| 122 | # OEIS-Catalogue: A005563 expression=i*(i+2) | ||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||
| 124 | 1 | $oeis_anum{'i*(4*i*i-1)/3'} = 'A000447'; # sum of odd squares | |||||||||||||||||||||||
| 125 | # OEIS-Catalogue: A000447 expression=i*(4*i*i-1)/3 | ||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||
| 127 | 1 | $oeis_anum{'(2*i)**3'} = 'A016743'; # even cubes (2i)^3 | |||||||||||||||||||||||
| 128 | # OEIS-Catalogue: A016743 expression=(2*i)**3 | ||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||
| 130 | # FIXME: should promote to bigint when necessary | ||||||||||||||||||||||||
| 131 | # cf A131577 zero and powers of 2 | ||||||||||||||||||||||||
| 132 | # A171449 powers of 2 with -1 instead of 1 | ||||||||||||||||||||||||
| 133 | 1 | $oeis_anum{'2**i'} = 'A000079'; # powers of 2 | |||||||||||||||||||||||
| 134 | 1 | $oeis_anum{'3**i'} = 'A000244'; # powers of 3 | |||||||||||||||||||||||
| 135 | 1 | $oeis_anum{'4**i'} = 'A000302'; # powers of 4 | |||||||||||||||||||||||
| 136 | 1 | $oeis_anum{'10**i'} = 'A011557'; # powers of 10 | |||||||||||||||||||||||
| 137 | # OEIS-Catalogue: A000079 expression=2**i | ||||||||||||||||||||||||
| 138 | # OEIS-Catalogue: A000244 expression=3**i | ||||||||||||||||||||||||
| 139 | # OEIS-Catalogue: A000302 expression=4**i | ||||||||||||||||||||||||
| 140 | # OEIS-Catalogue: A011557 expression=10**i | ||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||
| 142 | 0 | ▶ | sub oeis_anum { | ||||||||||||||||||||||
|
Subroutine
oeis_anum: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 143 | 0 | my ($self) = @_; | |||||||||||||||||||||||
| 144 | ### oeis_anum(): $self | ||||||||||||||||||||||||
| 145 | 0 | return $oeis_anum{$self->{'expression'}}; | |||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||
| 148 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||
| 154 | 1 | package Math::NumSeq::Expression::LanguageExpr; | |||||||||||||||||||||||
| 155 | 1 | ▶ | use List::Util 'min', 'max'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 156 | 1 | ▶ | use vars '$pi', '$e', '$phi', '$gam'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 157 | 1 | $pi = Math::Libm::M_PI(); | |||||||||||||||||||||||
| 158 | 1 | $e = Math::Libm::M_E(); | |||||||||||||||||||||||
| 159 | 1 | $phi = (1+sqrt(5))/2; | |||||||||||||||||||||||
| 160 | 1 | $gam = 0.5772156649015328606065120; | |||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||
| 163 | 5 | ▶ | sub new { | ||||||||||||||||||||||
|
Subroutine
new: called CC=22 SCAR=49.8
Pod
documented
| |||||||||||||||||||||||||
| 164 | 5 | my ($class, %options) = @_; | |||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||
| 166 | 5 | my $expression = $options{'expression'}; | |||||||||||||||||||||||
| 167 | 5 | ▶ | if (! defined $expression) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 168 | 0 | $expression = $class->parameter_default('expression'); | |||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||
| 171 | 5 | ▶ | my $evaluator = $options{'expression_evaluator'} | ||||||||||||||||||||||
|
Condition60%3/5
$options{'expression_evaluator'} || $class->parameter_default('expression_evaluator') || croak('No expression evaluator modules available')
$options{"expression_evaluator"} or $class->parameter_default("expression_evaluator")
MC/DC0%0/2
$options{'expression_evaluator'} || $class->parameter_default('expression_evaluator') || croak('No expression evaluator modules available')
$options{"expression_evaluator"}
$class->parameter_default("expression_evaluator")
Truth table66%2/3
A or B
A= $options{"expression_evaluator"}B= $class->parameter_default("expression_evaluator")
| |||||||||||||||||||||||||
| 172 | || $class->parameter_default('expression_evaluator') | ||||||||||||||||||||||||
| 173 | || croak "No expression evaluator modules available"; | ||||||||||||||||||||||||
| 174 | ### $evaluator | ||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||
| 176 | 5 | my $subr; | |||||||||||||||||||||||
| 177 | 5 | ▶ | if ($evaluator eq 'Perl') { | ||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||
| 179 | # Workaround: Something fishy in Safe 2.29 and perl 5.14.2 meant that | ||||||||||||||||||||||||
| 180 | # after a Safe->new(), any subsequently loaded code dragging in %- named | ||||||||||||||||||||||||
| 181 | # captures fails to load Tie::Hash::NamedCapture. Load it now, if it | ||||||||||||||||||||||||
| 182 | # exists. This affects Language::Expr which uses Regexp::Grammars which | ||||||||||||||||||||||||
| 183 | # has $-{'foo'}. | ||||||||||||||||||||||||
| 184 | # | ||||||||||||||||||||||||
| 185 | # Safe 2.30 has it fixed, so can skip there, unless or until want to | ||||||||||||||||||||||||
| 186 | # depend outright on that version | ||||||||||||||||||||||||
| 187 | # http://perl5.git.perl.org/perl.git/commitdiff/ad084f51cd17539ef55b510228156cd4f83c9729 | ||||||||||||||||||||||||
| 188 | # | ||||||||||||||||||||||||
| 189 | 4 | eval { Safe->VERSION(2.30); 1 } | |||||||||||||||||||||||
| 190 | 4 | ▶ | or eval { require Tie::Hash::NamedCapture }; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||
| 192 | 4 | require Safe; | |||||||||||||||||||||||
| 193 | 4 | my $safe = Safe->new; | |||||||||||||||||||||||
| 194 | 4 | $safe->permit('print', | |||||||||||||||||||||||
| 195 | ':base_math', # sqrt(), rand(), etc | ||||||||||||||||||||||||
| 196 | ); | ||||||||||||||||||||||||
| 197 | 4 | ▶ | if (eval { require List::Util; 1 }) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 198 | 4 | $safe->share_from('List::Util', [ 'min','max' ]); | |||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||
| 200 | 4 | require POSIX; | |||||||||||||||||||||||
| 201 | 4 | $safe->share_from('POSIX', [ 'floor','ceil' ]); | |||||||||||||||||||||||
| 202 | 4 | require Math::Trig; | |||||||||||||||||||||||
| 203 | 4 | $safe->share_from('Math::Trig', [qw(tan | |||||||||||||||||||||||
| 204 | asin acos atan | ||||||||||||||||||||||||
| 205 | csc cosec sec cot cotan | ||||||||||||||||||||||||
| 206 | acsc acosec asec acot acotan | ||||||||||||||||||||||||
| 207 | sinh cosh tanh | ||||||||||||||||||||||||
| 208 | csch cosech sech coth cotanh | ||||||||||||||||||||||||
| 209 | asinh acosh atanh | ||||||||||||||||||||||||
| 210 | acsch acosech asech acoth acotanh | ||||||||||||||||||||||||
| 211 | )]); | ||||||||||||||||||||||||
| 212 | 4 | require Math::Libm; | |||||||||||||||||||||||
| 213 | 4 | $safe->share_from('Math::Libm', [qw(cbrt | |||||||||||||||||||||||
| 214 | erf | ||||||||||||||||||||||||
| 215 | erfc | ||||||||||||||||||||||||
| 216 | expm1 | ||||||||||||||||||||||||
| 217 | hypot | ||||||||||||||||||||||||
| 218 | j0 | ||||||||||||||||||||||||
| 219 | j1 | ||||||||||||||||||||||||
| 220 | jn | ||||||||||||||||||||||||
| 221 | lgamma_r | ||||||||||||||||||||||||
| 222 | log10 | ||||||||||||||||||||||||
| 223 | log1p | ||||||||||||||||||||||||
| 224 | pow | ||||||||||||||||||||||||
| 225 | rint | ||||||||||||||||||||||||
| 226 | y0 | ||||||||||||||||||||||||
| 227 | y1 | ||||||||||||||||||||||||
| 228 | yn)]); | ||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||
| 230 | 4 | my $pi = Math::Libm::M_PI(); | |||||||||||||||||||||||
| 231 | 4 | my $e = Math::Libm::M_E(); | |||||||||||||||||||||||
| 232 | 4 | $subr = $safe->reval("\n#line ".(__LINE__+2)." \"".__FILE__."\"\n" | |||||||||||||||||||||||
| 233 | . <<"HERE"); | ||||||||||||||||||||||||
| 234 | my \$pi = $pi; | ||||||||||||||||||||||||
| 235 | my \$e = $e; | ||||||||||||||||||||||||
| 236 | my \$phi = (1+sqrt(5))/2; | ||||||||||||||||||||||||
| 237 | my \$gam = 0.5772156649015328606065120; | ||||||||||||||||||||||||
| 238 | my \$i; | ||||||||||||||||||||||||
| 239 | 3 | ▶ | sub i () { return \$i } | ||||||||||||||||||||||
|
Subroutine
i: called CC=1 SCAR=0.0
Subroutine
i: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 240 | sub { | ||||||||||||||||||||||||
| 241 | \$i = \$_[0]; | ||||||||||||||||||||||||
| 242 | return do { $expression } | ||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||
| 244 | HERE | ||||||||||||||||||||||||
| 245 | ### $subr | ||||||||||||||||||||||||
| 246 | 4 | ▶ | if (! $subr) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 247 | 0 | croak "Invalid or unsafe expression: $@\n"; | |||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||
| 250 | } elsif ($evaluator eq 'MS') { | ||||||||||||||||||||||||
| 251 | 0 | require Math::Symbolic; | |||||||||||||||||||||||
| 252 | 0 | my $tree = Math::Symbolic->parse_from_string($expression); | |||||||||||||||||||||||
| 253 | 0 | ▶ | if (! defined $tree) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 254 | 0 | croak "Cannot parse MS expression: $expression"; | |||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||
| 257 | # simplify wrong result on x+(-5)*y before 0.605 ... | ||||||||||||||||||||||||
| 258 | 0 | ▶ | if (eval { $tree->VERSION(0.605); 1 }) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 259 | 0 | $tree = $tree->simplify; | |||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||
| 262 | 0 | my @vars = $tree->signature; | |||||||||||||||||||||||
| 263 | 0 | ▶ | if (@vars > 1) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 264 | 0 | croak "More than one variable in MS expression: $expression\n(simplified to $tree)"; | |||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||
| 266 | ### code: $tree->to_code | ||||||||||||||||||||||||
| 267 | 0 | ($subr) = $tree->to_sub(\@vars); | |||||||||||||||||||||||
| 268 | ### $subr | ||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||
| 270 | } elsif ($evaluator eq 'MEE') { | ||||||||||||||||||||||||
| 271 | 0 | require Math::Expression::Evaluator; | |||||||||||||||||||||||
| 272 | 0 | my $me = Math::Expression::Evaluator->new; | |||||||||||||||||||||||
| 273 | 0 | $me->set_function('min', \&List::Util::min); | |||||||||||||||||||||||
| 274 | 0 | $me->set_function('max', \&List::Util::max); | |||||||||||||||||||||||
| 275 | 0 | $me->parse('pi='.Math::Libm::M_PI() | |||||||||||||||||||||||
| 276 | .'; e='.Math::Libm::M_E() | ||||||||||||||||||||||||
| 277 | .'; phi=(1+sqrt(5))/2' | ||||||||||||||||||||||||
| 278 | .'; gam=0.5772156649015328606065120'); | ||||||||||||||||||||||||
| 279 | 0 | $me->val; | |||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||
| 281 | 0 | ▶ | eval { $me->parse ($expression); 1 } | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 282 | or croak "Cannot parse MEE expression: $expression\n$@"; | ||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||
| 284 | # my @vars = $me->variables; | ||||||||||||||||||||||||
| 285 | 0 | my @vars = _me_free_variables($me); | |||||||||||||||||||||||
| 286 | 0 | ▶ | if (@vars > 1) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 287 | 0 | croak "More than one variable in MEE expression: $expression"; | |||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||
| 290 | 0 | my $hashsub = $me->compiled; | |||||||||||||||||||||||
| 291 | ### $hashsub | ||||||||||||||||||||||||
| 292 | ### _ast_to_perl: $me->_ast_to_perl($me->{ast}) | ||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||
| 294 | 0 | my $v = $vars[0]; | |||||||||||||||||||||||
| 295 | 0 | my %vars; | |||||||||||||||||||||||
| 296 | 0 | ▶ | if (@vars) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 297 | 0 | ▶ | $subr = sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 298 | 0 | $vars{$v} = $_[0]; | |||||||||||||||||||||||
| 299 | 0 | return &$hashsub(\%vars); | |||||||||||||||||||||||
| 300 | 0 | }; | |||||||||||||||||||||||
| 301 | } else { | ||||||||||||||||||||||||
| 302 | ### no variables in expression ... | ||||||||||||||||||||||||
| 303 | 0 | ▶ | $subr = sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 304 | 0 | return &$hashsub(\%vars); | |||||||||||||||||||||||
| 305 | 0 | }; | |||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||
| 308 | } elsif ($evaluator eq 'LE') { | ||||||||||||||||||||||||
| 309 | 1 | require Language::Expr; | |||||||||||||||||||||||
| 310 | 0 | my $le = Language::Expr->new; | |||||||||||||||||||||||
| 311 | 0 | my $var_enumer = $le->get_interpreter('var_enumer'); | |||||||||||||||||||||||
| 312 | 0 | my $varef; | |||||||||||||||||||||||
| 313 | 0 | ▶ | eval { $varef = $var_enumer->eval ($expression); 1 } | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 314 | or croak "Cannot parse LE expression: $expression\n$@"; | ||||||||||||||||||||||||
| 315 | ### $varef | ||||||||||||||||||||||||
| 316 | my @vars = grep { # only vars, not functions as such | ||||||||||||||||||||||||
| 317 | 0 | do { | |||||||||||||||||||||||
| 318 | 1 | ▶ | no strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 319 | 0 | ! defined ${"Math::NumSeq::Expression::LanguageExpr::$_"} | |||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||
| 321 | } @$varef; | ||||||||||||||||||||||||
| 322 | 0 | ▶ | if (@vars > 1) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 323 | 0 | croak "More than one variable in LE expression: $expression"; | |||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||
| 326 | 0 | my $pc = $le->get_compiler('perl'); | |||||||||||||||||||||||
| 327 | 0 | my $perlstr; | |||||||||||||||||||||||
| 328 | 0 | ▶ | eval { $perlstr = $pc->compile ($expression); 1 } | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 329 | or croak "Cannot parse LE expression: $expression\n$@"; | ||||||||||||||||||||||||
| 330 | ### $perlstr | ||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||
| 332 | 0 | ▶ | my $v = $vars[0] || 'i'; | ||||||||||||||||||||||
|
Condition0%0/2
$vars[0] || 'i'
MC/DC0%0/1
$vars[0] || 'i'
$vars[0]
Truth table0%0/2
A
A= $vars[0]
| |||||||||||||||||||||||||
| 333 | ### $v | ||||||||||||||||||||||||
| 334 | ### eval: "sub { my \$$v = \$_[0]; $perlstr }" | ||||||||||||||||||||||||
| 335 | 0 | ▶ | $subr = eval "package Math::NumSeq::Expression::LanguageExpr; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 336 | use strict; | ||||||||||||||||||||||||
| 337 | sub { my \$$v = \$_[0]; $perlstr }" | ||||||||||||||||||||||||
| 338 | or croak "Cannot compile $expression\n$perlstr\n$@"; | ||||||||||||||||||||||||
| 339 | ### $subr | ||||||||||||||||||||||||
| 340 | ### at zero: $subr->(0) | ||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||
| 347 | # require Language::Expr; | ||||||||||||||||||||||||
| 348 | # my $le = Language::Expr->new; | ||||||||||||||||||||||||
| 349 | # my $varef; | ||||||||||||||||||||||||
| 350 | # eval { $varef = $le->enum_vars ($expression); 1 } | ||||||||||||||||||||||||
| 351 | # or croak "Cannot parse LE expression: $expression\n$@"; | ||||||||||||||||||||||||
| 352 | # ### $varef | ||||||||||||||||||||||||
| 353 | # my @vars = grep { # only vars, not functions as such | ||||||||||||||||||||||||
| 354 | # do { | ||||||||||||||||||||||||
| 355 | # no strict; | ||||||||||||||||||||||||
| 356 | # ! defined ${"Math::NumSeq::Expression::LanguageExpr::$_"} | ||||||||||||||||||||||||
| 357 | # } | ||||||||||||||||||||||||
| 358 | # } @$varef; | ||||||||||||||||||||||||
| 359 | # if (@vars > 1) { | ||||||||||||||||||||||||
| 360 | # croak "More than one variable in LE expression: $expression"; | ||||||||||||||||||||||||
| 361 | # } | ||||||||||||||||||||||||
| 362 | # | ||||||||||||||||||||||||
| 363 | # require Language::Expr::Compiler::Perl; | ||||||||||||||||||||||||
| 364 | # my $pe = Language::Expr::Compiler::Perl->new; | ||||||||||||||||||||||||
| 365 | # my $perlstr; | ||||||||||||||||||||||||
| 366 | # eval { $perlstr = $pe->perl ($expression); 1 } | ||||||||||||||||||||||||
| 367 | # or croak "Cannot parse LE expression: $expression\n$@"; | ||||||||||||||||||||||||
| 368 | # | ||||||||||||||||||||||||
| 369 | # my $v = $vars[0] || 'i'; | ||||||||||||||||||||||||
| 370 | # ### $v | ||||||||||||||||||||||||
| 371 | # ### eval: "sub { my \$$v = \$_[0]; $perlstr }" | ||||||||||||||||||||||||
| 372 | # $subr = eval "package Math::NumSeq::Expression::LanguageExpr; | ||||||||||||||||||||||||
| 373 | # use strict; | ||||||||||||||||||||||||
| 374 | # sub { my \$$v = \$_[0]; $perlstr }" | ||||||||||||||||||||||||
| 375 | # or croak "Cannot compile $expression\n$perlstr\n$@"; | ||||||||||||||||||||||||
| 376 | # ### $subr | ||||||||||||||||||||||||
| 377 | # ### at zero: $subr->(0) | ||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||
| 379 | } else { | ||||||||||||||||||||||||
| 380 | 0 | croak "Unknown evaluator: $evaluator"; | |||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||
| 383 | 4 | my $self = bless { | |||||||||||||||||||||||
| 384 | # hi => $options{'hi'}, | ||||||||||||||||||||||||
| 385 | subr => $subr, | ||||||||||||||||||||||||
| 386 | expression => $expression, # for oeis_anum() and dumps | ||||||||||||||||||||||||
| 387 | }, $class; | ||||||||||||||||||||||||
| 388 | 4 | $self->rewind; | |||||||||||||||||||||||
| 389 | 4 | return $self; | |||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||
| 392 | 4 | ▶ | sub rewind { | ||||||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 393 | 4 | my ($self) = @_; | |||||||||||||||||||||||
| 394 | 4 | $self->{'i'} = $self->i_start; | |||||||||||||||||||||||
| 395 | 4 | $self->{'above'} = 0; | |||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||
| 398 | 10 | ▶ | sub next { | ||||||||||||||||||||||
|
Subroutine
next: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||
| 399 | 10 | my ($self) = @_; | |||||||||||||||||||||||
| 400 | 10 | my $i = $self->{'i'}++; | |||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||
| 402 | 10 | for (;;) { | |||||||||||||||||||||||
| 403 | 10 | ▶ | if ($self->{'above'} >= 10) { # || $i > $self->{'hi'} | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 404 | 0 | return; | |||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||
| 406 | 10 | my $n = eval { $self->{'subr'}->($i) }; | |||||||||||||||||||||||
| 407 | 10 | ▶ | if (! defined $n) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 408 | # eg. division by zero | ||||||||||||||||||||||||
| 409 | ### expression undef: $@ | ||||||||||||||||||||||||
| 410 | 0 | $self->{'above'}++; | |||||||||||||||||||||||
| 411 | 0 | next; | |||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||
| 413 | ### expression result: $n | ||||||||||||||||||||||||
| 414 | # if ($n > $self->{'hi'}) { | ||||||||||||||||||||||||
| 415 | # $self->{'above'}++; | ||||||||||||||||||||||||
| 416 | # } | ||||||||||||||||||||||||
| 417 | 10 | return ($i, $n); | |||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||
| 421 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||
| 422 | # Math::Expression::Evaluator helpers | ||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||
| 424 | # $me is a Math::Expression::Evaluator | ||||||||||||||||||||||||
| 425 | # return a list of the free variables in it | ||||||||||||||||||||||||
| 426 | 0 | ▶ | sub _me_free_variables { | ||||||||||||||||||||||
|
Subroutine
_me_free_variables: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||
| 427 | 0 | my ($me) = @_; | |||||||||||||||||||||||
| 428 | 0 | my %assigned = %{$me->{'variables'}}; | |||||||||||||||||||||||
| 429 | 0 | my %free; | |||||||||||||||||||||||
| 430 | 0 | my @pending = ($me->{'ast'}); | |||||||||||||||||||||||
| 431 | 0 | ▶ | while (@pending) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 432 | 0 | my $node = shift @pending; | |||||||||||||||||||||||
| 433 | 0 | ▶ | ref $node or next; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 434 | # ### $node | ||||||||||||||||||||||||
| 435 | 0 | push @pending, @$node[1..$#$node]; | |||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||
| 437 | 0 | ▶ | if ($node->[0] eq '$') { | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 438 | 0 | my $varname = $node->[1]; | |||||||||||||||||||||||
| 439 | 0 | ▶ | if (! $assigned{$varname}) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 440 | ### free: $varname | ||||||||||||||||||||||||
| 441 | 0 | $free{$varname} = 1; | |||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||
| 443 | } elsif ($node->[0] eq '=') { | ||||||||||||||||||||||||
| 444 | 0 | my $vnode = $node->[1]; | |||||||||||||||||||||||
| 445 | 0 | ▶ | if ($vnode->[0] eq '$') { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 446 | ### assigned: $vnode->[1] | ||||||||||||||||||||||||
| 447 | 0 | $assigned{$vnode->[1]} = 1; | |||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||
| 451 | 0 | return keys %free; | |||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||
| 455 | 1 | 1; | |||||||||||||||||||||||
| 456 | __END__ | ||||||||||||||||||||||||