def test_apply(): """ For calculations not supported by operator overloading, you can use the apply() function to re-map the generated values. It can be a many-to-one mapping, and can return a DRV. """ d6 = DRV({x: 1 / 6 for x in range(1, 7)}) assert d6.apply(lambda x: x @ d6, allow_drv=True).is_close(d6 @ d6)
def test_convolve(): """ There is an optimisation which uses numpy.convolve for large additions. Run some bigger jobs, to make sure it all works correctly. """ def check(result): result = result.to_dict() assert set(result) == set(range(2, 2001)) for idx in range(2, 1002): assert result[idx] == pytest.approx((idx - 1) / 1E6) for idx in range(1002, 2001): assert result[idx] == pytest.approx((2001 - idx) / 1E6) d1000 = DRV({idx: 0.001 for idx in range(1, 1001)}) check(d1000 + d1000) floaty = d1000.apply(float) check(floaty + floaty) sparse = d1000.apply(lambda x: x * 1000) check((sparse + sparse).apply(lambda x: x // 1000))
def _add_extra(explode: Union[bool, int], result: DRV, blown: Any) -> DRV: extra = _get_extra(explode) if type(blown) != int: extra = extra.apply(type(blown)) extra += blown return result.apply(lambda x: extra if x == blown else x, allow_drv=True)
def plain(pl: DRV) -> DRV: """ Convert all possible values to :obj:`PlainResult`, thus removing any special behaviour of the result_type. """ return pl.apply(PlainResult._from)