Re: Separating constants from nonconstants in an expression
- To: mathgroup at smc.vnet.net
- Subject: [mg52454] Re: Separating constants from nonconstants in an expression
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Sat, 27 Nov 2004 01:41:42 -0500 (EST)
- References: <co73p0$52k$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Nicolas Girard wrote: > Hi, > given any expression like > > expr := (4 Pi k1 x k2 y)/(12 k3 t k4 u) > > and knowing that k1...k4 are constants, how could I separate these terms > from the non constant ones, i.e. ending up with > > Pi k1 k2 x y > -------- . --- > 3 k3 k4 t u > > ? I've looked into the manual & browsed this newsgroup, but didn't find a > clue... > > Many thanks in advance ! > Nicolas > > Nicolas, The first thing to realise is that the output you want is 'unstable' in the sense that Mathematica will sort the terms in a multimplication and potentially mix up the variables that you wish to consider constant with those you wish to be considered variables. For this reason, the code that follows returns a list with a constant bit and a variable bit. First we define what you mean for something to be constant. I've allowed things like k1^4 to be considered constant as well: constant[x_?NumericQ] := True; constant[x_^p_.] := MemberQ[{k1, k2, k3, k4}, x] /; NumericQ[p]; constant[_] := False; Then it is easy to define a function that will recursively remove constant bits from your expression: split[{c_, x_. y_?constant}] := split[{c*y, x}] /; (y =!= 1); split[{c_, x_}] := {c, x}; split[x_] := split[{1, x}]; (4 Pi k1 x k2 y)/(12 k3 t k4 u) // split//InputForm {(k1*k2*Pi)/(3*k3*k4), (x*y)/(t*u)} Note that this is still quite specialised - it doesn't treat (k1+5) or Exp[k2], for example, to be constant. David Bailey