Commits

Brian Kearns committed 88878ea

move _numpypy.identity to numpypy.core.numeric

  • Participants
  • Parent commits 2568145

Comments (0)

Files changed (5)

File lib_pypy/numpypy/core/numeric.py

     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)

File pypy/module/micronumpy/__init__.py

 
     appleveldefs = {
         'average': 'app_numpy.average',
-        'identity': 'app_numpy.identity',
         'eye': 'app_numpy.eye',
         'arange': 'app_numpy.arange',
     }

File pypy/module/micronumpy/app_numpy.py

         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

File 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_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

File pypy/module/test_lib_pypy/numpypy/core/test_numeric.py

         assert base_repr(-12, 10, 4) == '-000012'
         assert base_repr(-12, 4) == '-30'
 
+
 class AppTestRepr(BaseNumpyAppTest):
     def test_repr(self):
         from numpypy import array
     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)
     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)
     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
                           [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()