Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14427 > unrolled thread
| Started by | Dan O'Brien <danobrie@gmail.com> |
|---|---|
| First post | 2013-05-06 08:10 +0000 |
| Last post | 2013-05-07 07:53 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Listable Attribute of Pure Function that returns a matrix Dan O'Brien <danobrie@gmail.com> - 2013-05-06 08:10 +0000
Re: Listable Attribute of Pure Function that returns a matrix albert.retey@gmail.com - 2013-05-07 07:53 +0000
| From | Dan O'Brien <danobrie@gmail.com> |
|---|---|
| Date | 2013-05-06 08:10 +0000 |
| Subject | Listable Attribute of Pure Function that returns a matrix |
| Message-ID | <km7oig$j75$1@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[25]:= ClearAll[func]
SetAttributes[func, Listable]
func[a, b, c, Array[d, 4]]
Out[27]= {func[a, b, c, d[1]], func[a, b, c, d[2]],
func[a, b, c, d[3]], func[a, b, c, d[4]]}
Ok, this is fine. Now, if I take the symbol 'f' and define a function
that returns a matrix
In[35]:= func[a_, b_, c_, d_] := {{a, b}, {c, d}}
func[a, b, c, Array[d, 4]]
Out[36]= {{{a, b}, {c, d[1]}}, {{a, b}, {c, d[2]}}, {{a, b}, {c,
d[3]}}, {{a, b}, {c, d[4]}}}
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[27] and then evaluates.
Now, I do the same but instead I define a pure function
In[37]:= ClearAll[func]
SetAttributes[func, Listable]
func = {{#1, #2}, {#3, #4}} &;
func[a, b, c, Array[d, 4]]
Out[40]= {{a, b}, {c, {d[1], d[2], d[3], d[4]}}}
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[42]:= ClearAll[func2]
SetAttributes[func2, Listable]
func2 = (#1 + #2)/(#3 - #4) &
func2[a, b, c, Array[d, 4]]
Out[44]= (#1 + #2)/(#3 - #4) &
Out[45]= {(a + b)/(c - d[1]), (a + b)/(c - d[2]), (a + b)/(
c - d[3]), (a + b)/(c - d[4])}
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[40] expected?
[toc] | [next] | [standalone]
| From | albert.retey@gmail.com |
|---|---|
| Date | 2013-05-07 07:53 +0000 |
| Message-ID | <kmabt9$qd3$1@smc.vnet.net> |
| In reply to | #14427 |
Hi,
>
> Now, I do the same but instead I define a pure function
>
> In[37]:= ClearAll[func]
>
> SetAttributes[func, Listable]
>
> func = {{#1, #2}, {#3, #4}} &;
>
> func[a, b, c, Array[d, 4]]
>
>
>
> Out[40]= {{a, b}, {c, {d[1], d[2], d[3], d[4]}}}
>
>
>
> Clearly this is not behaving as a listable function, its simply
>
> substituting the list of d's for the 4th argument.
>
I think this is expected, although probably not easy to see from the documentation: if you want the pure function to be listable you need to set the Listable attribute to the pure function, not the symbol which happens to be set to that pure function. There is a special three argument form of `Function` which allows to do that and a undocumented (?) feature which lets you use that three argument form and still use the Slot-syntax for the one-argument form. Searching for Function and Slot in the documentation will give you some more details and examples. Altogether this should work:
func = Function[Null, {{#1, #2}, {#3, #4}}, Listable]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web