Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge remote-tracking branch 'bubaflub/fix_aks' into fix_aks

Conflicts:
	lib/Math/Primality/BigPolynomial.pm
  • Loading branch information...
commit 8a5aca4958fa7488d5ab5664a996a67ef80cf8e3 2 parents c546f2c + 00bce30
Duke Leto authored
17 lib/Math/Primality/AKS.pm
@@ -114,12 +114,12 @@ sub is_aks_prime($) {
114 114 my $final_size = Math::GMPz->new(0);
115 115 Rmpz_mod($final_size, $n, $r);
116 116 my $compare = Math::Primality::BigPolynomial->new(Rmpz_get_ui($final_size));
117   - $compare->setCoef(1, Rmpz_get_ui($final_size));
118   - $compare->setCoef($a, 0);
  117 + $compare->setCoef(Math::GMPz->new(1), $final_size);
  118 + $compare->setCoef(Math::GMPz->new($a), 0);
119 119 my $res = Math::Primality::BigPolynomial->new($intr);
120 120 my $base = Math::Primality::BigPolynomial->new(1);
121   - $base->setCoef($a, 0);
122   - $base->setCoef(1, 1);
  121 + $base->setCoef(Math::GMPz->new(0), $a);
  122 + $base->setCoef(Math::GMPz->new(1), 1);
123 123
124 124 Math::Primality::BigPolynomial::mpz_poly_mod_power($res, $base, $n, $n, $intr);
125 125
@@ -139,11 +139,10 @@ Jonathan "Duke" Leto C<< <jonathan@leto.net> >>
139 139
140 140 =head1 BUGS
141 141
142   -Please report any bugs or feature requests to C<bug-math-primality-aks at rt.cpan.org>,
143   -or through the web interface at
144   -L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math::Primality::AKS>. I will be
145   -notified, and then you'll automatically be notified of progress on your bug as I
146   -make changes.
  142 +Please report any bugs or feature requests to
  143 +C<bug-math-primality-aks at rt.cpan.org>, or through the web interface at
  144 +L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math::Primality::AKS>.
  145 +I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
147 146
148 147 =head1 THANKS
149 148
47 lib/Math/Primality/BigPolynomial.pm
@@ -9,21 +9,24 @@ sub new {
9 9 my $class = shift;
10 10 my $construction_junk = shift;
11 11 if ($construction_junk) {
12   - if ( ref($construction_junk) eq 'ARRAY' ) {
  12 + my $type = ref $construction_junk;
  13 + if ( $type eq 'ARRAY' ) {
13 14 $self->{COEF} = $construction_junk;
14   - $self->{DEGREE} = scalar(@$construction_junk);
  15 + } elsif ( $type eq 'Math::Primality::BigPolynomial') {
  16 + foreach my $coef (@{$construction_junk->{COEF}}) {
  17 + my $temp = Rmpz_init_set($coef);
  18 + push @{$self->{COEF}}, $temp;
  19 + }
15 20 } else {
16   - $self->{DEGREE} = $construction_junk;
17   - my @a = [];
  21 + my $a = [];
18 22 for ( my $i = 0 ; $i < $construction_junk ; $i++ ) {
19   - push @a, Math::GMPz->new(0);
  23 + push @$a, Math::GMPz->new(0);
20 24 }
21   - $self->{COEF} = \@a;
  25 + $self->{COEF} = $a;
22 26 }
23 27 }
24 28 else {
25 29 $self->{COEF} = [ Math::GMPz->new(0) ];
26   - $self->{DEGREE} = 1;
27 30 }
28 31 bless( $self, $class );
29 32 return $self;
@@ -37,18 +40,17 @@ sub coef {
37 40
38 41 sub degree {
39 42 my $self = shift;
40   - if (@_) { $self->{DEGREE} = shift }
41   - return $self->{DEGREE};
  43 + return (scalar @{$self->{COEF}} - 1);
42 44 }
43 45
44 46 sub getCoef {
45 47 my $self = shift;
46 48 my $i = shift;
47 49 if ( $i > $self->degree() ) {
48   - return 0;
  50 + return Math::GMPz->new(0);
49 51 }
50 52 return undef if $i < 0;
51   - return ${ $self->{COEF} }[$i];
  53 + return $self->{COEF}->[$i];
52 54 }
53 55
54 56 sub isEqual {
@@ -74,41 +76,40 @@ sub setCoef {
74 76 }
75 77
76 78 if ( $index > $self->degree() ) {
77   - for ( my $j = $self->degree() + 1 ; $j <= $index ; $j++ ) {
  79 + for ( my $j = $self->degree() + 1 ; $j < $index ; $j++ ) {
78 80 push @{ $self->{COEF} }, Math::GMPz->new(0);
79 81 }
80   - push @{ $self->{COEF} }, $new_coef;
  82 + $self->{COEF}->[$index] = $new_coef;
81 83 $self->degree($index);
82 84 }
83 85 else {
84   - ${ $self->{COEF} }[$index] = $new_coef;
  86 + $self->{COEF}->[$index] = $new_coef;
85 87 }
86 88 }
87 89
88 90 sub compact {
89 91 my $self = shift;
90 92 my $i = 0;
91   - LOOP: for ( $i = $self->degree() - 1 ; $i > 0 ; $i-- ) {
  93 + LOOP: for ( $i = $self->degree(); $i > 0 ; $i-- ) {
92 94 if ( Math::GMPz::Rmpz_cmp_ui( $self->getCoef($i), 0 ) != 0 ) {
93 95 last LOOP;
94 96 }
95 97 pop @{ $self->{COEF} };
96 98 }
97 99 if ( $i != $self->degree() ) {
98   - $self->degree( $i + 1 );
  100 + $self->degree( $i );
99 101 }
100 102 }
101 103
102 104 sub clear {
103 105 my $self = shift;
104   - $self->{COEF} = undef;
105   - $self->{DEGREE} = undef;
106 106 $self->{COEF} = [ Math::GMPz->new(0) ];
107   - $self->{DEGREE} = 1;
108 107 }
109 108
110 109 sub mpz_poly_mod_mult {
111   - my ( $rop, $x, $y, $mod, $polymod ) = @_;
  110 + my ( $rop, $copy_x, $copy_y, $mod, $polymod ) = @_;
  111 + my $x = Math::Primality::BigPolynomial->new($copy_x);
  112 + my $y = Math::Primality::BigPolynomial->new($copy_y);
112 113
113 114 die "mpz_poly_mod_mult: polymod must be defined!" unless $polymod;
114 115
@@ -122,8 +123,8 @@ sub mpz_poly_mod_mult {
122 123 my $sum = Math::GMPz->new(0);
123 124 my $temp = Math::GMPz->new(0);
124 125 for ( my $j = 0 ; $j <= $i ; $j++ ) {
125   - Rmpz_add($temp, $temp,
126   - $y->getCoef( $i - $j ) + $y->getCoef( $i + $polymod - $j ) );
  126 + Rmpz_add($temp, $y->getCoef( $i - $j ),
  127 + $y->getCoef( $i + $polymod - $j ) );
127 128 Rmpz_mul( $temp, $x->getCoef($j), $temp );
128 129 Rmpz_add( $sum, $sum, $temp );
129 130 }
@@ -159,7 +160,7 @@ sub mpz_poly_mod_power {
159 160 mpz_poly_mod_mult( $rop, $rop, $rop, $mult_mod, $poly_mod );
160 161
161 162 if ( Rmpz_tstbit( $power, $i ) ) {
162   - mpz_poly_mod_mul( $rop, $rop, $x, $mult_mod, $poly_mod );
  163 + mpz_poly_mod_mult( $rop, $rop, $x, $mult_mod, $poly_mod );
163 164 }
164 165
165 166 if ( $i == 0 ) {
3  t/big_polynomial.t
@@ -18,7 +18,8 @@ isa_ok($b, 'Math::Primality::BigPolynomial');
18 18 is($b->getCoef(0), 1, 'coef(0) is 1');
19 19 is($b->getCoef(1), 3, 'coef(1) is 3');
20 20 is($b->getCoef(2), 7, 'coef(2) is 7');
21   -is($b->getCoef(3), undef, 'coef(3) is undef');
  21 +# this is a bit wonky
  22 +cmp_ok($b->getCoef(3),'==', Math::GMPz->new(0), 'coef(3) is 0');
22 23 is($b->getCoef(-1), undef, 'coef(-1) is undef');
23 24 is($b->getCoef(-10), undef, 'coef(-10) is undef');
24 25

0 comments on commit 8a5aca4

Please sign in to comment.
Something went wrong with that request. Please try again.