Re: Extract coefficients of a trig polynomial
- To: mathgroup at smc.vnet.net
- Subject: [mg126150] Re: Extract coefficients of a trig polynomial
- From: "Dr. Wolfgang Hintze" <weh at snafu.de>
- Date: Sat, 21 Apr 2012 00:29:59 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jmoge7$46g$1@smc.vnet.net>
On 19 Apr., 09:55, Sam Takoy <sam.ta... at yahoo.com> wrote: > Hi, > > Suppose I have an expression that is a trigonometric polynomial in > theta. Is there a way to neatly pick out the coefficients of the > polynomial. I find that FourierCoefficient takes quite a bit of time, > probably because it does a lot of integrations. My coefficients are > very complicated expressions but do not depend on theta. > > Many thanks in advance, > > Sam > > PS: expr = (1/1536)(1536 BesselJ[0,\[Rho]]-72 \[Epsilon]^2 \[Rho]^2 > BesselJ[0,\[Rho]]-80 \[Epsilon]^3 \[Rho]^2 BesselJ[0,\[Rho]]-384 \ > [Epsilon] \[Rho] BesselJ[1,\[Rho]]-144 \[Epsilon]^2 \[Rho] BesselJ[1,\ > [Rho]]-80 \[Epsilon]^3 \[Rho] BesselJ[1,\[Rho]]+10 \[Epsilon]^3 \ > [Rho]^3 BesselJ[1,\[Rho]]-96 \[Epsilon]^2 \[Rho]^2 BesselJ[0,\[Rho]] > Cos[2 \[Theta]]-120 \[Epsilon]^3 \[Rho]^2 BesselJ[0,\[Rho]] Cos[2 \ > [Theta]]-384 \[Epsilon] \[Rho] BesselJ[1,\[Rho]] Cos[2 \[Theta]]-192 \ > [Epsilon]^2 \[Rho] BesselJ[1,\[Rho]] Cos[2 \[Theta]]-120 \[Epsilon]^3 \ > [Rho] BesselJ[1,\[Rho]] Cos[2 \[Theta]]+15 \[Epsilon]^3 \[Rho]^3 > BesselJ[1,\[Rho]] Cos[2 \[Theta]]-24 \[Epsilon]^2 \[Rho]^2 BesselJ[0,\ > [Rho]] Cos[4 \[Theta]]-48 \[Epsilon]^3 \[Rho]^2 BesselJ[0,\[Rho]] > Cos[4 \[Theta]]-48 \[Epsilon]^2 \[Rho] BesselJ[1,\[Rho]] Cos[4 \ > [Theta]]-48 \[Epsilon]^3 \[Rho] BesselJ[1,\[Rho]] Cos[4 \[Theta]]+6 \ > [Epsilon]^3 \[Rho]^3 BesselJ[1,\[Rho]] Cos[4 \[Theta]]-8 \[Epsilon]^3 \ > [Rho]^2 BesselJ[0,\[Rho]] Cos[6 \[Theta]]-8 \[Epsilon]^3 \[Rho] > BesselJ[1,\[Rho]] Cos[6 \[Theta]]+\[Epsilon]^3 \[Rho]^3 BesselJ[1,\ > [Rho]] Cos[6 \[Theta]]) > > Hi, to avoid special characters I'm gong to rewrite expr as In[14]:= expr = (1/1536)*(1536*BesselJ[0, r] - 72*q^2*r^2*BesselJ[0, r] - 80*q^3*r^2*BesselJ[0, r] - 384*q*r*BesselJ[1, r] - 144*q^2*r*BesselJ[1, r] - 80*q^3*r*BesselJ[1, r] + 10*q^3*r^3*BesselJ[1, r] - 96*q^2*r^2*BesselJ[0, r]*Cos[2*t] - 120*q^3*r^2*BesselJ[0, r]*Cos[2*t] - 384*q*r*BesselJ[1, r]*Cos[2*t] - 192*q^2*r*BesselJ[1, r]*Cos[2*t] - 120*q^3*r*BesselJ[1, r]*Cos[2*t] + 15*q^3*r^3*BesselJ[1, r]*Cos[2*t] - 24*q^2*r^2*BesselJ[0, r]*Cos[4*t] - 48*q^3*r^2*BesselJ[0, r]*Cos[4*t] - 48*q^2*r*BesselJ[1, r]*Cos[4*t] - 48*q^3*r*BesselJ[1, r]*Cos[4*t] + 6*q^3*r^3*BesselJ[1, r]*Cos[4*t] - 8*q^3*r^2*BesselJ[0, r]*Cos[6*t] - 8*q^3*r*BesselJ[1, r]*Cos[6*t] + q^3*r^3*BesselJ[1, r]*Cos[6*t]) Out[14]= (1/1536)*(1536*BesselJ[0, r] - 72*q^2*r^2*BesselJ[0, r] - 80*q^3*r^2*BesselJ[0, r] - 384*q*r*BesselJ[1, r] - 144*q^2*r*BesselJ[1, r] - 80*q^3*r*BesselJ[1, r] + 10*q^3*r^3*BesselJ[1, r] - 96*q^2*r^2*BesselJ[0, r]*Cos[2*t] - 120*q^3*r^2*BesselJ[0, r]*Cos[2*t] - 384*q*r*BesselJ[1, r]*Cos[2*t] - 192*q^2*r*BesselJ[1, r]*Cos[2*t] - 120*q^3*r*BesselJ[1, r]*Cos[2*t] + 15*q^3*r^3*BesselJ[1, r]*Cos[2*t] - 24*q^2*r^2*BesselJ[0, r]*Cos[4*t] - 48*q^3*r^2*BesselJ[0, r]*Cos[4*t] - 48*q^2*r*BesselJ[1, r]*Cos[4*t] - 48*q^3*r*BesselJ[1, r]*Cos[4*t] + 6*q^3*r^3*BesselJ[1, r]*Cos[4*t] - 8*q^3*r^2*BesselJ[0, r]*Cos[6*t] - 8*q^3*r*BesselJ[1, r]*Cos[6*t] + q^3*r^3*BesselJ[1, r]*Cos[6*t]) > I would use Cases with apropriate patterns like this > > First make a list out of your expression: > In[15]:= el = List @@ (List @@ expr)[[2]] Out[15]= {1536*BesselJ[0, r], -72*q^2*r^2*BesselJ[0, r], -80*q^3*r^2*BesselJ[0, r], -384*q*r*BesselJ[1, r], -144*q^2*r*BesselJ[1, r], -80*q^3*r*BesselJ[1, r], 10*q^3*r^3*BesselJ[1, r], -96*q^2*r^2*BesselJ[0, r]*Cos[2*t], -120*q^3*r^2*BesselJ[0, r]*Cos[2*t], -384*q*r*BesselJ[1, r]*Cos[2*t], -192*q^2*r*BesselJ[1, r]*Cos[2*t], -120*q^3*r*BesselJ[1, r]*Cos[2*t], 15*q^3*r^3*BesselJ[1, r]*Cos[2*t], -24*q^2*r^2*BesselJ[0, r]*Cos[4*t], -48*q^3*r^2*BesselJ[0, r]*Cos[4*t], -48*q^2*r*BesselJ[1, r]*Cos[4*t], -48*q^3*r*BesselJ[1, r]*Cos[4*t], 6*q^3*r^3*BesselJ[1, r]*Cos[4*t], -8*q^3*r^2*BesselJ[0, r]*Cos[6*t], -8*q^3*r*BesselJ[1, r]*Cos[6*t], q^3*r^3*BesselJ[1, r]*Cos[6*t]} > > Then collect all terms the desired patterns in Cos[] > > Cos with any agrument: > In[22]:= c1 = Cases[el, _*Cos[_]] Out[22]= {-96*q^2*r^2*BesselJ[0, r]*Cos[2*t], -120*q^3*r^2*BesselJ[0, r]*Cos[2*t], -384*q*r*BesselJ[1, r]*Cos[2*t], -192*q^2*r*BesselJ[1, r]*Cos[2*t], -120*q^3*r*BesselJ[1, r]*Cos[2*t], 15*q^3*r^3*BesselJ[1, r]*Cos[2*t], -24*q^2*r^2*BesselJ[0, r]*Cos[4*t], -48*q^3*r^2*BesselJ[0, r]*Cos[4*t], -48*q^2*r*BesselJ[1, r]*Cos[4*t], -48*q^3*r*BesselJ[1, r]*Cos[4*t], 6*q^3*r^3*BesselJ[1, r]*Cos[4*t], -8*q^3*r^2*BesselJ[0, r]*Cos[6*t], -8*q^3*r*BesselJ[1, r]*Cos[6*t], q^3*r^3*BesselJ[1, r]*Cos[6*t]} > > Cos[2 times anything] and so on > In[17]:= Cases[el, _*Cos[2*_]] Out[17]= {-96*q^2*r^2*BesselJ[0, r]*Cos[2*t], -120*q^3*r^2*BesselJ[0, r]*Cos[2*t], -384*q*r*BesselJ[1, r]*Cos[2*t], -192*q^2*r*BesselJ[1, r]*Cos[2*t], -120*q^3*r*BesselJ[1, r]*Cos[2*t], 15*q^3*r^3*BesselJ[1, r]*Cos[2*t]} > In[18]:= Cases[el, _*Cos[4*_]] Out[18]= {-24*q^2*r^2*BesselJ[0, r]*Cos[4*t], -48*q^3*r^2*BesselJ[0, r]*Cos[4*t], -48*q^2*r*BesselJ[1, r]*Cos[4*t], -48*q^3*r*BesselJ[1, r]*Cos[4*t], 6*q^3*r^3*BesselJ[1, r]*Cos[4*t]} > In[19]:= Cases[el, _*Cos[6*_]] Out[19]= {-8*q^3*r^2*BesselJ[0, r]*Cos[6*t], -8*q^3*r*BesselJ[1, r]*Cos[6*t], q^3*r^3*BesselJ[1, r]*Cos[6*t]} > Finally, here is the list of terms without Cos > In[21]:= c0 = Complement[el, c1] Out[21]= {1536*BesselJ[0, r], -72*q^2*r^2*BesselJ[0, r], -80*q^3*r^2*BesselJ[0, r], -384*q*r*BesselJ[1, r], -144*q^2*r*BesselJ[1, r], -80*q^3*r*BesselJ[1, r], 10*q^3*r^3*BesselJ[1, r]} > > Hope this helps. > > Regards, > Wolfgang