Commits

Brian Kearns committed 79a8a52

move _numpypy.eye to numpypy.lib.twodim_base

Comments (0)

Files changed (8)

lib_pypy/numpypy/core/numeric.py

            [ 0.,  0.,  1.]])
 
     """
-    from _numpypy import eye
+    from numpy import eye
     return eye(n, dtype=dtype)

lib_pypy/numpypy/lib/__init__.py

 from .function_base import *
+from .twodim_base import *

lib_pypy/numpypy/lib/twodim_base.py

+from _numpypy import zeros
+
+def eye(N, M=None, k=0, dtype=float):
+    """
+    Return a 2-D array with ones on the diagonal and zeros elsewhere.
+
+    Parameters
+    ----------
+    N : int
+      Number of rows in the output.
+    M : int, optional
+      Number of columns in the output. If None, defaults to `N`.
+    k : int, optional
+      Index of the diagonal: 0 (the default) refers to the main diagonal,
+      a positive value refers to an upper diagonal, and a negative value
+      to a lower diagonal.
+    dtype : data-type, optional
+      Data-type of the returned array.
+
+    Returns
+    -------
+    I : ndarray of shape (N,M)
+      An array where all elements are equal to zero, except for the `k`-th
+      diagonal, whose values are equal to one.
+
+    See Also
+    --------
+    identity : (almost) equivalent function
+    diag : diagonal 2-D array from a 1-D array specified by the user.
+
+    Examples
+    --------
+    >>> np.eye(2, dtype=int)
+    array([[1, 0],
+           [0, 1]])
+    >>> np.eye(3, k=1)
+    array([[ 0.,  1.,  0.],
+           [ 0.,  0.,  1.],
+           [ 0.,  0.,  0.]])
+
+    """
+    if M is None:
+        M = N
+    m = zeros((N, M), dtype=dtype)
+    if k >= M:
+        return m
+    if k >= 0:
+        i = k
+    else:
+        i = (-k) * M
+    m[:M-k].flat[i::M+1] = 1
+    return m

pypy/module/micronumpy/__init__.py

         interpleveldefs[exposed] = "interp_ufuncs.get(space).%s" % impl
 
     appleveldefs = {
-        'eye': 'app_numpy.eye',
         'arange': 'app_numpy.arange',
     }
     def setup_after_space_initialization(self):

pypy/module/micronumpy/app_numpy.py

 
 import _numpypy
 
-def eye(n, m=None, k=0, dtype=None):
-    if m is None:
-        m = n
-    a = _numpypy.zeros((n, m), dtype=dtype)
-    ni = 0
-    mi = 0
-
-    if k < 0:
-        p = n + k
-        ni = -k
-    else:
-        p = n - k
-        mi = k
-
-    while ni < n and mi < m:
-        a[ni][mi] = 1
-        ni += 1
-        mi += 1
-    return a
-
 def arange(start, stop=None, step=1, dtype=None):
     '''arange([start], stop[, step], dtype=None)
     Generate values in the half-interval [start, stop).

pypy/module/micronumpy/test/test_complex.py

 
     def test_conjugate(self):
         from _numpypy import conj, conjugate, complex128, complex64
-        import _numpypy as np
+        import numpypy as np
 
         c0 = complex128(complex(2.5, 0))
         c1 = complex64(complex(1, 2))

pypy/module/micronumpy/test/test_numarray.py

         assert (array([[1,2],[3,4]]).prod(0) == [3, 8]).all()
         assert (array([[1,2],[3,4]]).prod(1) == [2, 12]).all()
 
-    def test_eye(self):
-        from _numpypy import eye
-        from _numpypy import int32, dtype
-        a = eye(0)
-        assert len(a) == 0
-        assert a.dtype == dtype('float64')
-        assert a.shape == (0, 0)
-        b = eye(1, dtype=int32)
-        assert len(b) == 1
-        assert b[0][0] == 1
-        assert b.shape == (1, 1)
-        assert b.dtype == dtype('int32')
-        c = eye(2)
-        assert c.shape == (2, 2)
-        assert (c == [[1, 0], [0, 1]]).all()
-        d = eye(3, dtype='int32')
-        assert d.shape == (3, 3)
-        assert d.dtype == dtype('int32')
-        assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all()
-        e = eye(3, 4)
-        assert e.shape == (3, 4)
-        assert (e == [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]).all()
-        f = eye(2, 4, k=3)
-        assert f.shape == (2, 4)
-        assert (f == [[0, 0, 0, 1], [0, 0, 0, 0]]).all()
-        g = eye(3, 4, k=-1)
-        assert g.shape == (3, 4)
-        assert (g == [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]).all()
-
     def test_prod(self):
         from _numpypy import array
         a = array(range(1, 6))

pypy/module/test_lib_pypy/numpypy/lib/test_twodim_base.py

+from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+
+class AppTestFunctionBase(BaseNumpyAppTest):
+    def test_eye(self):
+        from _numpypy import int32, dtype
+        from numpypy import eye
+        a = eye(0)
+        assert len(a) == 0
+        assert a.dtype == dtype('float64')
+        assert a.shape == (0, 0)
+        b = eye(1, dtype=int32)
+        assert len(b) == 1
+        assert b[0][0] == 1
+        assert b.shape == (1, 1)
+        assert b.dtype == dtype('int32')
+        c = eye(2)
+        assert c.shape == (2, 2)
+        assert (c == [[1, 0], [0, 1]]).all()
+        d = eye(3, dtype='int32')
+        assert d.shape == (3, 3)
+        assert d.dtype == dtype('int32')
+        assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all()
+        e = eye(3, 4)
+        assert e.shape == (3, 4)
+        assert (e == [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]).all()
+        f = eye(2, 4, k=3)
+        assert f.shape == (2, 4)
+        assert (f == [[0, 0, 0, 1], [0, 0, 0, 0]]).all()
+        g = eye(3, 4, k=-1)
+        assert g.shape == (3, 4)
+        assert (g == [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]).all()