pypy/module/test_lib_pypy/numpypy/core$ hg diff test_numeric.py
diff -r 09532c746387 pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py Tue Jan 24 22:03:33 2012 +0200
@@ -0,0 +1,42 @@
+from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+
+class AppTestNumeric(BaseNumpyAppTest):
+ def test_zeros_like(self):
+ import numpypy as np
+ for a in [
+ np.array(1), np.array([1, 2, 3]), np.array([[2.0, 4.0],[3,6]]), np.ones((3, 4, 5)),
+ np.zeros((3, 4, 5, 6)), np.array([1, 2], 'int16'), np.array([1, 2], 'float32')
+ ]:
+ b = np.zeros_like(a)
+ assert b.shape == a.shape and a.dtype == b.dtype
+
+ for a in [1, 1.0, np.int16(1), np.float32(1)]:
+ b = np.zeros_like(a)
+ assert b.shape == () and b.size == 1
+
+ def test_ones_like(self):
+ import numpypy as np
+ for a in [
+ np.array(1), np.array([1, 2, 3]), np.array([[2.0, 4.0],[3,6]]), np.ones((3, 4, 5)),
+ np.zeros((3, 4, 5, 6)), np.array([1, 2], 'int16'), np.array([1, 2], 'float32')
+ ]:
+ b = np.ones_like(a)
+ assert b.shape == a.shape and a.dtype == b.dtype
+
+ for a in [1, 1.0, np.int16(1), np.float32(1)]:
+ b = np.ones_like(a)
+ assert b.shape == () and b.size == 1
+
+ def test_empty_like(self):
+ import numpypy as np
+ for a in [
+ np.array(1), np.array([1, 2, 3]), np.array([[2.0, 4.0],[3,6]]), np.ones((3, 4, 5)),
+ np.zeros((3, 4, 5, 6)), np.array([1, 2], 'int16'), np.array([1, 2], 'float32')
+ ]:
+ b = np.empty_like(a)
+ assert b.shape == a.shape and a.dtype == b.dtype
+
+ for a in [1, 1.0, np.int16(1), np.float32(1),]:
+ b = np.empty_like(a)
+ assert b.shape == () and b.size == 1
+
lib_pypy/numpypy/core$ hg diff numeric.py
diff -r 09532c746387 lib_pypy/numpypy/core/numeric.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib_pypy/numpypy/core/numeric.py Tue Jan 24 22:05:47 2012 +0200
@@ -0,0 +1,207 @@
+
+######################################################################
+# This is a copy of numpy/core/numeric.py modified for numpypy
+######################################################################
+# Each name in __all__ was a function in 'numeric' that is now
+# a method in 'numpy'.
+# When the corresponding method is added to numpypy BaseArray
+# each function should be added as a module function
+# at the applevel
+# This can be as simple as doing the following
+#
+# def func(a, ...):
+# if not hasattr(a, 'func')
+# a = numpypy.array(a)
+# return a.func(...)
+#
+######################################################################
+
+import numpypy as np
+
+# Module containing non-deprecated functions borrowed from Numeric.
+__docformat__ = "restructuredtext en"
+
+# functions that are now methods
+__all__ = ['zeros_like', 'ones_like', 'empty_like',
+ ]
+
+def empty_like(a, dtype=None, order='K', subok=True):
+ '''
+ Return a new array with the same shape and type as a given array.
+
+ Parameters
+ ----------
+ a : array_like
+ The shape and data-type of `a` define these same attributes of the
+ returned array.
+ dtype : data-type, optional
+ Overrides the data type of the result.
+ order : {'C', 'F', 'A', or 'K'}, optional
+ Added for compatibility with cpython numpy, currently ignored.
+ In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
+ 'F' means F-order, 'A' means 'F' if ``a`` is Fortran contiguous,
+ 'C' otherwise. 'K' means match the layout of ``a`` as closely
+ as possible.
+ subok : bool, optional.
+ If True, then the newly created array will use the sub-class
+ type of 'a', otherwise it will be a base-class array. Defaults
+ to True.
+
+ Returns
+ -------
+ out : ndarray
+ Array of uninitialized (arbitrary) data with the same
+ shape and type as `a`.
+
+ See Also
+ --------
+ ones_like : Return an array of ones with shape and type of input.
+ zeros_like : Return an array of zeros with shape and type of input.
+ empty : Return a new uninitialized array.
+ ones : Return a new array setting values to one.
+ zeros : Return a new array setting values to zero.
+
+ Notes
+ -----
+ This function does *not* initialize the returned array; to do that use
+ `zeros_like` or `ones_like` instead. It may be marginally faster than
+ the functions that do set the array values.
+
+ Examples
+ --------
+ >>> a = ([1,2,3], [4,5,6]) # a is array-like
+ >>> np.empty_like(a)
+ array([[-1073741821, -1073741821, 3], #random
+ [ 0, 0, -1073741821]])
+ >>> a = np.array([[1., 2., 3.],[4.,5.,6.]])
+ >>> np.empty_like(a)
+ array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000],#random
+ [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]])
+
+ '''
+ if hasattr(a, 'shape') and hasattr(a, 'dtype'):
+ # may be ndarray, matrix, sparse matrix or their subclass
+ r = np.empty(a.shape, a.dtype if dtype is None else dtype)
+ Class = a.__class__
+ elif isinstance(a, (list, tuple)):
+ # we can't use len(), because a may be list of lists or like that
+ tmp = np.array(a)
+ Class = tmp.__class__
+ r = np.empty(tmp.shape, tmp.dtype if dtype is None else dtype)
+ else:
+ r = np.empty((), type(a))
+ Class = a.__class__ if hasattr(a,'__class__') else object
+ if subok and issubclass(Class, np.ndarray) and Class != r.__class__:
+ assert 0, 'empty_like with ndarray subclass output is unimplemented yet'
+ #r.__class__ = a.__class__ and other tried tricks doesn't work yet
+ return r
+
+def zeros_like(a, dtype=None, order='K', subok=True):
+ '''
+ zeros_like(a, dtype=None, order='K', subok=True)
+ Return an array of zeros with the same shape and type as a given array.
+
+ With default parameters, is equivalent to ``a.copy().fill(0)``.
+
+ Parameters
+ ----------
+ a : array_like
+ The shape and data-type of `a` define these same attributes of
+ the returned array.
+ dtype : data-type, optional
+ Overrides the data type of the result.
+ order : {'C', 'F', 'A', or 'K'}, optional
+ Added for compatibility with cpython numpy, currently ignored.
+ In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
+ 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
+ 'C' otherwise. 'K' means match the layout of `a` as closely
+ as possible.
+
+ Returns
+ -------
+ out : ndarray
+ Array of zeros with the same shape and type as `a`.
+
+ See Also
+ --------
+ ones_like : Return an array of ones with shape and type of input.
+ empty_like : Return an empty array with shape and type of input.
+ zeros : Return a new array setting values to zero.
+ ones : Return a new array setting values to one.
+ empty : Return a new uninitialized array.
+
+ Examples
+ --------
+ >>> x = np.arange(6)
+ >>> x = x.reshape((2, 3))
+ >>> x
+ array([[0, 1, 2],
+ [3, 4, 5]])
+ >>> np.zeros_like(x)
+ array([[0, 0, 0],
+ [0, 0, 0]])
+
+ >>> y = np.arange(3, dtype=np.float)
+ >>> y
+ array([ 0., 1., 2.])
+ >>> np.zeros_like(y)
+ array([ 0., 0., 0.])
+ '''
+ res = empty_like(a, dtype=dtype, order=order, subok=subok)
+ res.fill(0)
+ return res
+
+def ones_like(a, dtype=None, order='K', subok=True):
+ '''
+ ones_like(a, dtype=None, order='K', subok=True)
+ Return an array of ones with the same shape and type as a given array.
+
+ With default parameters, is equivalent to ``a.copy().fill(0)``.
+
+ Parameters
+ ----------
+ a : array_like
+ The shape and data-type of `a` define these same attributes of
+ the returned array.
+ dtype : data-type, optional
+ Overrides the data type of the result.
+ order : {'C', 'F', 'A', or 'K'}, optional
+ Added for compatibility with cpython numpy, currently ignored.
+ In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
+ 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
+ 'C' otherwise. 'K' means match the layout of `a` as closely
+ as possible.
+
+ Returns
+ -------
+ out : ndarray
+ Array of ones with the same shape and type as `a`.
+
+ See Also
+ --------
+ ones_like : Return an array of ones with shape and type of input.
+ empty_like : Return an empty array with shape and type of input.
+ zeros : Return a new array setting values to zero.
+ ones : Return a new array setting values to one.
+ empty : Return a new uninitialized array.
+
+ Examples
+ --------
+ >>> x = np.arange(6)
+ >>> x = x.reshape((2, 3))
+ >>> x
+ array([[0, 1, 2],
+ [3, 4, 5]])
+ >>> np.ones_like(x)
+ array([[1, 1, 1],
+ [1, 1, 1]])
+
+ >>> y = np.arange(3, dtype=np.float)
+ >>> y
+ array([ 0., 1., 2.])
+ >>> np.ones_like(y)
+ array([ 1., 1., 1.])
+ '''
+ res = empty_like(a, dtype=dtype, order=order, subok=subok)
+ res.fill(1)
+ return res
/lib_pypy/numpypy/core$ hg diff __init__.py
diff -r 09532c746387 lib_pypy/numpypy/core/__init__.py
--- a/lib_pypy/numpypy/core/__init__.py Mon Jan 23 02:30:27 2012 +0200
+++ b/lib_pypy/numpypy/core/__init__.py Tue Jan 24 22:06:19 2012 +0200
@@ -1,1 +1,2 @@
from .fromnumeric import *
+from .numeric import *