Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

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

Search the Archive

Re: Finding a function within an arbitrary expression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg131468] Re: Finding a function within an arbitrary expression
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Tue, 30 Jul 2013 06:40:35 -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: <20130630072942.2AF536A20@smc.vnet.net>

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]]}


The Select applies its test to each element of the list (data) -- elements
which are NOT themselves lists.


>From the documentation for MemberQ:


"MemberQ[list, form] returns True if an element of list matches form, and
False otherwise."


"The first argument of MemberQ can have any head, not necessarily List."


"The default value for levelspec in MemberQ is {1}."


The "list" for the MemberQ are not actually lists. The default level spec
of {1} is used as if the Head of each element of the original list were
replaced by List. Effectively,


data2 == Pick[data,
  MemberQ[#, u[1, 0]] & /@ ((# /. Head[#] :> List &) /@ data)]


True


However, if the u[1,0] is buried deeper than in your example a level spec
is needed


data = {Exp[3*u[1, 0]], u[0, 0]/10., 1/u[1, 0], f[u[1, 0]]};


This doesn't work


Select[data, MemberQ[#, u[1, 0]] &]


{1/u[1, 0], f[u[1, 0]]}


But these do


data2 = Select[data, MemberQ[#, u[1, 0], 2] &]


{E^(3*u[1, 0]), 1/u[1, 0],
   f[u[1, 0]]}


data2 == Select[data, MemberQ[#, u[1, 0], Infinity] &]


True



Bob Hanlon




On Mon, Jul 29, 2013 at 11:20 PM, amannucci <Anthony.J.Mannucci at jpl.nasa.gov
> wrote:

> On Monday, July 1, 2013 2:43:28 AM UTC-7, Bob Hanlon wrote:
> > 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: [snip]
> >
> >
> > Bob Hanlon
> >
> >
>
> Your top example is a good way to do it. I am completely mystified,
> however. I originally tried to do this with Cases, but that did not work
> because of expression levels. I did not specify an expression level when
> using Cases.
>
> You do not specify the level in your MemberQ call, but it works. Yet, this
> does not work:
> MemberQ[{3*u[1, 0]}, u[1, 0]] gives False
> but
> MemberQ[{3*u[1, 0]}, u[1, 0],Infinity] gives True
>
> So why don't you need a level spec in your example (definition of data2)?
> It's as if creating a lambda function automatically inserts the level spec.
>
>


  • Prev by Date: word documents
  • Next by Date: Pattern with powers
  • Previous by thread: Re: Finding a function within an arbitrary expression
  • Next by thread: Re: Graphing template