Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1996
*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 1996

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

Search the Archive

Re: patterns

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

In article <4ki9uv$9dm at dragonfly.wolfram.com>,
Susan Rempe  <rempe at euclid.chem.washington.edu> 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:

In[83]:=
  list1 = {Sin[theta]/r, Cos[theta]/(r*s), 2*Csc[theta]/(2*r)}
	
Out[83]=
   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.

In[90]:=
	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. *)
	
	Function[
		{location},
		Take[expressionList, location]
	],
	
	(* Returns a list of locations of successful matches. *)
	
	Position[
	
		(* Returns a list of results of comparison of each denominator
			with bottom. *)
	
		Map[
			
			(* Returns "True" if the denominator matches bottom *)
			
			Function[
				{expression},
				Denominator[expression] === bottom
			],
			expressionList
		],
		True
	]
  ] // Flatten; (* Flatten removes some unnecessary curly brackets. *)

  returnMatchingBottoms[list1, r]

Out[92]=
   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 gl.umbc.edu  |  Bubbles bear no introspection."  -Khushhal Khan Khatak

==== [MESSAGE SEPARATOR] ====


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