Re: Selecting Element from List via Pattern Matching

• To: mathgroup at smc.vnet.net
• Subject: [mg89409] Re: [mg89396] Selecting Element from List via Pattern Matching
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Mon, 9 Jun 2008 02:25:52 -0400 (EDT)

```Well, it seems to me very strange that one would voluntarily choose to
do so much unnecessary work, beginning with converting the original
expression into a list, but there is no accounting for tastes...

Even if you really love pattern matching there is no need to convert
the original expression to a list - you can simply do:

Cases[Expand[Sum[Subscript[a, i], {i, 1, 5}]^3],
_.*Subscript[a, 2]^2]

{3 Subscript[a, 1]
SubsuperscriptBox[ a ,  2 ,  2 ], 3
SubsuperscriptBox[ a ,  2 ,  2 ] Subscript[a, 3], 3
SubsuperscriptBox[ a ,  2 ,  2 ] Subscript[a, 4], 3
SubsuperscriptBox[ a ,  2 ,  2 ] Subscript[a, 5]}

More importantly,  note that your method will not work if you change

t = Apply[List,
Subscript[a, 1]^2 + Expand[(Sum[Subscript[a, i], {i, 1, 5}])^3]];

Select[t, MemberQ[#1, Subscript[a, 1]^2] & ]
{3*Subscript[a, 1]^2*Subscript[a, 2],
3*Subscript[a, 1]^2*Subscript[a, 3],
3*Subscript[a, 1]^2*Subscript[a, 4],
3*Subscript[a, 1]^2*Subscript[a, 5]}

Cases[t, (_.)*Subscript[a, 1]^2]

{Subscript[a, 1]^2, 3*Subscript[a, 1]^2*
Subscript[a, 2], 3*Subscript[a, 1]^2*
Subscript[a, 3], 3*Subscript[a, 1]^2*
Subscript[a, 4], 3*Subscript[a, 1]^2*
Subscript[a, 5]}

Andrzej

On 8 Jun 2008, at 20:32, Immanuel Bloch wrote:

> Hello Andrzej,
>
> Select[t, MemberQ[#, Power[Subscript[a, 1], 2]] &]
>
> seems to have solved my problem.
>
> Thanks!
>
> Immanuel
>
> On Jun 8, 2008, at 9:16 , Andrzej Kozlowski wrote:
>
>> On 8 Jun 2008, at 15:32, immanuel.bloch at googlemail.com wrote:
>>
>>> Hello,
>>>
>>> I am trying to extract from a Power Series all elements with a
>>> certain exponent, e.g.
>>> let us say I have generated a list via
>>>
>>> t = Apply[List, Expand[(Sum[Subscript[a, i], {i, 1, 5}])^3]]
>>>
>>> How can I then extract all terms with e.g. a_2^2 in it or any other
>>> exponent?
>>>
>>> I have tried something like
>>>
>>> Cases[t, Times[_, Power[Subscript[a,2],2]]]
>>>
>>> but this somehow only works with the exponent n=1 and no others...
>>>
>>> Does anybody know a solution to this pattern matching problem?
>>>
>>> Thanks!
>>> Immanuel
>>>
>>
>> But what's wrong with simply doing this:
>>
>> Expand[Coefficient[Sum[Subscript[a, i], {i, 1, 5}]^3, Subscript[a,
>> 2]^2]*
>>     Subscript[a, 2]^2]
>>
>> 3*Subscript[a, 1]*Subscript[a, 2]^2 +
>>   3*Subscript[a, 3]*Subscript[a, 2]^2 +
>>   3*Subscript[a, 4]*Subscript[a, 2]^2 +
>>   3*Subscript[a, 5]*Subscript[a, 2]^2
>>
>> ???
>>
>>
>> Andrzej Kozlowski
>>
>

```

• Prev by Date: Re: A question about N[...]
• Next by Date: Re: Dynamic GUI problems
• Previous by thread: Re: Selecting Element from List via Pattern Matching
• Next by thread: Re: Selecting Element from List via Pattern Matching