예제 #1
0
def test_power():
    """Boolean condition asserts that value and derivative of a power function using the AutoDiff class are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = [1, 2]

    def myfunc(x, y):
        f1 = 3**x**y**1
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)

    expectAns = {
        'diff': [
            math.log(3) * 3**(c[0]**c[1]) * c[1] * c[0]**(c[1] - 1),
            math.log(3) * 3**(c[0]**c[1]) * math.log(c[0]) * c[0]**c[1]
        ],
        'value':
        3**1**2
    }

    assert res == expectAns
예제 #2
0
def test_arccos():
    """Boolean condition asserts that value and derivative of the inverse of cosine of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 0.5

    def myfunc(x):
        f1 = EF.arccos(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)
    expectAns = {'diff': -1 / math.sqrt(1 - c**2), 'value': math.acos(c)}
    assert res == expectAns
예제 #3
0
def test_neg_sub():
    """Boolean condition asserts that value and derivative of a subtraction using the AutoDiff class are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = [1, 2]

    def myfunc(x, y):
        f1 = 1 - x - y - 2
        return -f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)

    expectAns = {'diff': [1, 1], 'value': 4}

    assert res == expectAns
예제 #4
0
def test_log():
    """Boolean condition asserts that value and derivative of the natural logarithm of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 14

    def myfunc(x):
        f1 = EF.log(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)

    expectAns = {'diff': 1 / c, 'value': math.log(c)}

    assert res == expectAns
예제 #5
0
def test_cot():
    """Boolean condition asserts that value and derivative of the cotangent of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 0.5

    def myfunc(x):
        f1 = EF.cot(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)
    expectAns = {
        'diff': 2 / (math.cos(c * 2) - 1),
        'value': math.cos(c) / math.sin(c)
    }
    assert res == expectAns
예제 #6
0
def test_cosh():
    """Boolean condition asserts that value and derivative of the cosecant of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 2

    def myfunc(x):
        f1 = EF.cosh(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)

    expectAns = {
        'diff': 3.626860407847019,
        'value': math.cosh(c)
    }  #sinh(x) differ in last digits
    assert res == expectAns
예제 #7
0
def test_tanh():
    """Boolean condition asserts that value and derivative of the cosecant of the AutoDiff instance are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    c = 14

    def myfunc(x):
        f1 = EF.tanh(x)
        return f1

    f_obj = ADiff(myfunc)
    res = f_obj.Jac(c)
    expectAns = {
        'diff': (1 / ((math.exp(c) + math.exp(-c)) / 2) *
                 ((math.exp(c) + math.exp(-c)) / 2)),
        'value':
        ((math.exp(c) - math.exp(-c)) / 2) / ((math.exp(c) + math.exp(-c)) / 2)
    }
    assert res == expectAns
예제 #8
0
def test_vec_func1():
    """Boolean condition asserts that value and derivative of a function of the Autodiff class comprising several elementary operations are equal to the expected value and derivative as calculated in the function.

    RETURNS
    ========
    If the boolean condition returns True nothing is returned. If it is computed to be false, then an AssertionError is raised.
    """
    def myfunc(x, y):
        f1 = x * y
        f2 = EF.sin(x)
        f3 = 10
        f4 = x + y + EF.sin(x * y) + 10
        return [f1 + f2, -(f3 - f4)]

    f_obj = ADiff(myfunc)
    res = f_obj.Jac([1, 2])

    expectAns = {
        'diff': [[2.5403023058681398, 1.0],
                 [0.1677063269057152, 0.5838531634528576]],
        'value': [2.8414709848078967, 3.909297426825681]
    }

    assert res == expectAns