def eq_test():
    '''
    Tests equality of AD objects
    '''
    x = AD.AADVariable(2, 1)
    y = AD.AADVariable(2, [1, 0])
    assert x == y
def neq2_test():
    '''
    Tests inequality of AD objects
    '''
    x = AD.AADVariable(2, 1)
    y = AD.AADVariable(3, [1])
    assert x != y
def div_func_case():
    '''
    Testing for type errors
    '''
    x = AD.AADVariable(1)
    y = AD.AADVariable(2)
    z = x / y
    assert z.val == 0.5
def multiVariable1():
    '''
    Tests GMRES with multiple variables and multiple vectors
    '''
    x = AD.AADVariable(3, [1 ,0])
    y = AD.AADVariable(2, [0, 1]) 

    solve = AAD_GMRES.solve(lambda x, y: [x+y+5, 3*x-y+1], [0.5, 1.5])
    assert (solve[0] +0.5 <tol and solve[1] +3.5< tol)
def power_case():
    '''
    Testing multiple variable AD object power
    '''
    x = AD.AADVariable(2, [1, 0])
    y = AD.AADVariable(2, [0, 1])
    z = x**y

    assert z.val == 4
    assert z.der[0] == 4
def rpower_case():
    '''
    Testing multiple variable AD object power alternate case
    '''
    x = AD.AADVariable(2, [1, 0])
    y = AD.AADVariable(2, [0, 1])
    z = y**x

    assert z.val == 4
    assert z.der[1] == 4
def multiVariableNonLinear1():
    '''
    Tests Newton's method with multiple variables that are nonlinear and multiple vectors
    '''
    x = AD.AADVariable(3, [1, 0])
    y = AD.AADVariable(2, [0, 1])

    solve = AAD_Newton.solve(lambda x, y: [x * y + 1, 3 * x + 5 * y - 1],
                             [1, 1])
    assert (solve[0] - (-1.1350416126511096) < tol
            and solve[1] - (0.8810249675906657) < tol)
def multiVariable2():
    '''
    Tests GMRES with multiple variables and multiple vectors
    '''
    
    x = AD.AADVariable(3, [1 ,0 ,0 ,0])
    y = AD.AADVariable(2, [0, 1, 0 ,0]) 
    z = AD.AADVariable(2, [0, 0, 1, 0])
    a = AD.AADVariable(2, [0, 0, 0, 1])

    solve = AAD_GMRES.solve(lambda x, y, z, a: [x+4*y-z+a, a-2, y*5+z*0.1, x+2*a], [1, 0, 1, 1])
    assert (solve[0]+4 <tol and solve[1] -0.0370 <tol and solve[2] +1.8518 <tol and solve[3]-2 <tol)
def singleVariable():
    '''
    Tests Gradient Descent with a single variable and a single vector
    '''
    x = AD.AADVariable(3, [1, 0])
    y = AD.AADVariable(2, [0, 1])
    solve = AAD_grad.solve(lambda X: AD.abs(2 * X[0] - 12), [x, x],
                           0.001,
                           progress_step=None,
                           max_iter=10000)

    assert (solve[0].val - 6) < tol
예제 #10
0
def test_11():
    '''
    Testing multiple variable AD object division
    '''
    x = AD.AADVariable(1, 1)
    y = AD.AADVariable(1, [0, 1])
    f = x / y
    #value check
    assert abs(f.val - 1.0) < tol
    #derivative check
    assert abs(f.der[0] - 1) < tol
    assert abs(f.der[1] + 1) < tol
def multiVariable1():
    '''
    Tests Gradient Descent with multiple variables
    '''
    x = AD.AADVariable(3, [1, 0])
    y = AD.AADVariable(2, [0, 1])

    solve = AAD_grad.solve(lambda X: AD.abs(AD.cos(X[0]) * X[1] - 12), [x, y],
                           0.001,
                           progress_step=None,
                           max_iter=10000)
    assert (solve[0].val < tol and solve[1].val - 12 < tol)
def multiVariable2():
    '''
    Tests Gradient Descent with multiple variables
    '''
    x = AD.AADVariable(3, [1, 0])
    y = AD.AADVariable(2, [0, 1])

    solve = AAD_grad.solve(lambda X: AD.abs(2 * X[0] - 12 + 2 * X[1]**2),
                           [x, y],
                           0.001,
                           progress_step=5000,
                           max_iter=10000)
    assert (solve[0].val - 2.972 < tol and solve[1].val - 1.745 < tol)
예제 #13
0
def repr_test():
    '''
    Tests __repr__ of AD object
    '''
    x = AD.AADVariable((math.pi))
    x = 2**AD.exp(x)
    repr(x)
def singleVariableNonLinear():
    '''
    Tests Newton's method with a single variable nonlinear, and a single vector
    '''
    x = AD.AADVariable(3, [1, 0])
    solve = AAD_Newton.solve(lambda x: [x**2 - 3], [3])

    assert (solve[0] - math.sqrt(3)) < tol
예제 #15
0
def jacobian_test():
    '''
    Tests formation of jacobian
    '''
    x = AD.AADVariable((math.pi))
    x = 2**AD.exp(x)
    assert abs(x.jacobian() -
               2**(math.exp(math.pi)) * math.exp(math.pi) * math.log(2)) < tol
