Permalink
Browse files

Rework the Polynomial + Polynomial code in hope of making it compatib…

…le with Vector-valued coefficients.
  • Loading branch information...
1 parent 5dfc043 commit 6a12e0d30547cfcc9094b437499a44957227bd5e SF committed Oct 12, 2009
Showing with 16 additions and 4 deletions.
  1. +16 −4 lib/Polynomial.pm
View
20 lib/Polynomial.pm
@@ -6,7 +6,7 @@ class Polynomial
multi method new (*@x is copy)
{
- while @x.elems > 1 && @x[*-1] == 0
+ while @x.elems > 1 && @x[*-1].abs < 1e-13
{
say @x.perl;
say @x[*-1];
@@ -25,7 +25,7 @@ class Polynomial
multi method new (@x is copy)
{
- while @x.elems > 1 && @x[*-1] == 0
+ while @x.elems > 1 && @x[*-1].abs < 1e-13
{
# say @x.perl;
# say @x[*-1];
@@ -45,7 +45,6 @@ class Polynomial
our Str multi method Str()
{
(^(@.coefficients.elems)).map({"{@.coefficients[$_]} x^$_"}).reverse.join(" + ");
- # (^3).map({"{@.coefficients[$_]} x^$_"}).reverse.join(" + ");
}
our Str multi method perl()
@@ -61,7 +60,20 @@ class Polynomial
multi sub infix:<+>(Polynomial $a, Polynomial $b)
{
- return Polynomial.new(($a.coefficients, 0) <<+>> ($b.coefficients, 0));
+ my $lct = 0;
+ my @leftover = ();
+ given $a.coefficients.elems <=> $b.coefficients.elems
+ {
+ when -1 { $lct = $a.coefficients.elems - 1; @leftover = $b.coefficients[($lct+1)..(*-1)]; }
+ when +1 { $lct = $b.coefficients.elems - 1; @leftover = $a.coefficients[($lct+1)..(*-1)]; }
+ when 0 { $lct = $b.coefficients.elems - 1; }
+ }
+ # say "a: {$a.coefficients}";
+ # say "b: {$b.coefficients}";
+ # say "c: {$a.coefficients[0..$lct].perl}";
+ # say "d: {$b.coefficients[0..$lct].perl}";
+ # say "e: {@leftover.perl}";
+ return Polynomial.new($a.coefficients[0..$lct] >>+<< $b.coefficients[0..$lct], @leftover);
}
multi sub infix:<+>(Polynomial $a, $b)

0 comments on commit 6a12e0d

Please sign in to comment.