def neq2_test(): ''' Tests inequality of AD objects ''' x = AD.AADVariable(2, 1) y = AD.AADVariable(3, [1]) assert x != y
def repr_test(): ''' Tests __repr__ of AD object ''' x = AD.AADVariable((math.pi)) x = 2**AD.exp(x) repr(x)
def eq_test(): ''' Tests equality of AD objects ''' x = AD.AADVariable(2, 1) y = AD.AADVariable(2, [1, 0]) 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 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 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 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
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
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
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
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
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
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 test_7(): ''' Testing x**2/sqrt(x) + arccos(x) derivative and value This also tests division and power overrides ''' v = 4 x = AD.AADVariable(v) x = x**2 / AD.sqrt(x) #value check assert abs(x.val - v**2 / math.sqrt(v)) < tol #derivative check assert abs(x.der - (3. * math.sqrt(v)) / 2.) < 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 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 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
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
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
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)
def test_6(): ''' Testing sqrt(x)*arctan(x) + arccos(x) derivative and value This also tests multiplication operator overides ''' v = 0.5 x = AD.AADVariable((v)) x = AD.sqrt(x) * AD.arctan(x) + AD.arccos(x) #value check assert abs(x.val - (math.sqrt(v) * math.atan(v) + math.acos(v))) < tol #derivative check assert abs(x.der - (math.sqrt(v) / (v**2 + 1.) - 1. / (math.sqrt(1. - v**2)) + (math.atan(v) / (2. * math.sqrt(v))))) < 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
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
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
def add_rev(): ''' Testing for for reverse addition ''' x = AD.AADVariable(.5) y = 1 + x assert y.val == 1.5
def sub_rev(): ''' Testing for reverse substitution ''' x = AD.AADVariable(.5) y = 1 - x assert y.val == 0.5
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)