confusion on tuples versus arrays

153 views
Skip to first unread message

Steven G. Johnson

unread,
Dec 27, 2012, 2:51:33 PM12/27/12
to juli...@googlegroups.com
Right now, tuples and arrays can be used in somewhat similar ways, but
with some seemingly random inconsistencies.

Consider `a = [1,2,3,4]` and `t = (1,2,3,4)`, for example. `a[1]` and
`t[1]` both work to give a single element, and `a[1:3]` and `t[1:3]`
both work to get subarray/tuple. However, arrays accept arrays of
indices while tuples do not, and neither accept tuples of indices:
`a[a]` works, but neither `t[a]` nor `a[t]` nor `t[t]` do.

`sum` and `prod` work for both arrays and tuples, but `mean` only works
for arrays.

`sum(A,dims)` works if `dims` is a tuple, but not if `dims` is an array.

Is there a general principle underlying when tuples are used and when
arrays are used?

One possible guiding principle might be that in any context involving an
array index, e.g. `[...]` or `Dimspec`, if it makes sense to allow
multiple indices then either an array or a tuple or a range should be
allowed.

Jeff Bezanson

unread,
Dec 28, 2012, 1:25:54 PM12/28/12
to juli...@googlegroups.com

This happened because tuples were not really supposed to be general containers, but functions were added for them one by one as they were needed. Probably various function signatures can be broadened. One problem is functions like max(a,b) where b might specify dimensions, or this might be an elementwise max over a and b.

--



Reply all
Reply to author
Forward
0 new messages