Commits

Alex Gaynor  committed 18b9462 Merge

merged upstream

  • Participants
  • Parent commits e921c3c, affc77d

Comments (0)

Files changed (17)

File lib_pypy/numpypy/__init__.py

 from _numpypy import *
 from .core import *
+from .lib import *
 
 import _numpypy
 __all__ = _numpypy.__all__

File lib_pypy/numpypy/core/fromnumeric.py

     array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])
 
     """
-    raise NotImplementedError('Waiting on interp level method')
+    if not hasattr(a, 'clip'):
+        a = numpypy.array(a)
+    return a.clip(a_min, a_max, out=out)
 
 
 def sum(a, axis=None, dtype=None, out=None):

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 numpy import eye
+    return eye(n, dtype=dtype)

File lib_pypy/numpypy/lib/__init__.py

+from .function_base import *
+from .twodim_base import *

File lib_pypy/numpypy/lib/function_base.py

+from _numpypy import array
+
+def average(a):
+    # This implements a weighted average, for now we don't implement the
+    # weighting, just the average part!
+    if not hasattr(a, "mean"):
+        a = array(a)
+    return a.mean()

File 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

File pypy/module/micronumpy/__init__.py

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

File pypy/module/micronumpy/app_numpy.py

 
 import _numpypy
 
-def average(a):
-    # This implements a weighted average, for now we don't implement the
-    # weighting, just the average part!
-    if not hasattr(a, "mean"):
-        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
-    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).

File pypy/module/micronumpy/interp_numarray.py

 
     @unwrap_spec(mode=str)
     def descr_choose(self, space, w_choices, mode='raise', w_out=None):
-        if w_out is not None and not isinstance(w_out, W_NDimArray):
+        if not space.is_none(w_out) and not isinstance(w_out, W_NDimArray):
             raise OperationError(space.w_TypeError, space.wrap(
                 "return arrays must be of ArrayType"))
         return interp_arrayops.choose(space, self, w_choices, w_out, mode)
 
     def descr_clip(self, space, w_min, w_max, w_out=None):
-        if w_out is not None and not isinstance(w_out, W_NDimArray):
+        if not space.is_none(w_out) and not isinstance(w_out, W_NDimArray):
             raise OperationError(space.w_TypeError, space.wrap(
                 "return arrays must be of ArrayType"))
         min = convert_to_array(space, w_min)

File pypy/module/micronumpy/test/test_arrayops.py

     def test_choose_out(self):
         from _numpypy import array
         a, b, c = array([1, 2, 3]), [4, 5, 6], 13
+        r = array([2, 1, 0]).choose([a, b, c], out=None)
+        assert (r == [13, 5, 3]).all()
+        assert (a == [1, 2, 3]).all()
         r = array([2, 1, 0]).choose([a, b, c], out=a)
         assert (r == [13, 5, 3]).all()
         assert (a == [13, 5, 3]).all()
-        
+
     def test_choose_modes(self):
         from _numpypy import array
         a, b, c = array([1, 2, 3]), [4, 5, 6], 13

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

File pypy/module/micronumpy/test/test_module.py

-from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
-
-
-class AppTestNumPyModule(BaseNumpyAppTest):
-    def test_average(self):
-        from _numpypy import array, average
-        assert average(range(10)) == 4.5
-        assert average(array(range(10))) == 4.5

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
-        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))
         from _numpypy import array
         a = array([1, 2, 17, -3, 12])
         assert (a.clip(-2, 13) == [1, 2, 13, -2, 12]).all()
-        assert (a.clip(-1, 1) == [1, 1, 1, -1, 1]).all()
+        assert (a.clip(-1, 1, out=None) == [1, 1, 1, -1, 1]).all()
+        assert (a == [1, 2, 17, -3, 12]).all()
         assert (a.clip(-1, [1, 2, 3, 4, 5]) == [1, 2, 3, -1, 5]).all()
         assert (a.clip(-2, 13, out=a) == [1, 2, 13, -2, 12]).all()
         assert (a == [1, 2, 13, -2, 12]).all()

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

         # a = array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
         # assert shape(a) == (2,)
 
+    def test_clip(self):
+        import numpypy as np
+        a = np.arange(10)
+        b = np.clip(a, 1, 8)
+        assert (b == [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]).all()
+        assert (a == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).all()
+        b = np.clip(a, 3, 6, out=a)
+        assert (b == [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]).all()
+        assert (a == [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]).all()
+        a = np.arange(10)
+        b = np.clip(a, [3,4,1,1,1,4,4,4,4,4], 8)
+        assert (b == [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]).all()
+        assert (a == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).all()
+
     def test_sum(self):
         # tests taken from numpy/core/fromnumeric.py docstring
         from numpypy import array, sum, ones, zeros

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()

File pypy/module/test_lib_pypy/numpypy/lib/test_function_base.py

+from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+
+class AppTestFunctionBase(BaseNumpyAppTest):
+    def test_average(self):
+        from numpypy import array, average
+        assert average(range(10)) == 4.5
+        assert average(array(range(10))) == 4.5

File 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()