Symbolic/Custom/DefaultMods.pm

filter: Statementstmt 92.6% 113 / 122 Branchbran 74.4% 70 / 94 Conditioncond 71.0% 27 / 38 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 80.4% 238 / 296 CC 61 SCAR 30.0
CC
61
Cov
83%
mod_multiply_constant
30.3
mod_add_constant
25.1
__ANON__
13.8
CRAP 20.1
« Symbolic/Custom/DefaultTests.pm ↑ Summary Symbolic/Base.pm »
1  
2 =encoding utf8
3  
4 =head1 NAME
5  
6 Math::Symbolic::Custom::DefaultMods - Default Math::Symbolic transformations
7  
8 =head1 SYNOPSIS
9  
10 use Math::Symbolic;
11  
12 =head1 DESCRIPTION
13  
14 This is a class of default transformations for Math::Symbolic trees. Likewise,
15 Math::Symbolic::Custom::DefaultTests defines default tree testing
16 routines.
17 For details on how the custom method delegation model works, please have
18 a look at the Math::Symbolic::Custom and Math::Symbolic::Custom::Base
19 classes.
20  
21 =head2 EXPORT
22  
23 Please see the docs for Math::Symbolic::Custom::Base for details, but
24 you should not try to use the standard Exporter semantics with this
25 class.
26  
27 =head1 SUBROUTINES
28  
29 =cut
30  
31 package Math::Symbolic::Custom::DefaultMods;
32  
33 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 23 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 23 our $VERSION = '0.613';
39  
40 23 ▶use Math::Symbolic::Custom::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 23 ▶BEGIN { *import = \&Math::Symbolic::Custom::Base::aggregate_import }
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 23 ▶use Math::Symbolic::ExportConstants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # Class Data: Special variable required by Math::Symbolic::Custom
48 # importing/exporting functionality.
49 # All subroutines that are to be exported to the Math::Symbolic::Custom
50 # namespace should be listed here.
51  
52 23 our $Aggregate_Export = [
53 qw/
54 apply_derivatives
55 apply_constant_fold
56 mod_add_constant
57 mod_multiply_constant
58 /
59 ];
60  
61 =head2 apply_derivatives()
62  
63 Never modifies the tree in-place, but returns a modified copy of the
64 original tree instead.
65  
66 Applied to variables and constants, this method just clones.
67  
68 Applied to operators and if the operator is a derivative, this applies
69 the derivative to the derivative's first operand.
70  
71 Regardless what kind of operator this is called on, apply_derivatives
72 will be applied recursively on its operands.
73  
74 If the first parameter to this function is an integer, at maximum that
75 number of derivatives are applied (from top down the tree if possible).
76  
77 =cut
78  
79 93 ▶sub apply_derivatives {
Subroutine apply_derivatives: called CC=2 SCAR=7.0
Pod documented
80 93 my $tree = shift;
81 93 ▶ my $n = shift || -1;
Condition50%1/2
shift() || -1
l!l
093
MC/DC0%0/1
shift() || -1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
82  
83 return $tree->descend(
84 in_place => 0,
85 1523 ▶ before => sub {
Subroutine __ANON__: called CC=13 SCAR=26.3
86 1523 my $tree = shift;
87 1523 my $ttype = $tree->term_type();
88 1523 ▶ if ( $ttype == T_CONSTANT || $ttype == T_VARIABLE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ttype == 1 or $ttype == 2) { } 695 828 1523
elsif ($ttype == 0) { } 828 0 828
Condition100%3/3
$ttype == 1 or $ttype == 2
l!l&&r!l&&!r
464231828
MC/DC100%2/2
$ttype == 1 or $ttype == 2
$ttype == 1 $ttype == 2
Truth table100%3/3
A or B
A= $ttype == 1B= $ttype == 2
ABresult
1X1
011
000
89 695 return undef;
90 }
91 elsif ( $ttype == T_OPERATOR ) {
92 828 my $max_derivatives = $n;
93 828 my $type = $tree->type();
94  
95 828 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($n and $type == 5 || $type == 6) 74 823 897
Condition83%5/6
$n and $type == 5 || $type == 6
!ll&&!rl&&r
082374
$type == 5 or $type == 6
l!l&&r!l&&!r
4232823
MC/DC66%2/3
$n and $type == 5 || $type == 6
$n $type == 5 $type == 6
Truth table75%3/4
A and B or C
A= $nB= $type == 5C= $type == 6
ABCresult
0XX0
1000
11X1
1011
96 $n
97 && ( $type == U_P_DERIVATIVE
98 or $type == U_T_DERIVATIVE )
99 )
100 {
101 74 my $op = $Math::Symbolic::Operator::Op_Types[$type];
102  
103 74 my $operands = $tree->{operands};
104 74 my $application = $op->{application};
105  
106 74 ▶ if ( $type == U_T_DERIVATIVE
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 6 and $operands->[0]->term_type == 2) 4 70 74
Condition100%3/3
$type == 6 and $operands->[0]->term_type == 2
!ll&&!rl&&r
42284
MC/DC100%2/2
$type == 6 and $operands->[0]->term_type == 2
$type == 6 $operands->[0]->term_type == 2
Truth table100%3/3
A and B
A= $type == 6B= $operands->[0]->term_type == 2
ABresult
0X0
100
111
107 and $operands->[0]->term_type() == T_VARIABLE )
108 {
109 4 my @sig = $operands->[0]->signature();
110  
111 4 my $name = $operands->[1]->name();
112  
113 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (grep({$_ eq $name;} @sig) > 0 and not @sig == 1 && $sig[0] eq $name) 4 0 4
Condition33%1/3
grep({$_ eq $name;} @sig) > 0 and not @sig == 1 && $sig[0] eq $name
!ll&&!rl&&r
004
MC/DC0%0/2
grep({$_ eq $name;} @sig) > 0 and not @sig == 1 && $sig[0] eq $name
grep({$_ eq $name;} @sig) > 0 not @sig == 1 && $sig[0] eq $name
Truth table33%1/3
A and not(B)
A= grep({$_ eq $name;} @sig) > 0B= not @sig == 1 && $sig[0] eq $name
ABresult
0X0
100
111
114 8 ▶ ( grep { $_ eq $name } @sig ) > 0
Condition33%1/3
@sig == 1 && $sig[0] eq $name
!ll&&!rl&&r
400
MC/DC0%0/2
@sig == 1 && $sig[0] eq $name
@sig == 1 $sig[0] eq $name
Truth table33%1/3
A && B
A= @sig == 1B= $sig[0] eq $name
ABresult
0X0
100
111
115 and not(@sig == 1
116 and $sig[0] eq $name )
117 )
118 {
119 4 return undef;
120 }
121 }
122 70 $tree->replace( $application->(@$operands) );
123 return undef
124 70 ▶ unless $tree->term_type() == T_OPERATOR;
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->term_type == 0 1 69 70
125  
126 69 $type = $tree->type();
127 69 $n--;
128 }
129 823 return ();
130 }
131 else {
132 0 croak "apply_derivatives called on invalid " . "tree type.";
133 }
134  
135 0 die "Sanity check in apply_derivatives() should not "
136 . "be reached.";
137 },
138 93 );
139 }
140  
141 =head2 apply_constant_fold()
142  
143 Does not modify the tree in-place by default, but returns a modified copy
144 of the original tree instead. If the first argument is true, the tree will
145 not be cloned. If it is false or not existant, the tree will be cloned.
146  
147 Applied to variables and constants, this method just clones.
148  
149 Applied to operators, all tree segments that contain constants and
150 operators only will be replaced with Constant objects.
151  
152 =cut
153  
154 5 ▶sub apply_constant_fold {
Subroutine apply_constant_fold: called CC=1 SCAR=0.0
Pod documented
155 5 my $tree = shift;
156 5 my $in_place = shift;
157  
158 return $tree->descend(
159 in_place => $in_place,
160 11 ▶ before => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
161 11 my $tree = shift;
162 11 ▶ if ( $tree->is_simple_constant() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->is_simple_constant) 4 7 11
163 4 ▶ $tree->replace( $tree->apply() )
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->term_type == 1 4 0 4
164 unless $tree->term_type() == T_CONSTANT;
165 4 return undef;
166 }
167  
168 7 ▶ return undef if $tree->term_type() == T_VARIABLE;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->term_type == 2 4 3 7
169 3 return { in_place => 1, descend_into => [] };
170 }
171 5 );
172  
173 0 return $tree;
174 }
175  
176 =head2 mod_add_constant
177  
178 Given a constant (object or number) as argument, this method tries
179 hard to fold it into an existing constant of the object this is called
180 on is already a sum or a difference.
181  
182 Basically, this is the same as C<$tree + $constant> but does some
183 simplification.
184  
185 =cut
186  
187 33 ▶sub mod_add_constant {
Subroutine mod_add_constant: called CC=22 SCAR=32.2
Pod documented
188 33 my $tree = shift;
189 33 my $constant = shift;
190  
191 33 ▶ return $tree if not $constant;
Branch: 1 branch
BranchTrueFalseTotal
unless $constant 1 32 33
192 32 ▶ $constant = $constant->value() if ref($constant);
Branch: 1 branch
BranchTrueFalseTotal
if ref $constant 16 16 32
193
194 32 my $tt = $tree->term_type();
195 32 ▶ if ($tt == T_CONSTANT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tt == 1) { } 0 32 32
elsif ($tt == 0) { } 32 0 32
196 0 return Math::Symbolic::Constant->new($tree->{value}+$constant);
197 }
198 elsif ($tt == T_OPERATOR) {
199 32 my $type = $tree->type();
200  
201 32 ▶ if ($type == B_SUM || $type == B_DIFFERENCE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 0 or $type == 1) 16 16 32
Condition100%3/3
$type == 0 or $type == 1
l!l&&r!l&&!r
15116
MC/DC100%2/2
$type == 0 or $type == 1
$type == 0 $type == 1
Truth table100%3/3
A or B
A= $type == 0B= $type == 1
ABresult
1X1
011
000
202 16 my $ops = $tree->{operands};
203 16 my $const_op;
204 16 ▶ if ($ops->[0]->is_simple_constant()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ops->[0]->is_simple_constant) { } 4 12 16
elsif ($ops->[1]->is_simple_constant) { } 5 7 12
205 4 $const_op = 0;
206 } elsif ($ops->[1]->is_simple_constant()) {
207 5 $const_op = 1;
208 }
209 16 ▶ if (defined $const_op) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $const_op) 9 7 16
210 9 my $value = $ops->[$const_op]->value();
211 9 my $other = $ops->[($const_op+1)%2];
212  
213 9 ▶ if ($const_op == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($const_op == 0) { } 4 5 9
214 4 $value += $constant;
215 }
216 else { # second
217 5 ▶ $value = $type==B_SUM ? $value + $constant : $value - $constant;
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 ? : 5 0 5
218 }
219  
220 9 ▶ if ($value == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == 0) 4 5 9
221 4 ▶ return $other if $const_op == 1 or $type == B_SUM;
Branch: 1 branch
BranchTrueFalseTotal
if $const_op == 1 or $type == 0 4 0 4
Condition66%2/3
$const_op == 1 or $type == 0
l!l&&r!l&&!r
220
MC/DC0%0/2
$const_op == 1 or $type == 0
$const_op == 1 $type == 0
Truth table66%2/3
A or B
A= $const_op == 1B= $type == 0
ABresult
1X1
011
000
222 0 return Math::Symbolic::Constant->new(-$other->{value});
223 }
224 5 ▶ return Math::Symbolic::Operator->new(
Branch: 2 branches
BranchTrueFalseTotal
$type == 1 ? : 0 5 5
$const_op == 0 ? : 2 3 5
225 ($type == B_DIFFERENCE ? '-' : '+'), # op-type
226 $const_op == 0 # order of ops
227 ?($value, $other)
228 :($other, $value)
229 );
230 }
231 7 ▶ if ($ops->[1]->term_type() == T_OPERATOR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ops->[1]->term_type == 0) { } 7 0 7
232 7 my $otype = $ops->[1]->type();
233 7 ▶ if ($otype == B_SUM || $otype == B_DIFFERENCE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otype == 0 or $otype == 1) 4 3 7
Condition66%2/3
$otype == 0 or $otype == 1
l!l&&r!l&&!r
403
MC/DC50%1/2
$otype == 0 or $otype == 1
$otype == 0 $otype == 1
Truth table66%2/3
A or B
A= $otype == 0B= $otype == 1
ABresult
1X1
011
000
234 4 ▶ return Math::Symbolic::Operator->new(
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 ? : 4 0 4
235 ($type == B_SUM ? '+' : '-'),
236 $ops->[0],
237 $ops->[1]->mod_add_constant($constant)
238 );
239 }
240 }
241 else {
242 0 ▶ return Math::Symbolic::Operator->new(
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 ? : 0 0 0
243 ($type == B_SUM ? '+' : '-'),
244 $ops->[0]->mod_add_constant($constant),
245 $ops->[1],
246 );
247 }
248 }
249 }
250  
251 # fallback: variable, didn't apply, etc.
252 19 return Math::Symbolic::Operator->new(
253 '+', Math::Symbolic::Constant->new($constant), $tree
254 );
255 }
256  
257  
258 =head2 mod_multiply_constant
259  
260 Given a constant (object or number) as argument, this method tries
261 hard to fold it into an existing constant of the object this is called
262 on is already a product or a division.
263  
264 Basically, this is the same as C<$tree * $constant> but does some
265 simplification.
266  
267 =cut
268  
269 16 ▶sub mod_multiply_constant {
Subroutine mod_multiply_constant: called CC=24 SCAR=34.1
Pod documented
270 16 my $tree = shift;
271 16 my $constant = shift;
272  
273 16 ▶ return $tree if not defined $constant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $constant 0 16 16
274 16 ▶ $constant = $constant->value() if ref($constant);
Branch: 1 branch
BranchTrueFalseTotal
if ref $constant 0 16 16
275 16 ▶ return $tree if $constant == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $constant == 1 1 15 16
276 15 ▶ return Math::Symbolic::Constant->zero() if $constant == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $constant == 0 1 14 15
277
278 14 my $tt = $tree->term_type();
279 14 ▶ if ($tt == T_CONSTANT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tt == 1) { } 0 14 14
elsif ($tt == 0) { } 14 0 14
280 0 return Math::Symbolic::Constant->new($tree->{value}*$constant);
281 }
282 elsif ($tt == T_OPERATOR) {
283 14 my $type = $tree->type();
284  
285 14 ▶ if ($type == B_PRODUCT || $type == B_DIVISION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 2 or $type == 3) 14 0 14
Condition66%2/3
$type == 2 or $type == 3
l!l&&r!l&&!r
1310
MC/DC0%0/2
$type == 2 or $type == 3
$type == 2 $type == 3
Truth table66%2/3
A or B
A= $type == 2B= $type == 3
ABresult
1X1
011
000
286 14 my $ops = $tree->{operands};
287 14 my $const_op;
288 14 ▶ if ($ops->[0]->is_simple_constant()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ops->[0]->is_simple_constant) { } 3 11 14
elsif ($ops->[1]->is_simple_constant) { } 5 6 11
289 3 $const_op = 0;
290 } elsif ($ops->[1]->is_simple_constant()) {
291 5 $const_op = 1;
292 }
293 14 ▶ if (defined $const_op) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $const_op) 8 6 14
294 8 my $value = $ops->[$const_op]->value();
295 8 my $other = $ops->[($const_op+1)%2];
296  
297 8 ▶ if ($const_op == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($const_op == 0) { } 3 5 8
298 3 $value *= $constant;
299 }
300 else { # second
301 5 ▶ $value = $type==B_PRODUCT ? $value * $constant : $value / $constant;
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 5 0 5
302 }
303  
304 8 ▶ if ($value == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == 1) 3 5 8
305 3 ▶ return $other if $const_op == 1 or $type == B_PRODUCT;
Branch: 1 branch
BranchTrueFalseTotal
if $const_op == 1 or $type == 2 3 0 3
Condition66%2/3
$const_op == 1 or $type == 2
l!l&&r!l&&!r
210
MC/DC0%0/2
$const_op == 1 or $type == 2
$const_op == 1 $type == 2
Truth table66%2/3
A or B
A= $const_op == 1B= $type == 2
ABresult
1X1
011
000
306 0 return Math::Symbolic::Constant->new(1/$other->{value});
307 }
308 5 ▶ return Math::Symbolic::Operator->new(
Branch: 2 branches
BranchTrueFalseTotal
$type == 3 ? : 0 5 5
$const_op == 0 ? : 2 3 5
309 ($type == B_DIVISION ? '/' : '*'), # op-type
310 $const_op == 0 # order of ops
311 ?($value, $other)
312 :($other, $value)
313 );
314 }
315 6 ▶ if ($ops->[1]->term_type() == T_OPERATOR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ops->[1]->term_type == 0) { } 6 0 6
316 6 my $otype = $ops->[1]->type();
317 6 ▶ if ($otype == B_PRODUCT || $otype == B_DIVISION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otype == 2 or $otype == 3) 4 2 6
Condition66%2/3
$otype == 2 or $otype == 3
l!l&&r!l&&!r
402
MC/DC50%1/2
$otype == 2 or $otype == 3
$otype == 2 $otype == 3
Truth table66%2/3
A or B
A= $otype == 2B= $otype == 3
ABresult
1X1
011
000
318 4 ▶ return Math::Symbolic::Operator->new(
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 4 0 4
319 ($type == B_PRODUCT ? '*' : '/'),
320 $ops->[0],
321 $ops->[1]->mod_multiply_constant($constant)
322 );
323 }
324 }
325 else {
326 0 ▶ return Math::Symbolic::Operator->new(
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 0 0 0
327 ($type == B_PRODUCT ? '*' : '('),
328 $ops->[0]->mod_multiply_constant($constant),
329 $ops->[1],
330 );
331 }
332 }
333 }
334  
335 # fallback: variable, didn't apply, etc.
336 2 return Math::Symbolic::Operator->new(
337 '*', Math::Symbolic::Constant->new($constant), $tree
338 );
339 }
340  
341 =begin comment
342  
343 warn "mod_join_simple to be implemented in DefaultMods!";
344 sub mod_join_simple {
345 my $o1 = shift;
346 my $o2 = shift;
347 my $type = shift;
348  
349 if ( $type == B_PRODUCT ) {
350 return undef
351 unless Math::Symbolic::Custom::is_identical_base( $o1, $o2 );
352  
353 my $tt1 = $o1->term_type();
354 my $tt2 = $o2->term_type();
355 my ( $base, $exp1 ) =
356 ( $tt1 == T_OPERATOR and $o1->type() == B_EXP )
357 ? ( $o1->op1(), $o1->op2() )
358 : ( $o1, Math::Symbolic::Constant->one() );
359  
360 my $exp2 =
361 ( $tt2 == T_OPERATOR and $o2->type() == B_EXP )
362 ? $o2->op2()
363 : Math::Symbolic::Constant->one();
364  
365 return Math::Symbolic::Operator->new( '^', $base,
366 Math::Symbolic::Operator->new( '+', $exp1, $exp2 )->simplify() );
367 }
368 }
369  
370 =end comment
371  
372 =cut
373  
374 23 1;
375 __END__
« Symbolic/Custom/DefaultTests.pm ↑ Summary Symbolic/Base.pm »