def test_axis(self): """With axis given, this just needs to match `numpy.cumsum` results. """ # Start with a known input and output test = [[1, 2, 3, 4], [0, 2, 1, 2], [3, 1, 0, 0]] chk_a1 = [[1, 3, 6, 10], [0, 2, 3, 5], [3, 4, 4, 4]] chk_a0 = [[1, 2, 3, 4], [1, 4, 4, 6], [4, 5, 4, 6]] checks = [chk_a0, chk_a1] print("input = \n", test) for aa, chk in enumerate(checks): print("---- axis=", aa) res = utils.cumsum(test, axis=aa) chk_np = np.cumsum(test, axis=aa) print("output = \n", res) print("numpy truth = \n", chk_np) msg = "`cumsum` does {{fail:}}match numpy result along axis={}".format( aa) utils.allclose(res, chk_np, rtol=1e-10, msg=msg) print("output = \n", res) print("brute-force truth = \n", chk) msg = "`cumsum` does {{fail:}}match known result along axis={}".format( aa) utils.allclose(res, chk, rtol=1e-10, msg=msg) for nd in range(1, 5): for ax in range(nd): self._test_axis_ndim(nd, ax) return
def test_no_axis(self): # Start with a known input and output test = [[1, 2, 3, 4], [0, 2, 1, 2], [3, 1, 0, 0]] check = [[1, 3, 6, 10], [1, 5, 9, 15], [4, 9, 13, 19]] res = utils.cumsum(test) print("input = \n", test) print("output = \n", res) print("brute-force truth = \n", check) utils.allclose(res, check, rtol=1e-10, msg="`cumsum` does {fail:}match known result") for nd in range(1, 5): self._test_no_axis_ndim(nd) return
def _test_no_axis_ndim(self, ndim): """Test cumsum over all axes (i.e. "no" axis given) for a `ndim` array """ # Construct a random shape in `ndim` dimensions shape = np.random.randint(2, 7, ndim) # Fill with random values vals = np.random.uniform(-20.0, 20.0, shape) # Get the `cumsum` result res = utils.cumsum(vals) # Get the brute-force result for comparison chk = self._brute_force_cumsum(vals) # Make sure they match print("input = \n", vals) print("output = \n", res) print("brute-force truth = \n", chk) msg = "cumsum ndim={} does {{fail:}}match brute-force values.".format( ndim) utils.allclose(res, chk, rtol=1e-10, msg=msg) return