Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

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

Search the Archive

Re: patterns

  • To: mathgroup at
  • Subject: [mg3738] Re: patterns
  • From: rhall2 at (hall robert)
  • Date: Sun, 14 Apr 1996 03:00:27 -0400
  • Organization: University of Maryland, Baltimore County
  • Sender: owner-wri-mathgroup at

In article <4ki9uv$9dm at>,
Susan Rempe  <rempe at> wrote:
>I have a list of numbers and symbols.
>How can I collect all the members of the list
>which have a common denominator?
>ex.  list={Sin[theta]/r, Cos[theta]/(r*s), 2*Csc[theta]/(2*r)}
> Say I want to collect all members of the list with r in the denominator;
> i.e.  answer=Sin[theta]/r;

Mathematica automatically evaluates the elements in a list,
with this result:

  list1 = {Sin[theta]/r, Cos[theta]/(r*s), 2*Csc[theta]/(2*r)}
   Sin[theta]  Cos[theta]  Csc[theta]
  {----------, ----------, ----------}
       r          r s          r

As a result, the following function returns the 1st and 3rd
elements, not just the 1st. The function name is "returnMatchingBottoms",
which sounds like a method used at a daycare center to make sure the
parents of twins get the correct two infants back at the end of the
day. Oh well.

	who's denominators match the pattern called "bottom". The first
	anonymous function goes to those locations and takes the 
	expressions, placing them in a list of expressions with matching
	denominators. *)

  returnMatchingBottoms[expressionList_, bottom_] := Map[
	(* Takes expressions from expressionList. *)
		Take[expressionList, location]
	(* Returns a list of locations of successful matches. *)
		(* Returns a list of results of comparison of each denominator
			with bottom. *)
			(* Returns "True" if the denominator matches bottom *)
				Denominator[expression] === bottom
  ] // Flatten; (* Flatten removes some unnecessary curly brackets. *)

  returnMatchingBottoms[list1, r]

   Sin[theta]  Csc[theta]
  {----------, ----------}
       r           r

For the sake of brevity, you can use the following:

returnMatchingBottoms[l_, b_] := Flatten[
	Take[l, #]& /@ Position[(Denominator[#] === b)& /@ l, True]

Bob Hall            | "Know thyself? Absurd direction!
rhall2 at  |  Bubbles bear no introspection."  -Khushhal Khan Khatak


  • Prev by Date: Re: patterns
  • Next by Date: RE: DSolveConstants question
  • Previous by thread: Re: patterns
  • Next by thread: Re:Trig. Simplification