| 1 | # Copyright (c) 2011-2022 Martin Becker, Blaubeuren. | ||||||||||||||||||||||||||||
| 2 | # This package is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 3 | # under the terms of the Artistic License 2.0 (see LICENSE file). | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Math::Polynomial::Multivariate; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use overload ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | q[neg] => \&_neg, | |||||||||||||||||||||||||||
| 14 | q[+] => \&_add, | ||||||||||||||||||||||||||||
| 15 | q[-] => \&_sub, | ||||||||||||||||||||||||||||
| 16 | q[*] => \&_mul, | ||||||||||||||||||||||||||||
| 17 | q[**] => \&_pow, | ||||||||||||||||||||||||||||
| 18 | q[==] => \&_eq, | ||||||||||||||||||||||||||||
| 19 | q[!=] => \&_ne, | ||||||||||||||||||||||||||||
| 20 | q[!] => 'is_null', | ||||||||||||||||||||||||||||
| 21 | q[bool] => 'is_not_null', | ||||||||||||||||||||||||||||
| 22 | q[""] => 'as_string', | ||||||||||||||||||||||||||||
| 23 | q[fallback] => undef, | ||||||||||||||||||||||||||||
| 24 | 5 | ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # ----- object definition ----- | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Math::Polynomial::Multivariate=ARRAY(...) | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # .......... index .......... # .......... value .......... | ||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use constant F_TERMS => 0; # monomial terms hashref | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 5 | ▶ | use constant F_ZERO => 1; # zero element of coefficient space | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 5 | ▶ | use constant F_ONE => 2; # unit element of coefficient space | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | use constant NFIELDS => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # a term value is an anonymous arrayref: | ||||||||||||||||||||||||||||
| 37 | # .......... index .......... # .......... value .......... | ||||||||||||||||||||||||||||
| 38 | 5 | ▶ | use constant T_COEFF => 0; # coefficient | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 5 | ▶ | use constant T_VARS => 1; # variables hashref, mapping names to exponents | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 5 | ▶ | use constant K_SEP => chr 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 5 | ▶ | use constant K_SEP_RE => qr/\0/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 5 | ▶ | use constant _MINUS_INFINITY => - (~0) ** (~0); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # ----- class data ----- | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | our $VERSION = '0.007'; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # ----- private subroutines ----- | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # create a "clean" copy of a variables hashref, without zero exponents | ||||||||||||||||||||||||||||
| 53 | 14 | ▶ | sub _vclean { | ||||||||||||||||||||||||||
|
Subroutine
_vclean: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 14 | my %vars = %{$_[0]}; | |||||||||||||||||||||||||||
| 55 | 14 | delete @vars{grep {!$vars{$_}} keys %vars}; | |||||||||||||||||||||||||||
| 56 | die "hash of integer exponents expected" | ||||||||||||||||||||||||||||
| 57 | 14 | ▶ | if grep {!eval { $_ == int abs $_ }} values %vars; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 13 | return \%vars; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # map a clean variables hashref to a key | ||||||||||||||||||||||||||||
| 62 | 117 | ▶ | sub _key { | ||||||||||||||||||||||||||
|
Subroutine
_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 117 | my ($vars) = @_; | |||||||||||||||||||||||||||
| 64 | return | ||||||||||||||||||||||||||||
| 65 | join K_SEP, | ||||||||||||||||||||||||||||
| 66 | 98 | map { ($_, chr $vars->{$_}) } | |||||||||||||||||||||||||||
| 67 | 117 | reverse sort keys %{$vars}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # map an arbitrary variables hashref to a key | ||||||||||||||||||||||||||||
| 71 | 8 | ▶ | sub _keyvc { | ||||||||||||||||||||||||||
|
Subroutine
_keyvc: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 8 | my ($vars) = @_; | |||||||||||||||||||||||||||
| 73 | return | ||||||||||||||||||||||||||||
| 74 | join K_SEP, | ||||||||||||||||||||||||||||
| 75 | 6 | map { ($_, chr $vars->{$_}) } | |||||||||||||||||||||||||||
| 76 | reverse sort | ||||||||||||||||||||||||||||
| 77 | 6 | grep { $vars->{$_} } | |||||||||||||||||||||||||||
| 78 | 8 | keys %{$vars}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # check if an arbitrary operand is a multivariate polynomial | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 104 | ▶ | sub _isa_pol ($) { ref($_[0]) && eval { $_[0]->isa(__PACKAGE__) } } | ||||||||||||||||||||||||||
|
Subroutine
_isa_pol: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # ----- private methods ----- | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # extract coefficient space information from an invocant and some coefficient | ||||||||||||||||||||||||||||
| 88 | 124 | ▶ | sub _space { | ||||||||||||||||||||||||||
|
Subroutine
_space: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 89 | 124 | my ($this, $const) = @_; | |||||||||||||||||||||||||||
| 90 | 124 | my $class = ref $this; | |||||||||||||||||||||||||||
| 91 | 124 | my ($zero, $one); | |||||||||||||||||||||||||||
| 92 | 124 | ▶ | if ($class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 113 | ($zero, $one) = @{$this}[F_ZERO, F_ONE]; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||
| 96 | 11 | $class = $this; | |||||||||||||||||||||||||||
| 97 | 11 | $zero = $const - $const; | |||||||||||||||||||||||||||
| 98 | 11 | $one = $const ** 0; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 124 | return ($class, $zero, $one); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # create an independent duplicate of an object | ||||||||||||||||||||||||||||
| 104 | 51 | ▶ | sub _clone { | ||||||||||||||||||||||||||
|
Subroutine
_clone: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 51 | my ($this) = @_; | |||||||||||||||||||||||||||
| 106 | 51 | my $terms = $this->[F_TERMS]; | |||||||||||||||||||||||||||
| 107 | return | ||||||||||||||||||||||||||||
| 108 | bless [ | ||||||||||||||||||||||||||||
| 109 | 61 | { map {($_ => [@{$terms->{$_}}])} keys %{$terms} }, | |||||||||||||||||||||||||||
| 110 | 51 | @{$this}[F_ZERO, F_ONE] | |||||||||||||||||||||||||||
| 111 | ], ref $this; | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # multiply a polynomial by variables with exponents | ||||||||||||||||||||||||||||
| 115 | 3 | ▶ | sub _mul_vars { | ||||||||||||||||||||||||||
|
Subroutine
_mul_vars: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 116 | 3 | my ($this, $vars) = @_; | |||||||||||||||||||||||||||
| 117 | return | ||||||||||||||||||||||||||||
| 118 | bless [ | ||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||
| 120 | map { | ||||||||||||||||||||||||||||
| 121 | 3 | my %tvars = %{$_->[T_VARS]}; | |||||||||||||||||||||||||||
| 122 | 3 | ▶ | while (my ($var, $exp) = each %{$vars}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 2 | $tvars{$var} += $exp; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 3 | _key(\%tvars) => [$_->[T_COEFF], \%tvars] | |||||||||||||||||||||||||||
| 126 | 3 | } values %{$this->[F_TERMS]} | |||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||
| 128 | 3 | @{$this}[F_ZERO, F_ONE] | |||||||||||||||||||||||||||
| 129 | ], ref $this; | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # overloaded operators | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub _neg { | ||||||||||||||||||||||||||
|
Subroutine
_neg: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 135 | 1 | my ($this) = @_; | |||||||||||||||||||||||||||
| 136 | 1 | my $result = $this->_clone; | |||||||||||||||||||||||||||
| 137 | 1 | foreach my $term (values %{$result->[F_TERMS]}) { | |||||||||||||||||||||||||||
| 138 | 1 | $term->[T_COEFF] = -$term->[T_COEFF]; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 1 | return $result; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 31 | ▶ | sub _add { | ||||||||||||||||||||||||||
|
Subroutine
_add: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 144 | 31 | my ($this, $that) = @_; | |||||||||||||||||||||||||||
| 145 | 31 | ▶ | $that = $this->const($that) if !_isa_pol($that); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 31 | my $result = $this->_clone; | |||||||||||||||||||||||||||
| 147 | 31 | my $rterms = $result->[F_TERMS]; | |||||||||||||||||||||||||||
| 148 | 31 | my $zero = $this->[F_ZERO]; | |||||||||||||||||||||||||||
| 149 | 31 | ▶ | while (my ($key, $term) = each %{$that->[F_TERMS]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 33 | ▶ | if (exists $rterms->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 7 | ▶ | if ($zero == ($rterms->{$key}->[T_COEFF] += $term->[T_COEFF])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 1 | delete $rterms->{$key}; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | else { | ||||||||||||||||||||||||||||
| 156 | 26 | $rterms->{$key} = [@{$term}]; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 31 | return $result; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 14 | ▶ | sub _sub { | ||||||||||||||||||||||||||
|
Subroutine
_sub: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 163 | 14 | my ($this, $that, $swap) = @_; | |||||||||||||||||||||||||||
| 164 | 14 | ▶ | $that = $this->const($that) if !_isa_pol($that); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 14 | ▶ | ($this, $that) = ($that, $this) if $swap; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 14 | my $result = $this->_clone; | |||||||||||||||||||||||||||
| 167 | 14 | my $rterms = $result->[F_TERMS]; | |||||||||||||||||||||||||||
| 168 | 14 | my $zero = $this->[F_ZERO]; | |||||||||||||||||||||||||||
| 169 | 14 | ▶ | while (my ($key, $term) = each %{$that->[F_TERMS]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 16 | ▶ | if (exists $rterms->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 7 | ▶ | if ($zero == ($rterms->{$key}->[T_COEFF] -= $term->[T_COEFF])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 6 | delete $rterms->{$key}; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||
| 176 | 9 | my $rterm = $rterms->{$key} = [@{$term}]; | |||||||||||||||||||||||||||
| 177 | 9 | $rterm->[T_COEFF] = -$rterm->[T_COEFF]; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 14 | return $result; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 40 | ▶ | sub _mul { | ||||||||||||||||||||||||||
|
Subroutine
_mul: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 184 | 40 | my ($this, $that) = @_; | |||||||||||||||||||||||||||
| 185 | 40 | ▶ | $that = $this->const($that) if !_isa_pol($that); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 40 | my $result = $this->null; | |||||||||||||||||||||||||||
| 187 | 40 | my $bterms = $that->[F_TERMS]; | |||||||||||||||||||||||||||
| 188 | 40 | my $rterms = $result->[F_TERMS]; | |||||||||||||||||||||||||||
| 189 | 40 | my $zero = $this->[F_ZERO]; | |||||||||||||||||||||||||||
| 190 | 40 | foreach my $aterm (values %{$this->[F_TERMS]}) { | |||||||||||||||||||||||||||
| 191 | 53 | my @avars = %{$aterm->[T_VARS]}; | |||||||||||||||||||||||||||
| 192 | 53 | foreach my $bterm (values %{$bterms}) { | |||||||||||||||||||||||||||
| 193 | 61 | my %vars = @avars; | |||||||||||||||||||||||||||
| 194 | 61 | my $const = $aterm->[T_COEFF] * $bterm->[T_COEFF]; | |||||||||||||||||||||||||||
| 195 | 61 | my $bvars = $bterm->[T_VARS]; | |||||||||||||||||||||||||||
| 196 | 61 | foreach my $bv (keys %{$bvars}) { | |||||||||||||||||||||||||||
| 197 | 36 | $vars{$bv} += $bvars->{$bv}; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 61 | my $key = _key(\%vars); | |||||||||||||||||||||||||||
| 200 | 61 | ▶ | if (exists $rterms->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 4 | $rterms->{$key}->[T_COEFF] += $const; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | else { | ||||||||||||||||||||||||||||
| 204 | 57 | $rterms->{$key} = [$const, \%vars]; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 40 | delete @{$rterms}{ | |||||||||||||||||||||||||||
| 209 | 40 | grep {$zero == $rterms->{$_}->[T_COEFF]} keys %{$rterms} | |||||||||||||||||||||||||||
| 210 | }; | ||||||||||||||||||||||||||||
| 211 | 40 | return $result; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 8 | ▶ | sub _pow { | ||||||||||||||||||||||||||
|
Subroutine
_pow: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 215 | 8 | my ($this, $exp, $swap) = @_; | |||||||||||||||||||||||||||
| 216 | 8 | ▶ | croak 'illegal exponent' if $swap || $exp != int abs $exp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$swap or $exp != int abs $exp
MC/DC100%2/2
$swap or $exp != int abs $exp
$swap
$exp != int abs $exp
Truth table100%3/3
A or B
A= $swapB= $exp != int abs $exp
| |||||||||||||||||||||||||||||
| 217 | 6 | ▶ | return $this->const($this->[F_ONE]) if !$exp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 5 | my $result = $this->_clone; | |||||||||||||||||||||||||||
| 219 | 5 | ▶ | while (--$exp > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 6 | $result *= $this; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 5 | return $result; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 17 | ▶ | sub _eq_ne { | ||||||||||||||||||||||||||
|
Subroutine
_eq_ne: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 226 | 17 | my ($this, $that, $eq) = @_; | |||||||||||||||||||||||||||
| 227 | 17 | ▶ | $that = $this->const($that) if !_isa_pol($that); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 17 | my $aterms = $this->[F_TERMS]; | |||||||||||||||||||||||||||
| 229 | 17 | my $bterms = $that->[F_TERMS]; | |||||||||||||||||||||||||||
| 230 | 17 | ▶ | return !$eq if keys(%{$aterms}) != keys(%{$bterms}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 13 | ▶ | while (my ($key, $term) = each %{$aterms}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | return !$eq | ||||||||||||||||||||||||||||
| 233 | if !exists($bterms->{$key}) || | ||||||||||||||||||||||||||||
| 234 | 15 | ▶ | $term->[T_COEFF] != $bterms->{$key}->[T_COEFF]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not exists $bterms->{$key} or $term->[0] != $bterms->{$key}[0]
MC/DC100%2/2
not exists $bterms->{$key} or $term->[0] != $bterms->{$key}[0]
not exists $bterms->{$key}
$term->[0] != $bterms->{$key}[0]
Truth table100%3/3
not(A) or B
A= not exists $bterms->{$key}B= $term->[0] != $bterms->{$key}[0]
| |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | 6 | return $eq; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 10 | ▶ | sub _eq { | ||||||||||||||||||||||||||
|
Subroutine
_eq: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 240 | 10 | my ($this, $that) = @_; | |||||||||||||||||||||||||||
| 241 | 10 | return $this->_eq_ne($that, !0); | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 7 | ▶ | sub _ne { | ||||||||||||||||||||||||||
|
Subroutine
_ne: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 245 | 7 | my ($this, $that) = @_; | |||||||||||||||||||||||||||
| 246 | 7 | return $this->_eq_ne($that, !1); | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # ----- public methods ----- | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | # constructors | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 75 | ▶ | sub null { | ||||||||||||||||||||||||||
|
Subroutine
null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 254 | 75 | my ($this) = @_; | |||||||||||||||||||||||||||
| 255 | 75 | my ($class, $zero, $one) = $this->_space(1); | |||||||||||||||||||||||||||
| 256 | 75 | return bless [{}, $zero, $one], $class; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 27 | ▶ | sub const { | ||||||||||||||||||||||||||
|
Subroutine
const: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 260 | 27 | my ($this, $value) = @_; | |||||||||||||||||||||||||||
| 261 | 27 | my ($class, $zero, $one) = $this->_space($value); | |||||||||||||||||||||||||||
| 262 | 27 | ▶ | my %terms = $zero == $value? (): (q[] => [$value, {}]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 27 | return bless [\%terms, $zero, $one], $class; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 8 | ▶ | sub var { | ||||||||||||||||||||||||||
|
Subroutine
var: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 267 | 8 | my ($this, $varname) = @_; | |||||||||||||||||||||||||||
| 268 | 8 | my ($class, $zero, $one) = $this->_space(1); | |||||||||||||||||||||||||||
| 269 | 8 | my $vars = { $varname => 1 }; | |||||||||||||||||||||||||||
| 270 | 8 | my $key = _key($vars); | |||||||||||||||||||||||||||
| 271 | 8 | my %terms = ($key => [$one, $vars]); | |||||||||||||||||||||||||||
| 272 | 8 | return bless [\%terms, $zero, $one], $class; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 14 | ▶ | sub monomial { | ||||||||||||||||||||||||||
|
Subroutine
monomial: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 276 | 14 | my ($this, $const, $vars) = @_; | |||||||||||||||||||||||||||
| 277 | 14 | my ($class, $zero, $one) = $this->_space($const); | |||||||||||||||||||||||||||
| 278 | 14 | $vars = _vclean($vars); | |||||||||||||||||||||||||||
| 279 | 13 | ▶ | my %terms = $zero == $const? (): (_key($vars) => [$const, $vars]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 13 | return bless[\%terms, $zero, $one], $class; | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 8 | ▶ | sub subst { | ||||||||||||||||||||||||||
|
Subroutine
subst: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 284 | 8 | my ($this, $varname, $that) = @_; | |||||||||||||||||||||||||||
| 285 | 8 | my @exps = (-1, $this->exponents_of($varname)); | |||||||||||||||||||||||||||
| 286 | 8 | ▶ | return $this->null if 1 == @exps; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 7 | my $exp = pop @exps; | |||||||||||||||||||||||||||
| 288 | 7 | my $result = $this->factor_of($varname, $exp); | |||||||||||||||||||||||||||
| 289 | 7 | my $nexp = pop @exps; | |||||||||||||||||||||||||||
| 290 | 7 | ▶ | while ($exp > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 13 | $result *= $that; | |||||||||||||||||||||||||||
| 292 | 13 | ▶ | if (--$exp == $nexp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 6 | $result += $this->factor_of($varname, $exp); | |||||||||||||||||||||||||||
| 294 | 6 | $nexp = pop @exps; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | 7 | return $result; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 3 | ▶ | sub partial_derivative { | ||||||||||||||||||||||||||
|
Subroutine
partial_derivative: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 301 | 3 | my ($this, $varname, $cast) = @_; | |||||||||||||||||||||||||||
| 302 | 3 | my $result = $this->null; | |||||||||||||||||||||||||||
| 303 | 3 | foreach my $exp ($this->exponents_of($varname)) { | |||||||||||||||||||||||||||
| 304 | 9 | ▶ | next if !$exp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 6 | ▶ | my $const = $cast? $cast->($exp): $exp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 6 | $result += | |||||||||||||||||||||||||||
| 307 | $this->factor_of($varname, $exp) * | ||||||||||||||||||||||||||||
| 308 | $this->monomial($const, { $varname => $exp-1 }); | ||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | 3 | return $result; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 1 | ▶ | sub flatten { | ||||||||||||||||||||||||||
|
Subroutine
flatten: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 314 | 1 | my ($this) = @_; | |||||||||||||||||||||||||||
| 315 | 1 | ▶ | while (_isa_pol(my $aux = $this->[F_ZERO])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 1 | foreach my $term (values %{$this->[F_TERMS]}) { | |||||||||||||||||||||||||||
| 317 | 3 | $aux += $term->[T_COEFF]->_mul_vars($term->[T_VARS]); | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 1 | $this = $aux; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 1 | return $this; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 1 | ▶ | sub extract { | ||||||||||||||||||||||||||
|
Subroutine
extract: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 325 | 1 | my ($this, $var) = @_; | |||||||||||||||||||||||||||
| 326 | my %terms = | ||||||||||||||||||||||||||||
| 327 | map { | ||||||||||||||||||||||||||||
| 328 | 1 | ▶ | my $vars = { $_? ($var => $_): () }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | 3 | _key($vars) => [$this->factor_of($var, $_), $vars] | |||||||||||||||||||||||||||
| 330 | } $this->exponents_of($var); | ||||||||||||||||||||||||||||
| 331 | return | ||||||||||||||||||||||||||||
| 332 | 1 | bless [ | |||||||||||||||||||||||||||
| 333 | \%terms, | ||||||||||||||||||||||||||||
| 334 | $this->null, | ||||||||||||||||||||||||||||
| 335 | $this->const($this->[F_ONE]) | ||||||||||||||||||||||||||||
| 336 | ], ref $this; | ||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | # inspection methods | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 2 | ▶ | sub is_null { !keys %{$_[0]->[F_TERMS]} } | ||||||||||||||||||||||||||
|
Subroutine
is_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 342 | 2 | ▶ | sub is_not_null { !!keys %{$_[0]->[F_TERMS]} } | ||||||||||||||||||||||||||
|
Subroutine
is_not_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 9 | ▶ | sub variables { | ||||||||||||||||||||||||||
|
Subroutine
variables: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 345 | 9 | my ($this) = @_; | |||||||||||||||||||||||||||
| 346 | 9 | my %vars = map {%{$_->[T_VARS]}} values %{$this->[F_TERMS]}; | |||||||||||||||||||||||||||
| 347 | 9 | return sort keys %vars; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 14 | ▶ | sub exponents_of { | ||||||||||||||||||||||||||
|
Subroutine
exponents_of: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 351 | 14 | my ($this, $varname) = @_; | |||||||||||||||||||||||||||
| 352 | 14 | my $terms = $this->[F_TERMS]; | |||||||||||||||||||||||||||
| 353 | 14 | my %exps = (); | |||||||||||||||||||||||||||
| 354 | 14 | foreach my $term (values %{$terms}) { | |||||||||||||||||||||||||||
| 355 | 36 | ▶ | $exps{$term->[T_VARS]->{$varname} || 0} = undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$term->[1]{$varname} || 0
MC/DC100%1/1
$term->[1]{$varname} || 0
$term->[1]{$varname}
Truth table100%2/2
A
A= $term->[1]{$varname}
| |||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | 14 | return sort { $a <=> $b } keys %exps; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 26 | ▶ | sub factor_of { | ||||||||||||||||||||||||||
|
Subroutine
factor_of: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 361 | 26 | my ($this, $varname, $exp) = @_; | |||||||||||||||||||||||||||
| 362 | 26 | my $terms = $this->[F_TERMS]; | |||||||||||||||||||||||||||
| 363 | 26 | my $result = $this->null; | |||||||||||||||||||||||||||
| 364 | 26 | my $rterms = $result->[F_TERMS]; | |||||||||||||||||||||||||||
| 365 | 26 | foreach my $term (values %{$terms}) { | |||||||||||||||||||||||||||
| 366 | 77 | ▶ | my $aexp = $term->[T_VARS]->{$varname} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$term->[1]{$varname} || 0
MC/DC100%1/1
$term->[1]{$varname} || 0
$term->[1]{$varname}
Truth table100%2/2
A
A= $term->[1]{$varname}
| |||||||||||||||||||||||||||||
| 367 | 77 | ▶ | if ($aexp == $exp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 30 | my %vars = %{$term->[T_VARS]}; | |||||||||||||||||||||||||||
| 369 | 30 | delete $vars{$varname}; | |||||||||||||||||||||||||||
| 370 | 30 | $rterms->{_key(\%vars)} = [$term->[T_COEFF], \%vars]; | |||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | 26 | return $result; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 8 | ▶ | sub coefficient { | ||||||||||||||||||||||||||
|
Subroutine
coefficient: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 377 | 8 | my ($this, $vars) = @_; | |||||||||||||||||||||||||||
| 378 | 8 | my $key = _keyvc($vars); | |||||||||||||||||||||||||||
| 379 | 8 | my $terms = $this->[F_TERMS]; | |||||||||||||||||||||||||||
| 380 | 8 | ▶ | return exists($terms->{$key})? $terms->{$key}->[T_COEFF]: $this->[F_ZERO]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 5 | ▶ | sub degree { | ||||||||||||||||||||||||||
|
Subroutine
degree: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 384 | 5 | my ($this) = @_; | |||||||||||||||||||||||||||
| 385 | 5 | my $terms = $this->[F_TERMS]; | |||||||||||||||||||||||||||
| 386 | 5 | ▶ | return _MINUS_INFINITY if !keys %{$terms}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 3 | my $max_degree = 0; | |||||||||||||||||||||||||||
| 388 | 3 | foreach my $term (values %{$terms}) { | |||||||||||||||||||||||||||
| 389 | 9 | my $degree = 0; | |||||||||||||||||||||||||||
| 390 | 9 | foreach my $e (values %{$term->[T_VARS]}) { | |||||||||||||||||||||||||||
| 391 | 9 | $degree += $e; | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | 9 | ▶ | if ($max_degree < $degree) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 5 | $max_degree = $degree; | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | 3 | return $max_degree; | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 4 | ▶ | sub multidegree { | ||||||||||||||||||||||||||
|
Subroutine
multidegree: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 401 | 4 | my ($this) = @_; | |||||||||||||||||||||||||||
| 402 | 4 | my $terms = $this->[F_TERMS]; | |||||||||||||||||||||||||||
| 403 | 4 | my %result = (); | |||||||||||||||||||||||||||
| 404 | 4 | foreach my $term (values %{$terms}) { | |||||||||||||||||||||||||||
| 405 | 4 | ▶ | while (my ($name, $exp) = each %{$term->[T_VARS]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | 6 | ▶ | if (($result{$name} || 0) < $exp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$result{$name} || 0
MC/DC100%1/1
$result{$name} || 0
$result{$name}
Truth table100%2/2
A
A= $result{$name}
| |||||||||||||||||||||||||||||
| 407 | 5 | $result{$name} = $exp; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | 4 | return \%result; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 2 | ▶ | sub number_of_terms { scalar keys %{$_[0]->[F_TERMS]} } | ||||||||||||||||||||||||||
|
Subroutine
number_of_terms: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 5 | ▶ | sub evaluate { | ||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 417 | 5 | my ($this, $values) = @_; | |||||||||||||||||||||||||||
| 418 | 5 | my @vars = $this->variables; | |||||||||||||||||||||||||||
| 419 | 5 | ▶ | if (my @miss = grep { !exists $values->{$_} } @vars) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | 2 | ▶ | my $s = 1 == @miss? q[]: 's'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | 2 | croak "missing variable$s: @miss"; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | 3 | my $result = $this; | |||||||||||||||||||||||||||
| 424 | 3 | foreach my $varname (@vars) { | |||||||||||||||||||||||||||
| 425 | 4 | $result = $result->subst($varname, $this->const($values->{$varname})); | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | 3 | return $result->coefficient({}); | |||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 43 | ▶ | sub as_monomials { | ||||||||||||||||||||||||||
|
Subroutine
as_monomials: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 431 | 43 | my ($this) = @_; | |||||||||||||||||||||||||||
| 432 | 43 | my $terms = $this->[F_TERMS]; | |||||||||||||||||||||||||||
| 433 | 43 | ▶ | return scalar keys %{$terms} if !wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 42 | ▶ | return ([$this->[F_ZERO], {}]) if !keys %{$terms}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | return | ||||||||||||||||||||||||||||
| 436 | map { | ||||||||||||||||||||||||||||
| 437 | 56 | my ($coeff, $vars) = @{$terms->{$_}}; | |||||||||||||||||||||||||||
| 438 | 56 | [$coeff, {%{$vars}}] | |||||||||||||||||||||||||||
| 439 | 33 | } sort keys %{$terms}; | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 41 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 443 | 41 | my ($this) = @_; | |||||||||||||||||||||||||||
| 444 | 41 | my $one = $this->[F_ONE]; | |||||||||||||||||||||||||||
| 445 | return | ||||||||||||||||||||||||||||
| 446 | sprintf '(%s)', | ||||||||||||||||||||||||||||
| 447 | join q[ + ], | ||||||||||||||||||||||||||||
| 448 | map { | ||||||||||||||||||||||||||||
| 449 | 41 | my ($const, $vars) = @{$_}; | |||||||||||||||||||||||||||
| 450 | join q[*], | ||||||||||||||||||||||||||||
| 451 | 62 | keys(%{$vars}) && $one == $const? (): $const, | |||||||||||||||||||||||||||
| 452 | map { | ||||||||||||||||||||||||||||
| 453 | 44 | my $exp = $vars->{$_}; | |||||||||||||||||||||||||||
| 454 | 44 | ▶ | 1 == $exp? $_: "$_^$exp" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||
| 456 | 62 | ▶ | sort keys %{$vars} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
keys %{$vars;} and $one == $const
MC/DC100%2/2
keys %{$vars;} and $one == $const
keys %{$vars;}
$one == $const
Truth table100%3/3
A and B
A= keys %{$vars;}B= $one == $const
| |||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | $this->as_monomials; | ||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 5 | 1; | |||||||||||||||||||||||||||
| 462 | __END__ | ||||||||||||||||||||||||||||