Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Selecting subscripted variables from an expression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109150] Re: Selecting subscripted variables from an expression
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Mon, 19 Apr 2010 04:12:10 -0400 (EDT)

myNumerator = 
  Subscript[a, 1]*Subscript[b, 0]^5 - 
   Subscript[a, 2]*Subscript[b, 0]^4*Subscript[b, 1] + 
   Subscript[a, 3]*Subscript[b, 0]^3*Subscript[b, 1]^2 - 
   Subscript[a, 4]*Subscript[b, 0]^2*Subscript[b, 1]^3 + 
   Subscript[a, 5]*Subscript[b, 0]*Subscript[b, 1]^4 - 
   Subscript[a, 6]*Subscript[b, 1]^5 - 
   Subscript[a, 3]*Subscript[b, 0]^4*Subscript[b, 2] + 
   2*Subscript[a, 4]*Subscript[b, 0]^3*Subscript[b, 1]*Subscript[b, 2] - 
   3*Subscript[a, 5]*Subscript[b, 0]^2*Subscript[b, 1]^2*Subscript[b, 2] + 
   4*Subscript[a, 6]*Subscript[b, 0]*Subscript[b, 1]^3*Subscript[b, 2] + 
   Subscript[a, 5]*Subscript[b, 0]^3*Subscript[b, 2]^2 - 
   3*Subscript[a, 6]*Subscript[b, 0]^2*Subscript[b, 1]*Subscript[b, 2]^2 - 
   Subscript[a, 4]*Subscript[b, 0]^4*Subscript[b, 3] + 
   2*Subscript[a, 5]*Subscript[b, 0]^3*Subscript[b, 1]*Subscript[b, 3] - 
   3*Subscript[a, 6]*Subscript[b, 0]^2*Subscript[b, 1]^2*Subscript[b, 3] + 
   2*Subscript[a, 6]*Subscript[b, 0]^3*Subscript[b, 2]*Subscript[b, 3] - 
   Subscript[a, 5]*Subscript[b, 0]^4*Subscript[b, 4] + 
   2*Subscript[a, 6]*Subscript[b, 0]^3*Subscript[b, 1]*Subscript[b, 4] - 
   Subscript[a, 6]*Subscript[b, 0]^4*Subscript[b, 5];

Use Cases

Cases[myNumerator, Subscript[a, 1]*_.]

{Subscript[a, 1]*Subscript[b, 0]^5}

Note the use of the default ( _. ).  Although not necessary for your specific example, the following two examples show why this is necessary in general.

Cases[Subscript[a, 1] + myNumerator, Subscript[a, 1]*_]

{Subscript[a, 1]*Subscript[b, 0]^5}

Cases[Subscript[a, 1] + myNumerator, Subscript[a, 1]*_.]

{Subscript[a, 1], Subscript[a, 1]*Subscript[b, 0]^5}

t = Table[{
   Subscript[a, n],
   Cases[myNumerator, Subscript[a, n]*_.]},
  {n, 6}]

Length[t[[All, 2]] // Flatten] == Length[myNumerator]

True


Bob Hanlon

---- Dominic <miliotodc at rtconline.com> wrote: 

=============
Hi guys.  I have an expression containing terms with subscripted "a" and "b" variables.  Can someone help me devise an algorithm to select from this sum, all the terms that have a particular subscripted variable as the first term, such as all the terms that begin with an a_1 in them.  For example, if I have the expression:

myNumerator=2a_1a_2b_2  +  a_2  +  a_1b_2b_3  +  a_3b_2_b_3

then the routine would pick out the 2a_1a_2b_2 term and the a_1b_2b_3 terms.

The expression below is a particular example.  So I suppose the routine might look like some version of:

Select[myNumerator, (all terms that begin with a_1 for example)]

myNumerator = Subscript[a, 1]*Subscript[b, 0]^5 -
Subscript[a, 2]*Subscript[b, 0]^4*Subscript[b, 1] +
Subscript[a, 3]*Subscript[b, 0]^3*Subscript[b, 1]^2 -
Subscript[a, 4]*Subscript[b, 0]^2*Subscript[b, 1]^3 +
Subscript[a, 5]*Subscript[b, 0]*Subscript[b, 1]^4 -
Subscript[a, 6]*Subscript[b, 1]^5 - Subscript[a, 3]*Subscript[b, 0]^4*
Subscript[b, 2] + 2*Subscript[a, 4]*Subscript[b, 0]^3*Subscript[b, 1]*
Subscript[b, 2] - 3*Subscript[a, 5]*Subscript[b, 0]^2*Subscript[b, 1]^2*
Subscript[b, 2] + 4*Subscript[a, 6]*Subscript[b, 0]*Subscript[b, 1]^3*
Subscript[b, 2] + Subscript[a, 5]*Subscript[b, 0]^3*Subscript[b, 2]^2 -
3*Subscript[a, 6]*Subscript[b, 0]^2*Subscript[b, 1]*Subscript[b, 2]^2 -
Subscript[a, 4]*Subscript[b, 0]^4*Subscript[b, 3] +
2*Subscript[a, 5]*Subscript[b, 0]^3*Subscript[b, 1]*Subscript[b, 3] -
3*Subscript[a, 6]*Subscript[b, 0]^2*Subscript[b, 1]^2*Subscript[b, 3] +
2*Subscript[a, 6]*Subscript[b, 0]^3*Subscript[b, 2]*Subscript[b, 3] -
Subscript[a, 5]*Subscript[b, 0]^4*Subscript[b, 4] +
2*Subscript[a, 6]*Subscript[b, 0]^3*Subscript[b, 1]*Subscript[b, 4] -
Subscript[a, 6]*Subscript[b, 0]^4*Subscript[b, 5];

Thanks guys,
Dominic




  • Prev by Date: Re: How to make style sheet with header
  • Next by Date: Easy way to label graphics grids?
  • Previous by thread: Re: Selecting subscripted variables from an expression
  • Next by thread: Washington DC Area Mathematica Special Interest Group