diff options
author | Danny Hermes <daniel.j.hermes@gmail.com> | 2017-11-12 11:17:12 -0800 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-11-12 11:17:12 -0800 |
commit | a4e47e0205b78eb96248e71918c2558e25a9d3c7 (patch) | |
tree | 85f60a4994556e0eaf8e2fd0d4d72e2cf66ea5b7 | |
parent | d87c4197c358a898372c178f6c24f00d54eff745 (diff) | |
download | numpy-a4e47e0205b78eb96248e71918c2558e25a9d3c7.tar.gz |
ENH: Add `order=` keyword to `np.eye()` (#9996)
Fixes #9995
-rw-r--r-- | numpy/lib/tests/test_twodim_base.py | 9 | ||||
-rw-r--r-- | numpy/lib/twodim_base.py | 9 | ||||
-rw-r--r-- | numpy/matlib.py | 9 | ||||
-rw-r--r-- | numpy/tests/test_matlib.py | 17 |
4 files changed, 36 insertions, 8 deletions
diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py index 6bf668dee..8183f7ca6 100644 --- a/numpy/lib/tests/test_twodim_base.py +++ b/numpy/lib/tests/test_twodim_base.py @@ -95,6 +95,15 @@ class TestEye(object): def test_bool(self): assert_equal(eye(2, 2, dtype=bool), [[True, False], [False, True]]) + def test_order(self): + mat_c = eye(4, 3, k=-1) + mat_f = eye(4, 3, k=-1, order='F') + assert_equal(mat_c, mat_f) + assert mat_c.flags.c_contiguous + assert not mat_c.flags.f_contiguous + assert not mat_f.flags.c_contiguous + assert mat_f.flags.f_contiguous + class TestDiag(object): def test_vector(self): diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py index a6259219a..402c18850 100644 --- a/numpy/lib/twodim_base.py +++ b/numpy/lib/twodim_base.py @@ -137,7 +137,7 @@ def flipud(m): return m[::-1, ...] -def eye(N, M=None, k=0, dtype=float): +def eye(N, M=None, k=0, dtype=float, order='C'): """ Return a 2-D array with ones on the diagonal and zeros elsewhere. @@ -153,6 +153,11 @@ def eye(N, M=None, k=0, dtype=float): to a lower diagonal. dtype : data-type, optional Data-type of the returned array. + order : {'C', 'F'}, optional + Whether the output should be stored in row-major (C-style) or + column-major (Fortran-style) order in memory. + + .. versionadded:: 1.14.0 Returns ------- @@ -178,7 +183,7 @@ def eye(N, M=None, k=0, dtype=float): """ if M is None: M = N - m = zeros((N, M), dtype=dtype) + m = zeros((N, M), dtype=dtype, order=order) if k >= M: return m if k >= 0: diff --git a/numpy/matlib.py b/numpy/matlib.py index 656ca3458..004e5f0c8 100644 --- a/numpy/matlib.py +++ b/numpy/matlib.py @@ -173,7 +173,7 @@ def identity(n,dtype=None): b.flat = a return b -def eye(n,M=None, k=0, dtype=float): +def eye(n,M=None, k=0, dtype=float, order='C'): """ Return a matrix with ones on the diagonal and zeros elsewhere. @@ -189,6 +189,11 @@ def eye(n,M=None, k=0, dtype=float): and a negative value to a lower diagonal. dtype : dtype, optional Data-type of the returned matrix. + order : {'C', 'F'}, optional + Whether the output should be stored in row-major (C-style) or + column-major (Fortran-style) order in memory. + + .. versionadded:: 1.14.0 Returns ------- @@ -210,7 +215,7 @@ def eye(n,M=None, k=0, dtype=float): [ 0., 0., 0.]]) """ - return asmatrix(np.eye(n, M, k, dtype)) + return asmatrix(np.eye(n, M=M, k=k, dtype=dtype, order=order)) def rand(*args): """ diff --git a/numpy/tests/test_matlib.py b/numpy/tests/test_matlib.py index 11227b19a..d16975934 100644 --- a/numpy/tests/test_matlib.py +++ b/numpy/tests/test_matlib.py @@ -28,10 +28,19 @@ def test_identity(): assert_array_equal(x, np.matrix([[1, 0], [0, 1]])) def test_eye(): - x = numpy.matlib.eye(3, k=1, dtype=int) - assert_array_equal(x, np.matrix([[ 0, 1, 0], - [ 0, 0, 1], - [ 0, 0, 0]])) + xc = numpy.matlib.eye(3, k=1, dtype=int) + assert_array_equal(xc, np.matrix([[ 0, 1, 0], + [ 0, 0, 1], + [ 0, 0, 0]])) + assert xc.flags.c_contiguous + assert not xc.flags.f_contiguous + + xf = numpy.matlib.eye(3, 4, dtype=int, order='F') + assert_array_equal(xf, np.matrix([[ 1, 0, 0, 0], + [ 0, 1, 0, 0], + [ 0, 0, 1, 0]])) + assert not xf.flags.c_contiguous + assert xf.flags.f_contiguous def test_rand(): x = numpy.matlib.rand(3) |