MCPcopy Index your code
hub / github.com/numpy/numpy / test_reduce_zero_axis

Method test_reduce_zero_axis

numpy/_core/tests/test_ufunc.py:1947–2010  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1945 assert_raises(ValueError, np.divide.reduce, a, axis=(0, 1))
1946
1947 def test_reduce_zero_axis(self):
1948 # If we have an n x m array and do a reduction with axis=1, then we are
1949 # doing n reductions, and each reduction takes an m-element array. For
1950 # a reduction operation without an identity, then:
1951 # n > 0, m > 0: fine
1952 # n = 0, m > 0: fine, doing 0 reductions of m-element arrays
1953 # n > 0, m = 0: can't reduce a 0-element array, ValueError
1954 # n = 0, m = 0: can't reduce a 0-element array, ValueError (for
1955 # consistency with the above case)
1956 # This test doesn't actually look at return values, it just checks to
1957 # make sure that error we get an error in exactly those cases where we
1958 # expect one, and assumes the calculations themselves are done
1959 # correctly.
1960
1961 def ok(f, *args, **kwargs):
1962 f(*args, **kwargs)
1963
1964 def err(f, *args, **kwargs):
1965 assert_raises(ValueError, f, *args, **kwargs)
1966
1967 def t(expect, func, n, m):
1968 expect(func, np.zeros((n, m)), axis=1)
1969 expect(func, np.zeros((m, n)), axis=0)
1970 expect(func, np.zeros((n // 2, n // 2, m)), axis=2)
1971 expect(func, np.zeros((n // 2, m, n // 2)), axis=1)
1972 expect(func, np.zeros((n, m // 2, m // 2)), axis=(1, 2))
1973 expect(func, np.zeros((m // 2, n, m // 2)), axis=(0, 2))
1974 expect(func, np.zeros((m // 3, m // 3, m // 3,
1975 n // 2, n // 2)),
1976 axis=(0, 1, 2))
1977 # Check what happens if the inner (resp. outer) dimensions are a
1978 # mix of zero and non-zero:
1979 expect(func, np.zeros((10, m, n)), axis=(0, 1))
1980 expect(func, np.zeros((10, n, m)), axis=(0, 2))
1981 expect(func, np.zeros((m, 10, n)), axis=0)
1982 expect(func, np.zeros((10, m, n)), axis=1)
1983 expect(func, np.zeros((10, n, m)), axis=2)
1984
1985 # np.maximum is just an arbitrary ufunc with no reduction identity
1986 assert_equal(np.maximum.identity, None)
1987 t(ok, np.maximum.reduce, 30, 30)
1988 t(ok, np.maximum.reduce, 0, 30)
1989 t(err, np.maximum.reduce, 30, 0)
1990 t(err, np.maximum.reduce, 0, 0)
1991 err(np.maximum.reduce, [])
1992 np.maximum.reduce(np.zeros((0, 0)), axis=())
1993
1994 # all of the combinations are fine for a reduction that has an
1995 # identity
1996 t(ok, np.add.reduce, 30, 30)
1997 t(ok, np.add.reduce, 0, 30)
1998 t(ok, np.add.reduce, 30, 0)
1999 t(ok, np.add.reduce, 0, 0)
2000 np.add.reduce([])
2001 np.add.reduce(np.zeros((0, 0)), axis=())
2002
2003 # OTOH, accumulate always makes sense for any combination of n and m,
2004 # because it maps an m-element array to an m-element array. These

Callers

nothing calls this directly

Calls 4

assert_equalFunction · 0.90
tFunction · 0.85
accumulateMethod · 0.80
reduceMethod · 0.45

Tested by

no test coverage detected