Re: Re: Extracting Terms From a List
- To: mathgroup at smc.vnet.net
- Subject: [mg44206] Re: [mg44193] Re: [mg44183] Extracting Terms From a List
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 28 Oct 2003 05:53:12 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
This question intrigued me so I thought some more about it. Here is a
more complicated function than before, which however does much more!
fun[l_] := Flatten[Cases[l, s:_Integer | _?( !FreeQ[#1, _^2] & ) ->
Switch[s, _Integer, s, _^2, s, _?( !FreeQ[#1, _^2] & ), fun[s]]]]
for example:
fun[{3, Sqrt[2], (x + y)^2, Exp[x^2 + y^2 + 1]}]
{3, (x + y)^2, 1, x^2, y^2}
Andrzej Kozlowski
Yokohama, Japan
http://www.mimuw.edu.pl/~akoz/
On Sunday, October 26, 2003, at 01:41 PM, Andrzej Kozlowski wrote:
>
> On Saturday, October 25, 2003, at 07:26 PM, Bruce W. Colletti wrote:
>
>>
>> How would I cull integers and squared terms from {3, Sqrt[2], (x+y)^2,
>> x+y^2}? The desired output is {3, (x+y)^2, y^2}. Unfortunately:
>>
>> Cases[{3, Sqrt[2], (x+y)^2, x+y^2}, _Integer | _^2]
>>
>> returns {3, (x+y)^2} .
>>
>> Thankx.
>>
>> Bruce
>>
>>
>>
>
> Probably you either have to use a rather elaborate pattern or apply
> Cases twice:
>
>
>
> Join[Cases[{3, Sqrt[2], (x + y)^2, x + y^2}, _Integer],
> Cases[{3, Sqrt[2], (x + y)^2, x + y^2}, _^2, {1, 2}]]
>
>
> {3, (x + y)^2, y^2}
>
>
> Andrzej Kozlowski
> Yokohama, Japan
> http://www.mimuw.edu.pl/~akoz/
>
>
>