Exemplo n.º 1
0
def test_zero_pow_zero():
    x = ad.DualNumber('x',0)
    y = ad.DualNumber('y',0)
    out = x**y
    assert out.value == 0**0
    assert math.isnan(out.derivatives['x'])
    assert out.derivatives['y']== -float("inf")
Exemplo n.º 2
0
def test_branch():
    def my_fun(x):
        if x<10:
            return x
        else:
            return x**2
    x = ad.DualNumber('x',10)
    with pytest.warns(UserWarning):
        my_fun(x)
    
    x2 = ad.DualNumber('x',20)
    my_fun(x2)
Exemplo n.º 3
0
def test_array():
    will = [[ad.DualNumber('x', x) for x in range(5)],
            [ad.DualNumber('y', 2 * x) for x in range(5)]]
    dave = ad.array(will)
    assert np.all(
        dave.value == np.array([[x
                                 for x in range(5)], [2 * x
                                                      for x in range(5)]]))
    assert np.all(dave.derivatives['x'] == np.array([[1 for x in range(5)],
                                                     [0 for x in range(5)]]))
    assert np.all(dave.derivatives['y'] == np.array([[0 for x in range(5)],
                                                     [1 for x in range(5)]]))
Exemplo n.º 4
0
def test_vert_tan():
    x = ad.DualNumber('x',0)
    output = x**(1/3)
    assert output.value == 0
    assert output.derivatives['x'] == float("inf")
    assert output.derivatives['x'] > 0
Exemplo n.º 5
0
def test_dual_value():
    z = ad.DualNumber('z',10)
    with pytest.raises(TypeError):
        ad.DualNumber('x',z)
Exemplo n.º 6
0
def test_bad_arr():
    with pytest.raises(TypeError):
        ad.DualNumber(7,np.array([10,12]))
Exemplo n.º 7
0
def test_bad_name_n():
    with pytest.raises(TypeError):
        ad.DualNumber(7,20)
Exemplo n.º 8
0
def test_bad_value():
    with pytest.raises(TypeError):
        ad.DualNumber('x',"yes")
Exemplo n.º 9
0
def test_missing_name_arr():
    with pytest.raises(TypeError):
        ad.DualNumber(value=np.array([10,12]))
Exemplo n.º 10
0
def y():
	y = ad.DualNumber('y',3.5)
	return y
Exemplo n.º 11
0
def test_creation_arr():
    x = ad.DualNumber('x',np.array([10,12]))
    assert np.all(x.value == np.array([10,12]))
    assert np.all(x.derivatives['x[0]'] == np.array([1,0]))
    assert np.all(x.derivatives['x[1]'] == np.array([0,1]))
Exemplo n.º 12
0
def test_n_by_zero():
    x = ad.DualNumber('x',0)
    with pytest.raises(ZeroDivisionError):
        5/x
Exemplo n.º 13
0
def test_oscilating():
    x = ad.DualNumber('x',0.001)
    output = ad.sin(1/x)
    assert output.value == math.sin(1/0.001)
    assert output.derivatives['x'] == math.cos(1/.001)*(-1/(.001**2))
Exemplo n.º 14
0
def x():
	return ad.DualNumber('x',2)
Exemplo n.º 15
0
def test_creation_num():
    x = ad.DualNumber('x',10)
    assert x.value == 10
    assert x.derivatives == {'x':1}
Exemplo n.º 16
0
def test_undefined_value():
    x = ad.DualNumber('x',0)
    with pytest.raises(ZeroDivisionError):
        1/x
Exemplo n.º 17
0
def test_missing_name_n():
    with pytest.raises(TypeError):
        ad.DualNumber('x')