Guest

Untitled

By: a guest on Jan 24th, 2012  |  syntax: Python  |  size: 10.20 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. pypy/module/test_lib_pypy/numpypy/core$ hg diff test_numeric.py
  2. diff -r 09532c746387 pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
  3. --- /dev/null   Thu Jan 01 00:00:00 1970 +0000
  4. +++ b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py    Tue Jan 24 22:03:33 2012 +0200
  5. @@ -0,0 +1,42 @@
  6. +from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
  7. +
  8. +class AppTestNumeric(BaseNumpyAppTest):
  9. +    def test_zeros_like(self):
  10. +        import numpypy as np
  11. +        for a in [
  12. +                  np.array(1), np.array([1, 2, 3]), np.array([[2.0, 4.0],[3,6]]), np.ones((3, 4, 5)),
  13. +                  np.zeros((3, 4, 5, 6)), np.array([1, 2], 'int16'), np.array([1, 2], 'float32')
  14. +                  ]:
  15. +                    b = np.zeros_like(a)
  16. +                    assert b.shape == a.shape and a.dtype == b.dtype
  17. +                    
  18. +        for a in [1, 1.0, np.int16(1), np.float32(1)]:
  19. +            b = np.zeros_like(a)
  20. +            assert b.shape == ()  and b.size == 1
  21. +    
  22. +    def test_ones_like(self):
  23. +        import numpypy as np
  24. +        for a in [
  25. +                  np.array(1), np.array([1, 2, 3]), np.array([[2.0, 4.0],[3,6]]), np.ones((3, 4, 5)),
  26. +                  np.zeros((3, 4, 5, 6)), np.array([1, 2], 'int16'), np.array([1, 2], 'float32')
  27. +                  ]:
  28. +                    b = np.ones_like(a)
  29. +                    assert b.shape == a.shape and a.dtype == b.dtype
  30. +                    
  31. +        for a in [1, 1.0, np.int16(1), np.float32(1)]:
  32. +            b = np.ones_like(a)
  33. +            assert b.shape == ()  and b.size == 1
  34. +    
  35. +    def test_empty_like(self):
  36. +        import numpypy as np
  37. +        for a in [
  38. +                  np.array(1), np.array([1, 2, 3]), np.array([[2.0, 4.0],[3,6]]), np.ones((3, 4, 5)),
  39. +                  np.zeros((3, 4, 5, 6)), np.array([1, 2], 'int16'), np.array([1, 2], 'float32')
  40. +                  ]:
  41. +                    b = np.empty_like(a)
  42. +                    assert b.shape == a.shape and a.dtype == b.dtype
  43. +                    
  44. +        for a in [1, 1.0, np.int16(1), np.float32(1),]:
  45. +            b = np.empty_like(a)
  46. +            assert b.shape == ()  and b.size == 1
  47. +
  48.  
  49. lib_pypy/numpypy/core$ hg diff numeric.py
  50. diff -r 09532c746387 lib_pypy/numpypy/core/numeric.py
  51. --- /dev/null   Thu Jan 01 00:00:00 1970 +0000
  52. +++ b/lib_pypy/numpypy/core/numeric.py  Tue Jan 24 22:05:47 2012 +0200
  53. @@ -0,0 +1,207 @@
  54. +
  55. +######################################################################    
  56. +# This is a copy of numpy/core/numeric.py modified for numpypy
  57. +######################################################################
  58. +# Each name in __all__ was a function in  'numeric' that is now
  59. +# a method in 'numpy'.
  60. +# When the corresponding method is added to numpypy BaseArray
  61. +# each function should be added as a module function
  62. +# at the applevel
  63. +# This can be as simple as doing the following
  64. +#
  65. +# def func(a, ...):
  66. +#     if not hasattr(a, 'func')
  67. +#         a = numpypy.array(a)
  68. +#     return a.func(...)
  69. +#
  70. +######################################################################
  71. +
  72. +import numpypy as np
  73. +
  74. +# Module containing non-deprecated functions borrowed from Numeric.
  75. +__docformat__ = "restructuredtext en"
  76. +
  77. +# functions that are now methods
  78. +__all__ = ['zeros_like', 'ones_like', 'empty_like',
  79. +          ]
  80. +
  81. +def empty_like(a, dtype=None, order='K', subok=True):
  82. +    '''
  83. +    Return a new array with the same shape and type as a given array.
  84. +    
  85. +    Parameters
  86. +    ----------
  87. +    a : array_like
  88. +        The shape and data-type of `a` define these same attributes of the
  89. +        returned array.
  90. +    dtype : data-type, optional
  91. +        Overrides the data type of the result.
  92. +    order : {'C', 'F', 'A', or 'K'}, optional
  93. +        Added for compatibility with cpython numpy, currently ignored.
  94. +        In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
  95. +        'F' means F-order, 'A' means 'F' if ``a`` is Fortran contiguous,
  96. +        'C' otherwise. 'K' means match the layout of ``a`` as closely
  97. +        as possible.
  98. +    subok : bool, optional.
  99. +        If True, then the newly created array will use the sub-class
  100. +        type of 'a', otherwise it will be a base-class array. Defaults
  101. +        to True.
  102. +    
  103. +    Returns
  104. +    -------
  105. +    out : ndarray
  106. +        Array of uninitialized (arbitrary) data with the same
  107. +        shape and type as `a`.
  108. +    
  109. +    See Also
  110. +    --------
  111. +    ones_like : Return an array of ones with shape and type of input.
  112. +    zeros_like : Return an array of zeros with shape and type of input.
  113. +    empty : Return a new uninitialized array.
  114. +    ones : Return a new array setting values to one.
  115. +    zeros : Return a new array setting values to zero.
  116. +    
  117. +    Notes
  118. +    -----
  119. +    This function does *not* initialize the returned array; to do that use
  120. +    `zeros_like` or `ones_like` instead.  It may be marginally faster than
  121. +    the functions that do set the array values.
  122. +
  123. +    Examples
  124. +    --------
  125. +    >>> a = ([1,2,3], [4,5,6])                         # a is array-like
  126. +    >>> np.empty_like(a)
  127. +    array([[-1073741821, -1073741821,           3],    #random
  128. +           [          0,           0, -1073741821]])
  129. +    >>> a = np.array([[1., 2., 3.],[4.,5.,6.]])
  130. +    >>> np.empty_like(a)
  131. +    array([[ -2.00000715e+000,   1.48219694e-323,  -2.00000572e+000],#random
  132. +           [  4.38791518e-305,  -2.00000715e+000,   4.17269252e-309]])
  133. +    
  134. +    '''
  135. +    if hasattr(a, 'shape') and hasattr(a, 'dtype'):
  136. +        # may be ndarray, matrix, sparse matrix or their subclass
  137. +        r = np.empty(a.shape, a.dtype if dtype is None else dtype)
  138. +        Class = a.__class__
  139. +    elif isinstance(a, (list, tuple)):
  140. +        # we can't use len(), because a may be list of lists or like that
  141. +        tmp = np.array(a)
  142. +        Class = tmp.__class__
  143. +        r = np.empty(tmp.shape, tmp.dtype if dtype is None else dtype)
  144. +    else:
  145. +        r = np.empty((), type(a))
  146. +        Class = a.__class__ if hasattr(a,'__class__') else object
  147. +    if subok and issubclass(Class, np.ndarray) and Class != r.__class__:
  148. +        assert 0, 'empty_like with ndarray subclass output is unimplemented yet'
  149. +        #r.__class__ = a.__class__ and other tried tricks doesn't work yet
  150. +    return r
  151. +
  152. +def zeros_like(a, dtype=None, order='K', subok=True):
  153. +    '''
  154. +    zeros_like(a, dtype=None, order='K', subok=True)
  155. +    Return an array of zeros with the same shape and type as a given array.
  156. +    
  157. +    With default parameters, is equivalent to ``a.copy().fill(0)``.
  158. +    
  159. +    Parameters
  160. +    ----------
  161. +    a : array_like
  162. +        The shape and data-type of `a` define these same attributes of
  163. +        the returned array.
  164. +    dtype : data-type, optional
  165. +        Overrides the data type of the result.
  166. +    order : {'C', 'F', 'A', or 'K'}, optional
  167. +        Added for compatibility with cpython numpy, currently ignored.
  168. +        In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
  169. +        'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
  170. +        'C' otherwise. 'K' means match the layout of `a` as closely
  171. +        as possible.
  172. +    
  173. +    Returns
  174. +    -------
  175. +    out : ndarray
  176. +        Array of zeros with the same shape and type as `a`.
  177. +    
  178. +    See Also
  179. +    --------
  180. +    ones_like : Return an array of ones with shape and type of input.
  181. +    empty_like : Return an empty array with shape and type of input.
  182. +    zeros : Return a new array setting values to zero.
  183. +    ones : Return a new array setting values to one.
  184. +    empty : Return a new uninitialized array.
  185. +    
  186. +    Examples
  187. +    --------
  188. +    >>> x = np.arange(6)
  189. +    >>> x = x.reshape((2, 3))
  190. +    >>> x
  191. +    array([[0, 1, 2],
  192. +           [3, 4, 5]])
  193. +    >>> np.zeros_like(x)
  194. +    array([[0, 0, 0],
  195. +           [0, 0, 0]])
  196. +
  197. +    >>> y = np.arange(3, dtype=np.float)
  198. +    >>> y
  199. +    array([ 0.,  1.,  2.])
  200. +    >>> np.zeros_like(y)
  201. +    array([ 0.,  0.,  0.])
  202. +    '''
  203. +    res = empty_like(a, dtype=dtype, order=order, subok=subok)
  204. +    res.fill(0)
  205. +    return res
  206. +
  207. +def ones_like(a, dtype=None, order='K', subok=True):
  208. +    '''
  209. +    ones_like(a, dtype=None, order='K', subok=True)
  210. +    Return an array of ones with the same shape and type as a given array.
  211. +    
  212. +    With default parameters, is equivalent to ``a.copy().fill(0)``.
  213. +    
  214. +    Parameters
  215. +    ----------
  216. +    a : array_like
  217. +        The shape and data-type of `a` define these same attributes of
  218. +        the returned array.
  219. +    dtype : data-type, optional
  220. +        Overrides the data type of the result.
  221. +    order : {'C', 'F', 'A', or 'K'}, optional
  222. +        Added for compatibility with cpython numpy, currently ignored.
  223. +        In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
  224. +        'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
  225. +        'C' otherwise. 'K' means match the layout of `a` as closely
  226. +        as possible.
  227. +    
  228. +    Returns
  229. +    -------
  230. +    out : ndarray
  231. +        Array of ones with the same shape and type as `a`.
  232. +    
  233. +    See Also
  234. +    --------
  235. +    ones_like : Return an array of ones with shape and type of input.
  236. +    empty_like : Return an empty array with shape and type of input.
  237. +    zeros : Return a new array setting values to zero.
  238. +    ones : Return a new array setting values to one.
  239. +    empty : Return a new uninitialized array.
  240. +    
  241. +    Examples
  242. +    --------
  243. +    >>> x = np.arange(6)
  244. +    >>> x = x.reshape((2, 3))
  245. +    >>> x
  246. +    array([[0, 1, 2],
  247. +           [3, 4, 5]])
  248. +    >>> np.ones_like(x)
  249. +    array([[1, 1, 1],
  250. +           [1, 1, 1]])
  251. +
  252. +    >>> y = np.arange(3, dtype=np.float)
  253. +    >>> y
  254. +    array([ 0.,  1.,  2.])
  255. +    >>> np.ones_like(y)
  256. +    array([ 1.,  1.,  1.])
  257. +    '''
  258. +    res = empty_like(a, dtype=dtype, order=order, subok=subok)
  259. +    res.fill(1)
  260. +    return res
  261.  
  262. /lib_pypy/numpypy/core$ hg diff __init__.py
  263. diff -r 09532c746387 lib_pypy/numpypy/core/__init__.py
  264. --- a/lib_pypy/numpypy/core/__init__.py Mon Jan 23 02:30:27 2012 +0200
  265. +++ b/lib_pypy/numpypy/core/__init__.py Tue Jan 24 22:06:19 2012 +0200
  266. @@ -1,1 +1,2 @@
  267.  from .fromnumeric import *
  268. +from .numeric import *