Re: Pattern Matching: Keep those summands not depending on x
- To: mathgroup at smc.vnet.net
- Subject: [mg63788] Re: Pattern Matching: Keep those summands not depending on x
- From: Peter Pein <petsie at dordos.net>
- Date: Sat, 14 Jan 2006 02:32:44 -0500 (EST)
- References: <dq7us0$40n$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
ben schrieb:
> Dear all,
>
> I have a complicated sum and want to extract all summands not depending
> on x.
>
> I have a clumsy way to do that, but I wonder whether there is a better
> solution.
>
> So that is what I have done:
>
> (* The sum *)
>
> exp0 = 1+x;
>
> (* Thread a dummy function foo1 over the summands using FixedPoint *)
> exp1=foo1[exp0];
>
> foo2[exp_]:=exp/.{foo1[a_+b_]\[Rule]foo1[a]+foo1[b]}
>
> exp2=FixedPoint[foo2/.{foo1[a_+b_]\[Rule]foo1[a]+foo1[b]},
> exp1]/.{foo1[exp_]\[Rule]foo3[exp]};
>
> (* Replace dummy function foo1 by another one f003
> that keeps the terms not depending on
> x and drops the others *)
> foo3[exp_]:=If[FreeQ[exp,s],exp,0]
>
> (* The result *)
> exp2
>
> Any help is appreciated
>
> Ben
>
Hi Ben,
you don't need dummy functions. Let's take a slightly more complicated expression:
(1 + x + y)*(z + x) /. a_Plus :> Plus @@ Select[List @@ a, FreeQ[#1, x] & ]
--> (1 + y)*z
or if your problem allows this:
(1 + x + y)*(z + x) /. x -> 0
--> (1 + y)*z
Peter