Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: (Golf) Multiply polynomials

by Masem (Monsignor)
on May 07, 2001 at 18:45 UTC ( #78512=note: print w/ replies, xml ) Need Help??


in reply to (Golf) Multiply polynomials

Here's a first shot, 110 characters in body only -- lacks some error check (it's going to fail with anything less than two arguements), but if you consider the second arg to be 0, then that makes sense.. :-)

use strict; my @poly1 = ( 2, 4, 6 ); my @poly2 = ( 3, 1, 0 ); my @poly4 = ( 5, 2, 1, 2 ); my @poly3 = p( \@poly1, \@poly2, \@poly4 ); print (join',',@poly3)."\n"; my @poly5 = p( \@poly4, \@poly1, \@poly2 ); print (join',',@poly5)."\n"; sub p { my$a=shift;my$b=shift;my(@c,$i,$j);for$i(0..$#$a){for$j(0..$#$b){$c[ +$i+$j]+=$$a[$i]*$$b[$j]}};@_?p(\@c,@_):\@c }

Update: Cut down 5 more characters to 105, since you don't need to have $j around in the inner loop:

sub p { my$a=shift;my$b=shift;my(@c,$i);for$i(0..$#$a){for(0..$#$b){$c[$i+$_ +]+=$$a[$i]*$$b[$_]}};@_?p(\@c,@_):\@c }

Update #2 as per tilly's reply, fixed the return problem to add that extra character.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain


Comment on Re: (Golf) Multiply polynomials
Select or Download Code
Re (tilly) 2: (Golf) Multiply polynomials
by tilly (Archbishop) on May 07, 2001 at 18:57 UTC
    Problem clarification.

    By "return a polynomial" I mean return one in the given representation, ie it should be an anonymous array. So you need to change the last @c for \@c. (Makes this 110 characters.) The rule about different polynomials means that if you call this twice, the second answer should be a reference to a different array than the first.

Re: Re: (Golf) Multiply polynomials
by Masem (Monsignor) on May 07, 2001 at 22:00 UTC
    New approach, but still 102. I don't I can break it any more...
    sub p { my($a,$b)=(shift,shift); my@c=map{my($d,$e);for$e(0..$_){$d+=$$a[$e]*$$b[$_-$e]}$d}0..$#$a+$# +$b; @_?p(\@c,@_):\@c }

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

      Simple improvement:

      my($a,$b)=(shift,shift); my$a=shift;my$b=shift; # a bit shorter my$a=pop;my$b=pop; # even shorter since multiplaction is commutative

              - tye (but my friends call me "Tye")
Re: Re: (Golf) Multiply polynomials
by MeowChow (Vicar) on May 07, 2001 at 22:05 UTC
    This can be trimmed down to 90 chars:
    my($a,$b,@r,$c)=@_;for my$i(0..$#$a){$$c[$i+$_]+=$$a[$i]*$$b[$_]for +0..$#$b}@r?p($c,@r):$c
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      (Posted at MeowChow's request.)

      My best is 77:

      sub p{ @m=1;for$p(@_){my@t;for$i(0..@m){my$j;$t[$i+$j++]+=$_*$m[$i]for@$p}@m= +@t}[@m] }
      Note that this introduces 0's through a fencepost error, but they don't change which polynomial is represented. I think this is fair, but if you think that is cheating, you can not save that character:
      sub p{ @m=1;for$p(@_){my@t;for$i(0..$#m){my$j;$t[$i+$j++]+=$_*$m[$i]for@$p}@m +=@t}[@m] }
      The trick lies in finding ways to not work through explicit lookups by index, and in finding ways to not access arrays through references. In fact there is not a single lookup by index of an element in an array reference. (It was cheaper to create and manually increment the index variable.)

      BTW note that the statement of the rules anticipated and forbade saving a character by ending with \@m without making @m a private variable.

      Finally at a request from chatter, here is the solution broken out and commented:

      sub p{ @m=1; # Start the product at 1. for$p(@_){ # Loop over the polynomials. my@t; # Create a private temp array. for$i(0..@m){ # Loop over the indexes of @m. my$j; # Create the *other* index var. $t[$i+$j++]+= # Manually increment $j while.. # Adding to the index of @t.. $_*$m[$i] # 2 terms multiplied together.. for@$p # for all the terms in the.. } # other polynomial. @m=@t # Make the temp array our new } # product. [@m] # Return our answer in the } # desired form.

      UPDATE
      Never say you are done, 2 more characters:

      sub p{ @m=1;for$p(@_){my@t;for$i(0..@m){$j=$i;$t[$j++]+=$_*$m[$i]for@$p}@m=@t +}[@m] }

      UPDATE 2
      (This is a couple of days later.) Truly never say never, there were 2 more wasted characters to 73:

      sub p{ @m=1;for$p(@_){$i=my@t;for$,(@m){$j=$i++;$t[$j++]+=$_*$,for@$p}@m=@t}[ +@m] }
        D'oh, I see where I was going wrong, I was reading it as [3,2] being 3x+2. Now I see ability to whack zeros with no problem :D

        However, to nitpick, the 75 char solution (update above), isn't strict; you need to add 12 characters to strict-ify it. (my@m, for my $i, for my $p, my$j ), so to compare with the other solutions, at least mine being all strict, you're at 87.

        update-fixed bad square brackets.


        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://78512]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-07-11 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls