Re: Pattern Matching: Keep those summands not depending on x
- To: mathgroup at smc.vnet.net
- Subject: [mg63828] Re: Pattern Matching: Keep those summands not depending on x
- From: "ben" <benjamin.friedrich at gmail.com>
- Date: Tue, 17 Jan 2006 04:33:24 -0500 (EST)
- References: <dq7us0$40n$1@smc.vnet.net><dqacpv$1dd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Thank you all for your solutions Valeri Astanoff wrote me the shortest one: Select[exp0, FreeQ[#, x] &]. However, the suggestion Coefficient[exp0,x,0] does not work, just think of exp0=1+Sin[x]. Peter Pein schrieb: > 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