def singleVariable():
    '''
    Tests Newton's method with a single variable and a single vector
    '''
    x = AD.AADVariable(3, [1, 0])
    solve = AAD_Newton.solve(lambda x: [x - 2], [300])

    assert (solve[0] - 2) < tol
예제 #17
0
def add_rev():
    '''
    Testing for for reverse addition
    '''
    x = AD.AADVariable(.5)

    y = 1 + x

    assert y.val == 1.5
def singleVariable():
    '''
    Tests GMRES with a single variable and a single vector
    '''
    x = AD.AADVariable(3, [1 ,0])

    solve = AAD_GMRES.solve(lambda x: [x-2], [300])

    assert (solve[0]-2)<tol
예제 #19
0
def div_edgecase_0():
    '''
    Testing for errors with division by 0
    '''
    x = AD.AADVariable(.5)
    try:
        y = x / 0
    except:
        assert (sys.exc_info()[0] == ZeroDivisionError)
예제 #20
0
def sub_rev():
    '''
    Testing for reverse substitution
    '''
    x = AD.AADVariable(.5)

    y = 1 - x

    assert y.val == 0.5
예제 #21
0
def test_1():
    '''
    Testing sin(x) derivative and value
    '''
    x = AD.AADVariable((math.pi / 2))
    x = AD.sin(x)
    #value check
    assert abs(x.val - math.sin(math.pi / 2)) < tol
    #derivative check
    assert abs(x.der - math.cos(math.pi / 2)) < tol
예제 #22
0
def sub_edgecase():
    '''
    Testing for type errors
    '''
    x = AD.AADVariable(.5)
    try:
        y = x - 'string'
    except:
        # Simply assert an error here - an exception has been thrown which means that the code correctly failed
        assert (True)
예제 #23
0
def test_5():
    '''
    Testing log(x) - arcsin(x) derivative and value
    This also tests subtraction operator overides
    '''
    x = AD.AADVariable(.5)
    x = AD.log(x) - AD.arcsin(x)
    #value check
    assert abs(x.val - (np.log(.5) - math.asin(.5))) < tol
    #derivative check
    assert abs(x.der - (1 / .5 - 1 / math.sqrt(1 - .5**2))) < tol
예제 #24
0
def test_3():
    '''
    Testing exp(tan(x)) derivative and value
    '''
    x = AD.AADVariable((math.pi))
    x = AD.exp(AD.tan(x))
    #value check
    assert abs(x.val - math.exp(math.tan(math.pi))) < tol
    #derivative check
    assert abs(x.der -
               math.exp(math.tan(math.pi)) * 1 / math.cos(math.pi)**2) < tol
예제 #25
0
def test_9():
    '''
    Testing 2/cos(x) derivative and value
    This also tests __rciv__ overrides
    '''
    x = AD.AADVariable((math.pi))
    x = 2 / AD.cos(x)
    #value check
    assert abs(x.val - 2 / math.cos(math.pi)) < tol
    #derivative check
    assert abs(x.der + 2 * math.tan(math.pi) / math.cos(math.pi)) < tol
예제 #26
0
def mul_edgecase():
    '''
    Testing for errors with types
    '''
    x = AD.AADVariable(.5)
    try:
        y = x * 'string'
    except:
        # assert(sys.exc_info()[0] == np.core._exceptions.UFuncTypeError)
        # Simply assert an error here - an exception has been thrown which means that the code correctly failed
        assert (True)
예제 #27
0
def test_2():
    '''
    Testing sin(cos(x)) derivative and value
    '''
    x = AD.AADVariable((math.pi))
    x = AD.sin(AD.cos(x))
    #value check
    assert abs(x.val - math.sin(math.cos(math.pi))) < tol
    #derivative check
    assert abs(x.der -
               (-math.cos(math.cos(math.pi)) * math.sin(math.pi))) < tol
예제 #28
0
def test_8():
    '''
    Testing 2*cos(x) derivative and value
    This also tests __rmul__ overrides
    '''
    x = AD.AADVariable((math.pi))
    x = 2 * AD.cos(x)
    #value check
    assert abs(x.val - 2 * math.cos(math.pi)) < tol
    #derivative check
    assert abs(x.der + 2 * math.sin(math.pi)) < tol
예제 #29
0
def test_4():
    '''
    Testing sinh(x) + cosh(x) + tanh(x) derivative and value
    This also tests addition operator overides
    '''
    x = AD.AADVariable(3)
    x = AD.sinh(x) + AD.cosh(x) + AD.tanh(x)
    #value check
    assert abs(x.val - (math.sinh(3) + math.cosh(3) + math.tanh(3))) < tol
    #derivative check
    assert abs(x.der - (math.cosh(3) + 1 /
                        (math.cosh(3)**2) + math.sinh(3))) < tol
예제 #30
0
def test_10():
    '''
    Testing 2^exp(x) derivative and value
    Testing alternative power symbol
    '''
    x = AD.AADVariable((math.pi))
    x = 2**AD.exp(x)
    #value check
    assert abs(x.val - 2**math.exp(math.pi)) < tol
    #derivative check
    assert abs(x.der -
               2**(math.exp(math.pi)) * math.exp(math.pi) * math.log(2)) < tol