The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Topic: Re: Finding a function within an arbitrary expression
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  

Posts: 29
Registered: 9/18/08
Re: Finding a function within an arbitrary expression
Posted: Sep 14, 2013 6:01 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


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 stand-alone u[1,0] expression (first element) is not found by MemberQ, but is found by FreeQ. The documentation for these two functions differs:


returns True if an element of list matches form, and False otherwise.


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 stand-alone 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 Mannucci
Supervisor, Ionospheric and Atmospheric Remote Sensing Group
Mail-Stop 138-308, Tel > (818) 354-1699
Jet Propulsion Laboratory, Fax > (818) 393-5115
California Institute of Technology, Email > <>
4800 Oak Grove Drive,
Pasadena, CA 91109

From: Bob Hanlon <<>>
Date: Sunday, June 30, 2013 7:01 AM
To: Tony Mannucci <<>>
Cc: MathGroup <<>>
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]]]]


Bob Hanlon

On Sun, Jun 30, 2013 at 3:29 AM, amannucci <<>> 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.

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2017. All Rights Reserved.