def test_basic_multiplication_v(): x = FD(3, 1) y = FD(2, 0) derivative = x * y assert ((float(derivative.val) == 6.0) & (float(derivative.der) == 2.0)), Exception(f'test_basic_multiplication_v() has error.')
def test_basic_addition_v(): x = FD(3, 1) y = FD(2, 0) derivative = x + y assert (float(derivative.get_value()) == 5.0) & (float( derivative.get_derivative()) == 1.0), Exception( f'test_basic_addition_c() has error.')
def test_basic_power_v1(): x = FD(3, 1) y = FD(2, 0) derivative = x**y assert ((float(derivative.val) == 9.0) & (float(derivative.der) == 6.0)), Exception(f'test_basic_power_v1() has error.')
def test_basic_subtraction_v(): x = FD(3, 1) y = FD(2, 0) derivative = x - y assert ((float(derivative.val)) == 1.0) & (float( derivative.der) == 1.0), Exception( f'test_basic_subtraction_v() has error.')
def test_basic_logarithm_v(): x = FD(3, 1) y = FD(2, 2) derivative = FD.logarithm(x, np.e) assert (float(derivative.val) == np.log(3)) & (float( derivative.der) == 1 / 3), Exception( f'test_basic_logarithm_v() has error.') derivative2 = FD.logarithm([x, y], base=np.e) assert (float(derivative2[1].val) == np.log(2)) & (float( derivative2[1].der) == 1), Exception( f'test_basic_logarithm_v() has error.')
def test_basic_logistic_v(): x = FD(3, 1) y = FD(2, 2) derivative = FD.logistic(x) assert (float(derivative.val) == 1 / (1 + np.e**(-3))) & ( float(derivative.der) == (np.e**(3)) / (1 + np.e**(3))**2), Exception(f'test_basic_logistic_v() has error.') derivative2 = FD.logistic([x, y]) assert (float(derivative2[1].val) == 1 / (1 + np.e**(-2))) & ( float(derivative2[1].der) == 2 * (np.e**(2)) / (1 + np.e**(2))**2), Exception(f'test_basic_logistic_v() has error.')
def Jacobian(arr): # initialize with input value num_var = len(arr) output = [[] for x in range(num_var)] for i in range(num_var): for j in range(num_var): if i == j: output[i].append(FD(arr[i], 1)) else: output[i].append(FD(arr[i], 0)) return np.array(output)
def test_function_jacobian(): x = Jacobian([1, 3, 4]) fun = np.sin(3 * x[0] + 2 * x[1] - x[2]) assert isinstance(fun[0], FD), AssertionError('Not an instance of AD.') assert isinstance(fun[0].val, int) or isinstance( fun[0].val, float), AssertionError('Value is not a number.') assert isinstance(fun[0].der, int) or isinstance( fun[0].der, float), AssertionError('Derivative is not a number.') jacob = FD.get_derivatives(fun) values = FD.get_values(fun) assert isinstance(jacob[0], np.ndarray), AssertionError( "get_derivatives method doesn't return numpy ndarray") assert isinstance(values[0], np.ndarray), AssertionError( "get_values method doesn't return numpy ndarray")
def test_multiple_functions_jacobian(): x = Jacobian([1, 3, 4]) fun = [np.sin(3 * x[0] + 2 * x[1] - x[2]), x[0]**x[1] - x[2]] assert len(fun) == 2, AssertionError( "Didn't return correct number of functions.") assert len(fun[0]) == 3, AssertionError( "Didn't return correct number of variables.") assert isinstance(fun[0][0], FD), AssertionError('Not an instance of AD.') assert isinstance(fun[0][0].val, int) or isinstance( fun[0][0].val, float), AssertionError('Value is not a number.') assert isinstance(fun[0][0].der, int) or isinstance( fun[0][0].der, float), AssertionError('Derivative is not a number.') jacob = FD.get_derivatives(fun) values = FD.get_values(fun) assert jacob.shape == ( 2, 3), AssertionError("Jacobian Matrix doesn't have correct dimensions") assert values.shape == ( 2, 3), AssertionError("Values Matrix doesn't have correct dimensions") assert isinstance(jacob[0], np.ndarray), AssertionError( "get_derivatives method doesn't return numpy ndarray") assert isinstance(values[0], np.ndarray), AssertionError( "get_values method doesn't return numpy ndarray")
def test_basic_rmul_c(): x = FD(3, 1) derivative = 2 * x assert ((float(derivative.val) == 6.0) & (float( derivative.der) == 2.0)), Exception(f'test_basic_rmul_c() has error.')
def test_basic_div_c(): x = FD(3, 1) derivative = x / 2 assert ((float(derivative.val) == 1.50) & (float( derivative.der) == 0.5)), Exception(f'test_basic_div_c() has error.')
def test_basic_addition_c(): x = FD(3, 1) derivative = x + 2 assert (float(derivative.val) == 5.0) & (float( derivative.der) == 1.0), Exception( f'test_basic_addition_c() has error.')
def test_basic_rsub_v(): x = FD(3, 1) y = FD(2, 0) derivative = y - x assert ((float(derivative.val) == -1.0) & (float( derivative.der) == -1.0)), Exception(f'test_basic_rsub_v() has error.')
def test_basic_str(): x = FD(3, 1) assert str(x) == 'FD(3, 1)'
def test_basic_arccos_v(): x = FD(0.5, 1) derivative = FD.arccos(x) assert (float(derivative.val) == np.arccos(0.5)) & (float( derivative.der) == -1 / np.sqrt(1 - 0.5**2)), Exception( f'test_basic_arccos_v() has error.')
def test_basic_ne_v(): x = FD(3, 1) y = FD(2, 0) assert x != y, Exception(f'test_basic_ne_v() has error.')
def test_basic_rpow_c(): x = FD(3, 1) derivative = 2**x assert (float(derivative.val) == 8.0) & (float(derivative.der) == 8 * np.log(2)), Exception(f'test_basic_rpow_c() has error.')
def test_basic_sqrt_v(): x = FD(3, 1) derivative = FD.sqrt(x) assert (float(derivative.val) == np.sqrt(3)) & (float( derivative.der) == 0.5 * 3**(-0.5)), Exception( f'test_basic_sqrt_v() has error.')
def test_basic_radd_c(): x = FD(3, 1) derivative = 2 + x assert ((float(derivative.val)) == 5.0) & (float( derivative.der) == 1.0), Exception(f'test_basic_radd_c() has error.')
def test_basic_tanh_v(): x = FD(3, 1) derivative = FD.tanh(x) assert (float(derivative.val) == np.tanh(3)) & (float( derivative.der) == 1 / np.cosh(3)**2), Exception( f'test_basic_tanh_v() has error.')
def test_basic_arctan_v(): x = FD(3, 1) derivative = FD.arctan(x) assert (float(derivative.val) == np.arctan(3)) & ( float(derivative.der) == 1 / (1 + 3**2)), Exception(f'test_basic_arctan_v() has error.')
def test_basic_neg_v(): x = FD(3, 1) derivative = -x assert (float(derivative.val) == -3.0) & (float( derivative.der) == -1.0), Exception(f'test_basic_neg_v() has error.')
def test_basic_power_v2(): x = FD(-1, 1) y = FD(2, 1) derivative = x**y assert ((float(derivative.val) == 1) & (float( derivative.der) == -2)), Exception(f'test_basic_power_v2() has error.')
def test_basic_rdiv_c(): # Account for when the divisor is 0 x = FD(3, 1) derivative = 2 / x assert (float(derivative.val) == (2 / 3)) & (float( derivative.der) == -(2 / 9)), Exception( f'test_basic_rdiv_c() has error.')
def test_basic_exponential_v(): x = FD(3, 1) derivative = np.exp(x) assert ((float(derivative.val) == np.exp(3)) & (float(derivative.der) == np.exp(3)) ), Exception(f'test_basic_exponential_v() has error.')
def test_basic_repr(): x = FD(3, 1) assert repr(x) == 'FD(3, 1)'
def test_basic_eq_v(): x = FD(3, 1) v = FD(3, 1) assert x == v, Exception(f'test_basic_eq_v() has error.')
def test_basic_cos_v(): x = FD(3, 1) derivative = FD.cos(x) assert (float(derivative.val) == np.cos(3)) & (float( derivative.der) == -np.sin(3)), Exception( f'test_basic_cos_v() has error.')