[Numpy-discussion] np.asfortranarray: unnecessary copying?
Kurt Smith
kwmsmith@gmail....
Fri Jul 30 12:50:09 CDT 2010
What are the rules for when 'np.asarray' and 'np.asfortranarray' make a copy?
This makes sense to me:
In [3]: carr = np.arange(3)
In [6]: carr2 = np.asarray(carr)
In [8]: carr2[0] = 1
In [9]: carr
Out[9]: array([1, 1, 2])
No copy is made.
But doing the same with a fortran array makes a copy:
In [10]: farr = np.arange(3).copy('F')
In [12]: farr2 = np.asfortranarray(farr)
In [13]: farr2[0] = 1
In [14]: farr
Out[14]: array([0, 1, 2])
Could it be a 1D thing, since it's both C contiguous & F contiguous?
Here's a 2D example:
In [15]: f2D = np.arange(10).reshape((2,5), order='F')
In [17]: f2D2 = np.asfortranarray(f2D)
In [19]: f2D2[0,0] = 10
In [20]: f2D
Out[20]:
array([[10, 2, 4, 6, 8],
[ 1, 3, 5, 7, 9]])
So it looks like np.asfortranarray makes an unnecessary copy if the
array is simultaneously 1D, C contiguous and F contiguous.
Coercing the array with np.atleast_2d() makes asfortranarry behave.
Looking further, np.isfortran always returns false if the array is 1D,
even if it's Fortran contiguous (and np.isfortran is documented as
such).
What is the rationale here? Is it a 'column' vs. 'row' thing?
Kurt
More information about the NumPy-Discussion
mailing list