[pypy-commit] pypy default: move _numpypy.identity to numpypy.core.numeric

bdkearns noreply at buildbot.pypy.org
Sat Feb 23 08:14:09 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r61639:88878ea32353
Date: 2013-02-23 00:50 -0500
http://bitbucket.org/pypy/pypy/changeset/88878ea32353/

Log:	move _numpypy.identity to numpypy.core.numeric

diff --git a/lib_pypy/numpypy/core/numeric.py b/lib_pypy/numpypy/core/numeric.py
--- a/lib_pypy/numpypy/core/numeric.py
+++ b/lib_pypy/numpypy/core/numeric.py
@@ -501,3 +501,34 @@
     a = asarray(a)
     b = asarray(b)
     return a.ravel()[:,newaxis]*b.ravel()[newaxis,:]
+
+def identity(n, dtype=None):
+    """
+    Return the identity array.
+
+    The identity array is a square array with ones on
+    the main diagonal.
+
+    Parameters
+    ----------
+    n : int
+        Number of rows (and columns) in `n` x `n` output.
+    dtype : data-type, optional
+        Data-type of the output.  Defaults to ``float``.
+
+    Returns
+    -------
+    out : ndarray
+        `n` x `n` array with its main diagonal set to one,
+        and all other elements 0.
+
+    Examples
+    --------
+    >>> np.identity(3)
+    array([[ 1.,  0.,  0.],
+           [ 0.,  1.,  0.],
+           [ 0.,  0.,  1.]])
+
+    """
+    from _numpypy import eye
+    return eye(n, dtype=dtype)
diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -164,7 +164,6 @@
 
     appleveldefs = {
         'average': 'app_numpy.average',
-        'identity': 'app_numpy.identity',
         'eye': 'app_numpy.eye',
         'arange': 'app_numpy.arange',
     }
diff --git a/pypy/module/micronumpy/app_numpy.py b/pypy/module/micronumpy/app_numpy.py
--- a/pypy/module/micronumpy/app_numpy.py
+++ b/pypy/module/micronumpy/app_numpy.py
@@ -9,12 +9,6 @@
         a = _numpypy.array(a)
     return a.mean()
 
-def identity(n, dtype=None):
-    a = _numpypy.zeros((n, n), dtype=dtype)
-    for i in range(n):
-        a[i][i] = 1
-    return a
-
 def eye(n, m=None, k=0, dtype=None):
     if m is None:
         m = n
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1188,26 +1188,6 @@
         assert (array([[1,2],[3,4]]).prod(0) == [3, 8]).all()
         assert (array([[1,2],[3,4]]).prod(1) == [2, 12]).all()
 
-    def test_identity(self):
-        from _numpypy import identity, array
-        from _numpypy import int32, float64, dtype
-        a = identity(0)
-        assert len(a) == 0
-        assert a.dtype == dtype('float64')
-        assert a.shape == (0, 0)
-        b = identity(1, dtype=int32)
-        assert len(b) == 1
-        assert b[0][0] == 1
-        assert b.shape == (1, 1)
-        assert b.dtype == dtype('int32')
-        c = identity(2)
-        assert c.shape == (2, 2)
-        assert (c == [[1, 0], [0, 1]]).all()
-        d = identity(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()
-
     def test_eye(self):
         from _numpypy import eye
         from _numpypy import int32, dtype
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
@@ -20,6 +20,7 @@
         assert base_repr(-12, 10, 4) == '-000012'
         assert base_repr(-12, 4) == '-30'
 
+
 class AppTestRepr(BaseNumpyAppTest):
     def test_repr(self):
         from numpypy import array
@@ -146,10 +147,10 @@
     def test_equal(self):
         from _numpypy import array
         from numpypy import array_equal
-        
+
         a = [1, 2, 3]
         b = [1, 2, 3]
-        
+
         assert array_equal(a, b)
         assert array_equal(a, array(b))
         assert array_equal(array(a), b)
@@ -158,10 +159,10 @@
     def test_not_equal(self):
         from _numpypy import array
         from numpypy import array_equal
-        
+
         a = [1, 2, 3]
         b = [1, 2, 4]
-        
+
         assert not array_equal(a, b)
         assert not array_equal(a, array(b))
         assert not array_equal(array(a), b)
@@ -170,17 +171,17 @@
     def test_mismatched_shape(self):
         from _numpypy import array
         from numpypy import array_equal
-        
+
         a = [1, 2, 3]
         b = [[1, 2, 3], [1, 2, 3]]
-        
+
         assert not array_equal(a, b)
         assert not array_equal(a, array(b))
         assert not array_equal(array(a), b)
         assert not array_equal(array(a), array(b))
 
+
 class AppTestNumeric(BaseNumpyAppTest):
-
     def test_outer(self):
         from _numpypy import array
         from numpypy import outer
@@ -192,3 +193,22 @@
                           [12, 15, 18]])
         assert (res == expected).all()
 
+    def test_identity(self):
+        from _numpypy import array, int32, float64, dtype
+        from numpypy import identity
+        a = identity(0)
+        assert len(a) == 0
+        assert a.dtype == dtype('float64')
+        assert a.shape == (0, 0)
+        b = identity(1, dtype=int32)
+        assert len(b) == 1
+        assert b[0][0] == 1
+        assert b.shape == (1, 1)
+        assert b.dtype == dtype('int32')
+        c = identity(2)
+        assert c.shape == (2, 2)
+        assert (c == [[1, 0], [0, 1]]).all()
+        d = identity(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()


More information about the pypy-commit mailing list