| 1 | # Copyright (c) 2013-2022 by Martin Becker. This package is free software, | ||||||||||||||||||||||||||||
| 2 | # licensed under The Artistic License 2.0 (GPL compatible). | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Math::Polynomial::ModInt; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Math::BigInt try => 'GMP'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Math::ModInt 0.012; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Math::ModInt::Event qw(UndefinedResult); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Math::Polynomial 1.015; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # ----- object definition ----- | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Math::Polynomial::ModInt=ARRAY(...) | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # .............. index .............. # .......... value .......... | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use constant _OFFSET => Math::Polynomial::_NFIELDS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use constant _F_INDEX => _OFFSET + 0; # ordinal number i, 0 <= i | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use constant _NFIELDS => _OFFSET + 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # ----- class data ----- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 3 | require Exporter; | |||||||||||||||||||||||||||
| 29 | 3 | our @ISA = qw(Math::Polynomial Exporter); | |||||||||||||||||||||||||||
| 30 | 3 | our @EXPORT_OK = qw(modpoly); | |||||||||||||||||||||||||||
| 31 | 3 | our $VERSION = '0.005'; | |||||||||||||||||||||||||||
| 32 | 3 | our @CARP_NOT = qw(Math::ModInt::Event::Trap Math::Polynomial); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | my $default_string_config = { | ||||||||||||||||||||||||||||
| 36 | 773 | ▶ | convert_coeff => sub { $_[0]->residue }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 3 | times => q[*], | |||||||||||||||||||||||||||
| 38 | wrap => \&_wrap, | ||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | my $lifted_string_config = { | |||||||||||||||||||||||||||
| 42 | times => q[*], | ||||||||||||||||||||||||||||
| 43 | fold_sign => 1, | ||||||||||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | my $ipol = Math::Polynomial->new(Math::BigInt->new); | |||||||||||||||||||||||||||
| 47 | 3 | $ipol->string_config($lifted_string_config); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # ----- private subroutines ----- | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # event catcher | ||||||||||||||||||||||||||||
| 52 | 3 | ▶ | sub _nonprime_modulus { | ||||||||||||||||||||||||||
|
Subroutine
_nonprime_modulus: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 3 | croak 'modulus not prime'; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # event catcher | ||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub _no_inverse { | ||||||||||||||||||||||||||
|
Subroutine
_no_inverse: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 1 | croak 'undefined inverse'; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # constructor diagnostic | ||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub _modulus_too_small { | ||||||||||||||||||||||||||
|
Subroutine
_modulus_too_small: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 2 | croak 'modulus must be greater than one'; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # wrapper for modular inverse to bail out early if not in a field | ||||||||||||||||||||||||||||
| 67 | 2 | ▶ | sub _inverse { | ||||||||||||||||||||||||||
|
Subroutine
_inverse: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 2 | my ($element) = @_; | |||||||||||||||||||||||||||
| 69 | 2 | my $trap = UndefinedResult->trap(\&_no_inverse); | |||||||||||||||||||||||||||
| 70 | 2 | return $element->inverse; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Does a set of coefficient vectors have a zero linear combination? | ||||||||||||||||||||||||||||
| 74 | # We transform and include them one by one into a matrix in echelon form. | ||||||||||||||||||||||||||||
| 75 | # Argument is an iterator so we can short-cut generating superfluous vectors. | ||||||||||||||||||||||||||||
| 76 | # Supplied vectors may be modified. | ||||||||||||||||||||||||||||
| 77 | 7 | ▶ | sub _linearly_dependent { | ||||||||||||||||||||||||||
|
Subroutine
_linearly_dependent: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 78 | 7 | my ($it) = @_; | |||||||||||||||||||||||||||
| 79 | 7 | my $trap = UndefinedResult->trap(\&_nonprime_modulus); | |||||||||||||||||||||||||||
| 80 | 7 | my @ech = (); | |||||||||||||||||||||||||||
| 81 | 7 | ▶ | while (defined(my $vec = $it->())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 22 | my $ex = 0; | |||||||||||||||||||||||||||
| 83 | 22 | ▶ | for (; $ex < @ech; ++$ex) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 25 | my $evec = $ech[$ex]; | |||||||||||||||||||||||||||
| 85 | 25 | ▶ | last if @{$evec} < @{$vec}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 20 | ▶ | if (@{$evec} == @{$vec}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 16 | my $i = $#{$vec}; | |||||||||||||||||||||||||||
| 88 | 16 | ▶ | return 1 if !$i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 15 | my $w = pop(@{$vec}) / $evec->[$i]; | |||||||||||||||||||||||||||
| 90 | 14 | ▶ | while ($i-- > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 42 | $vec->[$i] -= $evec->[$i] * $w; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 14 | ▶ | while (@{$vec} && !$vec->[-1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$vec;} and not $vec->[-1]
MC/DC100%2/2
@{$vec;} and not $vec->[-1]
@{$vec;}
not $vec->[-1]
Truth table100%3/3
A and not(B)
A= @{$vec;}B= not $vec->[-1]
| |||||||||||||||||||||||||||||
| 94 | 11 | pop(@{$vec}); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 20 | ▶ | return 1 if !@{$vec}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 19 | splice @ech, $ex, 0, $vec; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 4 | return 0; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # ----- private methods ----- | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # wrapper to decorate stringified polynomial | ||||||||||||||||||||||||||||
| 107 | 478 | ▶ | sub _wrap { | ||||||||||||||||||||||||||
|
Subroutine
_wrap: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 478 | my ($this, $text) = @_; | |||||||||||||||||||||||||||
| 109 | 478 | my $modulus = $this->modulus; | |||||||||||||||||||||||||||
| 110 | 478 | return "$text (mod $modulus)"; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # ----- protected methods ----- | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # constructor with index argument | ||||||||||||||||||||||||||||
| 116 | 148 | ▶ | sub _xnew { | ||||||||||||||||||||||||||
|
Subroutine
_xnew: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 148 | my $this = shift; | |||||||||||||||||||||||||||
| 118 | 148 | my $index = shift; | |||||||||||||||||||||||||||
| 119 | 148 | my $poly = $this->new(@_); | |||||||||||||||||||||||||||
| 120 | 148 | $poly->[_F_INDEX] = $index; | |||||||||||||||||||||||||||
| 121 | 148 | return $poly; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # ----- overridden public methods ----- | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 500 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 500 | my $this = shift; | |||||||||||||||||||||||||||
| 128 | 500 | ▶ | if (!@_ && !ref $this) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ or ref $this
MC/DC100%2/2
@_ or ref $this
@_
ref $this
Truth table100%3/3
A or B
A= @_B= ref $this
| |||||||||||||||||||||||||||||
| 129 | 1 | croak 'insufficient arguments'; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 499 | ▶ | if (grep {$_->is_undefined} @_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 2 | _nonprime_modulus(); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 497 | ▶ | if (grep {$_->modulus <= 1} @_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 1 | _modulus_too_small(); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 496 | return $this->SUPER::new(@_); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 963 | ▶ | sub string_config { | ||||||||||||||||||||||||||
|
Subroutine
string_config: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 963 | my $this = shift; | |||||||||||||||||||||||||||
| 142 | 963 | ▶ | return $this->SUPER::string_config(@_) if ref $this; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 482 | ▶ | ($default_string_config) = @_ if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 482 | return $default_string_config; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 10 | ▶ | sub is_equal { | ||||||||||||||||||||||||||
|
Subroutine
is_equal: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 10 | my ($this, $that) = @_; | |||||||||||||||||||||||||||
| 149 | 10 | my $i = $this->degree; | |||||||||||||||||||||||||||
| 150 | 10 | ▶ | my $eq = $this->modulus == $that->modulus && $i == $that->degree; | ||||||||||||||||||||||||||
|
Condition100%3/3
$this->modulus == $that->modulus && $i == $that->degree
MC/DC100%2/2
$this->modulus == $that->modulus && $i == $that->degree
$this->modulus == $that->modulus
$i == $that->degree
Truth table100%3/3
A && B
A= $this->modulus == $that->modulusB= $i == $that->degree
| |||||||||||||||||||||||||||||
| 151 | 10 | ▶ | while ($eq && 0 <= $i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$eq and 0 <= $i
MC/DC100%2/2
$eq and 0 <= $i
$eq
0 <= $i
Truth table100%3/3
A and B
A= $eqB= 0 <= $i
| |||||||||||||||||||||||||||||
| 152 | 22 | $eq = $this->coeff($i)->residue == $that->coeff($i)->residue; | |||||||||||||||||||||||||||
| 153 | 22 | --$i; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 10 | return $eq; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 5 | ▶ | sub is_unequal { | ||||||||||||||||||||||||||
|
Subroutine
is_unequal: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 5 | my ($this, $that) = @_; | |||||||||||||||||||||||||||
| 160 | 5 | my $i = $this->degree; | |||||||||||||||||||||||||||
| 161 | 5 | ▶ | my $eq = $this->modulus == $that->modulus && $i == $that->degree; | ||||||||||||||||||||||||||
|
Condition100%3/3
$this->modulus == $that->modulus && $i == $that->degree
MC/DC100%2/2
$this->modulus == $that->modulus && $i == $that->degree
$this->modulus == $that->modulus
$i == $that->degree
Truth table100%3/3
A && B
A= $this->modulus == $that->modulusB= $i == $that->degree
| |||||||||||||||||||||||||||||
| 162 | 5 | ▶ | while ($eq && 0 <= $i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$eq and 0 <= $i
MC/DC100%2/2
$eq and 0 <= $i
$eq
0 <= $i
Truth table100%3/3
A and B
A= $eqB= 0 <= $i
| |||||||||||||||||||||||||||||
| 163 | 10 | $eq = $this->coeff($i)->residue == $that->coeff($i)->residue; | |||||||||||||||||||||||||||
| 164 | 10 | --$i; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 5 | return !$eq; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 55 | ▶ | sub is_monic { | ||||||||||||||||||||||||||
|
Subroutine
is_monic: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 170 | 55 | my ($this) = @_; | |||||||||||||||||||||||||||
| 171 | 55 | my $degree = $this->degree; | |||||||||||||||||||||||||||
| 172 | 55 | ▶ | return 0 <= $degree && $this->coeff($degree)->residue == 1; | ||||||||||||||||||||||||||
|
Condition100%3/3
0 <= $degree && $this->coeff($degree)->residue == 1
MC/DC100%2/2
0 <= $degree && $this->coeff($degree)->residue == 1
0 <= $degree
$this->coeff($degree)->residue == 1
Truth table100%3/3
A && B
A= 0 <= $degreeB= $this->coeff($degree)->residue == 1
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 4 | ▶ | sub monize { | ||||||||||||||||||||||||||
|
Subroutine
monize: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | 4 | my ($this) = @_; | |||||||||||||||||||||||||||
| 177 | 4 | my $degree = $this->degree; | |||||||||||||||||||||||||||
| 178 | 4 | ▶ | return $this if $degree < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 3 | my $leader = $this->coeff($degree); | |||||||||||||||||||||||||||
| 180 | 3 | ▶ | return $this if $leader->residue == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 2 | return $this->mul_const(_inverse($leader)); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # ----- public subroutine ----- | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 146 | ▶ | sub modpoly { __PACKAGE__->from_index(@_) } | ||||||||||||||||||||||||||
|
Subroutine
modpoly: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # ----- class-specific public methods ----- | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 150 | ▶ | sub from_index { | ||||||||||||||||||||||||||
|
Subroutine
from_index: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | 150 | my ($this, $index, $modulus) = @_; | |||||||||||||||||||||||||||
| 192 | 150 | my $zero; | |||||||||||||||||||||||||||
| 193 | 150 | ▶ | if (defined $modulus) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 194 | 148 | ▶ | $modulus > 1 || _modulus_too_small(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 147 | $zero = Math::ModInt::mod(0, $modulus); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | elsif (ref $this) { | ||||||||||||||||||||||||||||
| 198 | 1 | $zero = $this->coeff_zero; | |||||||||||||||||||||||||||
| 199 | 1 | $modulus = $zero->modulus; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||||||||||
| 202 | 1 | croak('usage error: modulus parameter missing'); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 148 | my @coeff = (); | |||||||||||||||||||||||||||
| 205 | 148 | my $q = $index; | |||||||||||||||||||||||||||
| 206 | 148 | ▶ | while ($q > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 353 | ($q, my $r) = $zero->new2($q); | |||||||||||||||||||||||||||
| 208 | 353 | push @coeff, $r; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 148 | ▶ | return $this->_xnew(@coeff? ($index, @coeff): (0, $zero)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 3 | ▶ | sub from_int_poly { | ||||||||||||||||||||||||||
|
Subroutine
from_int_poly: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 214 | 3 | my ($this, $poly, $modulus) = @_; | |||||||||||||||||||||||||||
| 215 | 3 | ▶ | if (!defined $modulus) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 2 | ▶ | if (!ref $this) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 1 | croak('usage error: modulus parameter missing'); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 1 | $modulus = $this->modulus; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 2 | my @coeff = map { Math::ModInt::mod($_, $modulus) } $poly->coefficients; | |||||||||||||||||||||||||||
| 222 | 2 | return $this->new(@coeff); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1264 | ▶ | sub modulus { | ||||||||||||||||||||||||||
|
Subroutine
modulus: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | 1264 | my ($this) = @_; | |||||||||||||||||||||||||||
| 227 | 1264 | return $this->coeff_zero->modulus; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 6 | ▶ | sub index { | ||||||||||||||||||||||||||
|
Subroutine
index: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 231 | 6 | my ($this) = @_; | |||||||||||||||||||||||||||
| 232 | 6 | my $base = $this->modulus; | |||||||||||||||||||||||||||
| 233 | 6 | my $index = $this->[_F_INDEX]; | |||||||||||||||||||||||||||
| 234 | 6 | ▶ | if (!defined $index) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 2 | $index = Math::BigInt->new; | |||||||||||||||||||||||||||
| 236 | 2 | foreach my $c (reverse $this->coeff) { | |||||||||||||||||||||||||||
| 237 | 12 | $index->bmul($base)->badd($c->residue); | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | 2 | $this->[_F_INDEX] = $index; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 6 | return $index; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 181 | ▶ | sub number_of_terms { scalar grep { $_->is_not_zero } $_[0]->coeff } | ||||||||||||||||||||||||||
|
Subroutine
number_of_terms: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 1 | ▶ | sub lift { $ipol->new(map { $_->residue } $_[0]->coefficients) } | ||||||||||||||||||||||||||
|
Subroutine
lift: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 2 | ▶ | sub centerlift { $ipol->new(map { $_->centered_residue } $_[0]->coefficients) } | ||||||||||||||||||||||||||
|
Subroutine
centerlift: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 8 | ▶ | sub lambda_reduce { | ||||||||||||||||||||||||||
|
Subroutine
lambda_reduce: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 251 | 8 | my ($this, $lambda) = @_; | |||||||||||||||||||||||||||
| 252 | 8 | my $degree = $this->degree; | |||||||||||||||||||||||||||
| 253 | 8 | ▶ | return $this if $degree <= $lambda; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 7 | my @coeff = map { $this->coeff($_) } 0 .. $lambda; | |||||||||||||||||||||||||||
| 255 | 7 | ▶ | for (my $i = 1, my $n = $lambda+1; $n <= $degree; ++$i, ++$n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 16 | $coeff[$i] += $this->coeff($n); | |||||||||||||||||||||||||||
| 257 | 16 | ▶ | $i = 0 if $i == $lambda; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | 7 | return $this->new(@coeff); | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 16 | ▶ | sub first_root { | ||||||||||||||||||||||||||
|
Subroutine
first_root: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 263 | 16 | my ($this) = @_; | |||||||||||||||||||||||||||
| 264 | 16 | my $zero = $this->coeff_zero; | |||||||||||||||||||||||||||
| 265 | 16 | my $lsc = $this->coeff(0); | |||||||||||||||||||||||||||
| 266 | 16 | ▶ | return $zero if $lsc->is_zero; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 15 | ▶ | if ($this->degree == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 5 | my $mscr = $this->coeff(1)->centered_residue; | |||||||||||||||||||||||||||
| 269 | 5 | ▶ | return -$lsc if $mscr == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 4 | ▶ | return $lsc if $mscr == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 12 | foreach my $n (1 .. $zero->modulus - 1) { | |||||||||||||||||||||||||||
| 273 | 66 | my $root = $zero->new($n); | |||||||||||||||||||||||||||
| 274 | 66 | ▶ | return $root if $this->evaluate($root)->is_zero; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | 9 | return undef; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | # implementation restriction: defined for prime moduli only | ||||||||||||||||||||||||||||
| 280 | 16 | ▶ | sub is_irreducible { | ||||||||||||||||||||||||||
|
Subroutine
is_irreducible: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 281 | 16 | my ($this) = @_; | |||||||||||||||||||||||||||
| 282 | 16 | my $n = $this->degree; | |||||||||||||||||||||||||||
| 283 | 16 | ▶ | return 0 if $n <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 15 | ▶ | return 1 if $n == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 14 | ▶ | return 0 if !$this->coeff(0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 13 | ▶ | return 0 if $this->gcd($this->differentiate)->degree > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 11 | my $p = $this->modulus; | |||||||||||||||||||||||||||
| 288 | # optimization: O(p) zero search only for small p or very large n | ||||||||||||||||||||||||||||
| 289 | 11 | ▶ | if ($p <= 43 || log($p - 20) <= $n * 0.24 + 2.68) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$p <= 43 or log $p - 20 <= $n * "0.24" + "2.68"
MC/DC100%2/2
$p <= 43 or log $p - 20 <= $n * "0.24" + "2.68"
$p <= 43
log $p - 20 <= $n * "0.24" + "2.68"
Truth table100%3/3
A or B
A= $p <= 43B= log $p - 20 <= $n * "0.24" + "2.68"
| |||||||||||||||||||||||||||||
| 290 | 10 | ▶ | my $rp = 2 < $p && $p <= $n? $this->lambda_reduce($p-1): $this; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
2 < $p and $p <= $n
MC/DC100%2/2
2 < $p and $p <= $n
2 < $p
$p <= $n
Truth table100%3/3
A and B
A= 2 < $pB= $p <= $n
| |||||||||||||||||||||||||||||
| 291 | 10 | ▶ | return 0 if defined $rp->first_root; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 8 | ▶ | return 1 if $n <= 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | # Berlekamp rank < $n - 1? | ||||||||||||||||||||||||||||
| 295 | 7 | my $xp = $this->exp_mod($p); | |||||||||||||||||||||||||||
| 296 | 7 | my $aj = $xp; | |||||||||||||||||||||||||||
| 297 | 7 | my $bj = $this->monomial(1); | |||||||||||||||||||||||||||
| 298 | 7 | my $j = 0; | |||||||||||||||||||||||||||
| 299 | return 0 if _linearly_dependent( | ||||||||||||||||||||||||||||
| 300 | 26 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 301 | 26 | ▶ | return undef if $j >= $n - 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 22 | ▶ | if ($j++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 15 | $aj = $aj * $xp % $this; | |||||||||||||||||||||||||||
| 304 | 15 | $bj <<= 1; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | 22 | return [($aj - $bj)->coeff]; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | 7 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | 4 | return 1; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 3 | 1; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | __END__ | ||||||||||||||||||||||||||||