MathGroup Archive 2002

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

Search the Archive

Re: Sequence and Or

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34216] Re: [mg34179] Sequence and Or
  • From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
  • Date: Fri, 10 May 2002 03:04:58 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

You can always define your own version of Apply (myApply) which will 
achieve the same effect with any number of arguments generated with 
something like Table, e.g.

In[1]:=
p1 := 1 == 1

In[2]:=
p2 := FullSimplify[Gamma[z]*z == z!]

In[3]:=
SetAttributes[myApply,HoldAll]

In[4]:=
myApply[f_,{x__}]:=f[x]

In[5]:=
myApply[Or,{p1,p2}]//Timing

Out[5]=
{0. Second,True}

In[6]:=
Or@@{p1,p2}//Timing

Out[6]=
{0.87 Second,True}

Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/

On Thursday, May 9, 2002, at 02:34  PM, DrBob wrote:

> On second thought, I doubt this feature has much benefit, unless it
> works in a case like
>
> Or@@expr
>
> where expr resolves to a long list of logical expressions.  I'm
> wondering how that could be accomplished, and I fear it can't be.
>
> If this performance feature is limited to small numbers of arguments to
> Or... as many as you'd be willing to type... the benefit seems small.
> With large numbers of arguments, it seems we'd have to use While, or a
> similar construct.
>
> Bobby Treat
>
> -----Original Message-----
> From: Andrzej Kozlowski [mailto:andrzej at platon.c.u-tokyo.ac.jp]
To: mathgroup at smc.vnet.net
> Sent: Wednesday, May 08, 2002 11:56 PM
> To: drbob at bigfoot.com
> Cc: mathgroup at smc.vnet.net
> Subject: [mg34216] Re: [mg34179] Sequence and Or
>
> It seems to me this is just a matter of efficiency. Suppose you want to
> evaluate Or[p1,p2,p3,...,pn] for some statements p1, p2 ...pn. It is
> enough that just one pi is True for the answer to be true. So
> Mathematica evaluates each statement in turn and stops whenever it finds
>
> one that turns out to be true. This can produce a big gain in
> efficiency, particularly if you sensibly arranged the statements so that
>
> those more likely to be true come earlier. Of course for this sort of
> thing to be of any use you need to have the HoldAll attribute otherwise
> Mathematica would first evaluate all the statements even if only p1 was
> true and easy to check and the rest hard.
>

>
>
>
> On Wednesday, May 8, 2002, at 11:54  PM, DrBob wrote:
>
>> Thanks for the workaround!  It makes me wonder, though, why Or should
>> have the HoldAll attribute at all.
>>
>> Bobby
>>
>> -----Original Message-----
>> From: Andrzej Kozlowski [mailto:andrzej at platon.c.u-tokyo.ac.jp]
To: mathgroup at smc.vnet.net
>> Sent: Wednesday, May 08, 2002 5:27 AM
>> To: drbob at bigfoot.com
>> Cc: mathgroup at smc.vnet.net
>> Subject: [mg34216] Re: [mg34179] Sequence and Or
>>
>> Compare:
>>
>> In[19]:=
>> Or[Sequence@@{True,False,True}]
>>
>> Out[19]=
>> Sequence[True,False,True]
>>
>> In[20]:=
>> Or[Evaluate[Sequence@@{True,False,True}]]
>>
>> Out[20]=
>> True
>>
>> In[21]:=
>> Or[Sequence[True,False,True]]
>>
>> Out[21]=
>> True
>>
>> The point is the HoldAll  attribute of Or. Because of it, when you
> enter
>>
>> Or[Sequence@@{True,False,True}]  only a single argument is passed to
> Or
>> but Or with a single argument returns:
>>
>> In[22]:=
>> Or[x]
>>
>> Out[22]=
>> x
>>
>>
>>
>>
>> On Wednesday, May 8, 2002, at 02:57  PM, DrBob wrote:
>>
>>> When I evaluate the following...
>>>
>>> {True,False,True}
>>> Or@@%
>>> Sequence@@%
>>> Or[%]
>>>
>>> the outputs are:
>>>
>>> {True,False,True}
>>> True
>>> Sequence[True,False,True]
>>> Sequence[True,False,True]
>>>
>>> while I had expected:
>>>
>>> {True,False,True}
>>> True
>>> Sequence[True,False,True]
>>> True
>>>
>>> What am I missing?
>>>
>>> Bobby Treat
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>
>
>



  • Prev by Date: RE: Sequence and Or
  • Next by Date: Re: Q: Displaying Current time/date in Page headers
  • Previous by thread: RE: Sequence and Or
  • Next by thread: Re: Sequence and Or