Symbolic/Operator.pm

filter: Statementstmt 91.5% 259 / 283 Branchbran 79.5% 156 / 196 Conditioncond 79.7% 67 / 84 MC/DCmcdc 56.5% 26 / 46 Subroutinesub 100% 23 / 23 Podpod 100% 12 / 12 totaltotal 84.3% 543 / 644 CC 138 SCAR 46.5
CC
138
Cov
86%
simplify
167.8
new
12.0
_to_string_infix
11.0
CRAP 104.3
« Symbolic/Parser/Precompiled.pm ↑ Summary Symbolic/Custom/DefaultTests.pm »
1  
2 =encoding utf8
3  
4 =head1 NAME
5  
6 Math::Symbolic::Operator - Operators in symbolic calculations
7  
8 =head1 SYNOPSIS
9  
10 use Math::Symbolic::Operator;
11
12 my $sum = Math::Symbolic::Operator->new('+', $term1, $term2);
13
14 # or:
15 my $division =
16 Math::Symbolic::Operator->new(
17 {
18 type => B_DIVISON,
19 operands => [$term1, $term2],
20 }
21 );
22
23 my $derivative =
24 Math::Symbolic::Operator->new(
25 {
26 type => U_P_DERIVATIVE,
27 operands => [$term],
28 }
29 );
30  
31 =head1 DESCRIPTION
32  
33 This module implements all Math::Symbolic::Operator objects.
34 These objects are overloaded in stringification-context to call the
35 to_string() method on the object. In numeric and boolean context, they
36 evaluate to their numerical representation.
37  
38 For a list of supported operators, please refer to the list found below, in the
39 documentation for the new() constructor.
40  
41 Math::Symbolic::Operator inherits from Math::Symbolic::Base.
42  
43 =head2 EXPORT
44  
45 None.
46  
47 =cut
48  
49 package Math::Symbolic::Operator;
50  
51 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 23 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 23 ▶use Math::Symbolic::ExportConstants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 23 ▶use Math::Symbolic::Derivative qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 23 ▶use base 'Math::Symbolic::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 23 our $VERSION = '0.613';
64  
65 =head1 CLASS DATA
66  
67 Math::Symbolic::Operator contains several class data structures. Usually, you
68 should not worry about dealing with any of them because they are mostly an
69 implementation detail, but for the sake of completeness, here's the gist, but
70 feel free to skip this section of the docs:
71  
72 One of these is the %Op_Symbols hash that associates operator (and function)
73 symbols with the corresponding constant as exported by Math::Symbolic or
74 Math::Symbolic::ExportConstants. (For example, '+' => B_SUM which in turn is
75 0, if I recall correctly. But I didn't tell you that. Because you're supposed
76 to use the supplied (inlined and hence fast) constants so I can change their
77 internal order if I deem it necessary.)
78  
79 =cut
80  
81 23 our %Op_Symbols = (
82 '+' => B_SUM,
83 '-' => B_DIFFERENCE,
84 '*' => B_PRODUCT,
85 '/' => B_DIVISION,
86 'log' => B_LOG,
87 '^' => B_EXP,
88 'neg' => U_MINUS,
89 'partial_derivative' => U_P_DERIVATIVE,
90 'total_derivative' => U_T_DERIVATIVE,
91 'sin' => U_SINE,
92 'cos' => U_COSINE,
93 'tan' => U_TANGENT,
94 'cot' => U_COTANGENT,
95 'asin' => U_ARCSINE,
96 'acos' => U_ARCCOSINE,
97 'atan' => U_ARCTANGENT,
98 'acot' => U_ARCCOTANGENT,
99 'sinh' => U_SINE_H,
100 'cosh' => U_COSINE_H,
101 'asinh' => U_AREASINE_H,
102 'acosh' => U_AREACOSINE_H,
103 'atan2' => B_ARCTANGENT_TWO,
104 );
105  
106 =pod
107  
108 The array @Op_Types associates operator indices (recall those nifty constants?)
109 with anonymous hash datastructures that contain some info on the operator such
110 as its arity, the rule used to derive it, its infix string, its prefix string,
111 and information on how to actually apply it to numbers.
112  
113 =cut
114  
115 23 our @Op_Types = (
116  
117 # B_SUM
118 {
119 arity => 2,
120 derive => 'each operand',
121 infix_string => '+',
122 prefix_string => 'add',
123 application => '$_[0] + $_[1]',
124 commutative => 1,
125 },
126  
127 # B_DIFFERENCE
128 {
129 arity => 2,
130 derive => 'each operand',
131 infix_string => '-',
132 prefix_string => 'subtract',
133 application => '$_[0] - $_[1]',
134 #commutative => 0,
135 },
136  
137 # B_PRODUCT
138 {
139 arity => 2,
140 derive => 'product rule',
141 infix_string => '*',
142 prefix_string => 'multiply',
143 application => '$_[0] * $_[1]',
144 commutative => 1,
145 },
146  
147 # B_DIVISION
148 {
149 derive => 'quotient rule',
150 arity => 2,
151 infix_string => '/',
152 prefix_string => 'divide',
153 application => '$_[0] / $_[1]',
154 #commutative => 0,
155 },
156  
157 # U_MINUS
158 {
159 arity => 1,
160 derive => 'each operand',
161 infix_string => '-',
162 prefix_string => 'negate',
163 application => '-$_[0]',
164 },
165  
166 # U_P_DERIVATIVE
167 {
168 arity => 2,
169 derive => 'derivative commutation',
170 infix_string => undef,
171 prefix_string => 'partial_derivative',
172 application => \&Math::Symbolic::Derivative::partial_derivative,
173 },
174  
175 # U_T_DERIVATIVE
176 {
177 arity => 2,
178 derive => 'derivative commutation',
179 infix_string => undef,
180 prefix_string => 'total_derivative',
181 application => \&Math::Symbolic::Derivative::total_derivative,
182 },
183  
184 # B_EXP
185 {
186 arity => 2,
187 derive => 'logarithmic chain rule after ln',
188 infix_string => '^',
189 prefix_string => 'exponentiate',
190 application => '$_[0] ** $_[1]',
191 #commutative => 0,
192 },
193  
194 # B_LOG
195 {
196 arity => 2,
197 derive => 'logarithmic chain rule',
198 infix_string => undef,
199 prefix_string => 'log',
200 application => 'log($_[1]) / log($_[0])',
201 #commutative => 0,
202 },
203  
204 # U_SINE
205 {
206 arity => 1,
207 derive => 'trigonometric derivatives',
208 infix_string => undef,
209 prefix_string => 'sin',
210 application => 'sin($_[0])',
211 },
212  
213 # U_COSINE
214 {
215 arity => 1,
216 derive => 'trigonometric derivatives',
217 infix_string => undef,
218 prefix_string => 'cos',
219 application => 'cos($_[0])',
220 },
221  
222 # U_TANGENT
223 {
224 arity => 1,
225 derive => 'trigonometric derivatives',
226 infix_string => undef,
227 prefix_string => 'tan',
228 application => 'sin($_[0])/cos($_[0])',
229 },
230  
231 # U_COTANGENT
232 {
233 arity => 1,
234 derive => 'trigonometric derivatives',
235 infix_string => undef,
236 prefix_string => 'cot',
237 application => 'cos($_[0])/sin($_[0])',
238 },
239  
240 # U_ARCSINE
241 {
242 arity => 1,
243 derive => 'inverse trigonometric derivatives',
244 infix_string => undef,
245 prefix_string => 'asin',
246 #application => 'Math::Symbolic::AuxFunctions::asin($_[0])',
247 application => 'atan2( $_[0], sqrt( 1 - $_[0] * $_[0] ) )',
248 },
249  
250 # U_ARCCOSINE
251 {
252 arity => 1,
253 derive => 'inverse trigonometric derivatives',
254 infix_string => undef,
255 prefix_string => 'acos',
256 application => 'atan2( sqrt( 1 - $_[0] * $_[0] ), $_[0] ) ',
257 #application => 'Math::Symbolic::AuxFunctions::acos($_[0])',
258 },
259  
260 # U_ARCTANGENT
261 {
262 arity => 1,
263 derive => 'inverse trigonometric derivatives',
264 infix_string => undef,
265 prefix_string => 'atan',
266 application => 'atan2($_[0], 1)',
267 #application => 'Math::Symbolic::AuxFunctions::atan($_[0])',
268 },
269  
270 # U_ARCCOTANGENT
271 {
272 arity => 1,
273 derive => 'inverse trigonometric derivatives',
274 infix_string => undef,
275 prefix_string => 'acot',
276 application => 'atan2(1 / $_[0], 1)',
277 #application => 'Math::Symbolic::AuxFunctions::acot($_[0])',
278 },
279  
280 # U_SINE_H
281 {
282 arity => 1,
283 derive => 'trigonometric derivatives',
284 infix_string => undef,
285 prefix_string => 'sinh',
286 #application => '0.5*(EULER**$_[0] - EULER**(-$_[0]))',
287 application => '0.5*('.EULER.'**$_[0] - '.EULER.'**(-$_[0]))',
288 },
289  
290 # U_COSINE_H
291 {
292 arity => 1,
293 derive => 'trigonometric derivatives',
294 infix_string => undef,
295 prefix_string => 'cosh',
296 application => '0.5*('.EULER.'**$_[0] + '.EULER.'**(-$_[0]))',
297 #application => '0.5*(EULER**$_[0] + EULER**(-$_[0]))',
298 },
299  
300 # U_AREASINE_H
301 {
302 arity => 1,
303 derive => 'inverse trigonometric derivatives',
304 infix_string => undef,
305 prefix_string => 'asinh',
306 application => 'log( $_[0] + sqrt( $_[0] * $_[0] + 1 ) ) ',
307 #application => 'Math::Symbolic::AuxFunctions::asinh($_[0])',
308 },
309  
310 # U_AREACOSINE_H
311 {
312 arity => 1,
313 derive => 'inverse trigonometric derivatives',
314 infix_string => undef,
315 prefix_string => 'acosh',
316 application => 'log( $_[0] + sqrt( $_[0] * $_[0] - 1 ) ) ',
317 #application => 'Math::Symbolic::AuxFunctions::acosh($_[0])',
318 },
319  
320 # B_ARCTANGENT_TWO
321 {
322 arity => 2,
323 derive => 'inverse atan2',
324 infix_string => undef,
325 prefix_string => 'atan2',
326 application => 'atan2($_[0], $_[1])',
327 #application => 'Math::Symbolic::AuxFunctions::atan($_[0])',
328 #commutative => 0,
329 },
330  
331 );
332  
333 =head1 METHODS
334  
335 =head2 Constructor new
336  
337 Expects a hash reference as first argument. That hash's contents
338 will be treated as key-value pairs of object attributes.
339 Important attributes are 'type' => OPERATORTYPE (use constants as
340 exported by Math::Symbolic::ExportConstants!) and 'operands=>[op1,op2,...]'.
341 Where the operands themselves may either be valid Math::Symbolic::* objects
342 or strings that will be parsed as such.
343  
344 Special case: if no hash reference was found, first
345 argument is assumed to be the operator's symbol and the operator
346 is assumed to be binary. The following 2 arguments will be treated as
347 operands. This special case will ignore attempts to clone objects but if
348 the operands are no valid Math::Symbolic::* objects, they will be sent
349 through a Math::Symbolic::Parser to construct Math::Symbolic trees.
350  
351 Returns a Math::Symbolic::Operator.
352  
353 Supported operator symbols: (number of operands and their
354 function in parens)
355  
356 + => sum (2)
357 - => difference (2)
358 * => product (2)
359 / => division (2)
360 log => logarithm (2: base, function)
361 ^ => exponentiation (2: base, exponent)
362 neg => unary minus (1)
363 partial_derivative => partial derivative (2: function, var)
364 total_derivative => total derivative (2: function, var)
365 sin => sine (1)
366 cos => cosine (1)
367 tan => tangent (1)
368 cot => cotangent (1)
369 asin => arc sine (1)
370 acos => arc cosine (1)
371 atan => arc tangent (1)
372 atan2 => arc tangent of y/x (2: y, x)
373 acot => arc cotangent (1)
374 sinh => hyperbolic sine (1)
375 cosh => hyperbolic cosine (1)
376 asinh => hyperbolic area sine (1)
377 acosh => hyperbolic area cosine (1)
378  
379 =cut
380  
381 9251 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
382 9251 my $proto = shift;
383 9251 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
611731340
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
384  
385 9251 ▶ if ( @_ and not( ref( $_[0] ) eq 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not ref $_[0] eq "HASH") 2991 6260 9251
Condition100%3/3
@_ and not ref $_[0] eq "HASH"
!ll&&!rl&&r
56965642991
MC/DC100%2/2
@_ and not ref $_[0] eq "HASH"
@_ not ref $_[0] eq "HASH"
Truth table100%3/3
A and not(B)
A= @_B= not ref $_[0] eq "HASH"
ABresult
0X0
100
111
386 2991 my $symbol = shift;
387 2991 my $type = $Op_Symbols{$symbol};
388 2991 ▶ croak "Invalid operator type specified ($symbol)."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 2991 2991
389 unless defined $type;
390 2991 my $operands = [ @_[ 0 .. $Op_Types[$type]{arity} - 1 ] ];
391  
392 2991 ▶ croak "Undefined operands not supported by "
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_)), @$operands) 0 2991 2991
393 . "Math::Symbolic::Operator objects."
394 if grep +( not defined($_) ), @$operands;
395  
396 @$operands =
397 map {
398 2991 ▶ ref($_) =~ /^Math::Symbolic/
Branch: 1 branch
BranchTrueFalseTotal
ref($_) =~ /^Math::Symbolic/ ? : 5581 46 5627
399 ? $_
400 : Math::Symbolic::parse_from_string($_)
401 } @$operands;
402  
403 2991 return bless {
404 type => $type,
405 operands => $operands,
406 } => $class;
407 }
408  
409 6260 my %args;
410 6260 ▶ %args = %{ $_[0] } if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 564 5696 6260
411 # and ref( $_[0] ) eq 'HASH';
412 # above condition isn't necessary since that'd otherwise have been
413 # the above branch.
414  
415 6260 my $operands = [];
416 6260 ▶ if ( ref $proto ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 5613 647 6260
417 5613 foreach ( @{ $proto->{operands} } ) {
418 9161 push @$operands, $_->new();
419 }
420 }
421  
422 6260 ▶ my $self = {
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 5613 647 6260
423 type => undef,
424 ( ref($proto) ? %$proto : () ),
425 operands => $operands,
426 %args,
427 };
428  
429 6260 @{ $self->{operands} } =
430 map {
431 10108 ▶ ref($_) =~ /^Math::Symbolic/
Branch: 1 branch
BranchTrueFalseTotal
ref($_) =~ /^Math::Symbolic/ ? : 10102 6 10108
432 ? $_
433 : Math::Symbolic::parse_from_string($_)
434 6260 } @{ $self->{operands} };
435  
436 6260 bless $self => $class;
437 }
438  
439 =head2 Method arity
440  
441 Returns the operator's arity as an integer.
442  
443 =cut
444  
445 2216 ▶sub arity {
Subroutine arity: called CC=1 SCAR=0.0
Pod documented
446 2216 my $self = shift;
447 2216 return $Op_Types[ $self->{type} ]{arity};
448 }
449  
450 =head2 Method type
451  
452 Optional integer argument that sets the operator's type.
453 Returns the operator's type as an integer.
454  
455 =cut
456  
457 21556 ▶sub type {
Subroutine type: called CC=2 SCAR=7.1
Pod documented
458 21556 my $self = shift;
459 21556 ▶ $self->{type} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 21556 21556
460 21556 return $self->{type};
461 }
462  
463 =head2 Method to_string
464  
465 Returns a string representation of the operator and its operands.
466 Optional argument: 'prefix' or 'infix'. Defaults to 'infix'.
467  
468 =cut
469  
470 526 ▶sub to_string {
Subroutine to_string: called CC=4 SCAR=13.9
Pod documented
471 526 my $self = shift;
472 526 my $string_type = shift;
473 526 ▶ $string_type = 'infix'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string_type and $string_type eq "prefix" 357 169 526
Condition100%3/3
defined $string_type and $string_type eq "prefix"
!ll&&!rl&&r
77280169
MC/DC100%2/2
defined $string_type and $string_type eq "prefix"
defined $string_type $string_type eq "prefix"
Truth table100%3/3
A and B
A= defined $string_typeB= $string_type eq "prefix"
ABresult
0X0
100
111
474 unless defined $string_type
475 and $string_type eq 'prefix';
476 23 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
477  
478 526 my $string = '';
479 526 ▶ if ( $string_type eq 'prefix' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string_type eq 'prefix') { } 169 357 526
480 169 $string .= $self->_to_string_prefix();
481 }
482 else {
483 357 $string .= $self->_to_string_infix();
484 }
485 526 return $string;
486 }
487  
488 357 ▶sub _to_string_infix {
Subroutine _to_string_infix: called CC=11 SCAR=24.0
489 357 my $self = shift;
490 357 my $op = $Op_Types[ $self->{type} ];
491  
492 357 my $op_str = $op->{infix_string};
493 357 my $string;
494 357 ▶ if ( $op->{arity} == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->{'arity'} == 2) { } 323 34 357
elsif ($op->{'arity'} == 1) { } 34 0 34
495 323 my $op1 = $self->{operands}[0]->term_type() == T_OPERATOR;
496 323 my $op2 = $self->{operands}[1]->term_type() == T_OPERATOR;
497  
498 323 ▶ if ( not defined $op_str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $op_str) { } 11 312 323
499 11 $op_str = $op->{prefix_string};
500 11 $string = "$op_str(";
501 $string .= join( ', ',
502 11 map { $_->to_string('infix') } @{ $self->{operands} } );
503 11 $string .= ')';
504 }
505 else {
506 $string =
507 ( $op1 ? '(' : '' )
508 . $self->{operands}[0]->to_string('infix')
509 . ( $op1 ? ')' : '' )
510 . " $op_str "
511 . ( $op2 ? '(' : '' )
512 312 ▶ . $self->{operands}[1]->to_string('infix')
Branch: 4 branches
BranchTrueFalseTotal
$op1 ? : 88 224 312
$op1 ? : 88 224 312
$op2 ? : 166 146 312
$op2 ? : 166 146 312
513 . ( $op2 ? ')' : '' );
514 }
515 }
516 elsif ( $op->{arity} == 1 ) {
517 34 my $is_op1 = $self->{operands}[0]->term_type() == T_OPERATOR;
518 34 ▶ if ( not defined $op_str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $op_str) { } 23 11 34
519 23 $op_str = $op->{prefix_string};
520 $string =
521 23 "$op_str(" . $self->{operands}[0]->to_string('infix') . ")";
522 }
523 else {
524 $string = "$op_str"
525 . ( $is_op1 ? '(' : '' )
526 11 ▶ . $self->{operands}[0]->to_string('infix')
Branch: 2 branches
BranchTrueFalseTotal
$is_op1 ? : 3 8 11
$is_op1 ? : 3 8 11
527 . ( $is_op1 ? ')' : '' );
528 }
529 }
530 else {
531 0 $string = $self->_to_string_prefix();
532 }
533 357 return $string;
534 }
535  
536 169 ▶sub _to_string_prefix {
Subroutine _to_string_prefix: called CC=1 SCAR=0.0
537 169 my $self = shift;
538 169 my $op = $Op_Types[ $self->{type} ];
539  
540 169 my $op_str = $op->{prefix_string};
541 169 my $string = "$op_str(";
542 $string .=
543 169 join( ', ', map { $_->to_string('prefix') } @{ $self->{operands} } );
544 169 $string .= ')';
545 169 return $string;
546 }
547  
548 =head2 Method term_type
549  
550 Returns the type of the term. ( T_OPERATOR )
551  
552 =cut
553  
554 30875 ▶sub term_type {T_OPERATOR}
Subroutine term_type: called CC=1 SCAR=0.0
Pod documented
555  
556 =head2 Method simplify
557  
558 Term simpilification.
559 First argument: Boolean indicating that the tree does not
560 need to be cloned, but can be restructured instead.
561 While this is faster, you might not be able to use the old
562 tree any more.
563  
564 Example:
565  
566 my $othertree = $tree->simplify();
567 # can use $othertree and $tree now.
568  
569 my $yetanothertree = $tree->simplify(1);
570 # must not use $tree any more because its internal
571 # representation might have been destroyed.
572  
573 If you want to optimize a routine and you're sure that you
574 won't need the unsimplified tree any more, go ahead and use
575 the first parameter. In all other cases, you should go the
576 safe route.
577  
578 =cut
579  
580 1593 ▶sub simplify {
Subroutine simplify: called CC=97 SCAR=51.2
Pod documented
581 1593 my $self = shift;
582 1593 my $dont_clone = shift;
583 1593 ▶ $self = $self->new() unless $dont_clone;
Branch: 1 branch
BranchTrueFalseTotal
unless $dont_clone 69 1524 1593
584  
585 1593 my $operands = $self->{operands};
586 1593 my $op = $Op_Types[ $self->type() ];
587  
588 # simplify operands without cloning.
589 1593 @$operands = map { $_->simplify(1) } @$operands;
590  
591 1593 ▶ if ( $self->arity() == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->arity == 2) { } 970 623 1593
elsif ($self->arity == 1) { } 623 0 623
592 970 my $o1 = $operands->[0];
593 970 my $o2 = $operands->[1];
594 970 my $tt1 = $o1->term_type();
595 970 my $tt2 = $o2->term_type();
596 970 my $type = $self->type();
597  
598 970 ▶ if ( $self->is_simple_constant() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_simple_constant) 71 899 970
599 71 return $self->apply();
600 }
601  
602 899 ▶ if ( $o1->is_identical($o2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o1->is_identical($o2)) 17 882 899
603 17 ▶ if ( $type == B_PRODUCT ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 2) { } 12 5 17
elsif ($type == 0) { } 5 0 5
elsif ($type == 3) { } 0 0 0
elsif ($type == 1) { } 0 0 0
604 12 my $two = Math::Symbolic::Constant->new(2);
605 12 return $self->new( '^', $o1, $two )->simplify(1);
606 }
607 elsif ( $type == B_SUM ) {
608 5 my $two = Math::Symbolic::Constant->new(2);
609 5 return $self->new( '*', $two, $o1 )->simplify(1);
610 }
611 elsif ( $type == B_DIVISION ) {
612 0 ▶ croak "Symbolic division by zero."
Branch: 1 branch
BranchTrueFalseTotal
if $o2->term_type == 1 and $o2->value == 0 || $o2->special eq "zero" 0 0 0
Condition0%0/6
$o2->term_type == 1 and $o2->value == 0 || $o2->special eq "zero"
!ll&&!rl&&r
000
$o2->value == 0 or $o2->special eq "zero"
l!l&&r!l&&!r
000
MC/DC0%0/3
$o2->term_type == 1 and $o2->value == 0 || $o2->special eq "zero"
$o2->term_type == 1 $o2->value == 0 $o2->special eq "zero"
Truth table0%0/4
A and B or C
A= $o2->term_type == 1B= $o2->value == 0C= $o2->special eq "zero"
ABCresult
0XX0
1000
11X1
1011
613 if $o2->term_type() == T_CONSTANT
614 and ($o2->value() == 0
615 or $o2->special() eq 'zero' );
616 0 return Math::Symbolic::Constant->one();
617 }
618 elsif ( $type == B_DIFFERENCE ) {
619 0 return Math::Symbolic::Constant->zero();
620 }
621 }
622  
623 # exp(0) = 1
624 882 ▶ if ( $tt2 == T_CONSTANT
Branch: 1 branch
BranchTrueFalseTotal
if ($tt2 == 1 and $tt1 == 0 and $type == 7 and $o2->value == 0) 0 882 882
Condition88%8/9
$tt2 == 1 and $tt1 == 0 and $type == 7 and $o2->value == 0
!ll&&!rl&&r
6901920
$tt2 == 1 and $tt1 == 0 and $type == 7
!ll&&!rl&&r
65139192
$tt2 == 1 and $tt1 == 0
!ll&&!rl&&r
63615231
MC/DC0%0/4
$tt2 == 1 and $tt1 == 0 and $type == 7 and $o2->value == 0
$tt2 == 1 $tt1 == 0 $type == 7 $o2->value == 0
Truth table80%4/5
A and B and C and D
A= $tt2 == 1B= $tt1 == 0C= $type == 7D= $o2->value == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
625 and $tt1 == T_OPERATOR
626 and $type == B_EXP
627 and $o2->value() == 0 )
628 {
629 0 return Math::Symbolic::Constant->one();
630 }
631
632 # a^1 = a
633 882 ▶ if ( $tt2 == T_CONSTANT
Branch: 1 branch
BranchTrueFalseTotal
if ($tt2 == 1 and $type == 7 and $o2->value == 1 || $o2->special eq "one") 6 876 882
Condition88%8/9
$tt2 == 1 and $type == 7 and $o2->value == 1 || $o2->special eq "one"
!ll&&!rl&&r
6771996
$tt2 == 1 and $type == 7
!ll&&!rl&&r
63641205
$o2->value == 1 or $o2->special eq "one"
l!l&&r!l&&!r
06199
MC/DC75%3/4
$tt2 == 1 and $type == 7 and $o2->value == 1 || $o2->special eq "one"
$tt2 == 1 $type == 7 $o2->value == 1 $o2->special eq "one"
Truth table80%4/5
A and B and C or D
A= $tt2 == 1B= $type == 7C= $o2->value == 1D= $o2->special eq "one"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
634 and $type == B_EXP
635 and ( $o2->value() == 1 or $o2->special() eq 'one' ) )
636 {
637 6 return $o1;
638 }
639  
640 # (a^b)^const = a^(const*b)
641 876 ▶ if ( $tt2 == T_CONSTANT
Branch: 1 branch
BranchTrueFalseTotal
if ($tt2 == 1 and $tt1 == 0 and $type == 7 and $o1->type == 7) 11 865 876
Condition100%9/9
$tt2 == 1 and $tt1 == 0 and $type == 7 and $o1->type == 7
!ll&&!rl&&r
69017511
$tt2 == 1 and $tt1 == 0 and $type == 7
!ll&&!rl&&r
65139186
$tt2 == 1 and $tt1 == 0
!ll&&!rl&&r
63615225
MC/DC100%4/4
$tt2 == 1 and $tt1 == 0 and $type == 7 and $o1->type == 7
$tt2 == 1 $tt1 == 0 $type == 7 $o1->type == 7
Truth table100%5/5
A and B and C and D
A= $tt2 == 1B= $tt1 == 0C= $type == 7D= $o1->type == 7
ABCDresult
0XXX0
10XX0
110X0
11100
11111
642 and $tt1 == T_OPERATOR
643 and $type == B_EXP
644 and $o1->type() == B_EXP )
645 {
646 11 return $self->new( '^', $o1->op1(),
647 $self->new( '*', $o2, $o1->op2() ) )->simplify(1);
648 }
649  
650 # redundant
651 # if ( $tt1 == T_VARIABLE
652 # and $tt2 == T_VARIABLE
653 # and $o1->name() eq $o2->name() )
654 # {
655 # if ( $type == B_SUM ) {
656 # my $two = Math::Symbolic::Constant->new(2);
657 # return $self->new( '*', $two, $o1 );
658 # }
659 # elsif ( $type == B_DIFFERENCE ) {
660 # return Math::Symbolic::Constant->zero();
661 # }
662 # elsif ( $type == B_PRODUCT ) {
663 # my $two = Math::Symbolic::Constant->new(2);
664 # return $self->new( '^', $o1, $two );
665 # }
666 # elsif ( $type == B_DIVISION ) {
667 # return Math::Symbolic::Constant->one();
668 # }
669 # }
670  
671 865 ▶ if ( $tt1 == T_CONSTANT or $tt2 == T_CONSTANT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tt1 == 1 or $tt2 == 1) { } 511 354 865
elsif ($type == 2) { } 190 164 354
Condition100%3/3
$tt1 == 1 or $tt2 == 1
l!l&&r!l&&!r
282229354
MC/DC100%2/2
$tt1 == 1 or $tt2 == 1
$tt1 == 1 $tt2 == 1
Truth table100%3/3
A or B
A= $tt1 == 1B= $tt2 == 1
ABresult
1X1
011
000
672 511 ▶ my $const = ( $tt1 == T_CONSTANT ? $o1 : $o2 );
Branch: 1 branch
BranchTrueFalseTotal
$tt1 == 1 ? : 282 229 511
673 511 ▶ my $not_c = ( $tt1 == T_CONSTANT ? $o2 : $o1 );
Branch: 1 branch
BranchTrueFalseTotal
$tt1 == 1 ? : 282 229 511
674 511 my $constant_first = $tt1 == T_CONSTANT;
675  
676 511 ▶ if ( $type == B_SUM ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 0) 20 491 511
677 20 ▶ return $not_c if $const->value() == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $const->value == 0 4 16 20
678 16 return $not_c->mod_add_constant($const);
679 }
680
681 491 ▶ if ( $type == B_DIFFERENCE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) 4 487 491
682 4 ▶ if (!$constant_first) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($constant_first) 2 2 4
683 2 my $value = $const->value();
684 2 ▶ return $not_c if $value == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value == 0 0 2 2
685 2 return $not_c->mod_add_constant(-$value);
686 }
687 2 ▶ if ( $constant_first and $const->value == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constant_first and $const->value == 0) 0 2 2
Condition33%1/3
$constant_first and $const->value == 0
!ll&&!rl&&r
020
MC/DC0%0/2
$constant_first and $const->value == 0
$constant_first $const->value == 0
Truth table33%1/3
A and B
A= $constant_firstB= $const->value == 0
ABresult
0X0
100
111
688 0 return Math::Symbolic::Operator->new(
689 {
690 type => U_MINUS,
691 operands => [$not_c],
692 }
693 );
694 }
695 }
696
697 489 ▶ if ( $type == B_PRODUCT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 2) { } 250 239 489
elsif ($type == 3) { } 31 208 239
698 250 ▶ return $not_c if $const->value() == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $const->value == 1 1 249 250
699 249 ▶ return Math::Symbolic::Constant->zero()
Branch: 1 branch
BranchTrueFalseTotal
if $const->value == 0 20 229 249
700 if $const->value == 0;
701  
702 229 ▶ if ( $not_c->term_type() == T_OPERATOR
Branch: 2 branches
BranchTrueFalseTotal
if ($not_c->term_type == 0 and $not_c->type == 2 and $not_c->op1->term_type == 1 || $not_c->op2->term_type == 1) { } 20 209 229
elsif ($not_c->term_type == 0 and $not_c->type == 3 and $not_c->op1->term_type == 1) { } 7 376 383
Condition100%9/9
$not_c->term_type == 0 and $not_c->type == 2 and $not_c->op1->term_type == 1 || $not_c->op2->term_type == 1
!ll&&!rl&&r
1901920
$not_c->term_type == 0 and $not_c->type == 2
!ll&&!rl&&r
6112939
$not_c->op1->term_type == 1 or $not_c->op2->term_type == 1
l!l&&r!l&&!r
101019
MC/DC100%4/4
$not_c->term_type == 0 and $not_c->type == 2 and $not_c->op1->term_type == 1 || $not_c->op2->term_type == 1
$not_c->term_type == 0 $not_c->type == 2 $not_c->op1->term_type == 1 $not_c->op2->term_type == 1
Truth table100%5/5
A and B and C or D
A= $not_c->term_type == 0B= $not_c->type == 2C= $not_c->op1->term_type == 1D= $not_c->op2->term_type == 1
ABCDresult
0XXX0
10XX0
11000
111X1
11011
703 and $not_c->type() == B_PRODUCT
704 and $not_c->op1()->term_type() == T_CONSTANT
705 || $not_c->op2()->term_type() == T_CONSTANT )
706 {
707 20 ▶ my ( $c, $nc ) = (
Branch: 1 branch
BranchTrueFalseTotal
$not_c->op1->term_type == 1 ? : 10 10 20
708 $not_c->op1()->term_type() == T_CONSTANT
709 ? ( $not_c->op1, $not_c->op2 )
710 : ( $not_c->op2, $not_c->op1 )
711 );
712 20 my $c_product = $not_c->new( '*', $const, $c )->apply();
713 20 return $not_c->new( '*', $c_product, $nc );
714 }
715 ▶ elsif ( $not_c->term_type() == T_OPERATOR
Condition100%6/6
$not_c->term_type == 0 and $not_c->type == 3 and $not_c->op1->term_type == 1
!ll&&!rl&&r
174287
$not_c->term_type == 0 and $not_c->type == 3
!ll&&!rl&&r
6111335
MC/DC100%3/3
$not_c->term_type == 0 and $not_c->type == 3 and $not_c->op1->term_type == 1
$not_c->term_type == 0 $not_c->type == 3 $not_c->op1->term_type == 1
Truth table100%4/4
A and B and C
A= $not_c->term_type == 0B= $not_c->type == 3C= $not_c->op1->term_type == 1
ABCresult
0XX0
10X0
1100
1111
716 and $not_c->type() == B_DIVISION
717 and $not_c->op1()->term_type() == T_CONSTANT )
718 {
719 7 return Math::Symbolic::Operator->new(
720 '/',
721 Math::Symbolic::Constant->new(
722 $const->value() * $not_c->op1()->value()
723 ),
724 $not_c->op2()
725 );
726 }
727 }
728 elsif ( $type == B_DIVISION ) {
729 31 ▶ return $not_c
Branch: 1 branch
BranchTrueFalseTotal
if not $constant_first and $const->value == 1 0 31 31
Condition33%1/3
not $constant_first and $const->value == 1
!ll&&!rl&&r
3100
MC/DC0%0/2
not $constant_first and $const->value == 1
not $constant_first $const->value == 1
Truth table33%1/3
not(A) and B
A= not $constant_firstB= $const->value == 1
ABresult
0X0
100
111
730 if !$constant_first
731 and $const->value == 1;
732 31 ▶ return Math::Symbolic::Constant->new('#Inf')
Branch: 1 branch
BranchTrueFalseTotal
if not $constant_first and $const->value == 0 0 31 31
Condition33%1/3
not $constant_first and $const->value == 0
!ll&&!rl&&r
3100
MC/DC0%0/2
not $constant_first and $const->value == 0
not $constant_first $const->value == 0
Truth table33%1/3
not(A) and B
A= not $constant_firstB= $const->value == 0
ABresult
0X0
100
111
733 if !$constant_first
734 and $const->value == 0;
735 31 ▶ return Math::Symbolic::Constant->zero()
Branch: 1 branch
BranchTrueFalseTotal
if $const->value == 0 0 31 31
736 if $const->value == 0;
737  
738 }
739 }
740 elsif ( $type == B_PRODUCT ) {
741 190 ▶ if ( $tt2 == T_CONSTANT ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tt2 == 1) { } 0 190 190
elsif ($tt1 == 1) { } 0 190 190
elsif ($tt1 == 0 and $tt2 == 2) { } 7 190 197
742 0 return $o1->mod_multiply_constant($o2);
743 }
744 elsif ( $tt1 == T_CONSTANT ) {
745 0 return $o2->mod_multiply_constant($o1);
746 }
747 ▶ elsif ( $tt1 == T_OPERATOR and $tt2 == T_VARIABLE ) {
Condition100%3/3
$tt1 == 0 and $tt2 == 2
!ll&&!rl&&r
71767
MC/DC100%2/2
$tt1 == 0 and $tt2 == 2
$tt1 == 0 $tt2 == 2
Truth table100%3/3
A and B
A= $tt1 == 0B= $tt2 == 2
ABresult
0X0
100
111
748 7 return $self->new( '*', $o2, $o1 );
749 }
750 }
751  
752 788 ▶ if ( $type == B_SUM ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 0) 31 757 788
753 31 my @ops;
754 my @const;
755 31 my @todo = ( $o1, $o2 );
756 31 my %vars;
757 31 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 98 31 129
758 98 my $this = shift @todo;
759  
760 98 ▶ if ( $this->term_type() == T_OPERATOR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->term_type == 0) { } 92 6 98
elsif ($this->term_type == 2) { } 5 1 6
761 92 my $t = $this->type();
762 92 ▶ if ( $t == B_SUM ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($t == 0) { } 18 74 92
elsif ($t == 1) { } 0 74 74
elsif ($t == 4) { } 0 74 74
elsif ($t == 2) { } 64 10 74
763 18 push @todo, @{ $this->{operands} };
764 }
765 elsif ( $t == B_DIFFERENCE ) {
766 0 push @todo, $this->op1(),
767 Math::Symbolic::Operator->new( 'neg',
768 $this->op2() );
769 }
770 elsif ( $t == U_MINUS ) {
771 0 my $op = $this->op1();
772 0 my $tt = $op->term_type();
773 0 ▶ if ( $tt == T_VARIABLE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tt == 2) { } 0 0 0
elsif ($tt == 1) { } 0 0 0
774 0 $vars{$op->name}--;
775 }
776 elsif ( $tt == T_CONSTANT ) {
777 0 push @const, $todo[0]->value();
778 }
779 else {
780 0 my $ti = $op->type();
781 0 ▶ if ( $ti == U_MINUS ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ti == 4) { } 0 0 0
elsif ($ti == 0) { } 0 0 0
elsif ($ti == 1) { } 0 0 0
782 0 push @todo, $op->op1();
783 }
784 elsif ( $ti == B_SUM ) {
785 0 push @todo,
786 Math::Symbolic::Operator->new(
787 'neg', $op->op1()
788 ),
789 Math::Symbolic::Operator->new( 'neg',
790 $op->op2() );
791 }
792 elsif ( $ti == B_DIFFERENCE ) {
793 0 push @todo, $op->op2(),
794 Math::Symbolic::Operator->new( 'neg',
795 $op->op1() );
796 }
797 else {
798 0 push @ops, $this;
799 }
800 }
801 }
802 elsif ( $t == B_PRODUCT ) {
803 64 my ($o1, $o2) = @{$this->{operands}};
804 64 my $tl = $o1->term_type();
805 64 my $tr = $o2->term_type();
806
807 64 ▶ if ($tl == T_VARIABLE and $tr == T_CONSTANT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tl == 2 and $tr == 1) { } 0 64 64
elsif ($tr == 2 and $tl == 1) { } 2 62 64
Condition66%2/3
$tl == 2 and $tr == 1
!ll&&!rl&&r
6310
MC/DC0%0/2
$tl == 2 and $tr == 1
$tl == 2 $tr == 1
Truth table66%2/3
A and B
A= $tl == 2B= $tr == 1
ABresult
0X0
100
111
808 0 $vars{$o1->name}+= $o2->value();
809 }
810 ▶ elsif ($tr == T_VARIABLE and $tl == T_CONSTANT) {
Condition100%3/3
$tr == 2 and $tl == 1
!ll&&!rl&&r
6112
MC/DC100%2/2
$tr == 2 and $tl == 1
$tr == 2 $tl == 1
Truth table100%3/3
A and B
A= $tr == 2B= $tl == 1
ABresult
0X0
100
111
811 2 $vars{$o2->name}+= $o1->value();
812 }
813 else {
814 62 push @ops, $this;
815 }
816 }
817 else {
818 10 push @ops, $this;
819 }
820 }
821 elsif ( $this->term_type() == T_VARIABLE ) {
822 5 $vars{$this->name}++;
823 }
824 else {
825 1 push @const, $this->value();
826 }
827 }
828  
829 31 my @vars = ();
830 31 foreach (keys %vars) {
831 7 my $num = $vars{$_};
832 0 ▶ if (!$num) { next; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($num) 0 7 7
833
834 7 ▶ if ($num == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 1) 5 2 7
835 5 push @vars, Math::Symbolic::Variable->new($_);
836 5 next;
837 }
838 2 my $mul = Math::Symbolic::Operator->new(
839 '*',
840 Math::Symbolic::Constant->new(abs($num)),
841 Math::Symbolic::Variable->new($_)
842 );
843 2 ▶ push @ops, $num < 0
Branch: 1 branch
BranchTrueFalseTotal
$num < 0 ? : 1 1 2
844 ? Math::Symbolic::Operator->new('neg', $mul)
845 : $mul;
846 }
847  
848 31 my $const = 0;
849 31 $const += $_ foreach @const;
850 31 ▶ if ( $const == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($const == 0) { } 30 1 31
851 30 $const = shift @vars;
852 }
853 else {
854 1 $const = Math::Symbolic::Constant->new($const);
855 }
856  
857 31 foreach ( @vars ) {
858 1 $const = Math::Symbolic::Operator->new('+', $const, $_);
859 }
860
861 31 @ops = map {$_->simplify(1)} @ops;
862 31 my @newops;
863 31 ▶ push @newops, $const if defined $const;
Branch: 1 branch
BranchTrueFalseTotal
if defined $const 5 26 31
864 31 foreach my $out ( 0 .. $#ops ) {
865 74 ▶ next if not defined $ops[$out];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ops[$out] 2 72 74
866 72 my $identical = 0;
867 72 foreach my $in ( 0 .. $#ops ) {
868 218 ▶ next if $in == $out or not defined $ops[$in];
Branch: 1 branch
BranchTrueFalseTotal
if $in == $out or not defined $ops[$in] 75 143 218
Condition100%3/3
$in == $out or not defined $ops[$in]
l!l&&r!l&&!r
723143
MC/DC100%2/2
$in == $out or not defined $ops[$in]
$in == $out not defined $ops[$in]
Truth table100%3/3
A or not(B)
A= $in == $outB= not defined $ops[$in]
ABresult
1X1
011
000
869 143 ▶ if ( $ops[$out]->is_identical( $ops[$in] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ops[$out]->is_identical($ops[$in])) 2 141 143
870 2 $identical++;
871 2 $ops[$in] = undef;
872 }
873 }
874 72 ▶ if ( not $identical ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $identical) { } 70 2 72
875 70 push @newops, $ops[$out];
876 }
877 else {
878 2 push @newops,
879 Math::Symbolic::Operator->new( '*', $identical + 1,
880 $ops[$out] );
881 }
882 }
883
884 31 my $sumops;
885 31 ▶ if (@newops) {
Branch: 1 branch
BranchTrueFalseTotal
if (@newops) { } 31 0 31
886 31 $sumops = shift @newops;
887 31 $sumops += $_ foreach @newops;
888 }
889 0 else {return Math::Symbolic::Constant->zero()}
890  
891 31 return $sumops;
892 }
893 }
894 elsif ( $self->arity() == 1 ) {
895 623 my $o = $operands->[0];
896 623 my $tt = $o->term_type();
897 623 my $type = $self->type();
898 623 ▶ if ( $type == U_MINUS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 4) 181 442 623
899 181 ▶ if ( $tt == T_CONSTANT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tt == 1) { } 2 179 181
elsif ($tt == 0) { } 172 7 179
900 2 return Math::Symbolic::Constant->new( -$o->value(), );
901 }
902 elsif ( $tt == T_OPERATOR ) {
903 172 my $inner_type = $o->type();
904 172 ▶ if ( $inner_type == U_MINUS ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($inner_type == 4) { } 3 169 172
elsif ($inner_type == 1) { } 7 162 169
905 3 return $o->{operands}[0];
906 }
907 elsif ( $inner_type == B_DIFFERENCE ) {
908 7 return $o->new( '-', @{$o->{operands}}[1,0] );
909 }
910 }
911 }
912 }
913  
914 1368 return $self;
915 }
916  
917 =head2 Methods op1 and op2
918  
919 Returns first/second operand of the operator if it exists or undef.
920  
921 =cut
922  
923 156 ▶sub op1 {
Subroutine op1: called CC=2 SCAR=8.1
Pod documented
924 156 ▶ return $_[0]{operands}[0] if @{ $_[0]{operands} } >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$_[0]{"operands"};} >= 1 156 0 156
925 0 return undef;
926 }
927  
928 70 ▶sub op2 {
Subroutine op2: called CC=2 SCAR=7.2
Pod documented
929 70 ▶ return $_[0]{operands}[1] if @{ $_[0]{operands} } >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$_[0]{"operands"};} >= 2 70 0 70
930 }
931  
932 =head2 Method apply
933  
934 Applies the operation to its operands' value() and returns the result
935 as a constant (-object).
936  
937 Without arguments, all variables in the tree are required to have a value.
938 If any don't, the call to apply() returns undef.
939  
940 To (temorarily, for this single method call) assign values to
941 variables in the tree, you may provide key/value pairs of variable names
942 and values. Instead of passing a list of key/value pairs, you may also pass
943 a single hash reference containing the variable mappings.
944  
945 You usually want to call the value() instead of this.
946  
947 =cut
948  
949 4942 ▶sub apply {
Subroutine apply: called CC=9 SCAR=22.1
Pod documented
950 4942 my $self = shift;
951 4942 ▶ my $args = ( @_ == 1 ? $_[0] : +{ @_ } );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 4846 96 4942
952 4942 my $op_type = $self->type();
953 4942 my $op = $Op_Types[$op_type];
954 4942 my $operands = $self->{operands};
955 4942 my $application = $op->{application};
956  
957 4942 ▶ if ( ref($application) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $application ne 'CODE') { } 4935 7 4942
958 4935 local @_;
959 4935 local $@;
960 4935 eval {
961 @_ = map {
962 4935 my $v = $_->value($args);
963 (
964 9061 ▶ defined $v
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 9059 2 9061
965 ? $v
966 : croak
967 "Undefined operand in Math::Symbolic::Operator->apply()"
968 )
969 } @$operands;
970 };
971 4935 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 4933 4935
972 4933 ▶ return undef if $op_type == B_DIVISION and $_[1] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $op_type == 3 and $_[1] == 0 0 4933 4933
Condition66%2/3
$op_type == 3 and $_[1] == 0
!ll&&!rl&&r
47332000
MC/DC0%0/2
$op_type == 3 and $_[1] == 0
$op_type == 3 $_[1] == 0
Truth table66%2/3
A and B
A= $op_type == 3B= $_[1] == 0
ABresult
0X0
100
111
973 4933 my $result = eval $application;
974 4933 ▶ die "Invalid operator application: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4933 4933
975 4933 ▶ die "Undefined result from operator application."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 4933 4933
976 if not defined $result;
977  
978 4933 return Math::Symbolic::Constant->new($result);
979 }
980 else {
981 7 return $application->(@$operands);
982 }
983 }
984  
985 =head2 Method value
986  
987 value() evaluates the Math::Symbolic tree to its numeric representation.
988  
989 value() without arguments requires that every variable in the tree contains
990 a defined value attribute. Please note that this refers to every variable
991 I<object>, not just every named variable.
992  
993 value() with one argument sets the object's value if you're dealing with
994 Variables or Constants. In case of operators, a call with one argument will
995 assume that the argument is a hash reference. (see next paragraph)
996  
997 value() with named arguments (key/value pairs) associates variables in the tree
998 with the value-arguments if the corresponging key matches the variable name.
999 (Can one say this any more complicated?) Since version 0.132, an
1000 equivalent and valid syntax is to pass a single hash reference instead of a
1001 list.
1002  
1003 Example: $tree->value(x => 1, y => 2, z => 3, t => 0) assigns the value 1 to
1004 any occurrances of variables of the name "x", aso.
1005  
1006 If a variable in the tree has no value set (and no argument of value sets
1007 it temporarily), the call to value() returns undef.
1008  
1009 =cut
1010  
1011 4846 ▶sub value {
Subroutine value: called CC=3 SCAR=11.0
Pod documented
1012 4846 my $self = shift;
1013 4846 ▶ my $args = ( @_ == 1 ? $_[0] : +{@_} );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3968 878 4846
1014  
1015 4846 my $applied = $self->apply($args);
1016 4846 ▶ return undef unless defined $applied;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $applied 1 4845 4846
1017 4845 return $applied->value($args);
1018 }
1019  
1020 =head2 Method signature
1021  
1022 signature() returns a tree's signature.
1023  
1024 In the context of Math::Symbolic, signatures are the list of variables
1025 any given tree depends on. That means the tree "v*t+x" depends on the
1026 variables v, t, and x. Thus, applying signature() on the tree that would
1027 be parsed from above example yields the sorted list ('t', 'v', 'x').
1028  
1029 Constants do not depend on any variables and therefore return the empty list.
1030 Obviously, operators' dependencies vary.
1031  
1032 Math::Symbolic::Variable objects, however, may have a slightly more
1033 involved signature. By convention, Math::Symbolic variables depend on
1034 themselves. That means their signature contains their own name. But they
1035 can also depend on various other variables because variables themselves
1036 can be viewed as placeholders for more compicated terms. For example
1037 in mechanics, the acceleration of a particle depends on its mass and
1038 the sum of all forces acting on it. So the variable 'acceleration' would
1039 have the signature ('acceleration', 'force1', 'force2',..., 'mass', 'time').
1040  
1041 If you're just looking for a list of the names of all variables in the tree,
1042 you should use the explicit_signature() method instead.
1043  
1044 =cut
1045  
1046 873 ▶sub signature {
Subroutine signature: called CC=3 SCAR=11.0
Pod documented
1047 873 my $self = shift;
1048 873 my %sig;
1049 873 foreach my $o ( $self->descending_operands('all_vars') ) {
1050 1368 $sig{$_} = undef for $o->signature();
1051 }
1052 873 return sort keys %sig;
1053 }
1054  
1055 =head2 Method explicit_signature
1056  
1057 explicit_signature() returns a lexicographically sorted list of
1058 variable names in the tree.
1059  
1060 See also: signature().
1061  
1062 =cut
1063  
1064 172 ▶sub explicit_signature {
Subroutine explicit_signature: called CC=3 SCAR=11.0
Pod documented
1065 172 my $self = shift;
1066 172 my %sig;
1067 172 foreach my $o ( $self->descending_operands('all_vars') ) {
1068 330 $sig{$_} = undef for $o->explicit_signature();
1069 }
1070 172 return sort keys %sig;
1071 }
1072  
1073 23 1;
1074 __END__
« Symbolic/Parser/Precompiled.pm ↑ Summary Symbolic/Custom/DefaultTests.pm »