Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

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


  • Prev by Date: Re: filtering graphics
  • Next by Date: Re: launching a kernel on a remote linux machine through ssh from a linux machine
  • Previous by thread: Re: Separating constants from nonconstants in an expression
  • Next by thread: filtering of a graphics