def test_binary_function(backend, mode, func, u_d, v_d, u_domain, v_domain): if u_domain is None: u_arr = generate_test_data() else: u_arr = generate_test_data(a=u_domain[0], b=u_domain[1]) if v_domain is None: v_arr = generate_test_data() else: v_arr = generate_test_data(a=v_domain[0], b=v_domain[1]) expect_u_diff = [u_d(ua, va) for ua, va in zip(u_arr, v_arr)] expect_v_diff = [v_d(ua, va) for ua, va in zip(u_arr, v_arr)] try: with ua.set_backend(udiff.DiffArrayBackend(backend, mode=mode), coerce=True): u = np.asarray(u_arr) v = np.asarray(v_arr) y = func(u, v) u_diff = y.to(u) v_diff = y.to(v) except ua.BackendNotImplementedError: if backend in FULLY_TESTED_BACKENDS: raise pytest.xfail( reason="The backend has no implementation for this ufunc.") except NotImplementedError: pytest.xfail(reason="The func has no implementation in the {} mode.". format(mode)) if isinstance(y, da.Array): y.compute() assert_allclose(u_diff.value, expect_u_diff) assert_allclose(v_diff.value, expect_v_diff)
def test_jacobian(backend, mode, x, func, expect_jacobian): try: with ua.set_backend(udiff.DiffArrayBackend(backend, mode=mode), coerce=True): x = np.asarray(x) y = func(x) x_jacobian = y.to(x, jacobian=True) except ua.BackendNotImplementedError: if backend in FULLY_TESTED_BACKENDS: raise pytest.xfail( reason="The backend has no implementation for this ufunc.") if isinstance(y, da.Array): y.compute() assert_allclose(x_jacobian.value, expect_jacobian)
def test_separation_binary(backend, mode, u, v, func, expect_u_jacobian, expect_v_jacobian): try: with ua.set_backend(udiff.DiffArrayBackend(backend, mode=mode), coerce=True): u = np.asarray(u) v = np.asarray(v) y = func(u, v) u_jacobian = y.to(u, jacobian=True) v_jacobian = y.to(v, jacobian=True) except ua.BackendNotImplementedError: if backend in FULLY_TESTED_BACKENDS: raise pytest.xfail( reason="The backend has no implementation for this ufunc.") if isinstance(y, da.Array): y.compute() assert_allclose(u_jacobian.value, expect_u_jacobian) assert_allclose(v_jacobian.value, expect_v_jacobian)
def test_high_order_diff(backend, mode, func, y_d, domain): if domain is None: x_arr = generate_test_data() else: x_arr = generate_test_data(a=domain[0], b=domain[1]) expect_diff = [y_d(xa) for xa in x_arr] try: with ua.set_backend(udiff.DiffArrayBackend(backend, mode=mode), coerce=True): x = np.asarray(x_arr) y = func(x) x_diff = y.to(x).to(x) except ua.BackendNotImplementedError: if backend in FULLY_TESTED_BACKENDS: raise pytest.xfail( reason="The backend has no implementation for this ufunc.") if isinstance(y, da.Array): y.compute() assert_allclose(x_diff.value, expect_diff)