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: [mg34431] RE: [mg34392] A possible bug in Lists
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sun, 19 May 2002 04:15:03 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Doron,

No, this is not a bug. It is a matter of obtaining a deeper understanding of
what Listable means and how Thread works. Look at the Attributes of Plus.

Attributes[Plus]
{Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected}

In this case, the key attribute is Listable. If you look up Listable you
will find that it means that the function is threaded over lists. Most of
Mathematica's standard functions are listable. This means that you can add
vectors, or do simultaneous operations on lists, and is very useful.

What is a little confusing is the way that Thread works. Let's try Thread on
a function f.

Thread[f[{a1, a2}, {b1, b2}]]
{f[a1, b1], f[a2, b2]}

If f was Plus, it would add the vectors. Since Plus is Listable and
automatically Threads, that is what it does. If we try to thread lists of
unequal length, it balks.

Thread[f[{a1, a2, a3, a4}, {b1, b2}]]
Thread::"tdlen": "Objects of unequal length in \!\(f[\(\(\(\({a1, a2, a3,
a4}\
\)\), \(\({b1, b2}\)\)\)\)]\) cannot be combined."

You obtain the same error message if you try:

{a1, a2, a3, a4} + {b1, b2}

But, if you put in an item which is not a list, then Mathematica converts it
to a list equal to the other lists by repeating the element. That is the
behavior that is a bit unexpected.

Thread[f[{a, b}, c]]
{f[a1, b1], f[a2, b2]}

This is actually useful. For example, the following raises each item on a
list to a separate power, because Power is Listable.

{a, b}^{c, d}
{a^c, b^d}

But a more common operation would be to raise each item on the list to the
same power.

{a, b}^c
{a^c, b^c}

You couldn't do that if Thread did not expand nonlist items. {a, b}^{c} will
not work.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


> From: Doron [mailto:Klepachd at yahoo.com]
To: mathgroup at smc.vnet.net
>
>
> 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: Why can't I cut and paste between Mathematica and other applications?
  • Next by Date: RE: Re: cannot solve *trivial* equation
  • Previous by thread: Re: A possible bug in Lists
  • Next by thread: j instead of I