Why ndarray provides four ways to flatten?

Given an n-dim array x, I can do 1. x.flat 2. x.flatten() 3. x.ravel() 4. x.reshape(-1) Each of these expressions returns a flat version of x with some variations. Why does NumPy implement four different ways to do essentially the same thing?

Hi Alexander, In my opinion - because they don't do the same thing, especially when you think in terms in lower-level. ndarray.flat returns an iterator; ndarray.flatten() returns a copy; ndarray.ravel() only makes copies when necessary; ndarray.reshape() is more general purpose, even though you can use it to flatten arrays. They are very distinct in behavior - for example, copies and views may store in the memory very differently and you would have to pay attention to the stride size if you are passing them down onto C/Fortran code. (Correct me if I am wrong please) -Shawn On Mon, Oct 27, 2014 at 8:06 PM, Alexander Belopolsky <ndarray@mac.com> wrote:
Given an n-dim array x, I can do
1. x.flat 2. x.flatten() 3. x.ravel() 4. x.reshape(-1)
Each of these expressions returns a flat version of x with some variations. Why does NumPy implement four different ways to do essentially the same thing?
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Yuxiang "Shawn" Wang Gerling Research Lab University of Virginia yw5aj@virginia.edu +1 (434) 284-0836 https://sites.google.com/a/virginia.edu/yw5aj/
participants (2)
-
Alexander Belopolsky
-
Yuxiang Wang