def test_logsqrt(): x = ad.AutoDiff(2) func = ad.sqrt(ad.log(x)) assert (func.val, func.der, func.Jacobian()) == (np.sqrt(np.log(2)), .25 * (1 / (np.sqrt(np.log(2)))), .25 * (1 / (np.sqrt(np.log(2)))))
def test_trig2(): x = ad.AutoDiff(2) func = ad.sin(x) + 2 * ad.cosh(x) + 3 * ad.atan(x**2) assert (func.val, func.der, func.Jacobian()) == (np.sin(2) + 2 * np.cosh(2) + 3 * np.arctan(4), np.cos(2) + 2 * np.sinh(2) + 12 / 17, np.cos(2) + 2 * np.sinh(2) + 12 / 17)
def test_logistic(): x = ad.AutoDiff(2) func = ad.logistic(x) assert (func.val, func.der, func.Jacobian()) == (np.exp(2) / (np.exp(2) + 1), np.exp(2) / ((np.exp(2) + 1)**2), np.exp(2) / ((np.exp(2) + 1)**2))
def test_trig3(): x = ad.AutoDiff(1) func = ad.tan(x) - ad.asin((x / 2)) + ad.sinh((6 / x)) assert (func.val, func.der, func.Jacobian()) == (np.tan(1) - np.arcsin((1 / 2)) + np.sinh( (6 / 1)), 1 / (np.cos(1)**2) - .5 / np.sqrt(.75) - 6 * np.cosh(6), 1 / (np.cos(1)**2) - .5 / np.sqrt(.75) - 6 * np.cosh(6))
def test_trig4(): x = ad.AutoDiff(.5) func = ad.acos(x) * ad.tanh(x) assert np.isclose(func.val, np.arccos(.5) * np.tanh(.5)) assert np.isclose( func.der, np.arccos(.5) / (np.cosh(.5)**2) - np.tanh(.5) / (np.sqrt(.75))) assert np.isclose( func.Jacobian(), np.arccos(.5) / (np.cosh(.5)**2) - np.tanh(.5) / (np.sqrt(.75)))
def __init__(self, *args, **kwargs): """Instantiates multiple AutoDiff objects INPUTS ======= User defined variables and assigned values (for example, you might input all of the variables in a multivariate function) RETURNS ======== Returns len(kwargs) number of AutoDiff class objects with an associated derivative (seed) as an np.array NOTE: ======== This class acts as a helper function to allow the user to create multiple AutoDiff objects conveniently (instead of manually creating each AutoDiff object) These multiple AutoDiff objects may be useful in the evaluation of a multivariate function EXAMPLES ========= >>> X,Y = Multi_AutoDiff_Creator(X = 2, Y = 4).Vars >>> X.val 2 >>> X.der array([1., 0.]) >>> Y.val 4 >>> Y.der array([0., 1.]) """ deri = np.identity(len(kwargs)) i = 0 self.Vars = [] for key, value in kwargs.items(): self.Vars.append(ad.AutoDiff(value, deri[i])) i += 1
def __init__(self, Vals): """Instantiates multiple AutoDiff objects (for use in multivariate functions) INPUTS ======= User defined variables and assigned values (for example, you might input all of the variables in a multivariate function) RETURNS ======== Returns len(kwargs) number of AutoDiff class objects with an associated derivative (seed) as an np.array NOTE: ======== This class acts as a helper function to create multiple AutoDiff objects conveniently These multiple AutoDiff objects may be useful in the evaluation of a multivariate function """ deri = np.identity(len(Vals)) self.Vars = [] for i in range(len(Vals)): self.Vars.append(ad.AutoDiff(Vals[i], deri[i]))
def test_exp(): x = ad.AutoDiff(3) func = ad.exp(x) assert (func.val, func.der, func.Jacobian()) == (np.exp(3), np.exp(3), np.exp(3))
def test_revpower(): x = ad.AutoDiff(2) func = 2 - 2**x assert (func.val, func.der, func.Jacobian()) == (-2, -4 * np.log(2), -4 * np.log(2))
def test_trig1(): x = ad.AutoDiff(4) func = ad.cos(x) - 1 assert (func.val, func.der, func.Jacobian()) == (np.cos(4) - 1, -np.sin(4), -np.sin(4))
def test_powers(): x = ad.AutoDiff(2) func = x**x assert (func.val, func.der, func.Jacobian()) == (4, 4 * (np.log(2) + 1), 4 * (np.log(2) + 1))
def test_log_base10(): x = ad.AutoDiff(2) func = ad.log(x, 10) assert np.isclose(func.val, np.log10(2)) assert np.isclose(func.der, 1 / (np.log(10) * 2)) assert np.isclose(func.Jacobian(), 1 / (np.log(10) * 2))
def test_jacobian(): x = ad.AutoDiff(5, np.array([1, 0])) y = ad.AutoDiff(3, np.array([0, 1])) func = 5 * x + 3 * y assert np.all(func.Jacobian() == [5, 3])
def test_submulti(): x = ad.AutoDiff(3) func = 1 + x * 5 - 2 * x assert (func.val, func.der, func.Jacobian()) == (10, 3, 3)
def test_addnegexp(): x = ad.AutoDiff(5) func = -x + 2 * x**2 + 2 assert (func.val, func.der, func.Jacobian()) == (47, 19, 19)
def test_ne(): x = ad.AutoDiff(20, 1) func1 = x / 2 - 25 func2 = x / 3 - 25 assert (func1.val, func1.der, func1.Jacobian()) != (func2.val, func2.der, func2.Jacobian())
def test_eq(): x = ad.AutoDiff(20, 1) func1 = x / 2 - 25 func2 = func1 assert (func1.val, func1.der, func1.Jacobian()) == (func2.val, func2.der, func2.Jacobian())
def test_str(): x = ad.AutoDiff(5) func = 1 / x assert str(func) == "f(x) = 0.20, f'(x) = -0.04"