blib/lib/Math/Algebra/Symbols/Sum.pm

filter: Statementstmt 91.7% 753 / 821 Branchbran 70.9% 346 / 488 Conditioncond 51.3% 75 / 146 MC/DCmcdc 30.9% 26 / 84 Subroutinesub 87.1% 88 / 101 Podpod 76.3% 71 / 93 totaltotal 78.4% 1359 / 1733 CC 352 SCAR 34.5
CC
352
Cov
81%
normalizeSqrts
244.6
d
65.2
commonFactor
63.2
CRAP 31.6
« blib/lib/Math/Algebra/Symbols/Symbols.pm ↑ Summary blib/lib/Math/Algebra/Symbols.pm »
1 #!perl -w
2
3 =head1 Sums
4
5 Symbolic Algebra using Pure Perl: sums.
6
7 Operations on sums of terms.
8
9 PhilipRBrenan@yahoo.com, 2004, Perl License.
10 PhilipRBrenan@gmail.com, 2016, Perl License. www.appaapps.com
11
12 =cut
13
14
15 package Math::Algebra::Symbols::Sum;
16 45 $VERSION=1.26;
17 45 ▶use Math::Algebra::Symbols::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 45 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 45 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 #HashUtil use Hash::Util qw(lock_hash);
21 45 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
22
23
24 =head2 Constructors
25
26
27 =head3 new
28
29 Constructor
30
31 =cut
32
33
34 sub new
35 10064 ▶ {bless {t=>{}};
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 }
37
38
39 =head3 newFromString
40
41 New from String
42
43 =cut
44
45
46 sub newFromString($)
47 797 ▶ {my ($a) = @_;
Subroutine newFromString: called CC=2 SCAR=6.9
Pod documented
48 797 ▶ return $zero unless $a;
Branch: 1 branch
BranchTrueFalseTotal
unless $a 96 701 797
49 701 $a .='+';
50 701 my @a = $a =~ /(.+?)[\+\-]/g;
51 701 my @t = map {term($_)} @a;
52 701 sigma(@t);
53 }
54
55
56 =head3 n
57
58 New from Strings
59
60 =cut
61
62
63 sub n(@)
64 66 ▶ {return $zero unless @_;
Subroutine n: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 66 66
65 66 my @a = map {newFromString($_)} @_;
66 66 ▶ return @a if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 48 18 66
67 18 $a[0];
68 }
69
70
71 =head3 sigma
72
73 Create a sum from a list of terms.
74
75 =cut
76
77
78 sub sigma(@)
79 10123 ▶ {return $zero unless scalar(@_);
Subroutine sigma: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 59 10064 10123
80 10064 my $z = new();
81 10064 for my $t(@_)
82 145040 {my $s = $t->signature;
83 145040 ▶ if (exists($z->{t}{$s}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $z->{'t'}{$s}) { } 106060 38980 145040
84 106060 {my $a = $z->{t}{$s}->add($t);
85 106060 ▶ if ($a->c == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($a->c == 0) { } 3625 102435 106060
86 3625 {delete $z->{t}{$s};
87 }
88 else
89 102435 {$z->{t}{$s} = $a;
90 }
91 }
92 else
93 38980 {$z->{t}{$s} = $t
94 }
95 }
96 10064 $z->z;
97 }
98
99
100 =head3 makeInt
101
102 Construct an integer
103
104 =cut
105
106
107 sub makeInt($)
108 13 ▶ {sigma(term()->one->clone->c(shift())->z)
Subroutine makeInt: called CC=1 SCAR=0.0
Pod documented
109 }
110
111
112 =head2 Methods
113
114
115 =head3 isSum
116
117 Confirm type
118
119 =cut
120
121
122 8 ▶sub isSum($) {1};
Subroutine isSum: called CC=1 SCAR=0.0
Pod documented
123
124
125 =head3 t
126
127 Get list of terms from existing sum
128
129 =cut
130
131
132 sub t($)
133 28873 ▶ {my ($a) = @_;
Subroutine t: called CC=1 SCAR=0.0
Pod documented
134 28873 (map {$a->{t}{$_}} sort(keys(%{$a->{t}})));
135 }
136
137
138 =head3 count
139
140 Count terms in sum
141
142 =cut
143
144
145 sub count($)
146 0 ▶ {my ($a) = @_;
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
147 0 scalar(keys(%{$a->{t}}));
148 }
149
150
151 =head3 st
152
153 Get the single term from a sum containing just one term
154
155 =cut
156
157
158 sub st($)
159 4022 ▶ {my ($a) = @_;
Subroutine st: called CC=2 SCAR=6.9
Pod documented
160 4022 ▶ return (values(%{$a->{t}}))[0] if scalar(keys(%{$a->{t}})) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$$a{"t"};} == 1 1546 2476 4022
161 2476 undef;
162 }
163
164
165 =head3 negate
166
167 Multiply each term in a sum by -1
168
169 =cut
170
171
172 sub negate($)
173 828 ▶ {my ($s) = @_;
Subroutine negate: called CC=2 SCAR=6.9
Pod documented
174 828 my @t;
175 828 for my $t($s->t)
176 3430 {push @t, $t->clone->timesInt(-1)->z;
177 }
178 828 sigma(@t);
179 }
180
181
182 =head3 add
183
184 Add two sums together to make a new sum
185
186 =cut
187
188
189 sub add($$)
190 3961 ▶ {my ($a, $b) = @_;
Subroutine add: called CC=1 SCAR=0.0
Pod documented
191 3961 sigma($a->t, $b->t);
192 }
193
194
195 =head3 subtract
196
197 Subtract one sum from another
198
199 =cut
200
201
202 sub subtract($$)
203 529 ▶ {my ($a, $b) = @_;
Subroutine subtract: called CC=2 SCAR=6.9
Pod documented
204 529 ▶ return $b->negate if $a->{id} == $zero->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $a->{"id"} == $zero->{"id"} 75 454 529
205 454 $a->add($b->negate);
206 }
207
208
209 =head3 Conditional Multiply
210
211 Multiply two sums if both sums are defined, otherwise return
212 the defined sum. Assumes that at least one sum is defined.
213
214 =cut
215
216
217 sub multiplyC($$)
218 4664 ▶ {my ($a, $b) = @_;
Subroutine multiplyC: called CC=3 SCAR=11.0
Pod undocumented
219 4664 ▶ return $a unless defined($b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 3917 747 4664
220 747 ▶ return $b unless defined($a);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 564 183 747
221 183 $a->multiply($b);
222 }
223
224
225 =head3 multiply
226
227 Multiply two sums together
228
229 =cut
230
231
232 45 my %M; # Memoize multiplication
233
234 sub multiply($$)
235 2815 ▶ {my ($A, $B) = @_;
Subroutine multiply: called CC=39 SCAR=37.1
Pod documented
236
237 2815 ▶ my $m = $M{$A->{id}}{$B->{id}}; return $m if defined($m);
Branch: 1 branch
BranchTrueFalseTotal
if defined $m 1004 1811 2815
238
239 1811 ▶ return $A if $A->{id} == $zero->{id} or $B->{id} == $one->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $A->{"id"} == $zero->{"id"} or $B->{"id"} == $one->{"id"} 160 1651 1811
Condition100%3/3
$A->{"id"} == $zero->{"id"} or $B->{"id"} == $one->{"id"}
l!l&&r!l&&!r
421181651
MC/DC100%2/2
$A->{"id"} == $zero->{"id"} or $B->{"id"} == $one->{"id"}
$A->{"id"} == $zero->{"id"} $B->{"id"} == $one->{"id"}
Truth table100%3/3
A or B
A= $A->{"id"} == $zero->{"id"}B= $B->{"id"} == $one->{"id"}
ABresult
1X1
011
000
240 1651 ▶ return $B if $B->{id} == $zero->{id} or $A->{id} == $one->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $B->{"id"} == $zero->{"id"} or $A->{"id"} == $one->{"id"} 95 1556 1651
Condition100%3/3
$B->{"id"} == $zero->{"id"} or $A->{"id"} == $one->{"id"}
l!l&&r!l&&!r
33621556
MC/DC100%2/2
$B->{"id"} == $zero->{"id"} or $A->{"id"} == $one->{"id"}
$B->{"id"} == $zero->{"id"} $A->{"id"} == $one->{"id"}
Truth table100%3/3
A or B
A= $B->{"id"} == $zero->{"id"}B= $A->{"id"} == $one->{"id"}
ABresult
1X1
011
000
241
242 1556 my @t;
243
244 # Check for divides that match multiplier
245 1556 my @a = $A->t;
246 1556 for my $a(@a)
247 7231 {my $d = $a->Divide;
248 7231 ▶ next unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 6170 1061 7231
249 1061 ▶ if ($d->{id} == $B->{id})
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{"id"} == $B->{"id"}) 725 336 1061
250 725 {push @t, $a->removeDivide;
251 725 $a = undef;
252 }
253 }
254
255 1556 my @b = $B->t;
256 1556 for my $b(@b)
257 5509 {my $d = $b->Divide;
258 5509 ▶ next unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 5373 136 5509
259 136 ▶ if ($d->{id} == $A->{id})
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{"id"} == $A->{"id"}) 1 135 136
260 1 {push @t, $b->removeDivide;
261 1 $b = undef;
262 }
263 }
264
265 # Simple multiply
266 1556 for my $aa(@a)
267 7231 ▶ {next unless $aa;
Branch: 1 branch
BranchTrueFalseTotal
unless $aa 725 6506 7231
268 6506 for my $bb(@b)
269 109597 ▶ {next unless $bb;
Branch: 1 branch
BranchTrueFalseTotal
unless $bb 1 109596 109597
270 109596 my $m = $aa->multiply($bb);
271 109596 ▶ push (@t, $m), next if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 106405 3191 109596
272
273 # Complicated multiply
274 3191 my %a = $aa->split; my %b = $bb->split;
275 3191 my $a = $a{t}; my $b = $b{t};
276
277 # Sqrt
278 3191 my $s = 0;
279 3191 ▶ $s = $a{s} if $a{s} and $b{s} and $a{s}->{id} == $b{s}->{id}; # Equal sqrts
Branch: 1 branch
BranchTrueFalseTotal
if $a{"s"} and $b{"s"} and $a{"s"}{"id"} == $b{"s"}{"id"} 195 2996 3191
Condition83%5/6
$a{"s"} and $b{"s"} and $a{"s"}{"id"} == $b{"s"}{"id"}
!ll&&!rl&&r
294947195
$a{"s"} and $b{"s"}
!ll&&!rl&&r
29490242
MC/DC66%2/3
$a{"s"} and $b{"s"} and $a{"s"}{"id"} == $b{"s"}{"id"}
$a{"s"} $b{"s"} $a{"s"}{"id"} == $b{"s"}{"id"}
Truth table75%3/4
A and B and C
A= $a{"s"}B= $b{"s"}C= $a{"s"}{"id"} == $b{"s"}{"id"}
ABCresult
0XX0
10X0
1100
1111
280 3191 ▶ $a->Sqrt(multiplyC($a{s}, $b{s})) unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 2996 195 3191
281
282 # Divide
283 3191 ▶ $a->Divide(multiplyC($a{d}, $b{d})) if $a{d} or $b{d};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"d"} or $b{"d"} 1668 1523 3191
Condition100%3/3
$a{"d"} or $b{"d"}
l!l&&r!l&&!r
11045641523
MC/DC100%2/2
$a{"d"} or $b{"d"}
$a{"d"} $b{"d"}
Truth table100%3/3
A or B
A= $a{"d"}B= $b{"d"}
ABresult
1X1
011
000
284
285 # Exp
286 3191 ▶ $a->Exp($a{e} ? $a{e} : $b{e}) if $a{e} xor $b{e};
Branch: 2 branches
BranchTrueFalseTotal
if $a{"e"} xor $b{"e"} 4 3187 3191
$a{'e'} ? : 0 4 4
Condition75%3/4
$a{"e"} xor $b{"e"}
l&&rl&&!r!l&&r!l&&!r
292304264
MC/DC100%2/2
$a{"e"} xor $b{"e"}
$a{"e"} $b{"e"}
Truth table75%3/4
A xor B
A= $a{"e"}B= $b{"e"}
ABresult
110
101
011
000
287 3191 my $e;
288 3191 ▶ if ($a{e} and $b{e})
Branch: 1 branch
BranchTrueFalseTotal
if ($a{"e"} and $b{"e"}) 2923 268 3191
Condition66%2/3
$a{"e"} and $b{"e"}
!ll&&!rl&&r
26802923
MC/DC50%1/2
$a{"e"} and $b{"e"}
$a{"e"} $b{"e"}
Truth table66%2/3
A and B
A= $a{"e"}B= $b{"e"}
ABresult
0X0
100
111
289 2923 {my $s = $a{e}->add($b{e});
290 2923 $e = $s->st; # Check for single term
291 2923 ▶ $e = $e->exp2 if defined($e); # Simplify single term if possible
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 857 2066 2923
292 2923 ▶ $a->Exp($s) unless defined($e); # Reinstate Exp as sum of terms if no simplification possible
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 2821 102 2923
293 }
294 # Log
295 3191 ▶ $a->Log($a{l} ? $a{l} : $b{l}) if $a{l} xor $b{l};
Branch: 2 branches
BranchTrueFalseTotal
if $a{"l"} xor $b{"l"} 0 3191 3191
$a{'l'} ? : 0 0 0
Condition25%1/4
$a{"l"} xor $b{"l"}
l&&rl&&!r!l&&r!l&&!r
0003191
MC/DC0%0/2
$a{"l"} xor $b{"l"}
$a{"l"} $b{"l"}
Truth table25%1/4
A xor B
A= $a{"l"}B= $b{"l"}
ABresult
110
101
011
000
296 3191 ▶ die "Cannot multiply logs yet" if $a{l} and $b{l};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"l"} and $b{"l"} 0 3191 3191
Condition33%1/3
$a{"l"} and $b{"l"}
!ll&&!rl&&r
319100
MC/DC0%0/2
$a{"l"} and $b{"l"}
$a{"l"} $b{"l"}
Truth table33%1/3
A and B
A= $a{"l"}B= $b{"l"}
ABresult
0X0
100
111
297
298 # Combine results
299 3191 $a = $a->z;
300 3191 $b = $b->z;
301 3191 $a = $a->multiply($b);
302 3191 ▶ $a = $a->multiply($e) if defined($e);
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 102 3089 3191
303 3191 ▶ $a or die "Bad multiply";
Branch: 1 branch
BranchTrueFalseTotal
$a or die "Bad multiply" 0 3191 3191
304
305 3191 ▶ push @t, $a unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 2996 195 3191
306 3191 ▶ push @t, sigma($a)->multiply($s)->t if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 195 2996 3191
307 }
308 }
309
310 # Result
311 1556 my $C = sigma(@t);
312 1556 $M{$A->{id}}{$B->{id}} = $C;
313 1556 $C;
314 }
315
316
317 =head3 divide
318
319 Divide one sum by another
320
321 =cut
322
323
324 sub divide($$)
325 351 ▶ {my ($A, $B) = @_;
Subroutine divide: called CC=15 SCAR=27.1
Pod documented
326
327 # Obvious cases
328 351 ▶ $B->{id} == $zero->{id} and croak "Cannot divide by zero";
Branch: 1 branch
BranchTrueFalseTotal
$B->{"id"} == $zero->{"id"} and croak("Cannot divide by zero") 0 351 351
329 351 ▶ return $zero if $A->{id} == $zero->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $A->{"id"} == $zero->{"id"} 0 351 351
330 351 ▶ return $A if $B->{id} == $one->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $B->{"id"} == $one->{"id"} 43 308 351
331 308 ▶ return $A->negate if $B->{id} == $mOne->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $B->{"id"} == $mOne->{"id"} 0 308 308
332
333 # Divide term by term
334 308 my $a = $A->st; my $b = $B->st;
335 308 ▶ if (defined($a) and defined($b))
Branch: 2 branches
BranchTrueFalseTotal
if (defined $a and defined $b) { } 106 202 308
elsif ($b) { } 55 147 202
Condition100%3/3
defined $a and defined $b
!ll&&!rl&&r
15349106
MC/DC100%2/2
defined $a and defined $b
defined $a defined $b
Truth table100%3/3
A and B
A= defined $aB= defined $b
ABresult
0X0
100
111
336 106 {my $c = $a->divide2($b);
337 106 ▶ return sigma($c) if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 93 13 106
338 }
339
340 # Divide sum by term
341 elsif ($b)
342 55 {ST: for(1..1)
343 55 {my @t;
344 55 for my $t($A->t)
345 101 {my $c = $t->divide2($b);
346 101 ▶ last ST unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 16 85 101
347 85 push @t, $c;
348 }
349 39 return sigma(@t);
350 }
351 }
352
353 # Divide sum by sum
354 176 my @t;
355 176 for my $aa($A->t)
356 314 {my $a = $aa->clone;
357 314 my $d = $a->Divide;
358 314 ▶ $a->Divide($d->multiply($B)) if $d;
Branch: 1 branch
BranchTrueFalseTotal
if $d 20 294 314
359 314 ▶ $a->Divide($B) unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 294 20 314
360 314 push @t, $a->z;
361 }
362
363 # Result
364 176 sigma(@t);
365 }
366
367
368 =head3 sub
369
370 Substitute a sum for a variable.
371
372 =cut
373
374
375 sub sub($@)
376 7 ▶ {my $E = shift();
Subroutine sub: called CC=10 SCAR=23.1
Pod documented
377 7 my @R = @_;
378
379 # Each replacement
380 7 ▶ for(;@R > 0;)
Branch: 1 branch
BranchTrueFalseTotal
if (@R > 0) 8 7 15
381 8 {my $s = shift @R; # Replace this variable
382 8 my $w = shift @R; # With this expression
383 8 my $Z = $zero;
384
385 8 ▶ $s =~ /^[a-z]+$/i or croak "Can only substitute an expression for a variable, not $s";
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /^[a-z]+$/i or croak("Can only substitute an expression for a variable, not $s") 0 8 8
386 8 ▶ $w = newFromString($w) unless ref($w);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $w 2 6 8
387 8 $w->isSum;
388
389 # Each term of the sum comprising the replacement expression.
390 8 for my $t($E->t)
391 26 {my $n = $t->vp($s);
392 26 my %t = $t->split;
393 26 my $S = sigma($t{t}->vp($s, 0)->z); # Remove substitution variable
394 26 ▶ $S = $S->multiply(($t{s}->sub(@_))->Sqrt) if defined($t{s});
Branch: 1 branch
BranchTrueFalseTotal
if defined $t{"s"} 2 24 26
395 26 ▶ $S = $S->divide ($t{d}->sub(@_)) if defined($t{d});
Branch: 1 branch
BranchTrueFalseTotal
if defined $t{"d"} 1 25 26
396 26 ▶ $S = $S->multiply(($t{e}->sub(@_))->Exp) if defined($t{e});
Branch: 1 branch
BranchTrueFalseTotal
if defined $t{"e"} 0 26 26
397 26 ▶ $S = $S->multiply(($t{l}->sub(@_))->Log) if defined($t{l});
Branch: 1 branch
BranchTrueFalseTotal
if defined $t{"l"} 0 26 26
398 26 ▶ $S = $S->multiply($w->power(makeInt($n))) if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 13 13 26
399 26 $Z = $Z->add($S);
400 }
401 8 $E = $Z;
402 }
403
404 # Result
405 7 $E;
406 }
407
408
409 =head3 isEqual
410
411 Check whether one sum is equal to another after multiplying out all
412 divides and divisors.
413
414 =cut
415
416
417 sub isEqual($)
418 91 ▶ {my ($C) = @_;
Subroutine isEqual: called CC=12 SCAR=24.8
Pod documented
419
420 # Until there are no more divides
421 91 for(;;)
422 176 {my (%c, $D, $N); $N = 0;
423
424 # Most frequent divisor
425 176 for my $t($C->t)
426 1550 {my $d = $t->Divide;
427 1550 ▶ next unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 592 958 1550
428 958 my $s = $d->getSignature;
429 958 ▶ if (++$c{$s} > $N)
Branch: 1 branch
BranchTrueFalseTotal
if (++$c{$s} > $N) 675 283 958
430 675 {$N = $c{$s};
431 675 $D = $d;
432 }
433 }
434 176 ▶ last unless $N;
Branch: 1 branch
BranchTrueFalseTotal
unless $N 91 85 176
435 85 $C = $C->multiply($D);
436 }
437
438 # Until there are no more negative powers
439 91 for(;;)
440 100 {my %v;
441 100 for my $t($C->t)
442 161 {for my $v($t->v)
443 124 {my $p = $t->vp($v);
444 124 ▶ next unless $p < 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $p < 0 112 12 124
445 12 $p = -$p;
446 12 ▶ $v{$v} = $p if !defined($v{$v}) or $v{$v} < $p;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $v{$v} or $v{$v} < $p 10 2 12
Condition100%3/3
not defined $v{$v} or $v{$v} < $p
l!l&&r!l&&!r
912
MC/DC100%2/2
not defined $v{$v} or $v{$v} < $p
not defined $v{$v} $v{$v} < $p
Truth table100%3/3
not(A) or B
A= not defined $v{$v}B= $v{$v} < $p
ABresult
1X1
011
000
447 }
448 }
449 100 ▶ last unless scalar(keys(%v));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %v 91 9 100
450 9 my $m = term()->one->clone;
451 9 $m->vp($_, $v{$_}) for keys(%v);
452 9 my $M = sigma($m->z);
453 9 $C = $C->multiply($M);
454 }
455
456 # Result
457 91 $C;
458 }
459
460
461 =head3 normalizeSqrts
462
463 Normalize sqrts in a sum.
464
465 This routine needs fixing.
466
467 It should simplify square roots.
468
469 =cut
470
471
472 sub normalizeSqrts($)
473 108 ▶ {my ($s) = @_;
Subroutine normalizeSqrts: called CC=16 SCAR=55.0
Pod documented
474 108 return $s;
475 0 my (@t, @s);
476
477 # Find terms with single simple sqrts that can be normalized.
478 0 for my $t($s->t)
479 0 {push @t, $t;
480 0 ▶ my $S = $t->Sqrt; next unless $S; # Check for sqrt
Branch: 1 branch
BranchTrueFalseTotal
unless $S 0 0 0
481 0 ▶ my $St = $S->st; next unless $St; # Check for single term sqrt
Branch: 1 branch
BranchTrueFalseTotal
unless $St 0 0 0
482
483 0 my %T = $St->split; # Split single term sqrt
484 0 ▶ next if $T{s} or $T{d} or $T{e} or $T{l};
Branch: 1 branch
BranchTrueFalseTotal
if $T{"s"} or $T{"d"} or $T{"e"} or $T{"l"} 0 0 0
Condition0%0/9
$T{"s"} or $T{"d"} or $T{"e"} or $T{"l"}
l!l&&r!l&&!r
000
$T{"s"} or $T{"d"} or $T{"e"}
l!l&&r!l&&!r
000
$T{"s"} or $T{"d"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$T{"s"} or $T{"d"} or $T{"e"} or $T{"l"}
$T{"s"} $T{"d"} $T{"e"} $T{"l"}
Truth table0%0/5
A or B or C or D
A= $T{"s"}B= $T{"d"}C= $T{"e"}D= $T{"l"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
485 0 pop @t;
486 0 push @s, {t=>$t, s=>$T{t}->z}; # Sqrt with simple single term
487 }
488
489 # Already normalized unless there are several such terms
490 0 ▶ return $s unless scalar(@s) > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @s > 1 0 0 0
491
492 # Remove divisor for each normalized term
493 0 for my $r(@s)
494 0 ▶ {my $d = $r->{t}->d; next unless $d > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $d > 1 0 0 0
495 0 for my $s(@s)
496 0 {$s->{t} = $s->{t}->clone->divideInt($d) ->z;
497 0 $s->{s} = $s->{s}->clone->timesInt ($d*$d)->z;
498 }
499 }
500
501 # Eliminate duplicate squared factors
502 0 for my $s(@s)
503 0 {my $F = factorize($s->{s}->c);
504 0 my $p = 1;
505 0 for my $f(keys(%$F))
506 0 ▶ {$p *= $f**(int($F->{$f}/2)) if $F->{$f} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $F->{$f} > 1 0 0 0
507 }
508 0 $s->{t} = $s->{t}->clone->timesInt ($p) ->z;
509 0 $s->{s} = $s->{s}->clone->divideInt($p*$p)->z;
510
511 0 ▶ if ($s->{s}->isOne)
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{'s'}->isOne) { } 0 0 0
512 0 {push @t, $s->{t}->removeSqrt;
513 }
514 else
515 0 {push @t, $s->{t}->clone->Sqrt($s->{$s})->z;
516 }
517 }
518
519 # Result
520 0 sigma(@t);
521 }
522
523
524 =head3 isEqualSqrt
525
526 Check whether one sum is equal to another after multiplying out sqrts.
527
528 =cut
529
530
531 sub isEqualSqrt($)
532 91 ▶ {my ($C) = @_;
Subroutine isEqualSqrt: called CC=11 SCAR=24.0
Pod documented
533
534 #_______________________________________________________________________
535 # Each sqrt
536 #_______________________________________________________________________
537
538 91 for(1..99)
539 108 {$C = $C->normalizeSqrts;
540 108 my @s = grep { defined($_->Sqrt)} $C->t;
541 108 my @n = grep {!defined($_->Sqrt)} $C->t;
542 108 ▶ last unless scalar(@s) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @s > 0 91 17 108
543
544 #_______________________________________________________________________
545 # Partition by square roots.
546 #_______________________________________________________________________
547
548 17 my %S = ();
549 17 for my $t(@s)
550 47 {my $s = $t->Sqrt;
551 47 my $S = $s->signature;
552 47 push @{$S{$S}}, $t;
553 }
554
555 #_______________________________________________________________________
556 # Square each partitions, as required by the formulae below.
557 #_______________________________________________________________________
558
559 17 my @t;
560 17 ▶ push @t, sigma(@n)->power($two) if scalar(@n); # Non sqrt partition
Branch: 1 branch
BranchTrueFalseTotal
if scalar @n 10 7 17
561 17 for my $s(keys(%S))
562 32 {push @t, sigma(@{$S{$s}})->power($two); # Sqrt partition
563 }
564
565 #_______________________________________________________________________
566 # I can multiply out upto 4 square roots using the formulae below.
567 # There are formula to multiply out more than 4 sqrts, but they are big.
568 # These formulae are obtained by squaring out and rearranging:
569 # sqrt(a)+sqrt(b)+sqrt(c)+sqrt(d) == 0 until no sqrts remain, and
570 # then matching terms to produce optimal execution.
571 # This remarkable result was obtained with the help of this package:
572 # demonstrating its utility in optimizing complex calculations written
573 # in Perl: which in of itself cannot optimize broadly.
574 #_______________________________________________________________________
575
576 17 my $ns = scalar(@t);
577 17 ▶ $ns < 5 or die "There are $ns square roots present. I can handle less than 5";
Branch: 1 branch
BranchTrueFalseTotal
$ns < 5 or die "There are $ns square roots present. I can handle less than 5" 0 17 17
578
579 17 my ($a, $b, $c, $d) = @t;
580
581 17 ▶ if ($ns == 1)
Branch: 4 branches
BranchTrueFalseTotal
if ($ns == 1) { } 0 17 17
elsif ($ns == 2) { } 11 6 17
elsif ($ns == 3) { } 4 2 6
elsif ($ns == 4) { } 2 0 2
582 0 {$C = $a;
583 }
584 elsif ($ns == 2)
585 11 {$C = $a-$b;
586 }
587 elsif ($ns == 3)
588 4 {$C = -$a**2+2*$a*$b-$b**2+2*$c*$a+2*$c*$b-$c**2;
589 }
590 elsif ($ns == 4)
591 2 {my $a2 = $a * $a;
592 2 my $a3 = $a2 * $a;
593 2 my $a4 = $a3 * $a;
594 2 my $b2 = $b * $b;
595 2 my $b3 = $b2 * $b;
596 2 my $b4 = $b3 * $b;
597 2 my $c2 = $c * $c;
598 2 my $c3 = $c2 * $c;
599 2 my $c4 = $c3 * $c;
600 2 my $d2 = $d * $d;
601 2 my $d3 = $d2 * $d;
602 2 my $d4 = $d3 * $d;
603 2 my $bpd = $b + $d;
604 2 my $bpc = $b + $c;
605 2 my $cpd = $c + $d;
606 2 $C =
607 - ($a4 + $b4 + $c4 + $d4)
608 + 4*(
609 +$a3*($b+$cpd)+$b3*($a+$cpd)+$c3*($a+$bpd)+$d3*($a+$bpc)
610 -$a2*($b *($cpd)+ $c*$d)
611 -$a *($b2*($cpd)+$d2*($bpc))
612 )
613
614 - 6*($a2*$b2+($a2+$b2)*($c2+$d2)+$c2*$d2)
615
616 - 4*$c*($b2*$d+$b*$d2)
617 - 4*$c2*($a*($bpd)+$b*$d)
618 +40*$c*$a*$b*$d
619 ;
620 }
621 }
622
623 #________________________________________________________________________
624 # Test result
625 #________________________________________________________________________
626
627 # $C->isEqual($zero);
628 91 $C;
629 }
630
631
632 =head3 isZero
633
634 Transform a sum assuming that it is equal to zero
635
636 =cut
637
638
639 sub isZero($)
640 91 ▶ {my ($C) = @_;
Subroutine isZero: called CC=1 SCAR=0.0
Pod documented
641 91 $C->isEqualSqrt->isEqual;
642 }
643
644
645 =head3 powerOfTwo
646
647 Check that a number is a power of two
648
649 =cut
650
651
652 sub powerof2($)
653 1 ▶ {my ($N) = @_;
Subroutine powerof2: called CC=4 SCAR=14.0
Pod undocumented
654 1 my $n = 0;
655 1 ▶ return undef unless $N > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $N > 0 0 1 1
656 1 for (;;)
657 2 ▶ {return $n if $N == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $N == 1 1 1 2
658 1 ▶ return undef unless $N % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $N % 2 == 0 0 1 1
659 1 ++$n; $N /= 2;
660 }
661 }
662
663
664 =head3 solve
665
666 Solve an equation known to be equal to zero for a specified variable.
667
668 =cut
669
670
671 sub solve($$)
672 17 ▶ {my ($A, @x) = @_;
Subroutine solve: called CC=37 SCAR=36.9
Pod documented
673 17 ▶ croak 'Need variable to solve for' unless scalar(@x) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @x > 0 0 17 17
674
675 17 ▶ @x = @{$x[0]} if scalar(@x) == 1 and ref($x[0]) eq 'ARRAY'; # Array of variables supplied
Branch: 1 branch
BranchTrueFalseTotal
if scalar @x == 1 and ref $x[0] eq "ARRAY" 1 16 17
Condition100%3/3
scalar @x == 1 and ref $x[0] eq "ARRAY"
!ll&&!rl&&r
791
MC/DC100%2/2
scalar @x == 1 and ref $x[0] eq "ARRAY"
scalar @x == 1 ref $x[0] eq "ARRAY"
Truth table100%3/3
A and B
A= scalar @x == 1B= ref $x[0] eq "ARRAY"
ABresult
0X0
100
111
676 17 my %x;
677 17 for my $x(@x)
678 56 ▶ {if (!ref $x)
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $x) { } 51 5 56
elsif (ref $x eq __PACKAGE__) { } 5 0 5
679 51 ▶ {$x =~ /^[a-z]+$/i or croak "Cannot solve for: $x, not a variable name";
Branch: 1 branch
BranchTrueFalseTotal
$x =~ /^[a-z]+$/i or croak("Cannot solve for: $x, not a variable name") 0 51 51
680 }
681 elsif (ref $x eq __PACKAGE__)
682 5 ▶ {my $t = $x->st; $t or die "Cannot solve for multiple terms";
Branch: 1 branch
BranchTrueFalseTotal
$t or die "Cannot solve for multiple terms" 0 5 5
683 5 ▶ my @b = $t->v; scalar(@b) == 1 or die "Can only solve for one variable";
Branch: 1 branch
BranchTrueFalseTotal
scalar @b == 1 or die "Can only solve for one variable" 0 5 5
684 5 ▶ my $p = $t->vp($b[0]); $p == 1 or die "Can only solve by variable to power 1";
Branch: 1 branch
BranchTrueFalseTotal
$p == 1 or die "Can only solve by variable to power 1" 0 5 5
685 5 $x = $b[0];
686 }
687 else
688 0 {die "$x is not a variable name";
689 }
690 56 $x{$x} = 1;
691 }
692 17 my $x = $x[0];
693
694 17 $B = $A->isZero; # Eliminate sqrts and negative powers
695
696 # Strike all terms with free variables other than x: i.e. not x and not one of the named constants
697 17 my @t = ();
698 17 for my $t($B->t)
699 52 {my @v = $t->v;
700 52 push @t, $t;
701 52 for my $v($t->v)
702 57 ▶ {next if exists($x{$v});
Branch: 1 branch
BranchTrueFalseTotal
if exists $x{$v} 49 8 57
703 8 pop @t;
704 8 last;
705 }
706 }
707 17 my $C = sigma(@t);
708
709 # Find highest and lowest power of x
710 17 my $n = 0; my $N;
711 17 for my $t($C->t)
712 44 {my $p = $t->vp($x);
713 44 ▶ $n = $p if $p > $n;
Branch: 1 branch
BranchTrueFalseTotal
if $p > $n 26 18 44
714 44 ▶ $N = $p if !defined($N) or $p < $N;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $N or $p < $N 26 18 44
Condition100%3/3
not defined $N or $p < $N
l!l&&r!l&&!r
17918
MC/DC100%2/2
not defined $N or $p < $N
not defined $N $p < $N
Truth table100%3/3
not(A) or B
A= not defined $NB= $p < $N
ABresult
1X1
011
000
715 }
716 17 my $D = $C;
717 17 ▶ $D = $D->multiply(sigma(term()->one->clone->vp($x, -$N)->z)) if $N;
Branch: 1 branch
BranchTrueFalseTotal
if $N 1 16 17
718 17 ▶ $n -= $N if $N;
Branch: 1 branch
BranchTrueFalseTotal
if $N 1 16 17
719
720 # Find number of terms in x
721 17 my $c = 0;
722 17 for my $t($D->t)
723 44 ▶ {++$c if $t->vp($x) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t->vp($x) > 0 26 18 44
724 }
725
726 17 ▶ $n == 0 and croak "Equation not dependant on $x, so cannot solve for $x";
Branch: 1 branch
BranchTrueFalseTotal
$n == 0 and croak("Equation not dependant on $x, so cannot solve for $x") 0 17 17
727 17 ▶ $n > 4 and $c > 1 and croak "Unable to solve polynomial or power $n > 4 in $x (Galois)";
Branch: 1 branch
BranchTrueFalseTotal
$n > 4 and $c > 1 and croak("Unable to solve polynomial or power $n > 4 in $x (Galois)") 0 17 17
Condition33%1/3
$n > 4 and $c > 1
!ll&&!rl&&r
1700
MC/DC0%0/2
$n > 4 and $c > 1
$n > 4 $c > 1
Truth table33%1/3
A and B
A= $n > 4B= $c > 1
ABresult
0X0
100
111
728 17 ▶ ($n > 2 and $c > 1) and die "Need solver for polynomial of degree $n in $x";
Branch: 1 branch
BranchTrueFalseTotal
$n > 2 and $c > 1 and die "Need solver for polynomial of degree $n in $x" 0 17 17
Condition33%1/3
$n > 2 and $c > 1
!ll&&!rl&&r
1700
MC/DC0%0/2
$n > 2 and $c > 1
$n > 2 $c > 1
Truth table33%1/3
A and B
A= $n > 2B= $c > 1
ABresult
0X0
100
111
729
730 # Solve linear equation
731 17 ▶ if ($n == 1 or $c == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1 or $c == 1) 8 9 17
Condition100%3/3
$n == 1 or $c == 1
l!l&&r!l&&!r
719
MC/DC100%2/2
$n == 1 or $c == 1
$n == 1 $c == 1
Truth table100%3/3
A or B
A= $n == 1B= $c == 1
ABresult
1X1
011
000
732 8 {my (@c, @v);
733 8 for my $t($D->t)
734 17 ▶ {push(@c, $t), next if $t->vp($x) == 0; # Constants
Branch: 1 branch
BranchTrueFalseTotal
if $t->vp($x) == 0 9 8 17
735 8 push @v, $t; # Powers of x
736 }
737 8 my $d = sigma(@v)->multiply(sigma(term()->one->clone->vp($x, -$n)->negate->z));
738 8 $D = sigma(@c)->divide($d);
739
740 8 ▶ return $D if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 7 1 8
741
742 1 my $p = powerof2($n);
743 1 ▶ $p or croak "Fractional power 1/$n of $x unconstructable by sqrt";
Branch: 1 branch
BranchTrueFalseTotal
$p or croak("Fractional power 1/$n of $x unconstructable by sqrt") 0 1 1
744 1 $D = $D->Sqrt for(1..$p);
745 1 return $D;
746 }
747
748 # Solve quadratic equation
749 9 ▶ if ($n == 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 2) 9 0 9
750 9 {my @c = ($one, $one, $one);
751 9 $c[$_->vp($x)] = $_ for $D->t;
752 9 $_ = sigma($_->clone->vp($x, 0)->z) for (@c);
753 9 my ($c, $b, $a) = @c;
754 return
755 9 [ (-$b->add (($b->power($two)->subtract($four->multiply($a)->multiply($c)))->Sqrt))->divide($two->multiply($a)),
756 (-$b->subtract(($b->power($two)->subtract($four->multiply($a)->multiply($c)))->Sqrt))->divide($two->multiply($a))
757 ]
758 }
759
760 # Check that it works
761
762 # my $yy = $e->sub($x=>$xx);
763 # $yy == 0 or die "Proposed solution \$$x=$xx does not zero equation $e";
764 # $xx;
765 }
766
767
768 =head3 power
769
770 Raise a sum to an integer power or an integer/2 power.
771
772 =cut
773
774
775 sub power($$)
776 352 ▶ {my ($a, $b) = @_;
Subroutine power: called CC=18 SCAR=33.4
Pod documented
777
778 352 ▶ return $one if $b->{id} == $zero->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"id"} == $zero->{"id"} 0 352 352
779 352 ▶ return $a->multiply($a) if $b->{id} == $two->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"id"} == $two->{"id"} 291 61 352
780 61 ▶ return $a if $b->{id} == $one->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"id"} == $one->{"id"} 4 57 61
781 57 ▶ return $one->divide($a) if $b->{id} == $mOne->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"id"} == $mOne->{"id"} 0 57 57
782 57 ▶ return $a->sqrt if $b->{id} == $half->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"id"} == $half->{"id"} 0 57 57
783 57 ▶ return $one->divide($a->sqrt) if $b->{id} == $mHalf->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"id"} == $mHalf->{"id"} 0 57 57
784
785 57 my $T = $b->st;
786 57 ▶ $T or croak "Power by expression too complicated";
Branch: 1 branch
BranchTrueFalseTotal
$T or croak("Power by expression too complicated") 0 57 57
787
788 57 my %t = $T->split;
789 57 ▶ croak "Power by term too complicated" if $t{s} or $t{d} or $t{e} or $t{l};
Branch: 1 branch
BranchTrueFalseTotal
if $t{"s"} or $t{"d"} or $t{"e"} or $t{"l"} 0 57 57
Condition33%3/9
$t{"s"} or $t{"d"} or $t{"e"} or $t{"l"}
l!l&&r!l&&!r
0057
$t{"s"} or $t{"d"} or $t{"e"}
l!l&&r!l&&!r
0057
$t{"s"} or $t{"d"}
l!l&&r!l&&!r
0057
MC/DC0%0/4
$t{"s"} or $t{"d"} or $t{"e"} or $t{"l"}
$t{"s"} $t{"d"} $t{"e"} $t{"l"}
Truth table20%1/5
A or B or C or D
A= $t{"s"}B= $t{"d"}C= $t{"e"}D= $t{"l"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
790
791 57 my $t = $t{t};
792 57 ▶ $t->i == 0 or croak "Complex power not allowed yet";
Branch: 1 branch
BranchTrueFalseTotal
$t->i == 0 or croak("Complex power not allowed yet") 0 57 57
793
794 57 my ($p, $d) = ($t->c, $t->d);
795 57 ▶ $d == 1 or $d == 2 or croak "Fractional power other than /2 not allowed yet";
Branch: 1 branch
BranchTrueFalseTotal
$d == 1 or $d == 2 or croak("Fractional power other than /2 not allowed yet") 0 57 57
Condition33%1/3
$d == 1 or $d == 2
l!l&&r!l&&!r
5700
MC/DC0%0/2
$d == 1 or $d == 2
$d == 1 $d == 2
Truth table33%1/3
A or B
A= $d == 1B= $d == 2
ABresult
1X1
011
000
796
797 57 ▶ $a = $a->sqrt if $d == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $d == 2 0 57 57
798
799 57 ▶ return $one->divide($a)->power(sigma(term()->c($p)->z)) if $p < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p < 0 0 57 57
800
801 57 $p = abs($p);
802 57 my $r = $a; $r = $r->multiply($a) for (2..$p);
803 57 $r;
804 }
805
806
807 =head3 d
808
809 Differentiate.
810
811 =cut
812
813
814 sub d($;$);
815 sub d($;$)
816 195 ▶ {my $c = $_[0]; # Differentiate this sum
Subroutine d: called CC=42 SCAR=41.8
Pod documented
817 195 my $b = $_[1]; # With this variable
818
819 #_______________________________________________________________________
820 # Get differentrix. Assume 'x', 'y', 'z' or 't' if appropriate.
821 #_______________________________________________________________________
822
823 195 ▶ if (defined($b))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $b) { } 139 56 195
824 139 ▶ {if (!ref $b)
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $b) { } 136 3 139
elsif (ref $b eq __PACKAGE__) { } 3 0 3
825 136 ▶ {$b =~ /^[a-z]+$/i or croak "Cannot differentiate by $b";
Branch: 1 branch
BranchTrueFalseTotal
$b =~ /^[a-z]+$/i or croak("Cannot differentiate by $b") 0 136 136
826 }
827 elsif (ref $b eq __PACKAGE__)
828 3 ▶ {my $t = $b->st; $t or die "Cannot differentiate by multiple terms";
Branch: 1 branch
BranchTrueFalseTotal
$t or die "Cannot differentiate by multiple terms" 0 3 3
829 3 ▶ my @b = $t->v; scalar(@b) == 1 or die "Can only differentiate by one variable";
Branch: 1 branch
BranchTrueFalseTotal
scalar @b == 1 or die "Can only differentiate by one variable" 0 3 3
830 3 ▶ my $p = $t->vp($b[0]); $p == 1 or die "Can only differentiate by variable to power 1";
Branch: 1 branch
BranchTrueFalseTotal
$p == 1 or die "Can only differentiate by variable to power 1" 0 3 3
831 3 $b = $b[0];
832 }
833 else
834 0 {die "Cannot differentiate by $b";
835 }
836 }
837 else
838 56 {my %b;
839 56 for my $t($c->t)
840 108 {my %b; $b{$_}++ for ($t->v);
841 }
842 56 my $i = 0; my $n = scalar(keys(%b));
843 56 ▶ ++$i, $b = 'x' if $n == 0; # Constant expression anyway
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 56 0 56
844 56 ▶ ++$i, $b = (%b)[0] if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 56 56
845 56 for my $v(qw(t x y z))
846 224 ▶ {++$i, $b = 't' if $n > 1 and exists($b{$v});
Branch: 1 branch
BranchTrueFalseTotal
if $n > 1 and exists $b{$v} 0 224 224
Condition33%1/3
$n > 1 and exists $b{$v}
!ll&&!rl&&r
22400
MC/DC0%0/2
$n > 1 and exists $b{$v}
$n > 1 exists $b{$v}
Truth table33%1/3
A and B
A= $n > 1B= exists $b{$v}
ABresult
0X0
100
111
847 }
848 56 ▶ $i == 1 or croak "Please specify a single variable to differentiate by";
Branch: 1 branch
BranchTrueFalseTotal
$i == 1 or croak("Please specify a single variable to differentiate by") 0 56 56
849 }
850
851 #_______________________________________________________________________
852 # Each term
853 #_______________________________________________________________________
854
855 195 my @t = ();
856 195 for my $t($c->t)
857 258 {my %V = $t->split;
858 258 my $T = $V{t}->z->clone->z;
859 258 my ($S, $D, $E, $L) = @V{qw(s d e l)};
860 258 ▶ my $s = $S->d($b) if $S;
Branch: 1 branch
BranchTrueFalseTotal
if $S 5 253 258
861 258 ▶ my $d = $D->d($b) if $D;
Branch: 1 branch
BranchTrueFalseTotal
if $D 10 248 258
862 258 ▶ my $e = $E->d($b) if $E;
Branch: 1 branch
BranchTrueFalseTotal
if $E 120 138 258
863 258 ▶ my $l = $L->d($b) if $L;
Branch: 1 branch
BranchTrueFalseTotal
if $L 0 258 258
864
865 #_______________________________________________________________________
866 # Differentiate Variables: A*v**n->d == A*n*v**(n-1)
867 #_______________________________________________________________________
868
869 258 {my $v = $T->clone;
870 258 my $p = $v->vp($b);
871 258 ▶ if ($p != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($p != 0) 128 130 258
872 128 {$v->timesInt($p)->vp($b, $p-1);
873 128 ▶ $v->Sqrt ($S) if $S;
Branch: 1 branch
BranchTrueFalseTotal
if $S 1 127 128
874 128 ▶ $v->Divide($D) if $D;
Branch: 1 branch
BranchTrueFalseTotal
if $D 1 127 128
875 128 ▶ $v->Exp ($E) if $E;
Branch: 1 branch
BranchTrueFalseTotal
if $E 0 128 128
876 128 ▶ $v->Log ($L) if $L;
Branch: 1 branch
BranchTrueFalseTotal
if $L 0 128 128
877 128 push @t, $v->z;
878 }
879 }
880
881 #_______________________________________________________________________
882 # Differentiate Sqrt: A*sqrt(F(x))->d == 1/2*A*f(x)/sqrt(F(x))
883 #_______________________________________________________________________
884
885 258 ▶ if ($S)
Branch: 1 branch
BranchTrueFalseTotal
if ($S) 5 253 258
886 5 {my $v = $T->clone->divideInt(2);
887 5 ▶ $v->Divide($D) if $D;
Branch: 1 branch
BranchTrueFalseTotal
if $D 0 5 5
888 5 ▶ $v->Exp ($E) if $E;
Branch: 1 branch
BranchTrueFalseTotal
if $E 0 5 5
889 5 ▶ $v->Log ($L) if $L;
Branch: 1 branch
BranchTrueFalseTotal
if $L 0 5 5
890 5 push @t, sigma($v->z)->multiply($s)->divide($S->Sqrt)->t;
891 }
892
893 #_______________________________________________________________________
894 # Differentiate Divide: A/F(x)->d == -A*f(x)/F(x)**2
895 #_______________________________________________________________________
896
897 258 ▶ if ($D)
Branch: 1 branch
BranchTrueFalseTotal
if ($D) 10 248 258
898 10 {my $v = $T->clone->negate;
899 10 ▶ $v->Sqrt($S) if $S;
Branch: 1 branch
BranchTrueFalseTotal
if $S 0 10 10
900 10 ▶ $v->Exp ($E) if $E;
Branch: 1 branch
BranchTrueFalseTotal
if $E 6 4 10
901 10 ▶ $v->Log ($L) if $L;
Branch: 1 branch
BranchTrueFalseTotal
if $L 0 10 10
902 10 push @t, sigma($v->z)->multiply($d)->divide($D->multiply($D))->t;
903 }
904
905 #_______________________________________________________________________
906 # Differentiate Exp: A*exp(F(x))->d == A*f(x)*exp(F(x))
907 #_______________________________________________________________________
908
909 258 ▶ if ($E)
Branch: 1 branch
BranchTrueFalseTotal
if ($E) 120 138 258
910 120 {my $v = $T->clone;
911 120 ▶ $v->Sqrt ($S) if $S;
Branch: 1 branch
BranchTrueFalseTotal
if $S 0 120 120
912 120 ▶ $v->Divide($D) if $D;
Branch: 1 branch
BranchTrueFalseTotal
if $D 6 114 120
913 120 $v->Exp ($E);
914 120 ▶ $v->Log ($L) if $L;
Branch: 1 branch
BranchTrueFalseTotal
if $L 0 120 120
915 120 push @t, sigma($v->z)->multiply($e)->t;
916 }
917
918 #_______________________________________________________________________
919 # Differentiate Log: A*log(F(x))->d == A*f(x)/F(x)
920 #_______________________________________________________________________
921
922 258 ▶ if ($L)
Branch: 1 branch
BranchTrueFalseTotal
if ($L) 0 258 258
923 0 {my $v = $T->clone;
924 0 ▶ $v->Sqrt ($S) if $S;
Branch: 1 branch
BranchTrueFalseTotal
if $S 0 0 0
925 0 ▶ $v->Divide($D) if $D;
Branch: 1 branch
BranchTrueFalseTotal
if $D 0 0 0
926 0 ▶ $v->Exp ($E) if $E;
Branch: 1 branch
BranchTrueFalseTotal
if $E 0 0 0
927 0 push @t, sigma($v->z)->multiply($l)->divide($L)->t;
928 }
929 }
930
931 #_______________________________________________________________________
932 # Result
933 #_______________________________________________________________________
934
935 195 sigma(@t);
936 }
937
938
939 =head3 simplify
940
941 Simplify just before assignment.
942
943 There is no general simplification algorithm. So try various methods and
944 see if any simplifications occur. This is cheating really, because the
945 examples will represent these specific transformations as general
946 features which they are not. On the other hand, Mathematics is full of
947 specifics so I suppose its not entirely unacceptable.
948
949 Simplification cannot be done after every operation as it is
950 inefficient, doing it as part of += ameliorates this inefficiency.
951
952 Note: += only works as a synonym for simplify() if the left hand side is
953 currently undefined. This can be enforced by using my() as in: my $z +=
954 ($x**2+5x+6)/($x+2);
955
956 =cut
957
958
959 sub simplify($)
960 16 ▶ {my ($x) = @_;
Subroutine simplify: called CC=1 SCAR=0.0
Pod documented
961 16 $x = polynomialDivision($x);
962 16 $x = eigenValue($x);
963 }
964
965 #_______________________________________________________________________
966 # Common factor: find the largest factor in one or more expressions
967 #_______________________________________________________________________
968
969 sub commonFactor(@)
970 32 ▶ {return undef unless scalar(@_);
Subroutine commonFactor: called CC=35 SCAR=41.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 0 32 32
971 32 ▶ return undef unless scalar(keys(%{$_[0]->{t}}));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$_[0]{"t"};} 0 32 32
972 32 my $p = (values(%{$_[0]->{t}}))[0];
973
974 32 my %v = %{$p->{v}}; # Variables
975 32 my %s = $p->split;
976 32 my ($s, $d, $e, $l) = @s{qw(s d e l)}; # Sub expressions
977 32 my ($C, $D, $I) = ($p->c, $p->d, $p->i);
978
979 32 my @t;
980 32 for my $a(@_)
981 32 {for my $b($a->t)
982 88 {push @t, $b;
983 }
984 }
985
986 32 for my $t(@t)
987 88 {my %V = %v;
988 88 %v = ();
989 88 for my $v($t->v)
990 59 ▶ {next unless $V{$v};
Branch: 1 branch
BranchTrueFalseTotal
unless $V{$v} 9 50 59
991 50 my $p = $t->vp($v);
992 50 ▶ $v{$v} = ($V{$v} < $p ? $V{$v} : $p);
Branch: 1 branch
BranchTrueFalseTotal
$V{$v} < $p ? : 36 14 50
993 }
994 88 my %S = $t->split;
995 88 my ($S, $D, $E, $L) = @S{qw(s d e l)}; # Sub expressions
996 88 ▶ $s = undef unless defined($s) and defined($S) and $S->id eq $s->id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and defined $S and $S->id eq $s->id 0 0 0
Condition33%2/6
defined $s and defined $S and $S->id eq $s->id
!ll&&!rl&&r
8800
defined $s and defined $S
!ll&&!rl&&r
8800
MC/DC0%0/3
defined $s and defined $S and $S->id eq $s->id
defined $s defined $S $S->id eq $s->id
Truth table25%1/4
A and B and C
A= defined $sB= defined $SC= $S->id eq $s->id
ABCresult
0XX0
10X0
1100
1111
997 88 ▶ $d = undef unless defined($d) and defined($D) and $D->id eq $d->id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d and defined $D and $D->id eq $d->id 0 0 0
Condition33%2/6
defined $d and defined $D and $D->id eq $d->id
!ll&&!rl&&r
8800
defined $d and defined $D
!ll&&!rl&&r
8800
MC/DC0%0/3
defined $d and defined $D and $D->id eq $d->id
defined $d defined $D $D->id eq $d->id
Truth table25%1/4
A and B and C
A= defined $dB= defined $DC= $D->id eq $d->id
ABCresult
0XX0
10X0
1100
1111
998 88 ▶ $e = undef unless defined($e) and defined($E) and $E->id eq $e->id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e and defined $E and $E->id eq $e->id 2 0 2
Condition66%4/6
defined $e and defined $E and $E->id eq $e->id
!ll&&!rl&&r
8620
defined $e and defined $E
!ll&&!rl&&r
8602
MC/DC0%0/3
defined $e and defined $E and $E->id eq $e->id
defined $e defined $E $E->id eq $e->id
Truth table50%2/4
A and B and C
A= defined $eB= defined $EC= $E->id eq $e->id
ABCresult
0XX0
10X0
1100
1111
999 88 ▶ $l = undef unless defined($l) and defined($L) and $L->id eq $l->id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and defined $L and $L->id eq $l->id 0 0 0
Condition33%2/6
defined $l and defined $L and $L->id eq $l->id
!ll&&!rl&&r
8800
defined $l and defined $L
!ll&&!rl&&r
8800
MC/DC0%0/3
defined $l and defined $L and $L->id eq $l->id
defined $l defined $L $L->id eq $l->id
Truth table25%1/4
A and B and C
A= defined $lB= defined $LC= $L->id eq $l->id
ABCresult
0XX0
10X0
1100
1111
1000 88 ▶ $C = undef unless defined($C) and $C == $t->c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $C and $C == $t->c 15 73 88
Condition100%3/3
defined $C and $C == $t->c
!ll&&!rl&&r
7873
MC/DC100%2/2
defined $C and $C == $t->c
defined $C $C == $t->c
Truth table100%3/3
A and B
A= defined $CB= $C == $t->c
ABresult
0X0
100
111
1001 88 ▶ $D = undef unless defined($D) and $D == $t->d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $D and $D == $t->d 88 0 88
Condition33%1/3
defined $D and $D == $t->d
!ll&&!rl&&r
8800
MC/DC0%0/2
defined $D and $D == $t->d
defined $D $D == $t->d
Truth table33%1/3
A and B
A= defined $DB= $D == $t->d
ABresult
0X0
100
111
1002 88 ▶ $I = undef unless defined($I) and $I == $t->i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $I and $I == $t->i 7 81 88
Condition100%3/3
defined $I and $I == $t->i
!ll&&!rl&&r
5281
MC/DC100%2/2
defined $I and $I == $t->i
defined $I $I == $t->i
Truth table100%3/3
A and B
A= defined $IB= $I == $t->i
ABresult
0X0
100
111
1003 }
1004 32 my $r = term()->one->clone;
1005 32 ▶ $r->c($C) if defined($C);
Branch: 1 branch
BranchTrueFalseTotal
if defined $C 24 8 32
1006 32 ▶ $r->d($D) if defined($D);
Branch: 1 branch
BranchTrueFalseTotal
if defined $D 32 0 32
1007 32 ▶ $r->i($I) if defined($I);
Branch: 1 branch
BranchTrueFalseTotal
if defined $I 30 2 32
1008 32 $r->vp($_, $v{$_}) for(keys(%v));
1009 32 ▶ $r->Sqrt ($s) if defined($s);
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 32 32
1010 32 ▶ $r->Divide($d) if defined($d);
Branch: 1 branch
BranchTrueFalseTotal
if defined $d 0 32 32
1011 32 ▶ $r->Exp ($e) if defined($e);
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 32 32
1012 32 ▶ $r->Log ($l) if defined($l);
Branch: 1 branch
BranchTrueFalseTotal
if defined $l 0 32 32
1013 32 sigma($r->z);
1014 }
1015
1016 #_______________________________________________________________________
1017 # Find term of polynomial of highest degree.
1018 #_______________________________________________________________________
1019
1020 sub polynomialTermOfHighestDegree($$)
1021 160 ▶ {my ($p, $v) = @_; # Polynomial, variable
Subroutine polynomialTermOfHighestDegree: called CC=3 SCAR=11.0
Pod undocumented
1022 160 my $n = 0; # Current highest degree
1023 160 my $t; # Term with this degree
1024 160 for my $T($p->t)
1025 308 {my $N = $T->vp($v);
1026 308 ▶ if ($N > $n)
Branch: 1 branch
BranchTrueFalseTotal
if ($N > $n) 148 160 308
1027 148 {$n = $N;
1028 148 $t = $T;
1029 }
1030 }
1031 160 ($n, $t);
1032 }
1033
1034
1035 =head3 polynomialDivide
1036
1037 Polynomial divide - divide one polynomial (a) by another (b) in variable v
1038
1039 =cut
1040
1041
1042 sub polynomialDivide($$$)
1043 16 ▶ {my ($p, $q, $v) = @_;
Subroutine polynomialDivide: called CC=3 SCAR=11.0
Pod documented
1044
1045 16 my $r = zero()->clone()->z;
1046 16 for(;;)
1047 80 {my ($np, $mp) = $p->polynomialTermOfHighestDegree($v);
1048 80 my ($nq, $mq) = $q->polynomialTermOfHighestDegree($v);
1049 80 ▶ last unless $np >= $nq;
Branch: 1 branch
BranchTrueFalseTotal
unless $np >= $nq 16 64 80
1050 64 my $pq = sigma($mp->divide2($mq));
1051 64 $r = $r->add($pq);
1052 64 $p = $p->subtract($q->multiply($pq));
1053 }
1054 16 ▶ return $r if $p->isZero()->{id} == $zero->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $p->isZero->{"id"} == $zero->{"id"} 14 2 16
1055 2 undef;
1056 }
1057
1058
1059 =head3 eigenValue
1060
1061 Eigenvalue check
1062
1063 =cut
1064
1065
1066 sub eigenValue($)
1067 16 ▶ {my ($p) = @_;
Subroutine eigenValue: called CC=6 SCAR=17.9
Pod documented
1068
1069 # Find divisors
1070 16 my %d;
1071 16 for my $t($p->t)
1072 67 {my $d = $t->Divide;
1073 67 ▶ next unless defined($d);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 61 6 67
1074 6 $d{$d->id} = $d;
1075 }
1076
1077 # Consolidate numerator and denominator
1078 16 my $P = $p ->clone()->z; $P = $P->multiply($d{$_}) for(keys(%d));
1079 16 my $Q = one()->clone()->z; $Q = $Q->multiply($d{$_}) for(keys(%d));
1080
1081 # Check for P=nQ i.e. for eigenvalue
1082 16 my $cP = $P->commonFactor; my $dP = $P->divide($cP);
1083 16 my $cQ = $Q->commonFactor; my $dQ = $Q->divide($cQ);
1084
1085 16 ▶ return $cP->divide($cQ) if $dP->id == $dQ->id;
Branch: 1 branch
BranchTrueFalseTotal
if $dP->id == $dQ->id 2 14 16
1086 14 $p;
1087 }
1088
1089
1090 =head3 polynomialDivision
1091
1092 Polynomial division.
1093
1094 =cut
1095
1096
1097 sub polynomialDivision($)
1098 16 ▶ {my ($p) = @_;
Subroutine polynomialDivision: called CC=20 SCAR=31.4
Pod documented
1099
1100 # Find a plausible indeterminate
1101 16 my %v; # Possible indeterminates
1102 my $v; # Polynomial indeterminate
1103 16 my %D; # Divisors for each term
1104
1105 # Each term
1106 16 for my $t($p->t)
1107 32 {my @v = $t->v;
1108 32 $v{$_}{$t->vp($_)} = 1 for(@v);
1109 32 my %V = $t->split;
1110 32 my ($S, $D, $E, $L) = @V{qw(s d e l)};
1111 32 ▶ return $p if defined($S) or defined($E) or defined($L);
Branch: 1 branch
BranchTrueFalseTotal
if defined $S or defined $E or defined $L 1 31 32
Condition66%4/6
defined $S or defined $E or defined $L
l!l&&r!l&&!r
1031
defined $S or defined $E
l!l&&r!l&&!r
0131
MC/DC33%1/3
defined $S or defined $E or defined $L
defined $S defined $E defined $L
Truth table50%2/4
A or B or C
A= defined $SB= defined $EC= defined $L
ABCresult
1XX1
01X1
0011
0000
1112
1113 # Each divisor term
1114 31 ▶ if (defined($D))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $D) 30 1 31
1115 30 {for my $T($D->t)
1116 60 {my @v = $T->v;
1117 60 $v{$_}{$T->vp($_)} = 1 for(@v);
1118 60 my %V = $T->split;
1119 60 my ($S, $D, $E, $L) = @V{qw(s d e l)};
1120 60 ▶ return $p if defined($S) or defined($D) or defined($E) or defined($L);
Branch: 1 branch
BranchTrueFalseTotal
if defined $S or defined $D or defined $E or defined $L 0 60 60
Condition33%3/9
defined $S or defined $D or defined $E or defined $L
l!l&&r!l&&!r
0060
defined $S or defined $D or defined $E
l!l&&r!l&&!r
0060
defined $S or defined $D
l!l&&r!l&&!r
0060
MC/DC0%0/4
defined $S or defined $D or defined $E or defined $L
defined $S defined $D defined $E defined $L
Truth table20%1/5
A or B or C or D
A= defined $SB= defined $DC= defined $ED= defined $L
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1121 }
1122 30 $D{$D->id} = $D;
1123 }
1124 }
1125
1126 # Consolidate numerator and denominator
1127 15 my $P = $p ->clone()->z; $P = $P->multiply($D{$_}) for(keys(%D));
1128 15 my $Q = one()->clone()->z; $Q = $Q->multiply($D{$_}) for(keys(%D));
1129
1130 # Pick a possible indeterminate
1131 15 for(keys(%v))
1132 19 ▶ {delete $v{$_} if scalar(keys(%{$v{$_}})) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$v{$_};} == 1 0 19 19
1133 }
1134 15 ▶ return $p unless scalar(keys(%v));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %v 1 14 15
1135 14 $v = (keys(%v))[0];
1136
1137 # Divide P by Q
1138 14 my $r;
1139 14 ▶ $r = $P->polynomialDivide($Q, $v); return $r if defined($r);
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 12 2 14
1140 2 ▶ $r = $Q->polynomialDivide($P, $v); return one()->divide($r) if defined($r);
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 2 0 2
1141 0 $p;
1142 }
1143
1144
1145 =head3 Sqrt
1146
1147 Square root of a sum
1148
1149 =cut
1150
1151
1152 sub Sqrt($)
1153 140 ▶ {my ($x) = @_;
Subroutine Sqrt: called CC=3 SCAR=11.0
Pod documented
1154 140 my $s = $x->st;
1155 140 ▶ if (defined($s))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) 62 78 140
1156 62 {my $r = $s->sqrt2;
1157 62 ▶ return sigma($r) if defined($r);
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 26 36 62
1158 }
1159
1160 114 sigma(term()->c(1)->Sqrt($x)->z);
1161 }
1162
1163
1164 =head3 Exp
1165
1166 Exponential (B<e> raised to the power) of a sum
1167
1168 =cut
1169
1170
1171 sub Exp($)
1172 484 ▶ {my ($x) = @_;
Subroutine Exp: called CC=5 SCAR=16.1
Pod documented
1173 484 my $p = term()->one;
1174 484 my @r;
1175 484 for my $t($x->t)
1176 548 {my $r = $t->exp2;
1177 548 ▶ $p = $p->multiply($r) if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 27 521 548
1178 548 ▶ push @r, $t unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 521 27 548
1179 }
1180 484 ▶ return sigma($p) if scalar(@r) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @r == 0 11 473 484
1181 473 return sigma($p->clone->Exp(sigma(@r))->z);
1182 }
1183
1184
1185 =head3 Log
1186
1187 Log to base B<e> of a sum
1188
1189 =cut
1190
1191
1192 sub Log($)
1193 1 ▶ {my ($x) = @_;
Subroutine Log: called CC=3 SCAR=11.2
Pod documented
1194 1 my $s = $x->st;
1195 1 ▶ if (defined($s))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) 1 0 1
1196 1 {my $r = $s->log2;
1197 1 ▶ return sigma($r) if defined($r);
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 0 1 1
1198 }
1199
1200 1 sigma(term()->c(1)->Log($x)->z);
1201 }
1202
1203
1204 =head3 Sin
1205
1206 Sine of a sum
1207
1208 =cut
1209
1210
1211 sub Sin($)
1212 136 ▶ {my ($x) = @_;
Subroutine Sin: called CC=3 SCAR=11.0
Pod documented
1213 136 my $s = $x->st;
1214 136 ▶ if (defined($s))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) 120 16 136
1215 120 {my $r = $s->sin2;
1216 120 ▶ return sigma($r) if defined($r);
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 23 97 120
1217 }
1218
1219 113 my $a = $i->multiply($x);
1220 113 $i->multiply($half)->multiply($a->negate->Exp->subtract($a->Exp));
1221 }
1222
1223
1224 =head3 Cos
1225
1226 Cosine of a sum
1227
1228 =cut
1229
1230
1231 sub Cos($)
1232 141 ▶ {my ($x) = @_;
Subroutine Cos: called CC=3 SCAR=11.0
Pod documented
1233 141 my $s = $x->st;
1234 141 ▶ if (defined($s))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) 125 16 141
1235 125 {my $r = $s->cos2;
1236 125 ▶ return sigma($r) if defined($r);
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 23 102 125
1237 }
1238
1239 118 my $a = $i->multiply($x);
1240 118 $half->multiply($a->negate->Exp->add($a->Exp));
1241 }
1242
1243
1244 =head3 tan, Ssc, csc, cot
1245
1246 Tan, sec, csc, cot of a sum
1247
1248 =cut
1249
1250
1251 27 ▶sub tan($) {my ($x) = @_; $x->Sin()->divide($x->Cos())}
Subroutine tan: called CC=1 SCAR=0.0
Pod documented
1252 11 ▶sub sec($) {my ($x) = @_; $one ->divide($x->Cos())}
Subroutine sec: called CC=1 SCAR=0.0
Pod undocumented
1253 11 ▶sub csc($) {my ($x) = @_; $one ->divide($x->Sin())}
Subroutine csc: called CC=1 SCAR=0.0
Pod documented
1254 11 ▶sub cot($) {my ($x) = @_; $x->Cos()->divide($x->Sin())}
Subroutine cot: called CC=1 SCAR=0.0
Pod documented
1255
1256
1257 =head3 sinh
1258
1259 Hyperbolic sine of a sum
1260
1261 =cut
1262
1263
1264 sub sinh($)
1265 34 ▶ {my ($x) = @_;
Subroutine sinh: called CC=2 SCAR=7.0
Pod documented
1266
1267 34 ▶ return $zero if $x->{id} == $zero->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"id"} == $zero->{"id"} 0 34 34
1268
1269 34 my $n = $x->negate;
1270 34 sigma
1271 (term()->c( 1)->divideInt(2)->Exp($x)->z,
1272 term()->c(-1)->divideInt(2)->Exp($n)->z
1273 )
1274 }
1275
1276
1277 =head3 cosh
1278
1279 Hyperbolic cosine of a sum
1280
1281 =cut
1282
1283
1284 sub cosh($)
1285 34 ▶ {my ($x) = @_;
Subroutine cosh: called CC=2 SCAR=7.0
Pod documented
1286
1287 34 ▶ return $one if $x->{id} == $zero->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"id"} == $zero->{"id"} 0 34 34
1288
1289 34 my $n = $x->negate;
1290 34 sigma
1291 (term()->c(1)->divideInt(2)->Exp($x)->z,
1292 term()->c(1)->divideInt(2)->Exp($n)->z
1293 )
1294 }
1295
1296
1297 =head3 Tanh, Sech, Csch, Coth
1298
1299 Tanh, Sech, Csch, Coth of a sum
1300
1301 =cut
1302
1303
1304 14 ▶sub tanh($) {my ($x) = @_; $x->sinh()->divide($x->cosh())}
Subroutine tanh: called CC=1 SCAR=0.0
Pod undocumented
1305 4 ▶sub sech($) {my ($x) = @_; $one ->divide($x->cosh())}
Subroutine sech: called CC=1 SCAR=0.0
Pod undocumented
1306 4 ▶sub csch($) {my ($x) = @_; $one ->divide($x->sinh())}
Subroutine csch: called CC=1 SCAR=0.0
Pod undocumented
1307 4 ▶sub coth($) {my ($x) = @_; $x->cosh()->divide($x->sinh())}
Subroutine coth: called CC=1 SCAR=0.0
Pod undocumented
1308
1309
1310 =head3 dot
1311
1312 Dot - complex dot product of two complex sums
1313
1314 =cut
1315
1316
1317 sub dot($$)
1318 51 ▶ {my ($a, $b) = @_;
Subroutine dot: called CC=2 SCAR=7.1
Pod documented
1319 51 ▶ $b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 0 51 51
1320 51 $a->re->multiply($b->re)->add($a->im->multiply($b->im));
1321 }
1322
1323
1324 =head3 cross
1325
1326 The area of the parallelogram formed by two complex sums
1327
1328 =cut
1329
1330
1331 sub cross($$)
1332 12 ▶ {my ($a, $b) = @_;
Subroutine cross: called CC=1 SCAR=0.0
Pod documented
1333 12 $a->dot($a)->multiply($b->dot($b))->subtract($a->dot($b)->power($two))->Sqrt;
1334 }
1335
1336
1337 =head3 unit
1338
1339 Intersection of a complex sum with the unit circle.
1340
1341 =cut
1342
1343
1344 sub unit($)
1345 10 ▶ {my ($a) = @_;
Subroutine unit: called CC=1 SCAR=0.0
Pod documented
1346 10 my $b = $a->modulus;
1347 10 my $c = $a->divide($b);
1348 10 $a->divide($a->modulus);
1349 }
1350
1351
1352 =head3 re
1353
1354 Real part of a complex sum
1355
1356 =cut
1357
1358
1359 sub re($)
1360 163 ▶ {my ($A) = @_;
Subroutine re: called CC=4 SCAR=13.9
Pod documented
1361 163 ▶ $A = newFromString("$A") unless ref($A) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $A eq __PACKAGE__ 0 163 163
1362 163 my @r;
1363 163 for my $a($A->t)
1364 328 ▶ {next if $a->i == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $a->i == 1 154 174 328
1365 174 push @r, $a;
1366 }
1367 163 sigma(@r);
1368 }
1369
1370
1371 =head3 im
1372
1373 Imaginary part of a complex sum
1374
1375 =cut
1376
1377
1378 sub im($)
1379 164 ▶ {my ($A) = @_;
Subroutine im: called CC=4 SCAR=13.9
Pod documented
1380 164 ▶ $A = newFromString("$A") unless ref($A) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $A eq __PACKAGE__ 0 164 164
1381 164 my @r;
1382 164 for my $a($A->t)
1383 333 ▶ {next if $a->i == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $a->i == 0 177 156 333
1384 156 push @r, $a;
1385 }
1386 164 $mI->multiply(sigma(@r));
1387 }
1388
1389
1390 =head3 modulus
1391
1392 Modulus of a complex sum
1393
1394 =cut
1395
1396
1397 sub modulus($)
1398 47 ▶ {my ($a) = @_;
Subroutine modulus: called CC=1 SCAR=0.0
Pod documented
1399 47 $a->re->power($two)->add($a->im->power($two))->Sqrt;
1400 }
1401
1402
1403 =head3 conjugate
1404
1405 Conjugate of a complexs sum
1406
1407 =cut
1408
1409
1410 sub conjugate($)
1411 11 ▶ {my ($a) = @_;
Subroutine conjugate: called CC=1 SCAR=0.0
Pod documented
1412 11 $a->re->subtract($a->im->multiply($i));
1413 }
1414
1415
1416 =head3 clone
1417
1418 Clone
1419
1420 =cut
1421
1422
1423 sub clone($)
1424 78 ▶ {my ($t) = @_;
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
1425 78 ▶ $t->{z} or die "Attempt to clone unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$t->{"z"} or die "Attempt to clone unfinalized sum" 0 78 78
1426 78 my $c = bless {%$t};
1427 78 $c->{t} = {%{$t->{t}}};
1428 78 delete $c->{z};
1429 78 delete $c->{s};
1430 78 delete $c->{id};
1431 78 $c;
1432 }
1433
1434
1435 =head3 signature
1436
1437 Signature of a sum: used to optimize add().
1438 # Fix the problem of adding different logs
1439
1440 =cut
1441
1442
1443 sub signature($)
1444 4424 ▶ {my ($t) = @_;
Subroutine signature: called CC=2 SCAR=6.9
Pod documented
1445 4424 my $s = '';
1446 4424 for my $a($t->t)
1447 26523 {$s .= '+'. $a->print;
1448 }
1449 4424 $s;
1450 }
1451
1452
1453 =head3 getSignature
1454
1455 Get the signature (see L</signature>) of a sum
1456
1457 =cut
1458
1459
1460 sub getSignature($)
1461 958 ▶ {my ($t) = @_;
Subroutine getSignature: called CC=2 SCAR=7.2
Pod documented
1462 958 ▶ exists $t->{z} ? $t->{z} : die "Attempt to get signature of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
exists $t->{'z'} ? : 958 0 958
1463 }
1464
1465
1466 =head3 id
1467
1468 Get Id of sum: each sum has a unique identifying number.
1469
1470 =cut
1471
1472
1473 sub id($)
1474 80 ▶ {my ($t) = @_;
Subroutine id: called CC=2 SCAR=7.1
Pod documented
1475 80 ▶ $t->{id} or die "Sum $t not yet finalized";
Branch: 1 branch
BranchTrueFalseTotal
$t->{"id"} or die "Sum $t not yet finalized" 0 80 80
1476 80 $t->{id};
1477 }
1478
1479
1480 =head3 zz
1481
1482 Check sum finalized. See: L</z>.
1483
1484 =cut
1485
1486
1487 sub zz($)
1488 0 ▶ {my ($t) = @_;
Subroutine zz: not called CC=2 SCAR=17.9
Pod documented
1489 0 ▶ $t->{z} or die "Sum $t not yet finalized";
Branch: 1 branch
BranchTrueFalseTotal
$t->{"z"} or die "Sum $t not yet finalized" 0 0 0
1490 0 print $t->{z}, "\n";
1491 0 $t;
1492 }
1493
1494
1495 =head3 z
1496
1497 Finalize creation of the sum: Once a sum has been finalized it becomes
1498 read only.
1499
1500 =cut
1501
1502
1503 45 my $lock = 0; # Hash locking
1504 45 my $z = 0; # Term counter
1505 45 my %z; # Terms finalized
1506
1507 sub z($)
1508 10142 ▶ {my ($t) = @_;
Subroutine z: called CC=3 SCAR=11.0
Pod documented
1509 10142 ▶ !exists($t->{z}) or die "Already finalized this term";
Branch: 1 branch
BranchTrueFalseTotal
exists $t->{"z"} and die "Already finalized this term" 0 10142 10142
1510
1511 10142 my $p = $t->print;
1512 10142 ▶ return $z{$p} if defined($z{$p});
Branch: 1 branch
BranchTrueFalseTotal
if defined $z{$p} 5765 4377 10142
1513 4377 $z{$p} = $t;
1514 4377 weaken($z{$p}); # Greatly reduces memory usage.
1515
1516 4377 $t->{s} = $p;
1517 4377 $t->{z} = $t->signature;
1518 4377 $t->{id} = ++$z;
1519
1520 #HashUtil lock_hash(%{$t->{v}}) if $lock;
1521 #HashUtil lock_hash %$t if $lock;
1522 4377 $t;
1523 }
1524
1525 #sub DESTROY($)
1526 # {my ($t) = @_;
1527 # delete $z{$t->{s}} if defined($t) and exists $t->{s};
1528 # }
1529
1530 sub lockHashes()
1531 0 ▶ {my ($l) = @_;
Subroutine lockHashes: not called CC=1 SCAR=6.9
Pod undocumented
1532 #HashUtil for my $t(values %z)
1533 #HashUtil {lock_hash(%{$t->{v}});
1534 #HashUtil lock_hash %$t;
1535 #HashUtil }
1536 0 $lock = 1;
1537 }
1538
1539
1540 =head3 print
1541
1542 Print sum
1543
1544 =cut
1545
1546
1547 sub print($)
1548 80379 ▶ {my ($t) = @_;
Subroutine print: called CC=5 SCAR=16.1
Pod documented
1549 80379 ▶ return $t->{s} if defined($t->{s});
Branch: 1 branch
BranchTrueFalseTotal
if defined $t->{"s"} 70237 10142 80379
1550 10142 my $s = '';
1551 10142 for my $a($t->t)
1552 35500 {$s .= $a->print .'+';
1553 }
1554 10142 ▶ chop($s) if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 9964 178 10142
1555
1556 10142 $s =~ s/^\+//;
1557 10142 $s =~ s/\+\-/\-/g;
1558 10142 $s =~ s/\+1\*/\+/g; # change: +1* to +
1559 10142 $s =~ s/\*1\*/\*/g; # remove: *1* to *
1560 10142 $s =~ s/^1\*//g; # remove: 1* at start of expression
1561 10142 $s =~ s/^\-1\*/\-/g; # change: -1* at start of expression to -
1562 10142 $s =~ s/^0\+//g; # change: 0+ at start of expression to
1563 10142 $s =~ s/\+0$//; # remove: +0 at end of expression
1564 10142 $s =~ s#\(\+0\+#\(#g; # change: (+0+ to (
1565 10142 $s =~ s/\(\+/\(/g; # change: (+ to (
1566 10142 $s =~ s/\(1\*/\(/g; # change: (1* to (
1567 10142 $s =~ s/\(\-1\*/\(\-/g; # change: (-1* to (-
1568 10142 $s =~ s/([a-zA-Z0-9)])\-1\*/$1\-/g; # change: term-1* to term-
1569 10142 $s =~ s/\*(\$[a-zA-Z]+)\*\*\-1(?!\d)/\/$1/g; # change: *$y**-1 to /$y
1570 10142 $s =~ s/\*(\$[a-zA-Z]+)\*\*\-(\d+)/\/$1**$2/g; # change: *$y**-n to /$y**n
1571 10142 $s =~ s/([\+\-])(\$[a-zA-Z]+)\*\*\-1(?!\d)/1\/$1/g; # change: +-$y**-1 to +-1/$y
1572 10142 $s =~ s/([\+\-])(\$[a-zA-Z]+)\*\*\-(\d+)/${1}1\/$2**$3/g; # change: +-$y**-n to +-1/$y**n
1573 10142 ▶ $s = 0 if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 178 9964 10142
1574 10142 $s;
1575 }
1576
1577
1578 =head3 constants
1579
1580 Useful constants
1581
1582 =cut
1583
1584
1585 16 ▶$zero = sigma(term('0')); sub zero() {$zero}
Subroutine zero: called CC=1 SCAR=0.0
Pod undocumented
1586 33 ▶$one = sigma(term('1')); sub one() {$one}
Subroutine one: called CC=1 SCAR=0.0
Pod undocumented
1587 0 ▶$two = sigma(term('2')); sub two() {$two}
Subroutine two: not called CC=1 SCAR=1.2
Pod undocumented
1588 0 ▶$four = sigma(term('4')); sub four() {$four}
Subroutine four: not called CC=1 SCAR=1.2
Pod undocumented
1589 0 ▶$mOne = sigma(term('-1')); sub mOne() {$mOne}
Subroutine mOne: not called CC=1 SCAR=1.2
Pod undocumented
1590 0 ▶$i = sigma(term('i')); sub i() {$i}
Subroutine i: not called CC=1 SCAR=1.2
Pod undocumented
1591 0 ▶$mI = sigma(term('-i')); sub mI() {$mI}
Subroutine mI: not called CC=1 SCAR=1.2
Pod undocumented
1592 0 ▶$half = sigma(term('1/2')); sub half() {$half}
Subroutine half: not called CC=1 SCAR=1.2
Pod undocumented
1593 0 ▶$mHalf = sigma(term('-1/2')); sub mHalf() {$mHalf}
Subroutine mHalf: not called CC=1 SCAR=1.2
Pod undocumented
1594 0 ▶$pi = sigma(term('pi')); sub pi() {$pi}
Subroutine pi: not called CC=1 SCAR=1.2
Pod undocumented
1595
1596
1597 =head3 factorize
1598
1599 Factorize a number.
1600
1601 =cut
1602
1603
1604 45 @primes = qw(
1605 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61
1606 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151
1607 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251
1608 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359
1609 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463
1610 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593
1611 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701
1612 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827
1613 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953
1614 967 971 977 983 991 997);
1615
1616 sub factorize($)
1617 0 ▶ {my ($n) = @_;
Subroutine factorize: not called CC=4 SCAR=30.0
Pod documented
1618 0 my $f;
1619
1620 0 for my $p(@primes)
1621 0 ▶ {for(;$n % $p == 0;)
Branch: 1 branch
BranchTrueFalseTotal
if ($n % $p == 0) 0 0 0
1622 0 {$f->{$p}++;
1623 0 $n /= $p;
1624 }
1625 0 ▶ last unless $n > $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $n > $p 0 0 0
1626 }
1627 0 $f;
1628 };
1629
1630
1631 =head2 import
1632
1633 Export L</n> with either the default name B<sums>, or a name supplied by
1634 the caller of this package.
1635
1636 =cut
1637
1638
1639 sub import
1640 45 ▶ {my %P = (program=>@_);
Subroutine import: called CC=4 SCAR=13.9
1641 45 my %p; $p{lc()} = $P{$_} for(keys(%P));
1642
1643 #_______________________________________________________________________
1644 # New sum constructor - export to calling package.
1645 #_______________________________________________________________________
1646
1647 45 my $s = "package XXXX;\n". <<'END';
1648 no warnings 'redefine';
1649 sub NNNN
1650 {return SSSSn(@_);
1651 }
1652 use warnings 'redefine';
1653 END
1654
1655 #_______________________________________________________________________
1656 # Export to calling package.
1657 #_______________________________________________________________________
1658
1659 45 my $name = 'sum';
1660 45 ▶ $name = $p{sum} if exists($p{sum});
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"sum"} 0 45 45
1661 45 my ($main) = caller();
1662 45 my $pack = __PACKAGE__ . '::';
1663
1664 45 $s=~ s/XXXX/$main/g;
1665 45 $s=~ s/NNNN/$name/g;
1666 45 $s=~ s/SSSS/$pack/g;
1667 45 ▶ eval($s);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sum: called CC=1 SCAR=0.0
Pod undocumented
1668
1669 #_______________________________________________________________________
1670 # Check options supplied by user
1671 #_______________________________________________________________________
1672
1673 45 delete @p{qw(program sum)};
1674
1675 45 ▶ croak "Unknown option(s): ". join(' ', keys(%p))."\n\n". <<'END' if keys(%p);
Branch: 1 branch
BranchTrueFalseTotal
if keys %p 0 45 45
1676
1677 Valid options are:
1678
1679 sum =>'name' Create a routine with this name in the callers
1680 namespace to create new symbols. The default is
1681 'sum'.
1682 END
1683 }
1684
1685
1686 =head2 Operators
1687
1688
1689 =head3 Operator Overloads
1690
1691 Overload Perl operators. Beware the low priority of B<^>.
1692
1693 =cut
1694
1695
1696 use overload
1697 45 '+' =>\&add3,
1698 '-' =>\&negate3,
1699 '*' =>\&multiply3,
1700 '/' =>\&divide3,
1701 '**' =>\&power3,
1702 '==' =>\&equals3,
1703 '!=' =>\&nequal3,
1704 'eq' =>\&negate3,
1705 '>' =>\&solve3,
1706 '<=>' =>\&tequals3,
1707 'sqrt' =>\&sqrt3,
1708 'exp' =>\&exp3,
1709 'log' =>\&log3,
1710 #'tan' =>\&tan3, # 2016/01/20 15:23:53 No longer available
1711 'sin' =>\&sin3,
1712 'cos' =>\&cos3,
1713 '""' =>\&print3,
1714 '^' =>\&dot3, # Beware the low priority of this operator
1715 '~' =>\&conjugate3,
1716 'x' =>\&cross3,
1717 'abs' =>\&modulus3,
1718 '!' =>\&unit3,
1719 45 ▶ fallback=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1720
1721
1722 =head3 add3
1723
1724 Add operator.
1725
1726 =cut
1727
1728
1729 sub add3
1730 282 ▶ {my ($a, $b) = @_;
Subroutine add3: called CC=5 SCAR=16.6
Pod documented
1731 282 ▶ return simplify($a) unless defined($b); # += : simplify()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 13 269 282
1732 269 ▶ $b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 57 212 269
1733 269 ▶ $a->{z} and $b->{z} or die "Add using unfinalized sums";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} and $b->{"z"} or die "Add using unfinalized sums" 0 269 269
Condition33%1/3
$a->{"z"} and $b->{"z"}
!ll&&!rl&&r
00269
MC/DC0%0/2
$a->{"z"} and $b->{"z"}
$a->{"z"} $b->{"z"}
Truth table33%1/3
A and B
A= $a->{"z"}B= $b->{"z"}
ABresult
0X0
100
111
1734 269 $a->add($b);
1735 }
1736
1737
1738 =head3 negate3
1739
1740 Negate operator. Used in combination with the L</add3> operator to
1741 perform subtraction.
1742
1743 =cut
1744
1745
1746 sub negate3
1747 244 ▶ {my ($a, $b, $c) = @_;
Subroutine negate3: called CC=8 SCAR=24.7
Pod documented
1748
1749 244 ▶ if (defined($b))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $b) { } 244 0 244
1750 244 ▶ {$b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 117 127 244
1751 244 ▶ $a->{z} and $b->{z} or die "Negate using unfinalized sums";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} and $b->{"z"} or die "Negate using unfinalized sums" 0 244 244
Condition33%1/3
$a->{"z"} and $b->{"z"}
!ll&&!rl&&r
00244
MC/DC0%0/2
$a->{"z"} and $b->{"z"}
$a->{"z"} $b->{"z"}
Truth table33%1/3
A and B
A= $a->{"z"}B= $b->{"z"}
ABresult
0X0
100
111
1752 244 ▶ return $b->subtract($a) if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 83 161 244
1753 161 ▶ return $a->subtract($b) unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 161 0 161
1754 }
1755 else
1756 0 ▶ {$a->{z} or die "Negate single unfinalized terms";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Negate single unfinalized terms" 0 0 0
1757 0 return $a->negate;
1758 }
1759 }
1760
1761
1762 =head3 multiply3
1763
1764 Multiply operator.
1765
1766 =cut
1767
1768
1769 sub multiply3
1770 416 ▶ {my ($a, $b) = @_;
Subroutine multiply3: called CC=4 SCAR=14.6
Pod documented
1771 416 ▶ $b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 121 295 416
1772 416 ▶ $a->{z} and $b->{z} or die "Multiply using unfinalized sums";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} and $b->{"z"} or die "Multiply using unfinalized sums" 0 416 416
Condition33%1/3
$a->{"z"} and $b->{"z"}
!ll&&!rl&&r
00416
MC/DC0%0/2
$a->{"z"} and $b->{"z"}
$a->{"z"} $b->{"z"}
Truth table33%1/3
A and B
A= $a->{"z"}B= $b->{"z"}
ABresult
0X0
100
111
1773 416 $a->multiply($b);
1774 }
1775
1776
1777 =head3 divide3
1778
1779 Divide operator.
1780
1781 =cut
1782
1783
1784 sub divide3
1785 167 ▶ {my ($a, $b, $c) = @_;
Subroutine divide3: called CC=6 SCAR=18.8
Pod documented
1786 167 ▶ $b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 102 65 167
1787 167 ▶ $a->{z} and $b->{z} or die "Divide using unfinalized sums";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} and $b->{"z"} or die "Divide using unfinalized sums" 0 167 167
Condition33%1/3
$a->{"z"} and $b->{"z"}
!ll&&!rl&&r
00167
MC/DC0%0/2
$a->{"z"} and $b->{"z"}
$a->{"z"} $b->{"z"}
Truth table33%1/3
A and B
A= $a->{"z"}B= $b->{"z"}
ABresult
0X0
100
111
1788 167 ▶ return $b->divide($a) if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 24 143 167
1789 143 ▶ return $a->divide($b) unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 143 0 143
1790 }
1791
1792
1793 =head3 power3
1794
1795 Power operator.
1796
1797 =cut
1798
1799
1800 sub power3
1801 173 ▶ {my ($a, $b) = @_;
Subroutine power3: called CC=4 SCAR=15.6
Pod documented
1802 173 ▶ $b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 173 0 173
1803 173 ▶ $a->{z} and $b->{z} or die "Power using unfinalized sums";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} and $b->{"z"} or die "Power using unfinalized sums" 0 173 173
Condition33%1/3
$a->{"z"} and $b->{"z"}
!ll&&!rl&&r
00173
MC/DC0%0/2
$a->{"z"} and $b->{"z"}
$a->{"z"} $b->{"z"}
Truth table33%1/3
A and B
A= $a->{"z"}B= $b->{"z"}
ABresult
0X0
100
111
1804 173 $a->power($b);
1805 }
1806
1807
1808 =head3 equals3
1809
1810 Equals operator.
1811
1812 =cut
1813
1814
1815 sub equals3
1816 259 ▶ {my ($a, $b) = @_;
Subroutine equals3: called CC=6 SCAR=18.2
Pod documented
1817 259 ▶ $b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 66 193 259
1818 259 ▶ $a->{z} and $b->{z} or die "Equals using unfinalized sums";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} and $b->{"z"} or die "Equals using unfinalized sums" 0 259 259
Condition33%1/3
$a->{"z"} and $b->{"z"}
!ll&&!rl&&r
00259
MC/DC0%0/2
$a->{"z"} and $b->{"z"}
$a->{"z"} $b->{"z"}
Truth table33%1/3
A and B
A= $a->{"z"}B= $b->{"z"}
ABresult
0X0
100
111
1819
1820 259 ▶ return 1 if $a->{id} == $b->{id}; # Fast equals
Branch: 1 branch
BranchTrueFalseTotal
if $a->{"id"} == $b->{"id"} 201 58 259
1821
1822 58 my $c = $a->subtract($b);
1823
1824 58 ▶ return 1 if $c->isZero()->{id} == $zero->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $c->isZero->{"id"} == $zero->{"id"} 49 9 58
1825 9 return 0;
1826 }
1827
1828
1829 =head3 nequal3
1830
1831 Not equal operator.
1832
1833 =cut
1834
1835
1836 sub nequal3
1837 9 ▶ {my ($a, $b) = @_;
Subroutine nequal3: called CC=1 SCAR=0.0
Pod documented
1838 9 !equals3($a, $b);
1839 }
1840
1841
1842 =head3 tequals
1843
1844 Evaluate the expression on the left hand side, stringify it, then
1845 compare it for string equality with the string on the right hand side.
1846 This operator is useful for making examples written with Test::Simple
1847 more readable.
1848
1849 =cut
1850
1851
1852 sub tequals3
1853 27 ▶ {my ($a, $b) = @_;
Subroutine tequals3: called CC=2 SCAR=6.9
Pod undocumented
1854
1855 27 ▶ return 1 if "$a" eq $b;
Branch: 1 branch
BranchTrueFalseTotal
if "$a" eq $b 25 2 27
1856
1857 2 my $z = simplify($a);
1858
1859 2 "$z" eq "$b";
1860 }
1861
1862
1863 =head3 solve3
1864
1865 Solve operator.
1866
1867 =cut
1868
1869
1870 sub solve3
1871 7 ▶ {my ($a, $b) = @_;
Subroutine solve3: called CC=2 SCAR=7.1
Pod documented
1872 7 ▶ $a->{z} or die "Solve using unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Solve using unfinalized sum" 0 7 7
1873 # $b =~ /^[a-z]+$/i or croak "Bad variable $b to solve for";
1874 7 solve($a, $b);
1875 }
1876
1877
1878 =head3 print3
1879
1880 Print operator.
1881
1882 =cut
1883
1884
1885 sub print3
1886 70237 ▶ {my ($a) = @_;
Subroutine print3: called CC=2 SCAR=7.1
Pod documented
1887 70237 ▶ $a->{z} or die "Print of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Print of unfinalized sum" 0 70237 70237
1888 70237 $a->print();
1889 }
1890
1891
1892 =head3 sqrt3
1893
1894 Sqrt operator.
1895
1896 =cut
1897
1898
1899 sub sqrt3
1900 55 ▶ {my ($a) = @_;
Subroutine sqrt3: called CC=2 SCAR=7.1
Pod documented
1901 55 ▶ $a->{z} or die "Sqrt of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Sqrt of unfinalized sum" 0 55 55
1902 55 $a->Sqrt();
1903 }
1904
1905
1906 =head3 exp3
1907
1908 Exp operator.
1909
1910 =cut
1911
1912
1913 sub exp3
1914 22 ▶ {my ($a) = @_;
Subroutine exp3: called CC=2 SCAR=7.1
Pod documented
1915 22 ▶ $a->{z} or die "Exp of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Exp of unfinalized sum" 0 22 22
1916 22 $a->Exp();
1917 }
1918
1919
1920 =head3 sin3
1921
1922 Sine operator.
1923
1924 =cut
1925
1926
1927 sub sin3
1928 87 ▶ {my ($a) = @_;
Subroutine sin3: called CC=2 SCAR=7.1
Pod documented
1929 87 ▶ $a->{z} or die "Sin of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Sin of unfinalized sum" 0 87 87
1930 87 $a->Sin();
1931 }
1932
1933
1934 =head3 cos3
1935
1936 Cosine operator.
1937
1938 =cut
1939
1940
1941 sub cos3
1942 92 ▶ {my ($a) = @_;
Subroutine cos3: called CC=2 SCAR=7.1
Pod documented
1943 92 ▶ $a->{z} or die "Cos of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Cos of unfinalized sum" 0 92 92
1944 92 $a->Cos();
1945 }
1946
1947
1948 =head3 tan3
1949
1950 Tan operator.
1951
1952 =cut
1953
1954
1955 sub tan3
1956 0 ▶ {my ($a) = @_;
Subroutine tan3: not called CC=2 SCAR=17.9
Pod documented
1957 0 ▶ $a->{z} or die "Tan of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Tan of unfinalized sum" 0 0 0
1958 0 $a->tan();
1959 }
1960
1961
1962 =head3 log3
1963
1964 Log operator.
1965
1966 =cut
1967
1968
1969 sub log3
1970 1 ▶ {my ($a) = @_;
Subroutine log3: called CC=2 SCAR=7.1
Pod documented
1971 1 ▶ $a->{z} or die "Log of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Log of unfinalized sum" 0 1 1
1972 1 $a->Log();
1973 }
1974
1975
1976 =head3 dot3
1977
1978 Dot Product operator.
1979
1980 =cut
1981
1982
1983 sub dot3
1984 14 ▶ {my ($a, $b, $c) = @_;
Subroutine dot3: called CC=4 SCAR=14.6
Pod documented
1985 14 ▶ $b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 1 13 14
1986 14 ▶ $a->{z} and $b->{z} or die "Dot of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} and $b->{"z"} or die "Dot of unfinalized sum" 0 14 14
Condition33%1/3
$a->{"z"} and $b->{"z"}
!ll&&!rl&&r
0014
MC/DC0%0/2
$a->{"z"} and $b->{"z"}
$a->{"z"} $b->{"z"}
Truth table33%1/3
A and B
A= $a->{"z"}B= $b->{"z"}
ABresult
0X0
100
111
1987 14 dot($a, $b);
1988 }
1989
1990
1991 =head3 cross3
1992
1993 Cross operator.
1994
1995 =cut
1996
1997
1998 sub cross3
1999 11 ▶ {my ($a, $b, $c) = @_;
Subroutine cross3: called CC=4 SCAR=14.6
Pod documented
2000 11 ▶ $b = newFromString("$b") unless ref($b) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b eq __PACKAGE__ 2 9 11
2001 11 ▶ $a->{z} and $b->{z} or die "Cross of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} and $b->{"z"} or die "Cross of unfinalized sum" 0 11 11
Condition33%1/3
$a->{"z"} and $b->{"z"}
!ll&&!rl&&r
0011
MC/DC0%0/2
$a->{"z"} and $b->{"z"}
$a->{"z"} $b->{"z"}
Truth table33%1/3
A and B
A= $a->{"z"}B= $b->{"z"}
ABresult
0X0
100
111
2002 11 cross($a, $b);
2003 }
2004
2005
2006 =head3 unit3
2007
2008 Unit operator.
2009
2010 =cut
2011
2012
2013 sub unit3
2014 9 ▶ {my ($a, $b, $c) = @_;
Subroutine unit3: called CC=2 SCAR=7.1
Pod documented
2015 9 ▶ $a->{z} or die "Unit of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Unit of unfinalized sum" 0 9 9
2016 9 unit($a);
2017 }
2018
2019
2020 =head3 modulus3
2021
2022 Modulus operator.
2023
2024 =cut
2025
2026
2027 sub modulus3
2028 26 ▶ {my ($a, $b, $c) = @_;
Subroutine modulus3: called CC=2 SCAR=7.1
Pod documented
2029 26 ▶ $a->{z} or die "Modulus of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Modulus of unfinalized sum" 0 26 26
2030 26 modulus($a);
2031 }
2032
2033
2034 =head3 conjugate3
2035
2036 Conjugate.
2037
2038 =cut
2039
2040
2041 sub conjugate3
2042 10 ▶ {my ($a, $b, $c) = @_;
Subroutine conjugate3: called CC=2 SCAR=7.1
Pod documented
2043 10 ▶ $a->{z} or die "Conjugate of unfinalized sum";
Branch: 1 branch
BranchTrueFalseTotal
$a->{"z"} or die "Conjugate of unfinalized sum" 0 10 10
2044 10 conjugate($a);
2045 }
2046
2047 #________________________________________________________________________
2048 # Package installed successfully
2049 #________________________________________________________________________
2050
2051 45 1;
« blib/lib/Math/Algebra/Symbols/Symbols.pm ↑ Summary blib/lib/Math/Algebra/Symbols.pm »