Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15264

Re: Finding a function within an arbitrary expression

From Bob Hanlon <hanlonr357@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Finding a function within an arbitrary expression
Date 2013-07-30 10:37 +0000
Message-ID <kt851b$fnb$1@smc.vnet.net> (permalink)
References <20130630072942.2AF536A20@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


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@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.
>
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Finding a function within an arbitrary expression Bob Hanlon <hanlonr357@gmail.com> - 2013-07-30 10:37 +0000

csiph-web