Monomial.pm

filter: Statementstmt 96.8% 249 / 257 Branchbran 88.8% 48 / 54 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 21 / 21 Podpod 94.7% 18 / 19 totaltotal 95.7% 341 / 356 CC 39 SCAR 15.3
CC
39
Cov
96%
toString
11.4
fromString
6.0
getSignature
4.0
CRAP 4.6
« Polynomial.pm ↑ Summary MVPoly.pm »
1 package Math::MVPoly::Monomial;
2
3 # Copyright (c) 1998 by Brian Guarraci. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Math::MVPoly::Integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 sub
11 new
12 120 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
13 120 my $self;
14
15 120 $self = {};
16
17 120 $self->{COEFFICIENT} = undef;
18 120 $self->{VARIABLES} = {};
19 120 $self->{VARORDER} = [];
20 120 $self->{VERBOSE} = 1;
21
22 120 bless($self);
23 120 return $self;
24 }
25
26 sub
27 copy
28 88 ▶{
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
29 88 my $self = shift;
30 88 my $m = shift;
31 88 my $vars;
32 my $f;
33 88 my $copy_vars;
34 88 my $varOrder;
35 88 my $copy_varOrder;
36
37 88 $self->coefficient($m->coefficient());
38
39 88 $vars = $m->variables();
40 88 $copy_vars = {%$vars};
41 88 $self->variables($copy_vars);
42
43 88 $varOrder = $m->varOrder();
44 88 $copy_varOrder = [@$varOrder];
45 88 $self->varOrder($copy_varOrder);
46 }
47
48 sub
49 coefficient
50 601 ▶{
Subroutine coefficient: called CC=2 SCAR=6.9
Pod documented
51 601 my $self = shift;
52 601 ▶ if (@_) { $self->{COEFFICIENT} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 167 434 601
53 601 return $self->{COEFFICIENT};
54 }
55
56 sub
57 variables
58 1193 ▶{
Subroutine variables: called CC=2 SCAR=6.9
Pod documented
59 1193 my $self = shift;
60 1193 ▶ if (@_) { $self->{VARIABLES} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 120 1073 1193
61 1193 return $self->{VARIABLES};
62 }
63
64 sub
65 varOrder
66 460 ▶{
Subroutine varOrder: called CC=2 SCAR=6.9
Pod documented
67 460 my $self = shift;
68 460 ▶ if (@_) { $self->{VARORDER} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 111 349 460
69 460 return $self->{VARORDER};
70 }
71
72 sub
73 verbose
74 98 ▶{
Subroutine verbose: called CC=2 SCAR=6.9
Pod undocumented
75 98 my $self = shift;
76 98 ▶ if (@_) { $self->{VERBOSE} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 49 49 98
77 98 return $self->{VERBOSE};
78 }
79
80 sub
81 fromString
82 32 ▶{
Subroutine fromString: called CC=6 SCAR=17.9
Pod documented
83 32 my $self = shift();
84 32 my $string = shift();
85 32 my $i;
86 my $c;
87 32 my $h;
88 32 my $p;
89 32 my $f;
90 32 my @ve;
91 32 my @parts;
92 32 my $buildVarOrder;
93
94 32 @parts = grep {/\S/} ($string =~ /(^[+-]?\d+\.\d+)|(^[+-]?\d+)|(^[+-])|([A-Za-z]\^\d+)|([A-Za-z])/g);
95
96 # determine the coefficient
97 32 $c = $parts[0];
98 32 $i = 1;
99
100 # see if $c is numeric, otherwise deduce the coefficient
101 32 ▶ if ($c !~ /^[+-]?\d+/g)
Branch: 1 branch
BranchTrueFalseTotal
unless ($c =~ /^[+-]?\d+/g) 19 13 32
102 {
103 19 ▶ if ($c eq "-")
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq '-') { } 6 13 19
elsif ($c eq '+') { } 7 6 13
104 {
105 6 $c = -1;
106 }
107 elsif ($c eq "+")
108 {
109 7 $c = 1;
110 }
111 else
112 {
113 6 $i = 0;
114 6 $c = 1;
115 }
116 }
117
118 32 $self->coefficient($c*1);
119
120 # build the variable/exponent pairs
121 32 $h = {};
122
123 32 foreach $p (@parts[$i..$#parts])
124 {
125 # if there is an exponent, extract it, otherwise default to 1
126 40 ▶ if ($p =~ /\^/)
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /\^/) { } 22 18 40
127 {
128 22 @ve = split(/\^/,$p);
129 22 $h->{$ve[0]} = $ve[1];
130 }
131 else
132 {
133 18 $h->{$p} = 1;
134 }
135 }
136
137 32 $self->variables($h);
138 }
139
140 sub
141 toString
142 49 ▶{
Subroutine toString: called CC=11 SCAR=24.3
Pod documented
143 49 my $self = shift();
144 49 my $s;
145 my $myVars;
146 49 my $varOrder;
147 49 my $f;
148 49 my $varsExist;
149 49 my @nd;
150 49 my $c;
151
152 49 @nd = $self->coeff_to_ND();
153
154 49 $c = $nd[0]/$nd[1];
155
156 49 ▶ if ($c > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($c > 0) 27 22 49
157 {
158 27 $s = "+";
159 }
160
161 49 $myVars = $self->variables();
162 49 $varOrder = $self->varOrder();
163
164 49 ▶ if ($self->verbose())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) { } 0 49 49
165 {
166 0 $s .= $self->coefficient();
167
168 0 foreach $f (@$varOrder)
169 {
170 0 ▶ if (exists($myVars->{$f}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $myVars->{$f}) 0 0 0
171 {
172 0 $s .= "$f^".$myVars->{$f};
173 }
174 }
175 }
176 else
177 {
178 49 ▶ if (abs($c) != 1)
Branch: 2 branches
BranchTrueFalseTotal
if (abs $c != 1) { } 4 45 49
elsif ($self->coefficient == -1) { } 20 25 45
179 {
180 4 $s .= $self->coefficient();
181 }
182 elsif ($self->coefficient() == -1)
183 {
184 20 $s = "-";
185 }
186
187 49 $varsExist = 0;
188
189 49 foreach $f (@$varOrder)
190 {
191 127 ▶ if (exists($myVars->{$f}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $myVars->{$f}) 94 33 127
192 {
193 94 $varsExist = 1;
194 94 $s .= "$f";
195 94 ▶ if ($myVars->{$f} > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($myVars->{$f} > 1) 48 46 94
196 {
197 48 $s .= "^".$myVars->{$f};
198 }
199 }
200 }
201
202 49 ▶ if (! $varsExist)
Branch: 1 branch
BranchTrueFalseTotal
unless ($varsExist) 4 45 49
203 {
204 4 $s .= "1";
205 }
206 }
207
208 49 return $s;
209 }
210
211 sub
212 getSignature
213 154 ▶{
Subroutine getSignature: called CC=4 SCAR=13.9
Pod documented
214 154 my $self = shift();
215 154 my $s;
216 my $myVars;
217 154 my $varOrder;
218 154 my $f;
219
220 154 ▶ if ($self->getTotalDegree() == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getTotalDegree == 0) { } 19 135 154
221 {
222 19 $s = "CONST";
223 }
224 else
225 {
226 135 $myVars = $self->variables();
227 135 $varOrder = $self->varOrder();
228
229 135 foreach $f (@$varOrder)
230 {
231 331 ▶ if (exists($myVars->{$f}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $myVars->{$f}) 227 104 331
232 {
233 227 $s .= "$f^".$myVars->{$f};
234 }
235 }
236 }
237
238 154 return $s;
239 }
240
241 sub
242 getTotalDegree
243 518 ▶{
Subroutine getTotalDegree: called CC=2 SCAR=6.9
Pod documented
244 518 my $self = shift();
245 518 my $deg;
246 my $vars;
247 518 my $f;
248
249 518 $vars = $self->variables();
250
251 518 $deg = 0;
252 518 foreach $f (keys %$vars)
253 {
254 965 $deg += $vars->{$f};
255 }
256
257 518 return $deg;
258 }
259
260 sub
261 canDivide
262 7 ▶{
Subroutine canDivide: called CC=4 SCAR=13.9
Pod documented
263 7 my $self = shift;
264 7 my $m = shift;
265 7 my $flag;
266 my $myVars;
267 7 my $vars;
268 7 my $f;
269
270 # does $m have a subset of vars?
271 # Is each corresponding exponent <=?
272
273 7 $flag = 1;
274
275 7 $myVars = $self->variables();
276 7 $vars = $m->variables();
277
278 7 foreach $f (keys %$myVars)
279 {
280 7 ▶ if (! exists($vars->{$f}) || $myVars->{$f} > $vars->{$f})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $vars->{$f} or $myVars->{$f} > $vars->{$f}) 4 3 7
Condition100%3/3
not exists $vars->{$f} or $myVars->{$f} > $vars->{$f}
l!l&&r!l&&!r
223
MC/DC100%2/2
not exists $vars->{$f} or $myVars->{$f} > $vars->{$f}
not exists $vars->{$f} $myVars->{$f} > $vars->{$f}
Truth table100%3/3
not(A) or B
A= not exists $vars->{$f}B= $myVars->{$f} > $vars->{$f}
ABresult
1X1
011
000
281 {
282 4 $flag = 0;
283 4 last;
284 }
285 }
286
287 7 return $flag;
288 }
289
290 sub
291 add
292 10 ▶{
Subroutine add: called CC=1 SCAR=0.0
Pod documented
293 10 my $self = shift;
294 10 my $m = shift;
295 10 my $nm;
296 my @pa;
297 10 my @qa;
298 10 my $numer;
299 10 my $denom;
300 10 my $x;
301 10 my $y;
302 10 my $k;
303 10 my $j;
304
305 10 $nm = Math::MVPoly::Monomial->new();
306 10 $nm->copy($self);
307
308 #
309 # calc. the coefficient
310 #
311 10 @qa = $self->coeff_to_ND();
312 10 @pa = $m->coeff_to_ND();
313
314 # x/y + k/j = (yk+jx)/yj
315
316 10 $x = $qa[0];
317 10 $y = $qa[1];
318 10 $k = $pa[0];
319 10 $j = $pa[1];
320
321 10 $numer = $y*$k + $j*$x;
322 10 $denom = $y*$j;
323
324 10 $nm->coeff_from_ND($numer,$denom);
325
326 10 return $nm;
327 }
328
329 sub
330 divide
331 7 ▶{
Subroutine divide: called CC=3 SCAR=11.0
Pod documented
332 7 my $self = shift;
333 7 my $m = shift;
334 7 my $nm;
335 my $nmVars;
336 7 my $vars;
337 7 my $f;
338 7 my @pa;
339 7 my @qa;
340 7 my $numer;
341 7 my $denom;
342
343 7 $nm = Math::MVPoly::Monomial->new();
344 7 $nm->copy($self);
345
346 #
347 # calc. the coefficient
348 #
349 7 @qa = $self->coeff_to_ND();
350 7 @pa = $m->coeff_to_ND();
351
352 7 $numer = $qa[0]*$pa[1];
353 7 $denom = $qa[1]*$pa[0];
354
355 7 $nm->coeff_from_ND($numer,$denom);
356
357 #
358 # determine the variables and exponents
359 #
360 7 $nmVars = $nm->variables();
361 7 $vars = $m->variables();
362
363 7 foreach $f (keys %$vars)
364 {
365 11 ▶ if (! exists($nmVars->{$f}))
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $nmVars->{$f}) { } 0 11 11
366 {
367 0 $nmVars->{$f} = $vars->{$f};;
368 }
369 else
370 {
371 11 $nmVars->{$f} -= $vars->{$f};
372 }
373 }
374
375 # at this point, new variables may be present in the monomial
376 # and/or one or more exponents may now be 0, so reduce.
377 7 $nm->reduceVariables();
378
379 7 return $nm;
380 }
381
382 sub
383 mult
384 30 ▶{
Subroutine mult: called CC=3 SCAR=11.0
Pod documented
385 30 my $self = shift;
386 30 my $m = shift;
387 30 my $nm;
388 my $nmVars;
389 30 my $vars;
390 30 my $f;
391 30 my @pa;
392 30 my @qa;
393 30 my $numer;
394 30 my $denom;
395
396 30 $nm = Math::MVPoly::Monomial->new();
397 30 $nm->copy($self);
398
399 #
400 # calc. the coefficient
401 #
402 30 @qa = $self->coeff_to_ND();
403 30 @pa = $m->coeff_to_ND();
404
405 30 $numer = $qa[0]*$pa[0];
406 30 $denom = $qa[1]*$pa[1];
407
408 30 $nm->coeff_from_ND($numer,$denom);
409
410 #
411 # determine the variables and exponents
412 #
413 30 $nmVars = $nm->variables();
414 30 $vars = $m->variables();
415
416 30 foreach $f (keys %$vars)
417 {
418 16 ▶ if (! exists($nmVars->{$f}))
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $nmVars->{$f}) { } 2 14 16
419 {
420 2 $nmVars->{$f} = $vars->{$f};;
421 }
422 else
423 {
424 14 $nmVars->{$f} += $vars->{$f};
425 }
426 }
427
428 # at this point, new variables may be present in the monomial
429 # and/or one or more exponents may now be 0, so reduce.
430 30 $nm->reduceVariables();
431
432 30 return $nm;
433 }
434
435 sub
436 coeff_to_ND
437 287 ▶{
Subroutine coeff_to_ND: called CC=2 SCAR=6.9
Pod documented
438 287 my $self = shift;
439 287 my $x;
440 my @xa;
441
442 287 $x = $self->coefficient();
443
444 287 @xa = ($x,1);
445
446 287 ▶ if ($x =~ /\//)
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ m[/]) 14 273 287
447 {
448 14 @xa = split('\/',$x);
449 }
450
451 287 return (@xa);
452 }
453
454 sub
455 coeff_from_ND
456 47 ▶{
Subroutine coeff_from_ND: called CC=2 SCAR=6.9
Pod documented
457 47 my $self = shift;
458 47 my $numer = shift;
459 47 my $denom = shift;
460 47 my $c;
461
462 47 ▶ if ($numer/$denom != int($numer/$denom))
Branch: 1 branch
BranchTrueFalseTotal
if ($numer / $denom != int $numer / $denom) { } 6 41 47
463 {
464 6 $c = "$numer/$denom";
465 }
466 else
467 {
468 41 $c = $numer/$denom;
469 }
470
471 47 $self->coefficient($c);
472 }
473
474 sub
475 reduceCoefficient
476 144 ▶{
Subroutine reduceCoefficient: called CC=3 SCAR=12.3
Pod documented
477 144 my $self = shift;
478 144 my $i;
479 my $j;
480 144 my $r;
481
482 144 ($i,$j) = $self->coeff_to_ND();
483 144 ▶ if ($i != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 0) 144 0 144
484 {
485 144 $r = Math::MVPoly::Integer::gcd($i,$j);
486 }
487 144 ▶ if ($r > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($r > 1) 0 144 144
488 {
489 0 $i /= $r;
490 0 $j /= $r;
491 0 $self->coeff_from_ND($i,$j);
492 }
493 }
494
495 sub
496 reduceVariables
497 37 ▶{
Subroutine reduceVariables: called CC=3 SCAR=11.0
Pod documented
498 37 my $self = shift;
499 37 my $vars;
500 my $k;
501
502 37 $vars = $self->variables();
503
504 37 foreach $k (keys %$vars)
505 {
506 47 ▶ if ($vars->{$k} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($vars->{$k} == 0) 4 43 47
507 {
508 4 delete($vars->{$k});
509 }
510 }
511 }
512
513 sub
514 getLCM
515 4 ▶{
Subroutine getLCM: called CC=3 SCAR=11.0
Pod documented
516 4 my $self = shift;
517 4 my $b = shift;
518 4 my $varOrder;
519 my $f;
520 4 my $lcm;
521 4 my $a_vars;
522 4 my $b_vars;
523 4 my $lcm_vars;
524 4 my $ea;
525 4 my $eb;
526 4 my $e;
527
528 4 $lcm = Math::MVPoly::Monomial->new();
529 4 $lcm->copy($self);
530
531 4 $a_vars = $self->variables();
532 4 $b_vars = $b->variables();
533 4 $lcm_vars = $lcm->variables();
534
535 4 $varOrder = $self->varOrder();
536
537 4 foreach $f (@$varOrder)
538 {
539 8 $ea = $a_vars->{$f};
540 8 $eb = $b_vars->{$f};
541 8 ▶ $lcm_vars->{$f} = ($ea > $eb ? $ea : $eb);
Branch: 1 branch
BranchTrueFalseTotal
$ea > $eb ? : 4 4 8
542 }
543
544 4 return $lcm;
545 }
546
547 1 1;
548
549 __END__
« Polynomial.pm ↑ Summary MVPoly.pm »