MathGroup Archive 2013

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

Search the Archive

Re: Finding a function within an arbitrary expression

  • To: mathgroup at
  • Subject: [mg131633] Re: Finding a function within an arbitrary expression
  • From: "Mannucci, Anthony J (335G)" <anthony.j.mannucci at>
  • Date: Sat, 14 Sep 2013 06:03:39 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:


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 > Tony.Mannucci at <mailto:Tony.Mannucci at>
 4800 Oak Grove Drive,         
 Pasadena, CA 91109

From: Bob Hanlon <hanlonr357 at<mailto:hanlonr357 at>>
Date: Sunday, June 30, 2013 7:01 AM
To: Tony Mannucci <Anthony.J.Mannucci at<mailto:Anthony.J.Mannucci at>>
Cc: MathGroup <mathgroup at<mailto:mathgroup at>>
Subject: [mg131633] 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 <Anthony.J.Mannucci at<mailto:Anthony.J.Mannucci at>> 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.

  • Prev by Date: multiintegral and table
  • Next by Date: FindRoot
  • Previous by thread: Re: multiintegral and table
  • Next by thread: Re: Finding a function within an arbitrary expression