RE: polynomial operations through CoefficientList
- To: mathgroup at smc.vnet.net
- Subject: [mg44752] RE: [mg44729] polynomial operations through CoefficientList
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 25 Nov 2003 00:45:20 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Paolo Bientinesi [mailto:pauldj at cs.utexas.edu] To: mathgroup at smc.vnet.net >Sent: Monday, November 24, 2003 6:05 AM >To: mathgroup at smc.vnet.net >Subject: [mg44752] [mg44729] polynomial operations through CoefficientList > > >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 > Paolo, With In[1]:= cf1 = {1, k1, -2 k2}; In[2]:= cf2 = {-2, -k3}; you may add lists "component-wise": In[3]:= With[{len = Max[Length /@ {##}]}, PadRight[#, len] & /@ Unevaluated[Plus[##]]] &[cf1, cf2] Out[3]= {-1, k1 - k3, -2 k2} Adjusting for same length appears to be a bit nasty! Multiplication is easier: In[4]:= ListConvolve[cf1, cf2, {1, -1}, 0] Out[4]= {-2, -2 k1 - k3, 4 k2 - k1 k3, 2 k2 k3} To me however it appears to be much more appropriate to use Mathematicas built-in abilities to deal with polynomials, and come back to coefficient lists only when necessary for the results (if ever). In[5]:= p1 = Normal[SeriesData[x, 0, cf1]] Out[5]= 1 + k1*x - 2*k2*x^2 In[6]:= p2 = Normal[SeriesData[x, 0, cf2]] Out[6]= -2 - k3 x In[7]:= CoefficientList[p1 + p2, x] Out[7]= {-1, k1 - k3, -2 k2} In[8]:= CoefficientList[p1*p2, x] Out[8]= {-2, -2 k1 - k3, 4 k2 - k1 k3, 2 k2 k3} -- Hartmut Wolf