def power(x1, x2): """Calculate the exponential of the input x1 with base x2 Keyword arguments: x1 -- a real number or a dual number x2 -- a real number or a dual number Return: the exponential value x1 with base x2 """ if (isinstance(x1,Dual)) and (isinstance(x2,Dual)): # da^u/dx = ln(a) a^u du/dx factor = x1.val ** (x2.val -1) sum_1 = x2.val * x1.der sum_2 = x1.val * np.log(x1.val) * x2.der temp = factor * (sum_1 + sum_2) return Dual(x1.val ** x2.val, temp) elif (isinstance(x1,Dual)): # du^n/dx = n * u^(n-1) * du/dx temp = x2 * x1.val ** (x2-1) * x1.der return Dual(x1.val ** x2, temp) elif (isinstance(x2,Dual)): # da^u/dx = ln(a) a^u du/dx temp = np.log(x2.val) * x2.val ** x1 * x2.der return Dual(x1 ** x2.val, temp) else: return np.power(x1,x2)
def sum(xs): """Calculate the sum of the input Keyword arguments: xs -- a real value list Return: the sum of the array """ cur_val = 0 cur_der = 0 is_dual = False # print('xs') for x in xs: # print('val: ',x.val) if (isinstance(x,Dual)): is_dual = True cur_der += x.der cur_val += x.val else: cur_val += x if is_dual: return Dual(cur_val,cur_der) else: return cur_val
def test2__cos_fn__(): tests = np.linspace(-10, 100, 15) for test in tests: x = Dual(test) f = admath.cos(x) assert np.cos(test) == admath.cos(test) assert np.cos(test) == f.val assert -1 * np.sin(test) == f.der
def test_sqrt_fn__(): tests = np.linspace(1, 100, 15) for test in tests: x = Dual(test) f = admath.sqrt(x) assert np.sqrt(test) == admath.sqrt(test) assert np.sqrt(test) == f.val assert 1 / 2 * 1 / np.sqrt(test) == f.der
def test_logb__(): tests = np.linspace(1, 100, 15) for test in tests: x = Dual(test) f = admath.logb(x, np.exp(1)) assert np.log(test) == admath.logb(test, np.exp(1)) assert np.log(test) == f.val assert 1 / test == f.der
def test_exp_fn__(): tests = np.linspace(-100, 100, 15) for test in tests: x = Dual(test) f = admath.exp(x) assert np.exp(test) == admath.exp(test) assert np.exp(test) == f.val assert np.exp(test) == f.der
def test_log10_fn__(): tests = np.linspace(1, 100, 15) for test in tests: x = Dual(test) f = admath.log10(x) assert np.log10(test) == admath.log10(test) assert np.log10(test) == f.val assert 1 / (test * np.log(10)) == f.der
def test_log_fn__(): tests = np.linspace(1, 100, 15) for test in tests: x = Dual(test) f = admath.log(x) assert np.log(test) == admath.log(test) assert np.log(test) == f.val assert 1 / test == f.der
def test_power1(): x = Dual(2) f = admath.power(x, x) assert f.der == 2 * (2 + 2 * np.log(2)) assert f.val == 4.0 assert admath.power(2, 2) == 4.0 f = admath.power(x, 2) assert f.val == 4 assert f.der == 4 f = admath.power(2, x) assert f.val == 4.0 assert f.der == np.log(2) * 2**2
def test_arctan(): x = Dual(0) f = admath.arctan(x) assert 1 == f.der assert 0 == f.val assert 0 == admath.arctan(0)
def test1__sin_fn__(): x = Dual(0) f = admath.sin(x) assert f.val == 0 assert f.der == 1
def test1__tan_fn__(): x = Dual(0) f = admath.tan(x) assert f.val == 0 assert f.der == 1 assert admath.tan(0) == 0
def test1__cos_fn__(): x = Dual(0) f = admath.cos(x) assert f.val == np.cos(0) assert f.der == -1 * np.sin(0)
def test_arccos(): x = Dual(0) f = admath.arccos(x) assert -1 == f.der assert np.pi / 2 == f.val assert np.pi / 2 == admath.arccos(0)
def test_abs(): x = Dual(-1) f = admath.abs(x**3) assert -1 == f.der assert 1 == f.val assert 1 == admath.abs(-1)
def test_sum(): x = Dual(1) f = admath.sum([x**2, x**3]) assert 5 == f.der assert 2 == f.val assert 5 == admath.sum([2, 3])
def test_logistic(): x = Dual(1) f = admath.logistic(x) assert 0.25 == f.der assert 0.50 == f.val assert 0.50 == admath.logistic(1)
def test_tanh(): x = Dual(0) f = admath.tanh(x) assert 1 == f.der assert 0 == f.val assert 0 == admath.tanh(0)
def test_cosh(): x = Dual(0) f = admath.cosh(x) assert 0 == f.der assert 1 == f.val assert 1 == admath.cosh(0)