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] ====