Re: Finding a function within an arbitrary expression
- To: mathgroup at smc.vnet.net
- Subject: [mg131635] Re: Finding a function within an arbitrary expression
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Sat, 14 Sep 2013 06:04:19 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
- References: <CAEtRDSdto1VGsF3EdFnE+yuhhTaci98KC6Pc=CjtstSUQWgkLA@mail.gmail.com>
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 at 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
> Jet Propulsion Laboratory, Fax > (818) 393-5115
> California Institute of Technology, Email >
> Tony.Mannucci at jpl.nasa.gov
> 4800 Oak Grove Drive,
> http://scienceandtechnology.jpl.nasa.gov/people/a_mannucci/
> Pasadena, CA 91109
>
>
> From: Bob Hanlon <hanlonr357 at gmail.com>
> Date: Sunday, June 30, 2013 7:01 AM
> To: Tony Mannucci <Anthony.J.Mannucci at jpl.nasa.gov>
> Cc: MathGroup <mathgroup at 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 at 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.
>>
>>
>>
>