コード例 #1
0
def test_bisection_search_vec():
    l1 = [0.0, 1.0, 2.0, 3.0, 5.0, 10.0]
    l2 = [0.2, 1.2, 2.2, 3.2, 5.2, 9.2]
    expected_l2 = [0, 1, 2, 3, 4, 4]
    l3 = [0.9, 1.9, 2.9, 3.9, 5.9, 9.9]
    expected_l3 = [0, 1, 2, 3, 4, 4]
    l4 = [0.2, 9.9, 2.3, 5.5]
    expected_l4 = [0, 4, 2, 4]
    
    assert (D.all(de.utilities.search_bisection_vec(l1, l2) == D.asarray(expected_l2)))
    assert (D.all(de.utilities.search_bisection_vec(l1, l3) == D.asarray(expected_l3)))
    assert (D.all(de.utilities.search_bisection_vec(l1, l4) == D.asarray(expected_l4)))
コード例 #2
0
def test_logical_xor_out():
    a = D.array([True, False, False, True], dtype=D.bool)
    b = D.array([False, False, True, True], dtype=D.bool)
    ref = D.array([True, False, True, False], dtype=D.bool)
    out = D.zeros_like(a, dtype=D.bool)
    D.logical_xor(a, b, out=out)
    assert (D.all(out == ref))
コード例 #3
0
def test_logical_not_out_where():
    a = D.array([True, False, False, True], dtype=D.bool)
    ref = D.array([False, False, False, False], dtype=D.bool)
    out = D.zeros_like(a, dtype=D.bool)
    where = D.array([True, False, False, True], dtype=D.bool)
    D.logical_not(a, out=out, where=where)
    assert (D.all(out[where] == ref[where]))
コード例 #4
0
def test_dense_right_interval_vec():
    denseoutput = DenseOutput(None, None)
    inputs = D.array([0, 1, 0, 1, 1, 1])
    interpolator = CubicHermiteInterp(*inputs)
    denseoutput.add_interpolant(1, interpolator)
    denseoutput.add_interpolant(2, interpolator)
    assert (D.all(
        denseoutput.find_interval_vec([0.5, 0.99999, 1.00001, 1.5]) == D.array(
            [0, 0, 1, 1], dtype=D.int64)))
コード例 #5
0
ファイル: test_optimizer.py プロジェクト: izzortsi/desolver
def test_newtonraphson_dims(ffmt, tol, dim):
    print("Set dtype to:", ffmt)
    D.set_float_fmt(ffmt)
    np.random.seed(30)

    if tol is not None:
        tol = tol * D.epsilon()

    if D.backend() == 'torch':
        import torch

        torch.set_printoptions(precision=17)

        torch.autograd.set_detect_anomaly(False)

    if ffmt == 'gdual_vdouble':
        pytest.skip("Root-finding is ill-conceived with vectorised gduals")

    shift = D.array(np.random.uniform(1, 10, size=(dim, )))
    exponent = D.array(np.random.uniform(1, 5, size=(dim, )))
    gt_root1 = shift**(1 / exponent)
    gt_root2 = -shift**(1 / exponent)

    def fun(x):
        return x**exponent - shift

    def jac(x):
        return D.diag(exponent * D.reshape(x, (-1, ))**(exponent - 1))

    x0 = D.array(np.random.uniform(1, 3, size=(dim, )))
    print(gt_root1, gt_root2)
    print(x0)
    print(fun(x0))
    print(jac(x0))

    root, (success, num_iter,
           prec) = de.utilities.optimizer.newtonraphson(fun,
                                                        x0,
                                                        jac=jac,
                                                        tol=tol,
                                                        verbose=True)

    if tol is None:
        tol = D.epsilon()
    assert (success)
    conv_root1 = D.stack([
        D.array(np.allclose(D.to_numpy(D.to_float(r1)),
                            D.to_numpy(D.to_float(r)), 128 * tol, 32 * tol),
                dtype=D.bool) for r, r1 in zip(root, gt_root1)
    ])
    conv_root2 = D.stack([
        D.array(np.allclose(D.to_numpy(D.to_float(r2)),
                            D.to_numpy(D.to_float(r)), 128 * tol, 32 * tol),
                dtype=D.bool) for r, r2 in zip(root, gt_root2)
    ])
    assert (D.all(conv_root1 | conv_root2))
コード例 #6
0
def test_gradients_higher_nu():
    a = D.array([1.0], requires_grad=True)
    b = a * a
    assert (D.all(D.jacobian(b, a, nu=2, batch_mode=False) == D.array([2.0])))
コード例 #7
0
def test_gradients_unbatched():
    a = D.array([1.0, 1.0], requires_grad=True)
    assert (D.all(
        D.jacobian(a, a, batch_mode=False) == D.array([[1.0, 0.0], [0.0, 1.0]
                                                       ])))
コード例 #8
0
def test_gradients_batched():
    a = D.array([[1.0, 1.0], [1.0, 1.0]], requires_grad=True)
    b = a * D.array([[1.0, 1.0], [2.0, 2.0]])
    assert (D.all(
        D.jacobian(b, a, batch_mode=True) == D.array(
            [[[1.0, 0.0], [2.0, 0.0]], [[0.0, 1.0], [0.0, 2.0]]])))
コード例 #9
0
def test_gradients_no_grad():
    a = D.array([1.0, 1.0], requires_grad=False)
    assert (D.all(D.jacobian(a, a) == D.array([[0.0, 0.0], [0.0, 0.0]])))
コード例 #10
0
def test_gradients_nu_zero():
    a = D.array([1.0, 1.0], requires_grad=True)
    assert (D.all(a == D.jacobian(a, a, nu=0)))
コード例 #11
0
def test_subtraction_with_out():
    a = D.array([1.0])
    out = D.zeros((1, ))
    D.sub(a, a, out=out)
    assert (D.all(out == D.array([0.0])))
コード例 #12
0
def test_subtraction():
    a = D.array([1.0])
    assert (D.all(D.sub(a, a) == D.array([0.0])))
コード例 #13
0
def test_asarray():
    a = np.array([1.0, 2.0, 3.0])
    a_torch = D.array(a)
    assert (D.all(a_torch == D.asarray(a)))
コード例 #14
0
def test_logical_xor_where():
    a = D.array([True, False, False, True], dtype=D.bool)
    b = D.array([False, False, True, True], dtype=D.bool)
    ref = D.array([True, False, True, False], dtype=D.bool)
    where = D.array([True, False, False, True], dtype=D.bool)
    assert (D.all(D.logical_xor(a, b, where=where)[where] == ref[where]))
コード例 #15
0
def test_logical_xor():
    a = D.array([True, False, False, True], dtype=D.bool)
    b = D.array([False, False, True, True], dtype=D.bool)
    ref = D.array([True, False, True, False], dtype=D.bool)
    assert (D.all(D.logical_xor(a, b) == ref))
コード例 #16
0
def test_logical_not():
    a = D.array([True, False, False, True], dtype=D.bool)
    ref = D.array([False, True, True, False], dtype=D.bool)
    assert (D.all(D.logical_not(a) == ref))