MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: polynomial operations through CoefficientList

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44765] Re: [mg44729] polynomial operations through CoefficientList
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Tue, 25 Nov 2003 00:45:33 -0500 (EST)
  • References: <200311240505.AAA09907@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Paolo Bientinesi wrote:
> 
> Hello,
> does anybody have 2 functions ready to multiply and add
> 2 polynomials directly from their CoefficientList's?
> 
> example: the polynomials I'm considering are
> p1[x_] := 1 + k1 x - 2 k2 x^2
> p2[x_] := -2 -k3 x
> 
> Given
> cf1 = {1,k1,-2 k2}
> and
> cf2 = {-2,-k3}
> 
> I would like to get the list
> {-2,-2 k1-k3,4 k2-k1 k3,2 k2 k3}
> for p1[x]*p2[x]
> 
> and the list
> {-1,k1-k3,-2 k2}
> for p1[x]+p2[x]
> 
> without performing operations like
> 
> CoefficientList[
>  cf1.Table[x^i,{i,0,Length[cf1]-1}]*
>  cf2.Table[x^i,{i,0,Length[cf2]-1}], x]
> 
> Thanks!
> --
> Paolo
> 
> pauldj at cs.utexas.edu                    paolo.bientinesi at iit.cnr.it


The code below will do this.

truncateRight[c1_] := Module[{len=Length[c1]},
  While[c1[[len]]===0, len--]; Take[c1,len]]

coefficientListPlus[c1_,c2_] := With[{len=Max[Length[c1],Length[c2]]},
  truncateRight[PadRight[cf1,len] + PadRight[cf2,len]]]

coefficientListTimes[c1_,c2_] := ListConvolve[c1, c2, {1,-1}, 0]


For your example:

p1[x_] := 1 + k1*x - 2*k2*x^2
p2[x_] := -2 -k3*x

cf1 = CoefficientList[p1[x], x];
cf2 = CoefficientList[p2[x], x];

In[24]:= InputForm[coefficientListPlus[cf1,cf2]]
Out[24]//InputForm= {-1, k1 - k3, -2*k2}

In[25]:= InputForm[coefficientListTimes[cf1,cf2]]
Out[25]//InputForm= {-2, -2*k1 - k3, 4*k2 - k1*k3, 2*k2*k3}


For the special case where one works with univariate polynomials with
coefficients in a prime field there is more efficient code for
multiplication (though it does not much matter if the degree tends to be
low).


Daniel Lichtblau
Wolfram Research


  • Prev by Date: Re: polynomial operations through CoefficientList
  • Next by Date: RE: [Integrate] Why two results of same eq. are different?
  • Previous by thread: polynomial operations through CoefficientList
  • Next by thread: Re: polynomial operations through CoefficientList