|
16 | 16 | import umath as um
|
17 | 17 | import numerictypes as nt
|
18 | 18 | from numeric import asarray, array, asanyarray, concatenate
|
| 19 | +import _methods |
19 | 20 | _dt_ = nt.sctype2char
|
20 | 21 |
|
21 | 22 | import types
|
@@ -1482,12 +1483,12 @@ def sum(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False):
|
1482 | 1483 | try:
|
1483 | 1484 | sum = a.sum
|
1484 | 1485 | except AttributeError:
|
1485 |
| - return um.add.reduce(a, axis=axis, dtype=dtype, |
| 1486 | + return _methods._sum(a, axis=axis, dtype=dtype, |
1486 | 1487 | out=out, skipna=skipna, keepdims=keepdims)
|
1487 | 1488 | # NOTE: Dropping the skipna and keepdims parameters here...
|
1488 | 1489 | return sum(axis=axis, dtype=dtype, out=out)
|
1489 | 1490 | else:
|
1490 |
| - return um.add.reduce(a, axis=axis, dtype=dtype, |
| 1491 | + return _methods._sum(a, axis=axis, dtype=dtype, |
1491 | 1492 | out=out, skipna=skipna, keepdims=keepdims)
|
1492 | 1493 |
|
1493 | 1494 | def product (a, axis=None, dtype=None, out=None, skipna=False, keepdims=False):
|
@@ -1603,7 +1604,8 @@ def any(a, axis=None, out=None, skipna=False, keepdims=False):
|
1603 | 1604 | (191614240, 191614240)
|
1604 | 1605 |
|
1605 | 1606 | """
|
1606 |
| - return um.logical_or.reduce(a, axis=axis, out=out, skipna=skipna, keepdims=keepdims) |
| 1607 | + return _methods._any(a, axis=axis, out=out, |
| 1608 | + skipna=skipna, keepdims=keepdims) |
1607 | 1609 |
|
1608 | 1610 | def all(a, axis=None, out=None, skipna=False, keepdims=False):
|
1609 | 1611 | """
|
@@ -1674,7 +1676,8 @@ def all(a, axis=None, out=None, skipna=False, keepdims=False):
|
1674 | 1676 | (28293632, 28293632, array([ True], dtype=bool))
|
1675 | 1677 |
|
1676 | 1678 | """
|
1677 |
| - return um.logical_and.reduce(a, axis=axis, out=out, skipna=skipna, keepdims=keepdims) |
| 1679 | + return _methods._all(a, axis=axis, out=out, |
| 1680 | + skipna=skipna, keepdims=keepdims) |
1678 | 1681 |
|
1679 | 1682 | def cumsum (a, axis=None, dtype=None, out=None):
|
1680 | 1683 | """
|
@@ -1873,12 +1876,12 @@ def amax(a, axis=None, out=None, skipna=False, keepdims=False):
|
1873 | 1876 | try:
|
1874 | 1877 | amax = a.max
|
1875 | 1878 | except AttributeError:
|
1876 |
| - return um.maximum.reduce(a, axis=axis, |
| 1879 | + return _methods._amax(a, axis=axis, |
1877 | 1880 | out=out, skipna=skipna, keepdims=keepdims)
|
1878 | 1881 | # NOTE: Dropping the skipna and keepdims parameters
|
1879 | 1882 | return amax(axis=axis, out=out)
|
1880 | 1883 | else:
|
1881 |
| - return um.maximum.reduce(a, axis=axis, |
| 1884 | + return _methods._amax(a, axis=axis, |
1882 | 1885 | out=out, skipna=skipna, keepdims=keepdims)
|
1883 | 1886 |
|
1884 | 1887 | def amin(a, axis=None, out=None, skipna=False, keepdims=False):
|
@@ -1947,12 +1950,12 @@ def amin(a, axis=None, out=None, skipna=False, keepdims=False):
|
1947 | 1950 | try:
|
1948 | 1951 | amin = a.min
|
1949 | 1952 | except AttributeError:
|
1950 |
| - return um.minimum.reduce(a, axis=axis, |
| 1953 | + return _methods._amin(a, axis=axis, |
1951 | 1954 | out=out, skipna=skipna, keepdims=keepdims)
|
1952 | 1955 | # NOTE: Dropping the skipna and keepdims parameters
|
1953 | 1956 | return amin(axis=axis, out=out)
|
1954 | 1957 | else:
|
1955 |
| - return um.minimum.reduce(a, axis=axis, |
| 1958 | + return _methods._amin(a, axis=axis, |
1956 | 1959 | out=out, skipna=skipna, keepdims=keepdims)
|
1957 | 1960 |
|
1958 | 1961 | def alen(a):
|
@@ -2080,11 +2083,11 @@ def prod(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False):
|
2080 | 2083 | try:
|
2081 | 2084 | prod = a.prod
|
2082 | 2085 | except AttributeError:
|
2083 |
| - return um.multiply.reduce(a, axis=axis, dtype=dtype, |
| 2086 | + return _methods._prod(a, axis=axis, dtype=dtype, |
2084 | 2087 | out=out, skipna=skipna, keepdims=keepdims)
|
2085 | 2088 | return prod(axis=axis, dtype=dtype, out=out)
|
2086 | 2089 | else:
|
2087 |
| - return um.multiply.reduce(a, axis=axis, dtype=dtype, |
| 2090 | + return _methods._prod(a, axis=axis, dtype=dtype, |
2088 | 2091 | out=out, skipna=skipna, keepdims=keepdims)
|
2089 | 2092 |
|
2090 | 2093 | def cumprod(a, axis=None, dtype=None, out=None):
|
@@ -2459,22 +2462,8 @@ def mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False):
|
2459 | 2462 | except AttributeError:
|
2460 | 2463 | pass
|
2461 | 2464 |
|
2462 |
| - arr = asarray(a) |
2463 |
| - |
2464 |
| - # Upgrade bool, unsigned int, and int to float64 |
2465 |
| - if dtype is None and arr.dtype.kind in ['b','u','i']: |
2466 |
| - ret = um.add.reduce(arr, axis=axis, dtype='f8', |
| 2465 | + return _methods._mean(a, axis=axis, dtype=dtype, |
2467 | 2466 | out=out, skipna=skipna, keepdims=keepdims)
|
2468 |
| - else: |
2469 |
| - ret = um.add.reduce(arr, axis=axis, dtype=dtype, |
2470 |
| - out=out, skipna=skipna, keepdims=keepdims) |
2471 |
| - rcount = mu.count_reduce_items(arr, axis=axis, |
2472 |
| - skipna=skipna, keepdims=keepdims) |
2473 |
| - if isinstance(ret, mu.ndarray): |
2474 |
| - um.true_divide(ret, rcount, out=ret, casting='unsafe') |
2475 |
| - else: |
2476 |
| - ret = ret / float(rcount) |
2477 |
| - return ret |
2478 | 2467 |
|
2479 | 2468 |
|
2480 | 2469 | def std(a, axis=None, dtype=None, out=None, ddof=0,
|
@@ -2579,16 +2568,9 @@ def std(a, axis=None, dtype=None, out=None, ddof=0,
|
2579 | 2568 | except AttributeError:
|
2580 | 2569 | pass
|
2581 | 2570 |
|
2582 |
| - ret = var(a, axis=axis, dtype=dtype, out=out, ddof=ddof, |
| 2571 | + return _methods._std(a, axis=axis, dtype=dtype, out=out, ddof=ddof, |
2583 | 2572 | skipna=skipna, keepdims=keepdims)
|
2584 | 2573 |
|
2585 |
| - if isinstance(ret, mu.ndarray): |
2586 |
| - um.sqrt(ret, out=ret) |
2587 |
| - else: |
2588 |
| - ret = um.sqrt(ret) |
2589 |
| - |
2590 |
| - return ret |
2591 |
| - |
2592 | 2574 | def var(a, axis=None, dtype=None, out=None, ddof=0,
|
2593 | 2575 | skipna=False, keepdims=False):
|
2594 | 2576 | """
|
@@ -2692,43 +2674,6 @@ def var(a, axis=None, dtype=None, out=None, ddof=0,
|
2692 | 2674 | except AttributeError:
|
2693 | 2675 | pass
|
2694 | 2676 |
|
2695 |
| - arr = asarray(a) |
2696 |
| - |
2697 |
| - # First compute the mean, saving 'rcount' for reuse later |
2698 |
| - if dtype is None and arr.dtype.kind in ['b','u','i']: |
2699 |
| - arrmean = um.add.reduce(arr, axis=axis, dtype='f8', |
2700 |
| - skipna=skipna, keepdims=True) |
2701 |
| - else: |
2702 |
| - arrmean = um.add.reduce(arr, axis=axis, dtype=dtype, |
2703 |
| - skipna=skipna, keepdims=True) |
2704 |
| - rcount = mu.count_reduce_items(arr, axis=axis, |
2705 |
| - skipna=skipna, keepdims=True) |
2706 |
| - if isinstance(arrmean, mu.ndarray): |
2707 |
| - um.true_divide(arrmean, rcount, out=arrmean, casting='unsafe') |
2708 |
| - else: |
2709 |
| - arrmean = arrmean / float(rcount) |
2710 |
| - |
2711 |
| - # arr - arrmean |
2712 |
| - x = arr - arrmean |
2713 |
| - |
2714 |
| - # (arr - arrmean) ** 2 |
2715 |
| - if arr.dtype.kind == 'c': |
2716 |
| - um.multiply(x, um.conjugate(x), out=x) |
2717 |
| - x = x.real |
2718 |
| - else: |
2719 |
| - um.multiply(x, x, out=x) |
2720 |
| - |
2721 |
| - # add.reduce((arr - arrmean) ** 2, axis) |
2722 |
| - ret = um.add.reduce(x, axis=axis, dtype=dtype, out=out, |
| 2677 | + return _methods._var(a, axis=axis, dtype=dtype, out=out, ddof=ddof, |
2723 | 2678 | skipna=skipna, keepdims=keepdims)
|
2724 | 2679 |
|
2725 |
| - # add.reduce((arr - arrmean) ** 2, axis) / (n - ddof) |
2726 |
| - if not keepdims and isinstance(rcount, mu.ndarray): |
2727 |
| - rcount = rcount.squeeze(axis=axis) |
2728 |
| - rcount -= ddof |
2729 |
| - if isinstance(ret, mu.ndarray): |
2730 |
| - um.true_divide(ret, rcount, out=ret, casting='unsafe') |
2731 |
| - else: |
2732 |
| - ret = ret / float(rcount) |
2733 |
| - |
2734 |
| - return ret |
0 commit comments