Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Port divmod from p5 Math::Polynomial.

  • Loading branch information...
commit 4aeb742bebe649e387bbc9eca0bea9cfff2d07cc 1 parent fc9ac9c
@colomon authored
Showing with 51 additions and 50 deletions.
  1. +2 −0  CREDITS
  2. +23 −24 lib/Math/Polynomial.pm
  3. +26 −26 t/03_expressions.t
View
2  CREDITS
@@ -6,3 +6,5 @@ colomon@gmail.com
Bruce Gray (Refactoring and editing)
bruce.gray@acm.org
+
+Martin Becker (Code from Perl 5 Math::Polynomial)
View
47 lib/Math/Polynomial.pm
@@ -117,28 +117,27 @@ class Math::Polynomial {
!! ($a xx $b).reduce(* * *);
}
- # sub _divmod($this, $that) {
- # my @den = $that.coefficients;
- # @den or croak 'division by zero polynomial';
- # my $hd = pop @den;
- # if ($that->is_monic) {
- # undef $hd;
- # }
- # my @rem = $this->coeff;
- # my @quot = ();
- # my $i = $#rem - @den;
- # while (0 <= $i) {
- # my $q = pop(@rem);
- # if (defined $hd) {
- # $q /= $hd;
- # }
- # $quot[$i] = $q;
- # my $j = $i--;
- # foreach my $d (@den) {
- # $rem[$j++] -= $q * $d;
- # }
- # }
- # return (\@quot, \@rem);
- # }
-
+ method divmod($that) {
+ my @den = $that.coefficients;
+ @den or fail 'division by zero polynomial';
+ my $hd = @den.pop;
+ if $that.is-monic {
+ $hd = Any;
+ }
+ my @rem = self.coefficients;
+ my @quot;
+ my $i = (@rem - 1) - @den;
+ while (0 <= $i) {
+ my $q = @rem.pop;
+ if $hd.defined {
+ $q /= $hd;
+ }
+ @quot[$i] = $q;
+ my $j = $i--;
+ for @den -> $d {
+ @rem[$j++] -= $q * $d;
+ }
+ }
+ return Math::Polynomial.new(@quot), Math::Polynomial.new(@rem);
+ }
}
View
52 t/03_expressions.t
@@ -192,36 +192,36 @@ ok(has_coeff($qq)); # 0 * p
# $qq = eval { $zp->mmod($zp) };
# ok(!defined $qq); # not defined 0 mmod 0
# ok($@ =~ /division by zero polynomial/);
-#
-# my $rr;
-# ($qq, $rr) = $p->divmod($q);
-# ok(has_coeff($qq, 5)); # p / q
-# ok(has_coeff($rr, 1)); # p % q
-# ($qq, $rr) = $p->divmod($r);
-# ok(has_coeff($qq, 5/16, 5/8)); # p / r
-# ok(has_coeff($rr, 1/16)); # p % r
-# ($qq, $rr) = $p->divmod($mr);
-# ok(has_coeff($qq, 5/8, 5/4)); # p / mr
-# ok(has_coeff($rr, 1/16)); # p % mr
-# ($qq, $rr) = $p->divmod($c);
-# ok(has_coeff($qq, 0.5, 0, -2.5)); # p / c
-# ok(has_coeff($rr)); # p % c
+
+my $rr;
+($qq, $rr) = $p.divmod($q);
+ok(has_coeff($qq, 5)); # p / q
+ok(has_coeff($rr, 1)); # p % q
+($qq, $rr) = $p.divmod($r);
+ok(has_coeff($qq, 5/16, 5/8)); # p / r
+ok(has_coeff($rr, 1/16)); # p % r
+($qq, $rr) = $p.divmod($mr);
+ok(has_coeff($qq, 5/8, 5/4)); # p / mr
+ok(has_coeff($rr, 1/16)); # p % mr
+($qq, $rr) = $p.divmod($c);
+ok(has_coeff($qq, 0.5, 0, -2.5)); # p / c
+ok(has_coeff($rr)); # p % c
# ($qq, $rr) = eval { $p->divmod($zp) };
# ok(!defined $qq); # not defined p / 0
# ok(!defined $rr); # not defined p % 0
# ok($@ =~ /division by zero polynomial/);
-# ($qq, $rr) = $r->divmod($p);
-# ok(has_coeff($qq)); # r / p
-# ok(has_coeff($rr, -1, 2)); # r % p
-# ($qq, $rr) = $r->divmod($s);
-# ok(has_coeff($qq, 4)); # r / s
-# ok(has_coeff($rr, -3)); # r % s
-# ($qq, $rr) = $c->divmod($p);
-# ok(has_coeff($qq)); # c / p
-# ok(has_coeff($rr, -0.5)); # c % p
-# ($qq, $rr) = $zp->divmod($p);
-# ok(has_coeff($qq)); # zp / p
-# ok(has_coeff($rr)); # zp % p
+($qq, $rr) = $r.divmod($p);
+ok(has_coeff($qq)); # r / p
+ok(has_coeff($rr, -1, 2)); # r % p
+($qq, $rr) = $r.divmod($s);
+ok(has_coeff($qq, 4)); # r / s
+ok(has_coeff($rr, -3)); # r % s
+($qq, $rr) = $c.divmod($p);
+ok(has_coeff($qq)); # c / p
+ok(has_coeff($rr, -0.5)); # c % p
+($qq, $rr) = $zp.divmod($p);
+ok(has_coeff($qq)); # zp / p
+ok(has_coeff($rr)); # zp % p
# ($qq, $rr) = eval { $zp->divmod($zp) };
# ok(!defined $qq); # not defined 0 / 0
# ok(!defined $rr); # not defined 0 % 0
Please sign in to comment.
Something went wrong with that request. Please try again.