Symbolic/Base.pm

filter: Statementstmt 83.4% 171 / 205 Branchbran 68.8% 73 / 106 Conditioncond 60.0% 30 / 50 MC/DCmcdc 38.7% 12 / 31 Subroutinesub 80.9% 34 / 42 Podpod 92.8% 13 / 14 totaltotal 74.3% 333 / 448 CC 74 SCAR 28.8
CC
74
Cov
76%
descend
55.7
descending_operands
11.5
set_value
9.2
CRAP 17.9
« Symbolic/Custom/DefaultMods.pm ↑ Summary Symbolic/Derivative.pm »
1  
2 =encoding utf8
3  
4 =head1 NAME
5  
6 Math::Symbolic::Base - Base class for symbols in symbolic calculations
7  
8 =head1 SYNOPSIS
9  
10 use Math::Symbolic::Base;
11  
12 =head1 DESCRIPTION
13  
14 This is a base class for all Math::Symbolic::* terms such as
15 Math::Symbolic::Operator, Math::Symbolic::Variable and
16 Math::Symbolic::Constant objects.
17  
18 =head2 EXPORT
19  
20 None by default.
21  
22 =cut
23  
24 package Math::Symbolic::Base;
25  
26 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 23 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 use overload
34 23 ▶ "+" => \&_overload_addition,
Subroutine BEGIN: called CC=1 SCAR=0.0
35 "-" => \&_overload_subtraction,
36 "*" => \&_overload_multiplication,
37 "/" => \&_overload_division,
38 "**" => \&_overload_exponentiation,
39 "sqrt" => \&_overload_sqrt,
40 "log" => \&_overload_log,
41 "exp" => \&_overload_exp,
42 "sin" => \&_overload_sin,
43 "cos" => \&_overload_cos,
44 65 ▶ '""' => sub { $_[0]->to_string() },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 0 ▶ "0+" => sub { $_[0]->value() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 23 ▶ "bool" => sub { $_[0]->value() };
Subroutine __ANON__: called CC=1 SCAR=0.0
47  
48 23 ▶use Math::Symbolic::ExportConstants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 23 our $VERSION = '0.613';
51 23 our $AUTOLOAD;
52  
53 =head1 METHODS
54  
55 =cut
56  
57 =head2 Method to_string
58  
59 Default method for stringification just returns the object's value.
60  
61 =cut
62  
63 0 ▶sub to_string {
Subroutine to_string: not called CC=1 SCAR=6.9
Pod documented
64 0 my $self = shift;
65 0 return $self->value();
66 }
67  
68 =head2 Method value
69  
70 value() evaluates the Math::Symbolic tree to its numeric representation.
71  
72 value() without arguments requires that every variable in the tree contains
73 a defined value attribute. Please note that this refers to every variable
74 I<object>, not just every named variable.
75  
76 value() with one argument sets the object's value (in case of a variable or
77 constant).
78  
79 value() with named arguments (key/value pairs) associates variables in the tree
80 with the value-arguments if the corresponging key matches the variable name.
81 (Can one say this any more complicated?) Since version 0.132, an alternative
82 syntax is to pass a single hash reference.
83  
84 Example: $tree->value(x => 1, y => 2, z => 3, t => 0) assigns the value 1 to
85 any occurrances of variables of the name "x", aso.
86  
87 If a variable in the tree has no value set (and no argument of value sets
88 it temporarily), the call to value() returns undef.
89  
90 =cut
91  
92 0 ▶sub value {
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
93 0 croak "This is a method stub from Math::Symbolic::Base. Implement me.";
94 }
95  
96 =head2 Method signature
97  
98 signature() returns a tree's signature.
99  
100 In the context of Math::Symbolic, signatures are the list of variables
101 any given tree depends on. That means the tree "v*t+x" depends on the
102 variables v, t, and x. Thus, applying signature() on the tree that would
103 be parsed from above example yields the sorted list ('t', 'v', 'x').
104  
105 Constants do not depend on any variables and therefore return the empty list.
106 Obviously, operators' dependencies vary.
107  
108 Math::Symbolic::Variable objects, however, may have a slightly more
109 involved signature. By convention, Math::Symbolic variables depend on
110 themselves. That means their signature contains their own name. But they
111 can also depend on various other variables because variables themselves
112 can be viewed as placeholders for more compicated terms. For example
113 in mechanics, the acceleration of a particle depends on its mass and
114 the sum of all forces acting on it. So the variable 'acceleration' would
115 have the signature ('acceleration', 'force1', 'force2',..., 'mass', 'time').
116  
117 If you're just looking for a list of the names of all variables in the tree,
118 you should use the explicit_signature() method instead.
119  
120 =cut
121  
122 0 ▶sub signature {
Subroutine signature: not called CC=1 SCAR=6.9
Pod documented
123 0 croak "signature() implemented in the inheriting classes.";
124 }
125  
126 =head2 Method explicit_signature
127  
128 explicit_signature() returns a lexicographically sorted list of
129 variable names in the tree.
130  
131 See also: signature().
132  
133 =cut
134  
135 0 ▶sub explicit_signature {
Subroutine explicit_signature: not called CC=1 SCAR=6.9
Pod documented
136 0 croak "explicit_signature() implemented in the inheriting classes.";
137 }
138  
139 =head2 Method set_signature
140  
141 set_signature expects any number of variable identifiers as arguments.
142 It sets a variable's signature to this list of identifiers.
143  
144 =cut
145  
146 0 ▶sub set_signature {
Subroutine set_signature: not called CC=1 SCAR=6.9
Pod documented
147 0 croak "Cannot set signature of non-Variable Math::Symbolic tree element.";
148 }
149  
150 =head2 Method implement
151  
152 implement() works in-place!
153  
154 Takes key/value pairs as arguments. The keys are to be variable names
155 and the values must be valid Math::Symbolic trees. All occurrances
156 of the variables will be replaced with their implementation.
157  
158 =cut
159  
160 28 ▶sub implement {
Subroutine implement: called CC=1 SCAR=0.1
Pod documented
161 28 my $self = shift;
162 28 my %args = @_;
163  
164 return $self->descend(
165 in_place => 1,
166 810 ▶ after => sub {
Subroutine __ANON__: called CC=7 SCAR=20.2
167 810 my $tree = shift;
168 810 my $ttype = $tree->term_type();
169 810 ▶ if ( $ttype == T_VARIABLE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ttype == 2) { } 191 619 810
elsif ($ttype == 0 or $ttype == 1) { } 619 0 619
170 191 my $name = $tree->name();
171 191 ▶ if ( exists $args{$name}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$name} and defined $args{$name}) 187 4 191
Condition66%2/3
exists $args{$name} and defined $args{$name}
!ll&&!rl&&r
40187
MC/DC50%1/2
exists $args{$name} and defined $args{$name}
exists $args{$name} defined $args{$name}
Truth table66%2/3
A and B
A= exists $args{$name}B= defined $args{$name}
ABresult
0X0
100
111
172 and defined $args{$name} )
173 {
174 $args{$name} =
175 Math::Symbolic::parse_from_string( $args{$name} )
176 187 ▶ unless ref( $args{$name} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{$name} 0 187 187
177 187 $tree->replace( $args{$name} );
178 }
179 }
180 ▶ elsif ( $ttype == T_OPERATOR or $ttype == T_CONSTANT ) {
Condition66%2/3
$ttype == 0 or $ttype == 1
l!l&&r!l&&!r
4991200
MC/DC0%0/2
$ttype == 0 or $ttype == 1
$ttype == 0 $ttype == 1
Truth table66%2/3
A or B
A= $ttype == 0B= $ttype == 1
ABresult
1X1
011
000
181 }
182 else {
183 0 croak "'implement' called on invalid term " . "type.";
184 }
185 },
186 499 ▶ operand_finder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
187 499 return $_[0]->descending_operands('all_vars');
188 },
189 28 );
190 }
191  
192 =head2 Method replace
193  
194 First argument must be a valid Math::Symbolic tree.
195  
196 replace() modifies the object it is called on in-place in that it
197 replaces it with its first argument. Doing that, it retains the original
198 object reference. This destroys the object it is called on.
199  
200 However, this also means that you can create recursive trees of objects if
201 the new tree is to contain the old tree. So make sure you clone the old tree
202 using the new() method before using it in the replacement tree or you will
203 end up with a program that eats your memory fast.
204  
205 =cut
206  
207 12836 ▶sub replace {
Subroutine replace: called CC=1 SCAR=0.0
Pod documented
208 12836 my $tree = shift;
209 12836 my $new = shift;
210 12836 %$tree = %$new;
211 12836 bless $tree => ref $new;
212 12836 return $tree;
213 }
214  
215 =head2 fill_in_vars
216  
217 This method returns a modified copy of the tree it was called on.
218  
219 It walks the tree and replaces all variables whose value attribute is
220 defined (either done at the time of object creation or using set_value())
221 with the corresponding constant objects. Variables whose value is
222 not defined are unaffected. Take, for example, the following code:
223  
224 $tree = parse_from_string('a*b+a*c');
225 $tree->set_value(a => 4, c => 10); # value of b still not defined.
226 print $tree->fill_in_vars();
227 # prints "(4 * b) + (4 * 10)"
228  
229 =cut
230  
231 1 ▶sub fill_in_vars {
Subroutine fill_in_vars: called CC=1 SCAR=0.0
Pod documented
232 1 my $self = shift;
233 return $self->descend(
234 in_place => 0,
235 3 ▶ before => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
236 3 my $term = shift;
237 3 ▶ if ( $term->term_type() == T_VARIABLE and defined $term->{value} )
Branch: 1 branch
BranchTrueFalseTotal
if ($term->term_type == 2 and defined $term->{"value"}) 1 2 3
Condition100%3/3
$term->term_type == 2 and defined $term->{"value"}
!ll&&!rl&&r
111
MC/DC100%2/2
$term->term_type == 2 and defined $term->{"value"}
$term->term_type == 2 defined $term->{"value"}
Truth table100%3/3
A and B
A= $term->term_type == 2B= defined $term->{"value"}
ABresult
0X0
100
111
238 {
239 $term->replace(
240 1 Math::Symbolic::Constant->new( $term->{value} ) );
241 }
242 3 return ();
243 },
244 1 );
245 }
246  
247 =head2 Method simplify
248  
249 Minimum method for term simpilification just clones.
250  
251 =cut
252  
253 1142 ▶sub simplify {
Subroutine simplify: called CC=1 SCAR=0.0
Pod documented
254 1142 my $self = shift;
255 1142 return $self->new();
256 }
257  
258 =head2 Method descending_operands
259  
260 When called on an operator, descending_operands tries hard to determine
261 which operands to descend into. (Which usually means all operands.)
262 A list of these is returned.
263  
264 When called on a constant or a variable, it returns the empty list.
265  
266 Of course, some routines may have to descend into different branches of the
267 Math::Symbolic tree, but this routine returns the default operands.
268  
269 The first argument to this method may control its behaviour. If it is any of
270 the following key-words, behaviour is modified accordingly:
271  
272 default -- obvious. Use default heuristics.
273
274 These are all supersets of 'default':
275 all -- returns ALL operands. Use with caution.
276 all_vars -- returns all operands that may contain vars.
277  
278 =cut
279  
280 8945 ▶sub descending_operands {
Subroutine descending_operands: called CC=9 SCAR=24.4
Pod documented
281 8945 my $tree = shift;
282 8945 my $ttype = $tree->term_type();
283  
284 8945 ▶ if ( $ttype == T_CONSTANT or $ttype == T_VARIABLE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ttype == 1 or $ttype == 2) { } 0 8945 8945
elsif ($ttype == 0) { } 8945 0 8945
Condition33%1/3
$ttype == 1 or $ttype == 2
l!l&&r!l&&!r
008945
MC/DC0%0/2
$ttype == 1 or $ttype == 2
$ttype == 1 $ttype == 2
Truth table33%1/3
A or B
A= $ttype == 1B= $ttype == 2
ABresult
1X1
011
000
285 0 return ();
286 }
287 elsif ( $ttype == T_OPERATOR ) {
288 8945 ▶ my $action = shift || 'default';
Condition100%2/2
shift() || 'default'
l!l
15447401
MC/DC100%1/1
shift() || 'default'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
289 8945 my $type = $tree->type();
290  
291 8945 ▶ if ( $action eq 'all' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'all') { } 0 8945 8945
elsif ($action eq 'all_vars') { } 1544 7401 8945
292 0 return @{ $tree->{operands} };
293 }
294 elsif ( $action eq 'all_vars' ) {
295 1544 return @{ $tree->{operands} };
296 }
297 else { # default
298 7401 ▶ if ( $type == U_P_DERIVATIVE
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 5 or $type == 6) { } 11 7390 7401
Condition66%2/3
$type == 5 or $type == 6
l!l&&r!l&&!r
0117390
MC/DC50%1/2
$type == 5 or $type == 6
$type == 5 $type == 6
Truth table66%2/3
A or B
A= $type == 5B= $type == 6
ABresult
1X1
011
000
299 or $type == U_T_DERIVATIVE )
300 {
301 11 return $tree->{operands}[0];
302 }
303 else {
304 7390 return @{ $tree->{operands} };
305 }
306 }
307 }
308 else {
309 0 croak "'descending_operands' called on invalid term type.";
310 }
311 0 die "Sanity check in 'descending_operands'. Should not be reached.";
312 }
313  
314 =head2 Method descend
315  
316 The method takes named arguments (key/value pairs).
317 descend() descends (Who would have guessed?) into the Math::Symbolic tree
318 recursively and for each node, it calls code references with a copy of
319 the current node as argument. The copy may be modified and will be used for
320 construction of the returned tree. The automatic copying behaviour may be
321 turned off.
322  
323 Returns a (modified) copy of the original tree. If in-place modification is
324 turned on, the returned tree will not be a copy.
325  
326 Available parameters are:
327  
328 =over 2
329  
330 =item before
331  
332 A code reference to be used as a callback that will be invoked before descent.
333 Depending on whether or not the "in_place" option is set, the callback will
334 be passed a copy of the current node (default) or the original node itself.
335  
336 The callback may modify the tree node and the modified node will be used to
337 construct descend()'s return value.
338  
339 The return value of this callback describes the way descend() handles the
340 descent into the current node's operands.
341  
342 If it returns the empty list, the (possibly modified) copy of the current
343 that was passed to the callback is used as the return value of descend(),
344 but the recursive descent is continued for all of the current node's operands
345 which may or may not be modified by the callback. The "after" callback will
346 be called on the node after descent into the operands. (This is the
347 normal behavior.)
348  
349 If the callback returns undef, the descent is stopped for the current branch
350 and an exact copy of the current branch's children will be used for
351 descend()'s return value. The "after" callback will be called immediately.
352  
353 If the callback returns a list of integers, these numbers are assumed to
354 be the indexes of the current node's operands that are to be descended into.
355 That means if the callback returns (1), descend will be called for the
356 second operand and only the second. All other children/operands will be cloned.
357 As usual, the "after" callback will be called after descent.
358  
359 Any other return lists will lead to hard-to-debug errors. Tough luck.
360  
361 Returning a hash reference from the callback allows for complete control
362 over the descend() routine. The hash may contain the following elements:
363  
364 =over 2
365  
366 =item operands
367  
368 This is a referenced array that will be put in place of the previous
369 operands. It is the callback's job to make sure the number of operands stays
370 correct. The "operands" entry is evaluated I<before> the "descend_into"
371 entry.
372  
373 =item descend_into
374  
375 This is a referenced array of integers and references. The integers are
376 assumed to be indices of the array of operands. Returning (1) results in
377 descent into the second operand and only the second.
378  
379 References are assumed to be operands to descend into. descend() will be
380 directly called on them.
381  
382 If the array is empty, descend() will act just as if
383 an empty list had been returned.
384  
385 =item in_place
386  
387 Boolean indicating whether or not to modify the operands in-place or not.
388 If this is true, descend() will be called with the "in_place => 1" parameter.
389 If false, it will be called with "in_place => 0" instead.
390 Defaults to false. (Cloning)
391  
392 This does not affect the call to the "after" callback but only the descent
393 into operands.
394  
395 =item skip_after
396  
397 If this option exists and is set to true, the "after" callback will not be
398 invoked. This only applies to the current node, not to its children/operands.
399  
400 =back
401  
402 The list of options may grow in future versions.
403  
404 =item after
405  
406 This is a code reference which will be invoked as a callback after the descent
407 into the operands.
408  
409 =item in_place
410  
411 Controls whether or not to modify the current tree node in-place. Defaults to
412 false - cloning.
413  
414 =item operand_finder
415  
416 This option controls how the descend routine chooses which operands to
417 recurse into by default. That means it controls which operands descend()
418 recurses into if the 'before' routine returned the empty list or if
419 no 'before' routine was specified.
420  
421 The option may either be a code reference or a string. If it is a code
422 reference, this code reference will be called with the current node as
423 argument. If it is a string, the method with that name will be called
424 on the current node object.
425  
426 By default, descend() calls the 'descending_operands()' method on the current
427 node to determine the operands to descend into.
428  
429 =back
430  
431 =cut
432  
433 13812 ▶sub descend {
Subroutine descend: called CC=30 SCAR=40.2
Pod documented
434 13812 my ( $tree, %args ) = @_;
435 $tree = $tree->new()
436 unless exists $args{in_place}
437 13812 ▶ and $args{in_place};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"in_place"} and $args{"in_place"} 1531 12281 13812
Condition66%2/3
exists $args{"in_place"} and $args{"in_place"}
!ll&&!rl&&r
0153112281
MC/DC50%1/2
exists $args{"in_place"} and $args{"in_place"}
exists $args{"in_place"} $args{"in_place"}
Truth table66%2/3
A and B
A= exists $args{"in_place"}B= $args{"in_place"}
ABresult
0X0
100
111
438  
439 13812 my @opt;
440  
441 # Will be used at several locations inside this routine.
442 7900 ▶ my $operand_finder = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
443 7900 ▶ if ( exists $args{operand_finder} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'operand_finder'}) { } 499 7401 7900
444 499 my $op_f = $args{operand_finder};
445 499 ▶ return $tree->$op_f() if not ref $op_f;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $op_f 0 499 499
446 499 ▶ croak "Invalid 'operand_finder' option passed to "
Branch: 1 branch
BranchTrueFalseTotal
unless ref $op_f eq "CODE" 0 499 499
447 . "descend() routine."
448 if not ref($op_f) eq 'CODE';
449 499 return $op_f->($tree);
450 }
451 else {
452 7401 return $tree->descending_operands();
453 }
454 13812 };
455  
456 13812 ▶ if ( exists $args{before} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"before"}) 12992 820 13812
457 croak "'before' parameter to descend() must be code reference."
458 12992 ▶ unless ref( $args{before} ) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"before"} eq "CODE" 0 12992 12992
459 12992 @opt = $args{before}->($tree);
460 }
461 13812 ▶ if ( exists $args{after} and ref( $args{after} ) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"after"} and ref $args{"after"} ne "CODE") 0 13812 13812
Condition66%2/3
exists $args{"after"} and ref $args{"after"} ne "CODE"
!ll&&!rl&&r
129928200
MC/DC0%0/2
exists $args{"after"} and ref $args{"after"} ne "CODE"
exists $args{"after"} ref $args{"after"} ne "CODE"
Truth table66%2/3
A and B
A= exists $args{"after"}B= ref $args{"after"} ne "CODE"
ABresult
0X0
100
111
462 0 croak "'after' parameter to descend() must be code reference.";
463 }
464  
465 13812 ▶ my $has_control = ( @opt == 1 && ref( $opt[0] ) eq 'HASH' ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@opt == 1 && ref $opt[0] eq 'HASH' ? : 35 13777 13812
Condition100%3/3
@opt == 1 and ref $opt[0] eq "HASH"
!ll&&!rl&&r
8216556135
MC/DC100%2/2
@opt == 1 and ref $opt[0] eq "HASH"
@opt == 1 ref $opt[0] eq "HASH"
Truth table100%3/3
A and B
A= @opt == 1B= ref $opt[0] eq "HASH"
ABresult
0X0
100
111
466  
467 13812 my $ttype = $tree->term_type();
468  
469 # Do nothing!
470 13812 ▶ if ( $ttype != T_OPERATOR ) { }
Branch: 5 branches
BranchTrueFalseTotal
if ($ttype != 0) { } 5870 7942 13812
elsif ($has_control) { } 35 7907 7942
elsif (@opt == 0) { } 7897 10 7907
elsif (@opt == 1 and not defined $opt[0]) { } 10 0 10
elsif (@opt >= 1 and not grep({not $_ =~ /^[+-]?\d+$/;} @opt)) { } 0 0 0
471  
472 # Fine control!
473 elsif ($has_control) {
474 35 my $opt = $opt[0];
475 35 my %new_args = %args;
476 $new_args{in_place} = $opt->{in_place}
477 35 ▶ if exists $opt->{in_place};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"in_place"} 3 32 35
478  
479 35 ▶ if ( exists $opt->{operands} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"operands"}) 0 35 35
480 croak "'operands' return value of 'begin' callback\n"
481 . "in descend() must be array reference."
482 0 ▶ unless ref( $opt->{operands} ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt->{"operands"} eq "ARRAY" 0 0 0
483  
484 0 $tree->{operands} = $opt->{operands};
485 }
486  
487 35 ▶ if ( exists $opt->{descend_into} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"descend_into"}) 35 0 35
488 croak "'descend_into' return value of 'begin'\n"
489 . "callback in descend() must be array reference."
490 35 ▶ unless ref( $opt->{descend_into} ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt->{"descend_into"} eq "ARRAY" 0 35 35
491  
492 $opt->{descend_into} = [ $operand_finder->() ]
493 35 ▶ if @{ $opt->{descend_into} } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$opt->{"descend_into"};} == 0 3 32 35
494  
495 35 foreach ( @{ $opt->{descend_into} } ) {
496 68 ▶ if ( ref $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 68 0 68
497 68 $_->replace( $_->descend(%new_args) );
498 }
499 else {
500 $tree->{operands}[$_] =
501 0 $tree->{operands}[$_]->descend(%new_args);
502 }
503 }
504 }
505 }
506  
507 # descend into all operands.
508 elsif ( @opt == 0 ) {
509 7897 foreach ( $operand_finder->() ) {
510 12506 $_->replace( $_->descend(%args) );
511 }
512 }
513  
514 # Do nothing.
515 ▶ elsif ( @opt == 1 and not defined( $opt[0] ) ) {
Condition33%1/3
@opt == 1 and not defined $opt[0]
!ll&&!rl&&r
0010
MC/DC0%0/2
@opt == 1 and not defined $opt[0]
@opt == 1 not defined $opt[0]
Truth table33%1/3
A and not(B)
A= @opt == 1B= not defined $opt[0]
ABresult
0X0
100
111
516 }
517  
518 # Descend into indexed operands
519 0 ▶ elsif ( @opt >= 1 and not grep { $_ !~ /^[+-]?\d+$/ } @opt ) {
Condition0%0/3
@opt >= 1 and not grep({not $_ =~ /^[+-]?\d+$/;} @opt)
!ll&&!rl&&r
000
MC/DC0%0/2
@opt >= 1 and not grep({not $_ =~ /^[+-]?\d+$/;} @opt)
@opt >= 1 not grep({not $_ =~ /^[+-]?\d+$/;} @opt)
Truth table0%0/3
A and not(B)
A= @opt >= 1B= not grep({not $_ =~ /^[+-]?\d+$/;} @opt)
ABresult
0X0
100
111
520 0 foreach (@opt) {
521 0 $tree->{operands}[$_] = $tree->{operands}[$_]->descend(%args);
522 }
523 }
524  
525 # Error!
526 else {
527 0 croak "Invalid return list from descend() 'before' callback.";
528 }
529  
530 # skip the after callback?
531 13812 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"after"} and not $has_control && exists $opt[0]{"skip_after"} && $opt[0]{"skip_after"}) 820 12992 13812
Condition44%4/9
exists $args{"after"} and not $has_control && exists $opt[0]{"skip_after"} && $opt[0]{"skip_after"}
!ll&&!rl&&r
129920820
$has_control && exists $opt[0]{'skip_after'} && $opt[0]{'skip_after'}
!ll&&!rl&&r
82000
$has_control and exists $opt[0]{"skip_after"}
!ll&&!rl&&r
82000
MC/DC25%1/4
exists $args{"after"} and not $has_control && exists $opt[0]{"skip_after"} && $opt[0]{"skip_after"}
exists $args{"after"} $has_control exists $opt[0]{"skip_after"} $opt[0]{'skip_after'}
Truth table40%2/5
A and not(B and C && D)
A= exists $args{"after"}B= $has_controlC= exists $opt[0]{"skip_after"}D= $opt[0]{'skip_after'}
ABCDresult
0XXX0
11110
10XX1
110X1
11101
532 exists $args{after}
533 and not($has_control
534 and exists $opt[0]{skip_after}
535 and $opt[0]{skip_after} )
536 )
537 {
538 820 $args{after}->($tree);
539 }
540  
541 13812 return $tree;
542 }
543  
544 =head2 Method term_type
545  
546 Returns the type of the term. This is a stub to be overridden.
547  
548 =cut
549  
550 0 ▶sub term_type {
Subroutine term_type: not called CC=1 SCAR=6.9
Pod documented
551 0 croak "term_type not defined for " . __PACKAGE__;
552 }
553  
554 =head2 Method set_value
555  
556 set_value() returns the tree it modifies, but acts in-place on the
557 Math::Symbolic tree it was called on.
558  
559 set_value() requires named arguments (key/value pairs) that associate
560 variable names of variables in the tree with the value-arguments if the
561 corresponging key matches the variable name.
562 (Can one say this any more complicated?) Since version 0.132, an alternative
563 syntax is to pass a single hash reference to the method.
564  
565 Example: $tree->set_value(x => 1, y => 2, z => 3, t => 0) assigns the value 1
566 to any occurrances of variables of the name "x", aso.
567  
568 As opposed to value(), set_value() assigns to the variables I<permanently>
569 and does not evaluate the tree.
570  
571 When called on constants, set_value() sets their value to its first
572 argument, but only if there is only one argument.
573  
574 =cut
575  
576 2 ▶sub set_value {
Subroutine set_value: called CC=7 SCAR=22.2
Pod documented
577 2 my ( $self, %args );
578 2 ▶ if ( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 2 2
elsif (@_ == 2) { } 1 1 2
579 0 return();
580 }
581 elsif ( @_ == 2 ) {
582 1 $self = shift;
583 1 ▶ croak "Invalid arguments to method set_value()"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 1 1
584 unless ref $_[0] eq 'HASH';
585 1 %args = %{ $_[0] };
586 }
587 else {
588 1 ( $self, %args ) = @_;
589 }
590  
591 2 my $ttype = $self->term_type();
592 2 ▶ if ( $ttype == T_CONSTANT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttype == 1) 0 2 2
593 0 ▶ return $self unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
594 0 my $value = $_[1];
595 0 ▶ $self->{value} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
596 0 return $self;
597 }
598  
599 $self->descend(
600 in_place => 1,
601 10 ▶ after => sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
602 10 my $tree = shift;
603 10 my $ttype = $tree->term_type();
604 10 ▶ if ( $ttype == T_OPERATOR or $ttype == T_CONSTANT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ttype == 0 or $ttype == 1) { } 7 3 10
elsif ($ttype == 2) { } 3 0 3
Condition100%3/3
$ttype == 0 or $ttype == 1
l!l&&r!l&&!r
433
MC/DC100%2/2
$ttype == 0 or $ttype == 1
$ttype == 0 $ttype == 1
Truth table100%3/3
A or B
A= $ttype == 0B= $ttype == 1
ABresult
1X1
011
000
605 }
606 elsif ( $ttype == T_VARIABLE ) {
607 $tree->{value} = $args{ $tree->{name} }
608 3 ▶ if exists $args{ $tree->{name} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$tree->{"name"}} 3 0 3
609 }
610 else {
611 0 croak "'set_value' called on invalid term " . "type.";
612 }
613 },
614 2 );
615  
616 2 return $self;
617 }
618  
619 =begin comment
620  
621 Since version 0.102, there are several overloaded operators. The overloaded
622 interface is documented below. For more info, please have a look at the
623 Math::Symbolic man page.
624  
625 =end comment
626  
627 =cut
628  
629 399 ▶sub _overload_make_object {
Subroutine _overload_make_object: called CC=4 SCAR=13.9
630 399 my $operand = shift;
631 399 ▶ unless ( ref($operand) =~ /^Math::Symbolic/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($operand) =~ /^Math::Symbolic/) 20 379 399
632 20 ▶ if ( not defined $operand ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $operand) { } 1 19 20
elsif (not $operand =~ /^\s*\d+\s*$/) { } 5 14 19
633 1 return $operand;
634 }
635 elsif ( $operand !~ /^\s*\d+\s*$/ ) {
636 5 $operand = Math::Symbolic::parse_from_string($operand);
637 }
638 else {
639 14 $operand = Math::Symbolic::Constant->new($operand);
640 }
641 }
642 398 return $operand;
643 }
644  
645 87 ▶sub _overload_addition {
Subroutine _overload_addition: called CC=4 SCAR=14.0
646 87 my ( $obj, $operand, $reverse ) = @_;
647 87 $operand = _overload_make_object($operand);
648 87 ▶ return $obj if not defined $operand and $reverse;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $operand and $reverse 1 86 87
Condition66%2/3
not defined $operand and $reverse
!ll&&!rl&&r
8601
MC/DC50%1/2
not defined $operand and $reverse
not defined $operand $reverse
Truth table66%2/3
not(A) and B
A= not defined $operandB= $reverse
ABresult
0X0
100
111
649 86 ▶ ( $obj, $operand ) = ( $operand, $obj ) if $reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $reverse 0 86 86
650 86 my $n_obj = Math::Symbolic::Operator->new( '+', $obj, $operand );
651 86 return $n_obj;
652 }
653  
654 38 ▶sub _overload_subtraction {
Subroutine _overload_subtraction: called CC=4 SCAR=14.3
655 38 my ( $obj, $operand, $reverse ) = @_;
656 38 $operand = _overload_make_object($operand);
657 38 ▶ return Math::Symbolic::Operator->new( 'neg', $obj )
Branch: 1 branch
BranchTrueFalseTotal
if not defined $operand and $reverse 0 38 38
Condition33%1/3
not defined $operand and $reverse
!ll&&!rl&&r
3800
MC/DC0%0/2
not defined $operand and $reverse
not defined $operand $reverse
Truth table33%1/3
not(A) and B
A= not defined $operandB= $reverse
ABresult
0X0
100
111
658 if not defined $operand
659 and $reverse;
660 38 ▶ ( $obj, $operand ) = ( $operand, $obj ) if $reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $reverse 1 37 38
661 38 my $n_obj = Math::Symbolic::Operator->new( '-', $obj, $operand );
662 38 return $n_obj;
663 }
664  
665 258 ▶sub _overload_multiplication {
Subroutine _overload_multiplication: called CC=2 SCAR=7.0
666 258 my ( $obj, $operand, $reverse ) = @_;
667 258 $operand = _overload_make_object($operand);
668 258 ▶ ( $obj, $operand ) = ( $operand, $obj ) if $reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $reverse 0 258 258
669 258 my $n_obj = Math::Symbolic::Operator->new( '*', $obj, $operand );
670 258 return $n_obj;
671 }
672  
673 12 ▶sub _overload_division {
Subroutine _overload_division: called CC=2 SCAR=7.0
674 12 my ( $obj, $operand, $reverse ) = @_;
675 12 $operand = _overload_make_object($operand);
676 12 ▶ ( $obj, $operand ) = ( $operand, $obj ) if $reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $reverse 0 12 12
677 12 my $n_obj = Math::Symbolic::Operator->new( '/', $obj, $operand );
678 12 return $n_obj;
679 }
680  
681 4 ▶sub _overload_exponentiation {
Subroutine _overload_exponentiation: called CC=2 SCAR=6.9
682 4 my ( $obj, $operand, $reverse ) = @_;
683 4 $operand = _overload_make_object($operand);
684 4 ▶ ( $obj, $operand ) = ( $operand, $obj ) if $reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $reverse 1 3 4
685 4 my $n_obj = Math::Symbolic::Operator->new( '^', $obj, $operand );
686 4 return $n_obj;
687 }
688  
689 1 ▶sub _overload_sqrt {
Subroutine _overload_sqrt: called CC=1 SCAR=0.0
690 1 my ( $obj, undef, $reverse ) = @_;
691 1 my $n_obj =
692 Math::Symbolic::Operator->new( '^', $obj,
693 Math::Symbolic::Constant->new(0.5) );
694 1 return $n_obj;
695 }
696  
697 1 ▶sub _overload_exp {
Subroutine _overload_exp: called CC=1 SCAR=0.0
698 1 my ( $obj, undef, $reverse ) = @_;
699 1 my $n_obj =
700 Math::Symbolic::Operator->new( '^', Math::Symbolic::Constant->euler(),
701 $obj, );
702 1 return $n_obj;
703 }
704  
705 1 ▶sub _overload_log {
Subroutine _overload_log: called CC=1 SCAR=0.0
706 1 my ( $obj, undef, $reverse ) = @_;
707 1 my $n_obj =
708 Math::Symbolic::Operator->new( 'log', Math::Symbolic::Constant->euler(),
709 $obj, );
710 1 return $n_obj;
711 }
712  
713 2 ▶sub _overload_sin {
Subroutine _overload_sin: called CC=1 SCAR=0.0
714 2 my ( $obj, undef, $reverse ) = @_;
715 2 my $n_obj = Math::Symbolic::Operator->new( 'sin', $obj );
716 2 return $n_obj;
717 }
718  
719 1 ▶sub _overload_cos {
Subroutine _overload_cos: called CC=1 SCAR=0.0
720 1 my ( $obj, undef, $reverse ) = @_;
721 1 my $n_obj = Math::Symbolic::Operator->new( 'cos', $obj );
722 1 return $n_obj;
723 }
724  
725 =begin comment
726  
727 The following AUTOLOAD mechanism delegates all method calls that aren't found
728 in the normal Math::Symbolic inheritance tree and that start with
729 'is_', 'test_', 'contains_', 'apply_', 'mod_', or 'to_' to the
730 Math::Symbolic::Custom class.
731  
732 The 'is_' and 'test_' "namespaces" are intended for methods that test a
733 tree on whether or not it has certain characteristics that define a group.
734 Eg.: 'is_polynomial'
735  
736 The 'contains_' prefix is intended for tests as well.
737  
738 The 'apply_' and 'mod_' prefixes are intended for modifications to the tree
739 itself. Eg.: 'apply_derivatives'
740  
741 The 'to_' prefix is intended for output / conversion related routines.
742  
743 =end comment
744  
745 =cut
746  
747 2510 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=13.7
748 2510 my $call = $AUTOLOAD;
749 2510 $call =~ s/.*\:\:(\w+)$/$1/;
750 2510 ▶ if ( $call =~ /^((?:apply|mod|is|test|contains|to)_\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call =~ /^((?:apply|mod|is|test|contains|to)_\w+)/) { } 2510 0 2510
751 2510 my $method = $1;
752 2510 my $ref = Math::Symbolic::Custom->can($method);
753 2510 ▶ if ( defined $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) { } 2510 0 2510
754 2510 goto &$ref;
755 }
756 else {
757 0 my $obj = $_[0];
758 0 my $class = ref $obj;
759 0 croak "Invalid method '$call' called on Math::Symbolic "
760 ."tree. Tree was of type '$class'";
761 }
762 }
763 else {
764 0 my $obj = $_[0];
765 0 my $class = ref $obj;
766 0 croak "Invalid method '$call' called on Math::Symbolic "
767 ."tree. Tree was of type '$class'";
768 }
769 }
770  
771 =begin comment
772  
773 We override the UNIVERSAL::can routine to reflect method delegations.
774  
775 =end comment
776  
777 =cut
778  
779 9 ▶sub can {
Subroutine can: called CC=2 SCAR=6.9
Pod undocumented
780 9 my $obj = shift;
781 9 my $method = shift;
782  
783 9 my $sub = $obj->SUPER::can($method);
784 9 ▶ return $sub if defined $sub;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sub 3 6 9
785  
786 6 return Math::Symbolic::Custom->can($method);
787 }
788  
789 # to make AUTOLOAD happy: (because it would otherwise try to delegate DESTROY)
790 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
791  
792 23 1;
793 __END__
« Symbolic/Custom/DefaultMods.pm ↑ Summary Symbolic/Derivative.pm »