in reply to (Golf) Multiply polynomials
With the help of PDL I only use 43 chars for the sub, and if I looked right, it's a record in this thread. You may add 8 chars if you want to account for the extra 'use PDL;' statement:
This doesn't work with arrays of unequal length, but that wasn't in the spec. The output:use PDL: sub p{$t=pdl+pop;$t=$t*(pdl$_)for@_;[list$t]}
BTW, tilly, why did you name it 'polynomials'? Isn't it just a breed of recursive vector multiplication? Do I miss something?print join " ", @{p([1..5],[1..5])}; 1 4 9 16 25
And, 'strictness' requires only 3 extra chars with a total of 46/54 chars (54 still is the record):
Jeroensub p{my $t=pdl+pop;$t=$t*(pdl$_)for@_;[list$t]}
But that's difficult to code in PDL, unfortunately. Here is my 2nd attempt, works only if poly's are equal length (pathetic 164 chars):A = p1'*p2; product = crossdiags(A);
Will ponder it a bit more and get back....sub p{$t=pdl+pop;for(@_){ my $a=outer$t,pdl+pop; transpose($a); my $n=-1+nelem$t; $t=zeroes($n*2+1); $t->slice("$_:".($_+$n))+=$a->slice("$_,:")for 0..$n; } [list$t]}
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: (Golf) Multiply polynomials
by tye (Sage) on May 11, 2001 at 13:43 UTC |