-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iteration on 1-D arrays #818
Comments
FWIW, from an ideal perspecitive, I still think Once you define Remmber, that the other argument was e.g. sympy, which doesn't use the list of list analogy and iterates all elements in its Matrix. So the reason for that is, that conceptually there are other choices, and those choices may actually be better where it not for the fact that most users are indoctrinated to the list-of-list view of things. |
Well my question here is specifically about the 1-D case. I don't think there is any ambiguity in that case, and based on the scipy changes, it seems to be much more common. |
Ah, so allow 1-D iteration on 1-d arrays. Not sure how important it is, but that makes sense to me. |
Are there examples besides SymPy of linear indexing in Python? I know Matlab does it, too, |
I agree it would be useful to document whether 1-D iteration is supported, explicitly must raise, or is undefined. The most important data point is: do all libraries currently allow 1-D iteration? Would you be able to check @asmeurer? |
For the record: I don't think this is true, and I don't know of data on how to prove/disprove it either way. There's a lot of users who will think about this as 2-D/3-D regular grids and can visualize it like that (also the case for me), which is much more intuitive for for example physicists than "list of lists". |
N-D is intuitive, but the question is what you think when you see So yeah, it is intuitive for physicist. But I still think when it comes down to it, even many of those who find N-D intuitive, will probably reach to the list-of-list analogy when they see a for loop. |
PyTorch, jax.numpy, dask.array, and surprisingly even sparse all allow 1-D iteration. They all actually seem to just follow NumPy on n-D iteration (I didn't test CuPy but it's obviously the same as NumPy). |
In case it matters, I tested TF last night and it also seems to follow NumPy on n-D iteration. import tensorflow as tf
x = tf.constant([[1, 2, 3], [4, 5, 6]])
x[0] # <tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3], dtype=int32)>
for row in x:
print(row)
# tf.Tensor([1 2 3], shape=(3,), dtype=int32)
# tf.Tensor([4 5 6], shape=(3,), dtype=int32) Same with xarray. |
Fix at #821 |
Recently in array-api-strict, I accidentally disabled iteration on 1-D arrays. This broke a lot of code in SciPy. I've since reverted the change (array-api-strict disallows iteration on >1-D arrays but allows it for 1-D arrays).
There have been discussions in the past about now allowing iteration on arrays #188. Disallowing it for higher dimensional arrays is probably fine, but it's unclear whether a library like array-api-strict should disallow it for 1-D arrays. The reason is that technically speaking, an array object that implements on the methods defined in the standard would allow iteration on 1-D arrays. This is because by default if
__iter__
is not defined but__getitem__
is, Python defines iteration asa[0]
,a[1]
, etc.Given how painful this can be for upstream code, I wonder if we should make it explicit in the standard that iteration is defined for 1-D arrays.
A possible counterargument is that the new
unstack
function can be used to iterate on an array of any dimension.unstack(x)
is the same asiter(x)
in the NumPy sense of iteration (it iterates the elements ifx
is 1-dimensional and along the first axis if it is n-dimensional).The text was updated successfully, but these errors were encountered: