Author: Brian Kearns <bdkea...@gmail.com> Branch: Changeset: r67382:413dbfc0f55b Date: 2013-10-15 02:42 -0400 http://bitbucket.org/pypy/pypy/changeset/413dbfc0f55b/
Log: import a new fromnumeric/shape_base from numpy diff --git a/lib_pypy/numpypy/core/__init__.py b/lib_pypy/numpypy/core/__init__.py --- a/lib_pypy/numpypy/core/__init__.py +++ b/lib_pypy/numpypy/core/__init__.py @@ -9,7 +9,8 @@ from . import shape_base from .shape_base import * -from .fromnumeric import amax as max, amin as min +from .fromnumeric import amax as max, amin as min, \ + round_ as round from .numeric import absolute as abs __all__ = [] diff --git a/lib_pypy/numpypy/core/fromnumeric.py b/lib_pypy/numpypy/core/fromnumeric.py --- a/lib_pypy/numpypy/core/fromnumeric.py +++ b/lib_pypy/numpypy/core/fromnumeric.py @@ -1,36 +1,53 @@ ###################################################################### # This is a copy of numpy/core/fromnumeric.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 -from _numpypy.multiarray import array as numpyarray - -# Module containing non-deprecated functions borrowed from Numeric. -__docformat__ = "restructuredtext en" +"""Module containing non-deprecated functions borrowed from Numeric. + +""" +from __future__ import division, absolute_import, print_function + +import types + +from . import multiarray as mu +from . import umath as um +from . import numerictypes as nt +from .numeric import asarray, array, asanyarray, concatenate +from . import _methods + + +# functions that are methods +__all__ = [ + 'alen', 'all', 'alltrue', 'amax', 'amin', 'any', 'argmax', + 'argmin', 'argpartition', 'argsort', 'around', 'choose', 'clip', + 'compress', 'cumprod', 'cumproduct', 'cumsum', 'diagonal', 'mean', + 'ndim', 'nonzero', 'partition', 'prod', 'product', 'ptp', 'put', + 'rank', 'ravel', 'repeat', 'reshape', 'resize', 'round_', + 'searchsorted', 'shape', 'size', 'sometrue', 'sort', 'squeeze', + 'std', 'sum', 'swapaxes', 'take', 'trace', 'transpose', 'var', + ] + + +try: + _gentype = types.GeneratorType +except AttributeError: + _gentype = type(None) + +# save away Python sum +_sum_ = sum # functions that are now methods -__all__ = ['take', 'reshape', 'choose', 'repeat', 'put', - 'swapaxes', 'transpose', 'sort', 'argsort', 'argmax', 'argmin', - 'searchsorted', 'alen', - 'resize', 'diagonal', 'trace', 'ravel', 'nonzero', 'shape', - 'compress', 'clip', 'sum', 'product', 'prod', 'sometrue', 'alltrue', - 'any', 'all', 'cumsum', 'cumproduct', 'cumprod', 'ptp', 'ndim', - 'rank', 'size', 'around', 'round_', 'mean', 'std', 'var', 'squeeze', - 'amax', 'amin', - ] +def _wrapit(obj, method, *args, **kwds): + try: + wrap = obj.__array_wrap__ + except AttributeError: + wrap = None + result = getattr(asarray(obj), method)(*args, **kwds) + if wrap: + if not isinstance(result, mu.ndarray): + result = asarray(result) + result = wrap(result) + return result + def take(a, indices, axis=None, out=None, mode='raise'): """ @@ -46,6 +63,10 @@ The source array. indices : array_like The indices of the values to extract. + + .. versionadded:: 1.8.0 + + Also allow scalars for indices. axis : int, optional The axis over which to select values. By default, the flattened input array is used. @@ -85,8 +106,17 @@ >>> a[indices] array([4, 3, 6]) + If `indices` is not one dimensional, the output also has these dimensions. + + >>> np.take(a, [[0, 1], [2, 3]]) + array([[4, 3], + [5, 7]]) """ - raise NotImplementedError('Waiting on interp level method') + try: + take = a.take + except AttributeError: + return _wrapit(a, 'take', indices, axis, out, mode) + return take(indices, axis, out, mode) # not deprecated --- copy if necessary, view otherwise @@ -104,16 +134,23 @@ One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions. order : {'C', 'F', 'A'}, optional - Determines whether the array data should be viewed as in C - (row-major) order, FORTRAN (column-major) order, or the C/FORTRAN - order should be preserved. + Read the elements of `a` using this index order, and place the elements + into the reshaped array using this index order. 'C' means to + read / write the elements using C-like index order, with the last axis index + changing fastest, back to the first axis index changing slowest. 'F' + means to read / write the elements using Fortran-like index order, with + the first index changing fastest, and the last index changing slowest. + Note that the 'C' and 'F' options take no account of the memory layout + of the underlying array, and only refer to the order of indexing. 'A' + means to read / write the elements in Fortran-like index order if `a` is + Fortran *contiguous* in memory, C-like order otherwise. Returns ------- reshaped_array : ndarray This will be a new view object if possible; otherwise, it will - be a copy. - + be a copy. Note there is no guarantee of the *memory layout* (C- or + Fortran- contiguous) of the returned array. See Also -------- @@ -121,7 +158,6 @@ Notes ----- - It is not always possible to change the shape of an array without copying the data. If you want an error to be raise if the data is copied, you should assign the new shape to the shape attribute of the array:: @@ -129,12 +165,39 @@ >>> a = np.zeros((10, 2)) # A transpose make the array non-contiguous >>> b = a.T - # Taking a view makes it possible to modify the shape without modiying the + # Taking a view makes it possible to modify the shape without modifying the # initial object. >>> c = b.view() >>> c.shape = (20) AttributeError: incompatible shape for a non-contiguous array + The `order` keyword gives the index ordering both for *fetching* the values + from `a`, and then *placing* the values into the output array. For example, + let's say you have an array: + + >>> a = np.arange(6).reshape((3, 2)) + >>> a + array([[0, 1], + [2, 3], + [4, 5]]) + + You can think of reshaping as first raveling the array (using the given + index order), then inserting the elements from the raveled array into the + new array using the same kind of index ordering as was used for the + raveling. + + >>> np.reshape(a, (2, 3)) # C-like index ordering + array([[0, 1, 2], + [3, 4, 5]]) + >>> np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape + array([[0, 1, 2], + [3, 4, 5]]) + >>> np.reshape(a, (2, 3), order='F') # Fortran-like index ordering + array([[0, 4, 3], + [2, 1, 5]]) + >>> np.reshape(np.ravel(a, order='F'), (2, 3), order='F') + array([[0, 4, 3], + [2, 1, 5]]) Examples -------- @@ -148,12 +211,13 @@ array([[1, 2], [3, 4], [5, 6]]) - """ assert order == 'C' - if not hasattr(a, 'reshape'): - a = numpyarray(a) - return a.reshape(newshape) + try: + reshape = a.reshape + except AttributeError: + return _wrapit(a, 'reshape', newshape) + return reshape(newshape) def choose(a, choices, out=None, mode='raise'): @@ -275,7 +339,11 @@ [-1, -2, -3, -4, -5]]]) """ - return _numpypy.choose(a, choices, out, mode) + try: + choose = a.choose + except AttributeError: + return _wrapit(a, 'choose', choices, out=out, mode=mode) + return choose(choices, out=out, mode=mode) def repeat(a, repeats, axis=None): @@ -317,7 +385,11 @@ [3, 4]]) """ - return _numpypy.repeat(a, repeats, axis) + try: + repeat = a.repeat + except AttributeError: + return _wrapit(a, 'repeat', repeats, axis) + return repeat(repeats, axis) def put(a, ind, v, mode='raise'): @@ -368,7 +440,7 @@ array([ 0, 1, 2, 3, -5]) """ - raise NotImplementedError('Waiting on interp level method') + return a.put(ind, v, mode) def swapaxes(a, axis1, axis2): @@ -412,7 +484,10 @@ [3, 7]]]) """ - swapaxes = a.swapaxes + try: + swapaxes = a.swapaxes + except AttributeError: + return _wrapit(a, 'swapaxes', axis1, axis2) return swapaxes(axis1, axis2) @@ -456,9 +531,158 @@ """ if axes is not None: raise NotImplementedError('No "axes" arg yet.') - if not hasattr(a, 'T'): - a = numpyarray(a) - return a.T + try: + transpose = a.transpose + except AttributeError: + return _wrapit(a, 'transpose') + return transpose() + + +def partition(a, kth, axis=-1, kind='introselect', order=None): + """ + Return a partitioned copy of an array. + + Creates a copy of the array with its elements rearranged in such a way that + the value of the element in kth position is in the position it would be in + a sorted array. All elements smaller than the kth element are moved before + this element and all equal or greater are moved behind it. The ordering of + the elements in the two partitions is undefined. + + .. versionadded:: 1.8.0 + + Parameters + ---------- + a : array_like + Array to be sorted. + kth : int or sequence of ints + Element index to partition by. The kth value of the element will be in + its final sorted position and all smaller elements will be moved before + it and all equal or greater elements behind it. + The order all elements in the partitions is undefined. + If provided with a sequence of kth it will partition all elements + indexed by kth of them into their sorted position at once. + axis : int or None, optional + Axis along which to sort. If None, the array is flattened before + sorting. The default is -1, which sorts along the last axis. + kind : {'introselect'}, optional + Selection algorithm. Default is 'introselect'. + order : list, optional + When `a` is a structured array, this argument specifies which fields + to compare first, second, and so on. This list does not need to + include all of the fields. + + Returns + ------- + partitioned_array : ndarray + Array of the same type and shape as `a`. + + See Also + -------- + ndarray.partition : Method to sort an array in-place. + argpartition : Indirect partition. + sort : Full sorting + + Notes + ----- + The various selection algorithms are characterized by their average speed, + worst case performance, work space size, and whether they are stable. A + stable sort keeps items with the same key in the same relative order. The + three available algorithms have the following properties: + + ================= ======= ============= ============ ======= + kind speed worst case work space stable + ================= ======= ============= ============ ======= + 'introselect' 1 O(n) 0 no + ================= ======= ============= ============ ======= + + All the partition algorithms make temporary copies of the data when + partitioning along any but the last axis. Consequently, partitioning + along the last axis is faster and uses less space than partitioning + along any other axis. + + The sort order for complex numbers is lexicographic. If both the real + and imaginary parts are non-nan then the order is determined by the + real parts except when they are equal, in which case the order is + determined by the imaginary parts. + + Examples + -------- + >>> a = np.array([3, 4, 2, 1]) + >>> np.partition(a, 3) + array([2, 1, 3, 4]) + + >>> np.partition(a, (1, 3)) + array([1, 2, 3, 4]) + + """ + if axis is None: + a = asanyarray(a).flatten() + axis = 0 + else: + a = asanyarray(a).copy() + a.partition(kth, axis=axis, kind=kind, order=order) + return a + + +def argpartition(a, kth, axis=-1, kind='introselect', order=None): + """ + Perform an indirect partition along the given axis using the algorithm + specified by the `kind` keyword. It returns an array of indices of the + same shape as `a` that index data along the given axis in partitioned + order. + + .. versionadded:: 1.8.0 + + Parameters + ---------- + a : array_like + Array to sort. + kth : int or sequence of ints + Element index to partition by. The kth element will be in its final + sorted position and all smaller elements will be moved before it and + all larger elements behind it. + The order all elements in the partitions is undefined. + If provided with a sequence of kth it will partition all of them into + their sorted position at once. + axis : int or None, optional + Axis along which to sort. The default is -1 (the last axis). If None, + the flattened array is used. + kind : {'introselect'}, optional + Selection algorithm. Default is 'introselect' + order : list, optional + When `a` is an array with fields defined, this argument specifies + which fields to compare first, second, etc. Not all fields need be + specified. + + Returns + ------- + index_array : ndarray, int + Array of indices that partition `a` along the specified axis. + In other words, ``a[index_array]`` yields a sorted `a`. + + See Also + -------- + partition : Describes partition algorithms used. + ndarray.partition : Inplace partition. + argsort : Full indirect sort + + Notes + ----- + See `partition` for notes on the different selection algorithms. + + Examples + -------- + One dimensional array: + + >>> x = np.array([3, 4, 2, 1]) + >>> x[np.argpartition(x, 3)] + array([2, 1, 3, 4]) + >>> x[np.argpartition(x, (1, 3))] + array([1, 2, 3, 4]) + + """ + return a.argpartition(kth, axis, kind=kind, order=order) + def sort(a, axis=-1, kind='quicksort', order=None): """ @@ -489,6 +713,7 @@ argsort : Indirect sort. lexsort : Indirect stable sort on multiple keys. searchsorted : Find elements in a sorted array. + partition : Partial sort. Notes ----- @@ -559,7 +784,13 @@ dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')]) """ - raise NotImplementedError('Waiting on interp level method') + if axis is None: + a = asanyarray(a).flatten() + axis = 0 + else: + a = asanyarray(a).copy() + a.sort(axis, kind, order) + return a def argsort(a, axis=-1, kind='quicksort', order=None): @@ -595,6 +826,7 @@ sort : Describes sorting algorithms used. lexsort : Indirect stable sort with multiple keys. ndarray.sort : Inplace sort. + argpartition : Indirect partial sort. Notes ----- @@ -640,7 +872,11 @@ array([0, 1]) """ - raise NotImplementedError('Waiting on interp level method') + try: + argsort = a.argsort + except AttributeError: + return _wrapit(a, 'argsort', axis, kind, order) + return argsort(axis, kind, order) def argmax(a, axis=None): @@ -694,9 +930,11 @@ """ assert axis is None - if not hasattr(a, 'argmax'): - a = numpyarray(a) - return a.argmax() + try: + argmax = a.argmax + except AttributeError: + return _wrapit(a, 'argmax') + return argmax() def argmin(a, axis=None): @@ -710,29 +948,37 @@ """ assert axis is None - if not hasattr(a, 'argmin'): - a = numpyarray(a) - return a.argmin() - - -def searchsorted(a, v, side='left'): + try: + argmin = a.argmin + except AttributeError: + return _wrapit(a, 'argmin') + return argmin() + + +def searchsorted(a, v, side='left', sorter=None): """ Find indices where elements should be inserted to maintain order. - Find the indices into a sorted array `a` such that, if the corresponding - elements in `v` were inserted before the indices, the order of `a` would - be preserved. + Find the indices into a sorted array `a` such that, if the + corresponding elements in `v` were inserted before the indices, the + order of `a` would be preserved. Parameters ---------- a : 1-D array_like - Input array, sorted in ascending order. + Input array. If `sorter` is None, then it must be sorted in + ascending order, otherwise `sorter` must be an array of indices + that sort it. v : array_like Values to insert into `a`. side : {'left', 'right'}, optional - If 'left', the index of the first suitable location found is given. If - 'right', return the last such index. If there is no suitable + If 'left', the index of the first suitable location found is given. + If 'right', return the last such index. If there is no suitable index, return either 0 or N (where N is the length of `a`). + sorter : 1-D array_like, optional + .. versionadded:: 1.7.0 + Optional array of integer indices that sort array a into ascending + order. They are typically the result of argsort. Returns ------- @@ -761,7 +1007,11 @@ array([0, 5, 1, 2]) """ - raise NotImplementedError('Waiting on interp level method') + try: + searchsorted = a.searchsorted + except AttributeError: + return _wrapit(a, 'searchsorted', v, side, sorter) + return searchsorted(v, side, sorter) def resize(a, new_shape): @@ -802,10 +1052,30 @@ [0, 1, 2, 3]]) """ - raise NotImplementedError('Waiting on interp level method') - - -def squeeze(a): + if isinstance(new_shape, (int, nt.integer)): + new_shape = (new_shape,) + a = ravel(a) + Na = len(a) + if not Na: return mu.zeros(new_shape, a.dtype.char) + total_size = um.multiply.reduce(new_shape) + n_copies = int(total_size / Na) + extra = total_size % Na + + if total_size == 0: + return a[:0] + + if extra != 0: + n_copies = n_copies+1 + extra = Na-extra + + a = concatenate( (a,)*n_copies) + if extra > 0: + a = a[:-extra] + + return reshape(a, new_shape) + + +def squeeze(a, axis=None): """ Remove single-dimensional entries from the shape of an array. @@ -813,12 +1083,19 @@ ---------- a : array_like Input data. + axis : None or int or tuple of ints, optional + .. versionadded:: 1.7.0 + + Selects a subset of the single-dimensional entries in the + shape. If an axis is selected with shape entry greater than + one, an error is raised. Returns ------- squeezed : ndarray - The input array, but with with all dimensions of length 1 - removed. Whenever possible, a view on `a` is returned. + The input array, but with with all or a subset of the + dimensions of length 1 removed. This is always `a` itself + or a view into `a`. Examples -------- @@ -827,9 +1104,20 @@ (1, 3, 1) >>> np.squeeze(x).shape (3,) + >>> np.squeeze(x, axis=(2,)).shape + (1, 3) """ - raise NotImplementedError('Waiting on interp level method') + try: + squeeze = a.squeeze + except AttributeError: + return _wrapit(a, 'squeeze') + try: + # First try to use the new axis= parameter + return squeeze(axis=axis) + except TypeError: + # For backwards compatibility + return squeeze() def diagonal(a, offset=0, axis1=0, axis2=1): @@ -844,6 +1132,27 @@ removing `axis1` and `axis2` and appending an index to the right equal to the size of the resulting diagonals. + In versions of NumPy prior to 1.7, this function always returned a new, + independent array containing a copy of the values in the diagonal. + + In NumPy 1.7 and 1.8, it continues to return a copy of the diagonal, + but depending on this fact is deprecated. Writing to the resulting + array continues to work as it used to, but a FutureWarning is issued. + + In NumPy 1.9 it returns a read-only view on the original array. + Attempting to write to the resulting array will produce an error. + + In NumPy 1.10, it will return a read/write view, Writing to the returned + array will alter your original array. + + If you don't write to the array returned by this function, then you can + just ignore all of the above. + + If you depend on the current behavior, then we suggest copying the + returned array explicitly, i.e., use ``np.diagonal(a).copy()`` instead of + just ``np.diagonal(a)``. This will work with both past and future versions + of NumPy. + Parameters ---------- a : array_like @@ -913,7 +1222,7 @@ [5, 7]]) """ - raise NotImplementedError('Waiting on interp level method') + return asarray(a).diagonal(offset, axis1, axis2) def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None): @@ -972,7 +1281,7 @@ (2, 3) """ - raise NotImplementedError('Waiting on interp level method') + return asarray(a).trace(offset, axis1, axis2, dtype, out) def ravel(a, order='C'): """ @@ -984,21 +1293,25 @@ Parameters ---------- a : array_like - Input array. The elements in ``a`` are read in the order specified by + Input array. The elements in `a` are read in the order specified by `order`, and packed as a 1-D array. order : {'C','F', 'A', 'K'}, optional - The elements of ``a`` are read in this order. 'C' means to view - the elements in C (row-major) order. 'F' means to view the elements - in Fortran (column-major) order. 'A' means to view the elements - in 'F' order if a is Fortran contiguous, 'C' order otherwise. - 'K' means to view the elements in the order they occur in memory, - except for reversing the data when strides are negative. - By default, 'C' order is used. + The elements of `a` are read using this index order. 'C' means to + index the elements in C-like order, with the last axis index changing + fastest, back to the first axis index changing slowest. 'F' means to + index the elements in Fortran-like index order, with the first index + changing fastest, and the last index changing slowest. Note that the 'C' + and 'F' options take no account of the memory layout of the underlying + array, and only refer to the order of axis indexing. 'A' means to read + the elements in Fortran-like index order if `a` is Fortran *contiguous* + in memory, C-like order otherwise. 'K' means to read the elements in + the order they occur in memory, except for reversing the data when + strides are negative. By default, 'C' index order is used. Returns ------- 1d_array : ndarray - Output of the same dtype as `a`, and of shape ``(a.size(),)``. + Output of the same dtype as `a`, and of shape ``(a.size,)``. See Also -------- @@ -1008,11 +1321,11 @@ Notes ----- - In row-major order, the row index varies the slowest, and the column - index the quickest. This can be generalized to multiple dimensions, - where row-major order implies that the index along the first axis - varies slowest, and the index along the last quickest. The opposite holds - for Fortran-, or column-major, mode. + In C-like (row-major) order, in two dimensions, the row index varies the + slowest, and the column index the quickest. This can be generalized to + multiple dimensions, where row-major order implies that the index along the + first axis varies slowest, and the index along the last quickest. The + opposite holds for Fortran-like, or column-major, index ordering. Examples -------- @@ -1056,9 +1369,8 @@ array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) """ - if not hasattr(a, 'ravel'): - a = numpyarray(a) - return a.ravel(order=order) + return asarray(a).ravel(order) + def nonzero(a): """ @@ -1180,9 +1492,11 @@ (2,) """ - if not hasattr(a, 'shape'): - a = numpyarray(a) - return a.shape + try: + result = a.shape + except AttributeError: + result = asarray(a).shape + return result def compress(condition, a, axis=None, out=None): @@ -1217,7 +1531,8 @@ See Also -------- take, choose, diag, diagonal, select - ndarray.compress : Equivalent method. + ndarray.compress : Equivalent method in ndarray + np.extract: Equivalent method when working on 1-D arrays numpy.doc.ufuncs : Section "Output arguments" Examples @@ -1244,7 +1559,11 @@ array([2]) """ - raise NotImplementedError('Waiting on interp level method') + try: + compress = a.compress + except AttributeError: + return _wrapit(a, 'compress', condition, axis, out) + return compress(condition, axis, out) def clip(a, a_min, a_max, out=None): @@ -1297,12 +1616,14 @@ array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8]) """ - if not hasattr(a, 'clip'): - a = numpyarray(a) - return a.clip(a_min, a_max, out=out) - - -def sum(a, axis=None, dtype=None, out=None): + try: + clip = a.clip + except AttributeError: + return _wrapit(a, 'clip', a_min, a_max, out) + return clip(a_min, a_max, out) + + +def sum(a, axis=None, dtype=None, out=None, keepdims=False): """ Sum of array elements over a given axis. @@ -1310,9 +1631,16 @@ ---------- a : array_like Elements to sum. - axis : integer, optional - Axis over which the sum is taken. By default `axis` is None, - and all elements are summed. + axis : None or int or tuple of ints, optional + Axis or axes along which a sum is performed. + The default (`axis` = `None`) is perform a sum over all + the dimensions of the input array. `axis` may be negative, in + which case it counts from the last to the first axis. + + .. versionadded:: 1.7.0 + + If this is a tuple of ints, a sum is performed on multiple + axes, instead of a single axis or all the axes as before. dtype : dtype, optional The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of `a` is used. @@ -1325,6 +1653,10 @@ (the shape of `a` with `axis` removed, i.e., ``numpy.delete(a.shape, axis)``). Its type is preserved. See `doc.ufuncs` (Section "Output arguments") for more details. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- @@ -1368,13 +1700,25 @@ -128 """ - assert dtype is None - if not hasattr(a, "sum"): - a = numpyarray(a) - return a.sum(axis=axis, out=out) - - -def product (a, axis=None, dtype=None, out=None): + if isinstance(a, _gentype): + res = _sum_(a) + if out is not None: + out[...] = res + return out + return res + elif type(a) is not mu.ndarray: + try: + sum = a.sum + except AttributeError: + return _methods._sum(a, axis=axis, dtype=dtype, + out=out, keepdims=keepdims) + # NOTE: Dropping the keepdims parameters here... + return sum(axis=axis, dtype=dtype, out=out) + else: + return _methods._sum(a, axis=axis, dtype=dtype, + out=out, keepdims=keepdims) + +def product (a, axis=None, dtype=None, out=None, keepdims=False): """ Return the product of array elements over a given axis. @@ -1383,10 +1727,10 @@ prod : equivalent function; see for details. """ - raise NotImplementedError('Waiting on interp level method') - - -def sometrue(a, axis=None, out=None): + return um.multiply.reduce(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims) + + +def sometrue(a, axis=None, out=None, keepdims=False): """ Check whether some values are true. @@ -1397,14 +1741,14 @@ any : equivalent function """ - assert axis is None - assert out is None - if not hasattr(a, 'any'): - a = numpyarray(a) - return a.any() - - -def alltrue (a, axis=None, out=None): + arr = asanyarray(a) + + try: + return arr.any(axis=axis, out=out, keepdims=keepdims) + except TypeError: + return arr.any(axis=axis, out=out) + +def alltrue (a, axis=None, out=None, keepdims=False): """ Check if all elements of input array are true. @@ -1413,13 +1757,14 @@ numpy.all : Equivalent function; see for details. """ - assert axis is None - assert out is None - if not hasattr(a, 'all'): - a = numpyarray(a) - return a.all() - -def any(a,axis=None, out=None): + arr = asanyarray(a) + + try: + return arr.all(axis=axis, out=out, keepdims=keepdims) + except TypeError: + return arr.all(axis=axis, out=out) + +def any(a, axis=None, out=None, keepdims=False): """ Test whether any array element along a given axis evaluates to True. @@ -1429,17 +1774,26 @@ ---------- a : array_like Input array or object that can be converted to an array. - axis : int, optional - Axis along which a logical OR is performed. The default - (`axis` = `None`) is to perform a logical OR over a flattened - input array. `axis` may be negative, in which case it counts - from the last to the first axis. + axis : None or int or tuple of ints, optional + Axis or axes along which a logical OR reduction is performed. + The default (`axis` = `None`) is perform a logical OR over all + the dimensions of the input array. `axis` may be negative, in + which case it counts from the last to the first axis. + + .. versionadded:: 1.7.0 + + If this is a tuple of ints, a reduction is performed on multiple + axes, instead of a single axis or all the axes as before. out : ndarray, optional Alternate output array in which to place the result. It must have the same shape as the expected output and its type is preserved (e.g., if it is of type float, then it will remain so, returning 1.0 for True and 0.0 for False, regardless of the type of `a`). See `doc.ufuncs` (Section "Output arguments") for details. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- @@ -1483,14 +1837,14 @@ (191614240, 191614240) """ - assert axis is None - assert out is None - if not hasattr(a, 'any'): - a = numpyarray(a) - return a.any() - - -def all(a,axis=None, out=None): + arr = asanyarray(a) + + try: + return arr.any(axis=axis, out=out, keepdims=keepdims) + except TypeError: + return arr.any(axis=axis, out=out) + +def all(a, axis=None, out=None, keepdims=False): """ Test whether all array elements along a given axis evaluate to True. @@ -1498,17 +1852,26 @@ ---------- a : array_like Input array or object that can be converted to an array. - axis : int, optional - Axis along which a logical AND is performed. - The default (`axis` = `None`) is to perform a logical AND - over a flattened input array. `axis` may be negative, in which - case it counts from the last to the first axis. + axis : None or int or tuple of ints, optional + Axis or axes along which a logical AND reduction is performed. + The default (`axis` = `None`) is perform a logical OR over all + the dimensions of the input array. `axis` may be negative, in + which case it counts from the last to the first axis. + + .. versionadded:: 1.7.0 + + If this is a tuple of ints, a reduction is performed on multiple + axes, instead of a single axis or all the axes as before. out : ndarray, optional Alternate output array in which to place the result. It must have the same shape as the expected output and its type is preserved (e.g., if ``dtype(out)`` is float, the result will consist of 0.0's and 1.0's). See `doc.ufuncs` (Section "Output arguments") for more details. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- @@ -1547,12 +1910,12 @@ (28293632, 28293632, array([ True], dtype=bool)) """ - assert axis is None - assert out is None - if not hasattr(a, 'all'): - a = numpyarray(a) - return a.all() - + arr = asanyarray(a) + + try: + return arr.all(axis=axis, out=out, keepdims=keepdims) + except TypeError: + return arr.all(axis=axis, out=out) def cumsum (a, axis=None, dtype=None, out=None): """ @@ -1592,6 +1955,8 @@ trapz : Integration of array values using the composite trapezoidal rule. + diff : Calculate the n-th order discrete difference along given axis. + Notes ----- Arithmetic is modular when using integer types, and no error is @@ -1616,7 +1981,11 @@ [ 4, 9, 15]]) """ - raise NotImplementedError('Waiting on interp level method') + try: + cumsum = a.cumsum + except AttributeError: + return _wrapit(a, 'cumsum', axis, dtype, out) + return cumsum(axis, dtype, out) def cumproduct(a, axis=None, dtype=None, out=None): @@ -1629,7 +1998,11 @@ cumprod : equivalent function; see for details. """ - raise NotImplementedError('Waiting on interp level method') + try: + cumprod = a.cumprod + except AttributeError: + return _wrapit(a, 'cumprod', axis, dtype, out) + return cumprod(axis, dtype, out) def ptp(a, axis=None, out=None): @@ -1670,10 +2043,14 @@ array([1, 1]) """ - raise NotImplementedError('Waiting on interp level method') - - -def amax(a, axis=None, out=None): + try: + ptp = a.ptp + except AttributeError: + return _wrapit(a, 'ptp', axis, out) + return ptp(axis, out) + + +def amax(a, axis=None, out=None, keepdims=False): """ Return the maximum of an array or maximum along an axis. @@ -1682,11 +2059,15 @@ a : array_like Input data. axis : int, optional - Axis along which to operate. By default flattened input is used. + Axis along which to operate. By default, flattened input is used. out : ndarray, optional - Alternate output array in which to place the result. Must be of - the same shape and buffer length as the expected output. See - `doc.ufuncs` (Section "Output arguments") for more details. + Alternative output array in which to place the result. Must + be of the same shape and buffer length as the expected output. + See `doc.ufuncs` (Section "Output arguments") for more details. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- @@ -1697,27 +2078,40 @@ See Also -------- - nanmax : NaN values are ignored instead of being propagated. - fmax : same behavior as the C99 fmax function. - argmax : indices of the maximum values. + amin : + The minimum value of an array along a given axis, propagating any NaNs. + nanmax : + The maximum value of an array along a given axis, ignoring any NaNs. + maximum : + Element-wise maximum of two arrays, propagating any NaNs. + fmax : + Element-wise maximum of two arrays, ignoring any NaNs. + argmax : + Return the indices of the maximum values. + + nanmin, minimum, fmin Notes ----- NaN values are propagated, that is if at least one item is NaN, the - corresponding max value will be NaN as well. To ignore NaN values + corresponding max value will be NaN as well. To ignore NaN values (MATLAB behavior), please use nanmax. + Don't use `amax` for element-wise comparison of 2 arrays; when + ``a.shape[0]`` is 2, ``maximum(a[0], a[1])`` is faster than + ``amax(a, axis=0)``. + Examples -------- >>> a = np.arange(4).reshape((2,2)) >>> a array([[0, 1], [2, 3]]) - >>> np.amax(a) + >>> np.amax(a) # Maximum of the flattened array 3 - >>> np.amax(a, axis=0) + >>> np.amax(a, axis=0) # Maxima along the first axis array([2, 3]) - >>> np.amax(a, axis=1) + >>> np.amax(a, axis=1) # Maxima along the second axis array([1, 3]) >>> b = np.arange(5, dtype=np.float) @@ -1728,14 +2122,19 @@ 4.0 """ - if not hasattr(a, "max"): - a = numpyarray(a) - if a.size < 1: - return numpyarray([]) - return a.max(axis=axis, out=out) - - -def amin(a, axis=None, out=None): + if type(a) is not mu.ndarray: + try: + amax = a.max + except AttributeError: + return _methods._amax(a, axis=axis, + out=out, keepdims=keepdims) + # NOTE: Dropping the keepdims parameter + return amax(axis=axis, out=out) + else: + return _methods._amax(a, axis=axis, + out=out, keepdims=keepdims) + +def amin(a, axis=None, out=None, keepdims=False): """ Return the minimum of an array or minimum along an axis. @@ -1744,30 +2143,47 @@ a : array_like Input data. axis : int, optional - Axis along which to operate. By default a flattened input is used. + Axis along which to operate. By default, flattened input is used. out : ndarray, optional Alternative output array in which to place the result. Must be of the same shape and buffer length as the expected output. See `doc.ufuncs` (Section "Output arguments") for more details. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- - amin : ndarray - A new array or a scalar array with the result. + amin : ndarray or scalar + Minimum of `a`. If `axis` is None, the result is a scalar value. + If `axis` is given, the result is an array of dimension + ``a.ndim - 1``. See Also -------- - nanmin: nan values are ignored instead of being propagated - fmin: same behavior as the C99 fmin function - argmin: Return the indices of the minimum values. - - amax, nanmax, fmax + amax : + The maximum value of an array along a given axis, propagating any NaNs. + nanmin : + The minimum value of an array along a given axis, ignoring any NaNs. + minimum : + Element-wise minimum of two arrays, propagating any NaNs. + fmin : + Element-wise minimum of two arrays, ignoring any NaNs. + argmin : + Return the indices of the minimum values. + + nanmax, maximum, fmax Notes ----- - NaN values are propagated, that is if at least one item is nan, the - corresponding min value will be nan as well. To ignore NaN values (matlab - behavior), please use nanmin. + NaN values are propagated, that is if at least one item is NaN, the + corresponding min value will be NaN as well. To ignore NaN values + (MATLAB behavior), please use nanmin. + + Don't use `amin` for element-wise comparison of 2 arrays; when + ``a.shape[0]`` is 2, ``minimum(a[0], a[1])`` is faster than + ``amin(a, axis=0)``. Examples -------- @@ -1777,9 +2193,9 @@ [2, 3]]) >>> np.amin(a) # Minimum of the flattened array 0 - >>> np.amin(a, axis=0) # Minima along the first axis + >>> np.amin(a, axis=0) # Minima along the first axis array([0, 1]) - >>> np.amin(a, axis=1) # Minima along the second axis + >>> np.amin(a, axis=1) # Minima along the second axis array([0, 2]) >>> b = np.arange(5, dtype=np.float) @@ -1790,11 +2206,17 @@ 0.0 """ - if not hasattr(a, 'min'): - a = numpyarray(a) - if a.size < 1: - return numpyarray([]) - return a.min(axis=axis, out=out) + if type(a) is not mu.ndarray: + try: + amin = a.min + except AttributeError: + return _methods._amin(a, axis=axis, + out=out, keepdims=keepdims) + # NOTE: Dropping the keepdims parameter + return amin(axis=axis, out=out) + else: + return _methods._amin(a, axis=axis, + out=out, keepdims=keepdims) def alen(a): """ @@ -1807,7 +2229,7 @@ Returns ------- - l : int + alen : int Length of the first dimension of `a`. See Also @@ -1823,12 +2245,13 @@ 7 """ - if not hasattr(a, 'shape'): - a = numpyarray(a) - return a.shape[0] - - -def prod(a, axis=None, dtype=None, out=None): + try: + return len(a) + except TypeError: + return len(array(a, ndmin=1)) + + +def prod(a, axis=None, dtype=None, out=None, keepdims=False): """ Return the product of array elements over a given axis. @@ -1836,9 +2259,16 @@ ---------- a : array_like Input data. - axis : int, optional - Axis over which the product is taken. By default, the product - of all elements is calculated. + axis : None or int or tuple of ints, optional + Axis or axes along which a product is performed. + The default (`axis` = `None`) is perform a product over all + the dimensions of the input array. `axis` may be negative, in + which case it counts from the last to the first axis. + + .. versionadded:: 1.7.0 + + If this is a tuple of ints, a product is performed on multiple + axes, instead of a single axis or all the axes as before. dtype : data-type, optional The data-type of the returned array, as well as of the accumulator in which the elements are multiplied. By default, if `a` is of @@ -1849,6 +2279,10 @@ Alternative output array in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- @@ -1902,8 +2336,16 @@ True """ - raise NotImplementedError('Waiting on interp level method') - + if type(a) is not mu.ndarray: + try: + prod = a.prod + except AttributeError: + return _methods._prod(a, axis=axis, dtype=dtype, + out=out, keepdims=keepdims) + return prod(axis=axis, dtype=dtype, out=out) + else: + return _methods._prod(a, axis=axis, dtype=dtype, + out=out, keepdims=keepdims) def cumprod(a, axis=None, dtype=None, out=None): """ @@ -1965,7 +2407,11 @@ [ 4, 20, 120]]) """ - raise NotImplementedError('Waiting on interp level method') + try: + cumprod = a.cumprod + except AttributeError: + return _wrapit(a, 'cumprod', axis, dtype, out) + return cumprod(axis, dtype, out) def ndim(a): @@ -1999,9 +2445,10 @@ 0 """ - if not hasattr(a, 'ndim'): - a = numpyarray(a) - return a.ndim + try: + return a.ndim + except AttributeError: + return asarray(a).ndim def rank(a): @@ -2044,9 +2491,10 @@ 0 """ - if not hasattr(a, 'ndim'): - a = numpyarray(a) - return a.ndim + try: + return a.ndim + except AttributeError: + return asarray(a).ndim def size(a, axis=None): @@ -2083,7 +2531,16 @@ 2 """ - raise NotImplementedError('Waiting on interp level method') + if axis is None: + try: + return a.size + except AttributeError: + return asarray(a).size + else: + try: + return a.shape[axis] + except AttributeError: + return asarray(a).shape[axis] def around(a, decimals=0, out=None): @@ -2152,7 +2609,11 @@ array([ 0, 0, 0, 10]) """ - raise NotImplementedError('Waiting on interp level method') + try: + round = a.round + except AttributeError: + return _wrapit(a, 'round', decimals, out) + return round(decimals, out) def round_(a, decimals=0, out=None): @@ -2166,10 +2627,14 @@ around : equivalent function """ - raise NotImplementedError('Waiting on interp level method') - - -def mean(a, axis=None, dtype=None, out=None): + try: + round = a.round + except AttributeError: + return _wrapit(a, 'round', decimals, out) + return round(decimals, out) + + +def mean(a, axis=None, dtype=None, out=None, keepdims=False): """ Compute the arithmetic mean along the specified axis. @@ -2194,6 +2659,10 @@ is ``None``; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See `doc.ufuncs` for details. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- @@ -2204,6 +2673,7 @@ See Also -------- average : Weighted average + std, var, nanmean, nanstd, nanvar Notes ----- @@ -2240,14 +2710,17 @@ 0.55000000074505806 """ - assert dtype is None - assert out is None - if not hasattr(a, "mean"): - a = numpyarray(a) - return a.mean(axis=axis) - - -def std(a, axis=None, dtype=None, out=None, ddof=0): + if type(a) is not mu.ndarray: + try: + mean = a.mean + return mean(axis=axis, dtype=dtype, out=out) + except AttributeError: + pass + + return _methods._mean(a, axis=axis, dtype=dtype, + out=out, keepdims=keepdims) + +def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): """ Compute the standard deviation along the specified axis. @@ -2274,6 +2747,10 @@ Means Delta Degrees of Freedom. The divisor used in calculations is ``N - ddof``, where ``N`` represents the number of elements. By default `ddof` is zero. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- @@ -2283,7 +2760,7 @@ See Also -------- - var, mean + var, mean, nanmean, nanstd, nanvar numpy.doc.ufuncs : Section "Output arguments" Notes @@ -2291,14 +2768,15 @@ The standard deviation is the square root of the average of the squared deviations from the mean, i.e., ``std = sqrt(mean(abs(x - x.mean())**2))``. - The average squared deviation is normally calculated as ``x.sum() / N``, where - ``N = len(x)``. If, however, `ddof` is specified, the divisor ``N - ddof`` - is used instead. In standard statistical practice, ``ddof=1`` provides an - unbiased estimator of the variance of the infinite population. ``ddof=0`` - provides a maximum likelihood estimate of the variance for normally - distributed variables. The standard deviation computed in this function - is the square root of the estimated variance, so even with ``ddof=1``, it - will not be an unbiased estimate of the standard deviation per se. + The average squared deviation is normally calculated as + ``x.sum() / N``, where ``N = len(x)``. If, however, `ddof` is specified, + the divisor ``N - ddof`` is used instead. In standard statistical + practice, ``ddof=1`` provides an unbiased estimator of the variance + of the infinite population. ``ddof=0`` provides a maximum likelihood + estimate of the variance for normally distributed variables. The + standard deviation computed in this function is the square root of + the estimated variance, so even with ``ddof=1``, it will not be an + unbiased estimate of the standard deviation per se. Note that, for complex numbers, `std` takes the absolute value before squaring, so that the result is always real and nonnegative. @@ -2333,15 +2811,18 @@ 0.44999999925552653 """ - assert dtype is None - assert out is None - assert ddof == 0 - if not hasattr(a, "std"): - a = numpyarray(a) - return a.std(axis=axis) - - -def var(a, axis=None, dtype=None, out=None, ddof=0): + if type(a) is not mu.ndarray: + try: + std = a.std + return std(axis=axis, dtype=dtype, out=out, ddof=ddof) + except AttributeError: + pass + + return _methods._std(a, axis=axis, dtype=dtype, out=out, ddof=ddof, + keepdims=keepdims) + +def var(a, axis=None, dtype=None, out=None, ddof=0, + keepdims=False): """ Compute the variance along the specified axis. @@ -2369,6 +2850,10 @@ "Delta Degrees of Freedom": the divisor used in the calculation is ``N - ddof``, where ``N`` represents the number of elements. By default `ddof` is zero. + keepdims : bool, optional + If this is set to True, the axes which are reduced are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the original `arr`. Returns ------- @@ -2378,8 +2863,7 @@ See Also -------- - std : Standard deviation - mean : Average + std , mean, nanmean, nanstd, nanvar numpy.doc.ufuncs : Section "Output arguments" Notes @@ -2408,9 +2892,9 @@ >>> a = np.array([[1,2],[3,4]]) >>> np.var(a) 1.25 - >>> np.var(a,0) + >>> np.var(a, axis=0) array([ 1., 1.]) - >>> np.var(a,1) + >>> np.var(a, axis=1) array([ 0.25, 0.25]) In single precision, var() can be inaccurate: @@ -2421,7 +2905,7 @@ >>> np.var(a) 0.20405951142311096 - Computing the standard deviation in float64 is more accurate: + Computing the variance in float64 is more accurate: >>> np.var(a, dtype=np.float64) 0.20249999932997387 @@ -2429,10 +2913,12 @@ 0.20250000000000001 """ - assert dtype is None - assert out is None - assert ddof == 0 - if not hasattr(a, "var"): - a = numpyarray(a) - return a.var(axis=axis) - + if type(a) is not mu.ndarray: + try: + var = a.var + return var(axis=axis, dtype=dtype, out=out, ddof=ddof) + except AttributeError: + pass + + return _methods._var(a, axis=axis, dtype=dtype, out=out, ddof=ddof, + keepdims=keepdims) diff --git a/lib_pypy/numpypy/core/shape_base.py b/lib_pypy/numpypy/core/shape_base.py --- a/lib_pypy/numpypy/core/shape_base.py +++ b/lib_pypy/numpypy/core/shape_base.py @@ -1,7 +1,9 @@ +from __future__ import division, absolute_import, print_function + __all__ = ['atleast_1d', 'atleast_2d', 'atleast_3d', 'vstack', 'hstack'] -import numeric as _nx -from numeric import array, asanyarray, newaxis +from . import numeric as _nx +from .numeric import array, asanyarray, newaxis def atleast_1d(*arys): """ @@ -55,7 +57,6 @@ else: return res - def atleast_2d(*arys): """ View inputs as arrays with at least two dimensions. @@ -99,7 +100,7 @@ if len(ary.shape) == 0 : result = ary.reshape(1, 1) elif len(ary.shape) == 1 : - result = ary[newaxis, :] + result = ary[newaxis,:] else : result = ary res.append(result) @@ -161,11 +162,11 @@ for ary in arys: ary = asanyarray(ary) if len(ary.shape) == 0: - result = ary.reshape(1,1,1) + result = ary.reshape(1, 1, 1) elif len(ary.shape) == 1: - result = ary[newaxis,:,newaxis] + result = ary[newaxis,:, newaxis] elif len(ary.shape) == 2: - result = ary[:,:,newaxis] + result = ary[:,:, newaxis] else: result = ary res.append(result) @@ -174,6 +175,7 @@ else: return res + def vstack(tup): """ Stack arrays in sequence vertically (row wise). @@ -223,7 +225,7 @@ [4]]) """ - return _nx.concatenate(map(atleast_2d,tup),0) + return _nx.concatenate([atleast_2d(_m) for _m in tup], 0) def hstack(tup): """ @@ -267,7 +269,7 @@ [3, 4]]) """ - arrs = map(atleast_1d,tup) + arrs = [atleast_1d(_m) for _m in tup] # As a special case, dimension 0 of 1-dimensional arrays is "horizontal" if arrs[0].ndim == 1: return _nx.concatenate(arrs, 0) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit