Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: Finding a function within an arbitrary expression
Posted:
Sep 14, 2013 6:01 AM


Bob,
This does not quite work, for reasons that I do not understand. I add the errant case here that is not one you considered, which is why your tests all passed. The following code snippet will suffice to show what I mean. It comes down to the difference between FreeQ and MemberQ. Any insight you have here is greatly appreciated.
u[1,0] and u[2,0] evaluate to themselves (no function evaluation occurs. These are undefined symbols.)
Try this:
datav = {u[1, 0], u[2, 0], 1.1*u[1, 0]}; Select[datav, MemberQ[#, u[1, 0], Infinity] &] Cases[datav, _?(MemberQ[#, u[1, 0], Infinity] &)] Select[datav, ! FreeQ[#, u[1, 0]] &]
You will see that only the last version gives the correct answer. The Infinity level in MemberQ is needed for other reasons (this differs from your example and was covered in a separate email).
Basically, the standalone u[1,0] expression (first element) is not found by MemberQ, but is found by FreeQ. The documentation for these two functions differs:
MemberQ[list,form]
returns True if an element of list matches form, and False otherwise.
FreeQ[expr,form]
yields True if no subexpression in expr matches form, and yields False otherwise.
The documentation does not immediately tell me why these two functions should behave as in this example. FreeQ wants an expression and gets a list. That puts a head on the expression and somehow allows FreeQ to find the standalone u[1,0]. MemberQ wants a list, so looks at the input differently than FreeQ.
And yet, this yields True:
MemberQ[{u[1, 0]}, u[1, 0], Infinity]
Mathematica 8.0.4
Tony
 Tony Mannucci Supervisor, Ionospheric and Atmospheric Remote Sensing Group MailStop 138308, Tel > (818) 3541699 Jet Propulsion Laboratory, Fax > (818) 3935115 California Institute of Technology, Email > Tony.Mannucci@jpl.nasa.gov <mailto:Tony.Mannucci@jpl.nasa.gov> 4800 Oak Grove Drive, http://scienceandtechnology.jpl.nasa.gov/people/a_mannucci/ Pasadena, CA 91109
From: Bob Hanlon <hanlonr357@gmail.com<mailto:hanlonr357@gmail.com>> Date: Sunday, June 30, 2013 7:01 AM To: Tony Mannucci <Anthony.J.Mannucci@jpl.nasa.gov<mailto:Anthony.J.Mannucci@jpl.nasa.gov>> Cc: MathGroup <mathgroup@smc.vnet.net<mailto:mathgroup@smc.vnet.net>> Subject: Re: Finding a function within an arbitrary expression
data = {3*u[1, 0], u[0, 0]/10., 1/u[1, 0], f[u[1, 0]]};
data2 = Select[data, MemberQ[#, u[1, 0]] &]
{3*u[1, 0], 1/u[1, 0], f[u[1, 0]]}
Some other methods:
data2 == Cases[data, _?(MemberQ[#, u[1, 0]] &)] == DeleteCases[data, _?(! MemberQ[#, u[1, 0]] &)] == Select[data, ! FreeQ[#, u[1, 0]] &] == Cases[data, _?(! FreeQ[#, u[1, 0]] &)] == DeleteCases[data, _?(FreeQ[#, u[1, 0]] &)] == data[[First /@ Position[data, u[1, 0]]]]
True
Bob Hanlon
On Sun, Jun 30, 2013 at 3:29 AM, amannucci <Anthony.J.Mannucci@jpl.nasa.gov<mailto:Anthony.J.Mannucci@jpl.nasa.gov>> wrote: I am trying to figure out what that pattern is for the following. The difficulty here is that I can think textually, but not in Mathematica patterns.
I am looking for a function u[1,0] within a list of expressions. The function might not be defined yet. For example, a list of expressions might be:
{3*u[1,0], u[0,0]/10., 1/u[1,0], ...}
I want to find all the expressions that have the function u[1,0] in it. There is certainly a way to do this "textually". How would one do this with patterns? Is it possible? Thank you.



