def slope(self, x): if self.option == 0: return self.gradient(x) if self.option == 1: # implement AD here obj = AD.AutoDiff(x) return self.curve(obj).dx if self.option == 2: # implement numerical method here h = 1e-2 return (self.curve(x + h) - self.curve(x)) / h
def test_log_invalid_base(): with pytest.raises(ValueError): AD.log(x1, base='a')
def test_cos_log10_rpow(): f = AD.cos(AD.log(2**x1, base=10)) assert tol > abs(f.x - 0.82417070595) assert tol > abs(f.dx - -0.17048576675265)
def test_tan_exp_div_neg(): f = -AD.tan(x1) / AD.exp(x1) assert tol > abs(f.x - 0.29571298878) assert tol > abs(f.dx - -1.077192940578)
def test_sin_pow_log(): f = AD.sin(x1)**2 * AD.log(x1) assert tol > abs(f.x - 0.573109206726) assert tol > abs(f.dx - -0.111164610647518727)
def test_logistic(): f = AD.logistic(x1) assert f.x == 1 / (1 + np.exp(-2)) assert f.dx == np.exp(-2) / (1 + np.exp(-2))**2 f = AD.logistic(2.0) assert f == 1 / (1 + np.exp(-2.0))
def test_exp(): f = AD.exp(x1) assert f.x == np.exp(2) assert f.dx == np.exp(2) f = AD.exp(2) assert f == np.exp(2)
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation import modules.rollingball as RB import AutoDiff_CKMZ.modules.AutoDiff as AD from time import time function_list = [lambda x: AD.sin(x), lambda x: AD.exp(x), lambda x: x * AD.sin(x), lambda x: - AD.log(x), lambda x: AD.sinh(x), lambda x: AD.exp(AD.cos(x)), lambda x: 2 ** x, lambda x: 1 / AD.tan(x), lambda x: AD.sin(x) / x, lambda x: x ** 2] derivative_list = [lambda x: np.cos(x), lambda x: np.exp(x), lambda x: np.sin(x) + x * np.cos(x), lambda x: -1 / x, lambda x: np.cosh(x), lambda x: - np.exp(np.cos(x)) * np.sin(x), lambda x: 2 ** x * np.log(2), lambda x: - 1 / np.sin(x) ** 2, lambda x: (x * np.cos(x) - np.sin(x)) / x ** 2, lambda x: 2 * x] class function():
def test_arcsin(): f = AD.arcsin(AD.AutoDiff(0.5)) assert f.x == np.arcsin(0.5) assert f.dx == 1 / np.sqrt(1 - 0.5**2) f = AD.arcsin(0.5) assert f == np.arcsin(0.5)
def test_arccos(): f = AD.arccos(AD.AutoDiff(0.5)) assert f.x == np.arccos(0.5) assert f.dx == -1 / np.sqrt(1 - 0.5**2) f = AD.arccos(0.5) assert f == np.arccos(0.5)
def test_arctan(): f = AD.arctan(x1) assert f.x == np.arctan(2) assert f.dx == 1 / (1 + 2**2) f = AD.arctan(0.5) assert f == np.arctan(0.5)
def test_tan(): f = AD.tan(x1) assert f.x == np.tan(2) assert f.dx == 1 / np.cos(2)**2 f = AD.tan(0.5) assert f == np.tan(0.5)
def test_cos(): f = AD.cos(x1) assert f.x == np.cos(2) assert f.dx == -np.sin(2) f = AD.cos(0.2) assert f == np.cos(0.2)
def test_sin(): f = AD.sin(x1) assert f.x == np.sin(2) assert f.dx == np.cos(2) f = AD.sin(1) assert f == np.sin(1)
def test_log(): f = AD.log(x1) assert f.x == np.log(2) assert f.dx == 1 / 2 f = AD.log(2) assert f == np.log(2)
def test_multivariate(): f = x3[0] * AD.exp(3 * x3[1]) assert f.dx[0] == 1 assert f.dx[1] == 6
def test_eq(): assert x1 == AD.AutoDiff(2.0) assert AD.AutoDiff(2.0, 0) == 2.0
def test_tanh(): f = AD.tanh(x1) assert f.x == np.tanh(2) assert f.dx == 1 / (np.cosh(2)**2) f = AD.tanh(2) assert f == np.tanh(2)
def test_sinh(): f = AD.sinh(x1) assert f.x == np.sinh(2) assert f.dx == np.cosh(2) f = AD.sinh(0.5) assert f == np.sinh(0.5)
import pytest import numpy as np #from AutoDiff_CKMZ.modules.AutoDiff import AutoDiff import AutoDiff_CKMZ.modules.AutoDiff as AD x1 = AD.AutoDiff(2.0) x2 = AD.AutoDiff(3.0) tol = 1e-8 def test_add(): f = x1 + 3 assert f.x == 5 assert f.dx == 1 def test_radd(): f = 1 + x1 assert f.x == 3 assert f.dx == 1 def test_sub(): f = x1 - 3 assert f.x == -1 assert f.dx == 1 def test_rsub(): f = 3 - x1