Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #15436
| From | "Mannucci, Anthony J (335G)" <anthony.j.mannucci@jpl.nasa.gov> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Finding a function within an arbitrary expression |
| Date | 2013-09-14 10:01 +0000 |
| Message-ID | <l11c66$j02$1@smc.vnet.net> (permalink) |
| Organization | Time-Warner Telecom |
Bob,
Thanks for these elegant solutions. In my view, these subtle distinctions are not helpful to users of the language. Perhaps there are "too many" ways to do things in Mathematica, although of course that can help with optimization. It appears the language kind of evolved through a somewhat haphazard process, or at least evolved by "extreme" experts. It would have been helpful to design these functions to solve specific problems and encourage standardized uses of the functions. Instead, users must parse through a multitude of possibilities to get at the one that works. Perhaps "too many choices". Of course, Mathematica remains an extremely powerful language.
-Tony
--
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@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: Friday, September 13, 2013 10:24 PM
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
On closer reading of the documentation, use any of the following
datav = {u[1, 0], u[2, 0], 1.1*u[1, 0]};
Either include level 0 so that the expresion does not need a head
Select[datav, MemberQ[#, u[1, 0],
{0, Infinity}] &]
{u[1, 0], 1.1 u[1, 0]}
Cases[datav, _?(MemberQ[#, u[1, 0],
{0, Infinity}] &)]
{u[1, 0], 1.1 u[1, 0]}
or give it a head, say of List
Select[datav, MemberQ[{#}, u[1, 0],
Infinity] &]
{u[1, 0], 1.1 u[1, 0]}
Cases[datav, _?(MemberQ[{#}, u[1, 0],
Infinity] &)]
{u[1, 0], 1.1 u[1, 0]}
or of any other arbitrary head since the head need not be List
Select[datav, MemberQ[arbitraryHead[#],
u[1, 0], Infinity] &]
{u[1, 0], 1.1 u[1, 0]}
Cases[datav, _?(MemberQ[arbitraryHead[#],
u[1, 0], Infinity] &)]
{u[1, 0], 1.1 u[1, 0]}
Bob Hanlon
On Fri, Sep 13, 2013 at 5:27 PM, Mannucci, Anthony J (335G) <anthony.j.mannucci@jpl.nasa.gov<mailto:anthony.j.mannucci@jpl.nasa.gov>> wrote:
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 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:
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 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
--
Tony Mannucci
Supervisor, Ionospheric and Atmospheric Remote Sensing Group
Mail-Stop 138-308, Tel > (818) 354-1699<tel:%28818%29%20354-1699>
Jet Propulsion Laboratory, Fax > (818) 393-5115<tel:%28818%29%20393-5115>
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.
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Finding a function within an arbitrary expression "Mannucci, Anthony J (335G)" <anthony.j.mannucci@jpl.nasa.gov> - 2013-09-14 10:01 +0000
csiph-web