       Listable Attribute of Pure Function that returns a matrix

• To: mathgroup at smc.vnet.net
• Subject: [mg130724] Listable Attribute of Pure Function that returns a matrix
• From: Dan O'Brien <danobrie at gmail.com>
• Date: Mon, 6 May 2013 04:24:13 -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

```As I understand it, a function which has its attribute set to Listable
should thread over any arguments presented as lists.

In:= ClearAll[func]
SetAttributes[func, Listable]
func[a, b, c, Array[d, 4]]

Out= {func[a, b, c, d], func[a, b, c, d],
func[a, b, c, d], func[a, b, c, d]}

Ok, this is fine.  Now, if I take the symbol 'f' and define a function
that returns a matrix

In:= func[a_, b_, c_, d_] := {{a, b}, {c, d}}
func[a, b, c, Array[d, 4]]

Out= {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c,
d}}, {{a, b}, {c, d}}}

Still doing fine, this threaded over my array of d's and returned a list
of arrays as expected. It appears that mathematica constructs the list
of Out and then evaluates.

Now, I do the same but instead I define a pure function
In:= ClearAll[func]
SetAttributes[func, Listable]
func = {{#1, #2}, {#3, #4}} &;
func[a, b, c, Array[d, 4]]

Out= {{a, b}, {c, {d, d, d, d}}}

Clearly this is not behaving as a listable function, its simply
substituting the list of d's for the 4th argument.

If I do the same for a pure function that does not return a list,
everything is fine:

In:= ClearAll[func2]
SetAttributes[func2, Listable]
func2 = (#1 + #2)/(#3 - #4) &
func2[a, b, c, Array[d, 4]]

Out= (#1 + #2)/(#3 - #4) &

Out= {(a + b)/(c - d), (a + b)/(c - d), (a + b)/(
c - d), (a + b)/(c - d)}

And in any case, mathematica behaves the same here if I don't do
anything with the Attributes of func2, that is, there is no need to
explicitly SetAttributes to Listable for this particular example.

I don't pretend to know all the ins and outs of Attributes, but is my
result for the pure function 'func' in Out expected?

```

• Prev by Date: Re: Minimize Headscratcher
• Next by Date: Re: Preventing a plot used as background from updating
• Previous by thread: Re: space between frame and plot on contourplot
• Next by thread: Re: Listable Attribute of Pure Function that returns a