MathGroup Archive 2002

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

Search the Archive

Re: A possible bug in Lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34417] Re: [mg34392] A possible bug in Lists
  • From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
  • Date: Sun, 19 May 2002 04:14:29 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

I must say, I am a little surprised that you think there could be a bug 
at such a basic level, which must have presumably survived over a decade 
of Mathematica's existence without anyone noticing ??

Th answer to why you  can do this is: because it is very useful. 
Remember, Mathematica is above all a programming language.

The mechanism responsible for this is called "listability". Suppose you 
have a function, say f, and give it the attribute Listable:

In[1]:=
SetAttributes[f,Listable]

f will now thread over list of the same length, as follows:

In[2]:=
f[{a,b,c},{d,e,f}]

Out[2]=
{f[a,d],f[b,e],f[c,f]}

But additionally it will work if one of the arguments is not a list but 
just a single element:

In[3]:=
f[{a,b,c},d]

Out[3]=
{f[a,d],f[b,d],f[c,d]}

The function Plus, like many mathematica functions has the attribute 
Listable:

In[4]:=
Attributes[Plus]

Out[4]=
{Flat,Listable,NumericFunction,OneIdentity,Orderless,Protected}

That basically should answer your question. One might add that 
listability is defined in terms of the Mathematica function thread:

In[5]:=
ClearAll[f]

In[6]:=
Thread[f[{a,b,c},{d,e,f}]]

Out[6]=
{f[a,d],f[b,e],f[c,f]}

In[7]:=
Thread[f[{a,b,c},d]]

Out[7]=
{f[a,d],f[b,d],f[c,d]}



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




On Saturday, May 18, 2002, at 04:50  PM, Doron wrote:

> Hello there ,
>
> I am having a problem in Mathematica 4.1 :
>
> Evaluating : 1-{1,1} or {1,1}-1 returns {0,0}
>
> Is that ok ?
>
> how is it that I can subtract a vector from a scalar ?
>
> Thank you for your help , Doron .
>
>
>



  • Prev by Date: Re: Re[2]: cannot solve *trivial* equation
  • Next by Date: RE: Why can't I cut and paste between Mathematica and other applications?
  • Previous by thread: Re: A possible bug in Lists
  • Next by thread: RE: A possible bug in Lists