def test_string_values(): with pytest.raises(TypeError): AutoDiff("a", 2) with pytest.raises(TypeError): AutoDiff(2, "a") with pytest.raises(TypeError): AutoDiff([2, "a"], 3) with pytest.raises(TypeError): AutoDiff(2, ["a", 3])
def test_divide_objects(): AD1 = AutoDiff(15, 45) AD2 = AutoDiff(30, 90) AD3 = AD1 / AD2 assert AD3.val == 0.5 assert AD3.der == (45 * 30 - 15 * 90) / (30**2) AD4 = AD2.__rtruediv__(AD1) assert AD4.val == 0.5 assert AD4.der == (45 * 30 - 15 * 90) / (30**2)
def test_subtract_objects(): AD1 = AutoDiff(5, 10) AD2 = AutoDiff(3, 4) AD3 = AD1 - AD2 assert AD3.val == 2 assert AD3.der == 6 AD4 = AD2 - AD1 assert AD4.val == -2 assert AD4.der == -6
def test_gt(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff(3, 3) assert AD1.__gt__(2) == False assert AD1.__gt__(0) == True assert AD1.__gt__(AD2) == False assert AD2.__gt__(AD1) == True
def test_le(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff(3, 3) assert AD1.__le__(2) == True assert AD1.__le__(4) == True assert AD1.__le__(AD2) == True assert AD2.__le__(AD1) == False
def test_ne(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff(3, 3) AD3 = AutoDiff(2, -4) assert AD1.__ne__(AD2) == True assert AD1.__ne__(AD3) == False assert AD1.__ne__(2) == False assert AD1.__ne__(3) == True
def test_subtract_constant(): AD1 = AutoDiff(5, 10) AD2 = AD1 - 3 assert AD2.val == 2 assert AD2.der == 10
def test_vector_values2(): AD1 = AutoDiff(2, [3, 5]) AD2 = AD1 + 2 assert AD2.val == 4 assert all(AD2.der == [3, 5])
def test_vector_values3(): AD1 = AutoDiff(2, [3, 5]) AD2 = AutoDiff.cos(AD1) assert AD2.val == np.cos(2) assert all(AD2.der == [-3 * np.sin(2), -5 * np.sin(2)])
def test_tanh(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff.tanh(AD1) assert AD2.val == np.tanh(2) assert AD2.der == 3 * 1 / (np.cosh(2)**2)
def test_vector_values1(): AD1 = AutoDiff(2, [3, 5]) AD2 = 2 * AD1 assert AD2.val == 4 assert all(AD2.der == [6, 10])
def test_cosh(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff.cosh(AD1) assert AD2.val == np.cosh(2) assert AD2.der == 3 * np.sinh(2)
def test_log(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff.log(AD1, 5) assert AD2.val == np.log(2) / np.log(5) assert AD2.der == (1 / (2 * np.log(5))) * 3
def test_rsub(): AD1 = AutoDiff(5, 10) AD2 = 8 - AD1 assert AD2.val == 3 assert AD2.der == -10
def test_multiple_constant(): AD1 = AutoDiff(5, 15) AD2 = AD1 * 2 assert AD2.val == 10 assert AD2.der == 30
def test_str(): AD1 = AutoDiff(2, 3) assert AutoDiff.__str__(AD1) == "AutoDiff(2,3)"
def test_repr(): AD1 = AutoDiff(2, 3) assert AutoDiff.__repr__(AD1) == "AutoDiff(2,3)"
def test_sqrt(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff.sqrt(AD1) assert AD2.val == 2**(1 / 2) assert AD2.der == 3 * (1 / 2) * (2**(-1 / 2))
def test_logistic(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff.logistic(AD1) assert AD2.val == 1 / (1 + np.exp(-2)) assert AD2.der == 3 * np.exp(-2) / (1 + np.exp(-2))**2
def test_radd(): AD1 = AutoDiff(1, 2) AD2 = 4 + AD1 assert AD2.val == 5 assert AD2.der == 2
def test_expm(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff.expm(AD1, 10) assert AD2.val == 10**2 assert AD2.der == 3 * (10**2) * np.log(10)
def test_exp(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff.exp(AD1) assert AD2.val == np.exp(2) assert AD2.der == 3 * np.exp(2)
def test_multiply_objects(): AD1 = AutoDiff(5, 15) AD2 = AutoDiff(2, 3) AD3 = AD1 * AD2 assert AD3.val == 10 assert AD3.der == 45
def test_divide_constant(): AD1 = AutoDiff(15, 45) AD3 = AD1 / 3 assert AD3.val == 5 assert AD3.der == (1 / 3) * (45)
def test_rmul(): AD1 = AutoDiff(5, 15) AD2 = 2 * AD1 assert AD2.val == 10 assert AD2.der == 30
def test_arctangent(): AD1 = AutoDiff(0.5, 3) AD2 = AutoDiff.arctangent(AD1) assert AD2.val == np.arctan(0.5) assert AD2.der == 3 / (0.5**2 + 1)
def test_add_objects(): AD1 = AutoDiff(1, 2) AD2 = AutoDiff(3, 4) AD3 = AD1 + AD2 assert AD3.val == 4 assert AD3.der == 6
def test_ln(): AD1 = AutoDiff(2, 3) AD2 = AutoDiff.ln(AD1) assert AD2.val == np.log(2) assert AD2.der == (1 / 2) * 3
def test_rtruediv(): AD1 = AutoDiff(3, 5) AD3 = 15 / AD1 assert AD3.val == 5 assert AD3.der == (-15 * 5) / (3**2)
def test_arccosine(): AD1 = AutoDiff(0.5, 3) AD2 = AutoDiff.arccosine(AD1) assert AD2.val == np.arccos(0.5) assert AD2.der == -3 / ((1 - 0.5**2)**(1 / 2))