rust-ndarray / ndarray

ndarray: an N-dimensional array with array views, multidimensional slicing, and efficient operations

Home Page:https://docs.rs/ndarray/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

len() implementation differs from NumPy len(arr)

WillAyd opened this issue · comments

Not sure if this is considered a bug but it tripped me up porting some numpy code over. Looks like the len() method implementation in ndarray as akin to the size property of numpy. By contrast len(array) in numpy gives you back the length of the very first dimension in an array - should these be equivalent or is that a non-goal?

Thanks for the work on this - awesome library

I had to test because I didn't believe it

>>> import numpy as np
>>> a = np.array([[1, 2, 3], [4, 5, 6]])
>>> len(a)
2

(I'm not the creator of this crate. This is only my opinion.) AFAIK, this is a non-goal. And a bad idea, I might add, because this is unclear what one wants when asking for the length of a matrix. There are other (better) tools for that, such as dim, shape and len_of.

commented

It hasn't been a goal to replicate numpy, even if a lot has been shamelessly adapted. The definition of .len() follows what I thought was the logical continuation of Rust conventions.

ndarray also does not lean strongly into the "outermost axis" that numpy.ndarray does, which has a default iterator that is like ndarray's .axis_iter(Axis(0)) I think. ndarray tries a little bit more to let it all axes be equally important.

Awesome thanks for the quick responses. That all makes sense to me - no strong opinion on this either way; this issue can just serve as a reference for anyone else that encounters this in the future