コード例 #1
0
    def test_truediv(self):
        x = AD(4)
        f = el.log(x, 2) / 3 ** x
        assert (np.round(f.val, 6), np.round(f.der[-1], 6)) == (
        np.round(2 / 81, 6), np.round((81 / (4 * np.log(2)) - 162 * np.log(3)) / 3 ** 8, 6))

        f = el.sin(x) / 4
        assert (f.val, f.der) == ((np.sin(4)) / 4, (np.cos(4)) / 4)

        with pytest.raises(ZeroDivisionError):
            f = el.cos(x) / el.sin(0)

        with pytest.raises(ZeroDivisionError):
            f = el.cos(x) / 0

        f = 3 ** x / el.log(x, 2)
        assert (np.round(f.val, 6), np.round(f.der, 6)) == (np.round(81 / 2, 6),
                                                            np.round((162 * np.log(3) - 81 / (4 * np.log(2))) / (
                                                                        np.log(4) / np.log(2)) ** 2, 6))

        with pytest.raises(AttributeError):
            f = el.cos(x) / "el.sin(0)"

        x = AD(0)
        f = el.sin(x) / el.cos(x)
        assert (f.val, f.der) == (0, 1)

        with pytest.raises(ZeroDivisionError):
            f = el.cos(x) / el.sin(x)
コード例 #2
0
    def test_sin(self):
        # N = 1
        x = AD(0)
        f = el.sin(x)
        assert (f.val, f.der) == (0.0, 1.0)

        x = 13
        f = el.sin(x)
        assert (f.val, f.der) == (np.sin(13), 0)

        x = 13.0
        f = el.sin(x)
        assert (f.val, f.der) == (np.sin(13), 0)

        # N > 1
        x = AD(2, N=4)
        f = el.sin(x)
        # print(f)
        assert (f.val, np.round(f.der[-1], 6)) == (np.sin(2), np.round(np.sin(2), 6))

        x = AD(2, N=3)
        f = el.sin(x)
        # print(f)
        assert (f.val, np.round(f.der[-1], 6)) == (np.sin(2), np.round(-np.cos(2), 6))

        f = el.sin(x) + 3
        assert (f.val, np.round(f.der[-1], 6)) == (np.sin(2) + 3, np.round(-np.cos(2), 6))

        f = el.sin(x) - 2
        assert (f.val, np.round(f.der[-1], 6)) == (np.sin(2) - 2, np.round(-np.cos(2), 6))

        f = el.sin(x) * 4
        assert (f.val, np.round(f.der[-1], 6)) == (4 * np.sin(2), np.round(-4 * np.cos(2), 6))
コード例 #3
0
    def test_sqrt(self):
        x = AD(4)
        f = el.sqrt(x)
        assert (f.val, f.der) == (2, 0.5 * 4 ** (-0.5))

        x = 4
        f = el.sqrt(x)
        assert (f.val, f.der) == (2, 0)

        x = 16.0
        f = el.sqrt(x)
        assert (f.val, f.der) == (4, 0)

        x = AD(-5)
        with pytest.raises(ValueError):
            f = el.sqrt(x)

        x = -5
        with pytest.raises(ValueError):
            f = el.sqrt(x)

        with pytest.raises(AttributeError):
            x = "World"
            f = el.sqrt(x)

        x = AD(4, N=3)
        f = el.sqrt(x)
        assert (f.val, f.der[-1]) == (2, (3 / 8) * 4 ** (-2.5))
コード例 #4
0
    def test_exp(self):
        # N = 1
        f = el.exp(1)
        assert (f.val, f.der) == (np.exp(1), 0)

        x = AD(1)
        f = el.exp(x)
        assert (f.val, f.der) == (np.exp(1), np.exp(1))

        x = AD(2)
        f = el.power(x, 3)
        g = el.exp(f)
        print(g)
        assert (round(g.val, 6), round(g.der[0], 6)) == (round(np.exp(1) ** 8, 6), round(12 * np.exp(1) ** 8, 6))

        with pytest.raises(AttributeError):
            x = "hello"
            f = el.exp(x)

        # N > 1
        for N in range(2, 10):
            x = AD(3, N=N)
            f = el.exp(x)
            print("N=%d" % N, f)

        x = AD(2, N=5)
        f = el.exp(x)
        assert (f.val, f.der[-1]) == (np.exp(2), np.exp(2))
コード例 #5
0
    def test_log(self):
        x = AD(12)
        f = el.log(x, 10)
        assert (f.val, f.der) == (np.log(12) / np.log(10), 1 / (12 * np.log(10)))

        x = 8
        f = el.log(x, 2)
        assert (np.round(f.val, 6), f.der) == (3, 0)

        x = 9.0
        f = el.log(x, 3)
        assert (f.val, f.der) == (2, 0)

        x = AD(0)
        with pytest.raises(ValueError):
            f = el.log(x, 2)

        x = 0
        with pytest.raises(ValueError):
            f = el.log(x, 2)

        with pytest.raises(AttributeError):
            x = "s"
            f = el.log(x, 3)

        with pytest.raises(ValueError):
            x = -3.9
            f = el.log(x, 3)
コード例 #6
0
    def test_arccos(self):
        x = AD(0.5)
        f = el.arccos(x)
        assert (f.val, f.der) == (np.arccos(0.5), -1 / np.sqrt(1 - 0.5 ** 2))

        x = 0.5
        f = el.arccos(x)
        assert (f.val, f.der) == (np.arccos(0.5), 0)

        x = -5
        with pytest.raises(ValueError):
            f = el.arccos(x)

        x = 10.0
        with pytest.raises(ValueError):
            f = el.arccos(x)

        x = AD(-5.0)
        with pytest.raises(ValueError):
            f = el.arccos(x)

        x = AD(10)
        with pytest.raises(ValueError):
            f = el.arccos(x)

        with pytest.raises(AttributeError):
            x = "--"
            f = el.arccos(x)
コード例 #7
0
    def test_arcsin(self):
        x = AD(0.5)
        f = el.arcsin(x)
        assert (f.val, f.der) == (np.arcsin(0.5), 1 / np.sqrt(1 - 0.5 ** 2))

        x = 0
        f = el.arcsin(x)
        assert (f.val, f.der) == (np.arcsin(0), 0)

        x = 1.0
        f = el.arcsin(x)
        assert (f.val, f.der) == (np.arcsin(1), 0)

        x = -5
        with pytest.raises(ValueError):
            f = el.arcsin(x)

        x = 10.0
        with pytest.raises(ValueError):
            f = el.arcsin(x)

        x = AD(-5.0)
        with pytest.raises(ValueError):
            f = el.arcsin(x)

        x = AD(10)
        with pytest.raises(ValueError):
            f = el.arcsin(x)

        with pytest.raises(AttributeError):
            x = "."
            f = el.arcsin(x)
コード例 #8
0
    def test_compare(self):
        n = 20
        x = AD(20)
        y = 100
        z = AD(100)

        assert (n < z.val) == n.__lt__(z.val)
        assert (x.val < y) == x.val.__lt__(y)

        assert (n <= z.val) == n.__le__(z.val)
        assert (x.val <= y) == x.val.__le__(y)
        assert (y <= z.val) == y.__le__(z.val)

        assert (y > x.val) == y.__gt__(x.val)
        assert (z.val > n) == z.val.__gt__(n)

        assert (y >= x.val) == y.__ge__(x.val)
        assert (z.val >= n) == z.val.__ge__(n)
        assert (n >= y) == n.__ge__(y)

        assert (y == z.val) == y.__eq__(z.val)
        assert (z.val == y) == z.val.__eq__(y)

        assert (y != x.val) == y.__ne__(x.val)
        assert (x.val != y) == x.val.__ne__(y)

        assert ("val = 20; der = [1.]") == x.__str__()
コード例 #9
0
    def test_inv(self):
        x = 10
        assert el.inv(x) == 0.1

        y = -1 / 30
        assert el.inv(y) == -30

        with pytest.raises(ZeroDivisionError):
            x = 0
            f = el.inv(x)

        with pytest.raises(AttributeError):
            x = "zero"
            f = el.inv(x)

        x = AD(8)
        f = el.inv(x)
        assert (f.val, f.der) == (1 / 8, -1 / 64)

        x = 92
        f = el.inv(x)
        assert (f.val, f.der) == (1 / 92, 0)

        x = AD(10, N=3)
        f = el.inv(x)
        assert (f.val, f.der[-1]) == (0.1, -6 * 1 / 10000)
コード例 #10
0
    def test_rtruediv(self):
        x = AD(4, N=2)
        f = 3 / (2 * x)
        assert (f.val, f.der[-1]) == (3 / 8, 3 / 64)

        x = AD(4, N=3)
        f = 3 / (2 * x)  # shows rtruediv should work
        assert (f.val, f.der[-1]) == (3 / 8, -9 / 4 ** 4)
コード例 #11
0
    def test_cosh(self):
        # N > 1
        x = AD(10, N=4)
        f = el.cosh(x)
        assert (f.val, f.der[-1]) == (np.cosh(10), np.cosh(10))

        x = AD(10, N=7)
        f = el.cosh(x)
        assert (f.val, f.der[-1]) == (np.cosh(10), np.sinh(10))
コード例 #12
0
    def test_truediv(self):
        # N = 1 tests all passed in solvers_test
        x = AD(3)
        f = 5 * x / 3
        assert (f.val, f.der[-1]) == (5, 5 / 3)

        x = AD(4, N=2)
        f = x / x
        assert (f.val, f.der[-1]) == (1, 0)
コード例 #13
0
    def test_tan(self):
        # N > 1
        x = AD(10, N=2)
        f = el.tan(x)
        assert (f.val, np.round(f.der[-1], 6)) == (np.tan(10), np.round(2 * np.tan(10) * (1 / (np.cos(10) ** 2)), 6))

        x = AD(10, N=3)
        f = el.tan(x)
        assert (f.val, np.round(f.der[-1], 6)) == (
        np.tan(10), np.round(-2 * (-2 + np.cos(20)) * (1 / (np.cos(10) ** 4)), 6))
コード例 #14
0
    def test_ln(self):
        x = AD(-10)
        with pytest.raises(ValueError):
            f = el.ln(x)

        x = AD(10, N=3)
        f = el.ln(x)
        assert (f.val, f.der[-1]) == (np.log(10), 2 * 1 / (1000))

        x = AD(10, N=4)
        f = el.ln(x)
        assert (f.val, f.der[-1]) == (np.log(10), -6 * 1 / (10000))
コード例 #15
0
    def test_tanh(self):
        # N > 1
        x = AD(0.5, N=2)
        f = el.tanh(x)
        assert (f.val, np.round(f.der[-1], 6)) == (np.tanh(0.5), -0.726862)

        x = AD(0.5, N=3)
        f = el.tanh(x)
        assert (f.val, np.round(f.der[-1], 6)) == (np.tanh(0.5), -0.565209)

        x = AD(0, N=3)
        f = el.tanh(x)
        assert (f.val, f.der[-1]) == (np.tanh(0), -2)
コード例 #16
0
    def test_power_k(self):
        with pytest.raises(AttributeError):
            x = AD(10)
            n = "1"
            f = el.power_k_order(x, n, 3)

        with pytest.raises(AttributeError):
            x = AD(10)
            n = AD(3)
            f = el.power_k_order(x, n, 3)

        with pytest.raises(ValueError):
            x = AD(-3)
            n = 1 / 2
            f = el.power_k_order(x, n, 3)
コード例 #17
0
    def test_sub(self):
        x = AD(5)
        f = el.power(x, 2) + -5 * x
        assert (f.val, f.der) == (0, 5)

        f = el.power(x, 2) - 50
        assert (f.val, f.der) == (-25, 10)

        f = - 50 + el.power(x, 2)
        assert (f.val, f.der) == (-25, 10)

        f = 50 - el.power(x, 2)
        assert (f.val, f.der) == (25, -10)

        f = -5 * x + el.power(x, 2)
        assert (f.val, f.der) == (0, 5)

        f = -x * 5 + el.power(x, 2)
        assert (f.val, f.der) == (0, 5)

        f = el.power(x, 2) - 5 * x
        assert (f.val, f.der) == (0, 5)

        f = x * 5 - el.power(x, 2)
        assert (f.val, f.der) == (0, -5)

        with pytest.raises(AttributeError):
            f = el.sin(x) - "5"
コード例 #18
0
    def test_add(self):
        x = AD(5)
        f = el.power(x, 2) + 5
        assert (f.val, f.der) == (30, 10)

        f = 5 + el.power(x, 2)
        assert (f.val, f.der) == (30, 10)

        f = el.power(x, 2) + 5 * x
        assert (f.val, f.der) == (50, 15)

        f = x * 5 + el.power(x, 2)
        assert (f.val, f.der) == (50, 15)

        with pytest.raises(AttributeError):
            f = el.power(x, 2) + "5"

        f = el.power(x, 2)
        g = 5
        h = f + g
        assert (h.val, h.der) == (30, 10)

        f = 5
        g = el.power(x, 2)
        h = f + g
        assert (h.val, h.der) == (30, 10)
コード例 #19
0
    def test_expn(self):
        x = AD(2)
        f = el.expn(x, 25)
        assert (f.val, f.der) == (625, 625 * np.log(25))

        x = 2
        f = el.expn(x, 25)
        assert (f.val, f.der) == (625, 0)

        with pytest.raises(AttributeError):
            f = el.expn('25', 0)

        with pytest.raises(ValueError):
            f = el.expn(x, -10)

        x = AD(4)
        f = el.expn(x, 2)
コード例 #20
0
    def test_exp(self):
        f = el.exp(1)
        assert (f.val, f.der) == (np.exp(1), 0)

        x = AD(1)
        f = el.exp(x)
        assert (f.val, f.der) == (np.exp(1), np.exp(1))

        x = AD(2)
        f = el.power(x, 3)
        g = el.exp(f)
        assert (np.round(g.val, 6), np.round(g.der, 6)) == (
        np.round(np.exp(1) ** 8, 6), np.round(12 * np.exp(1) ** 8, 6))

        with pytest.raises(AttributeError):
            x = "hello"
            f = el.exp(x)
コード例 #21
0
    def test_inv(self):
        # N = 1
        f = el.ln(np.e)
        assert (f.val, f.der) == (1, 0)

        x = AD(np.e)
        f = el.ln(x)
        assert (f.val, f.der) == (1, 1 / np.e)

        # N > 1
        # See explicit formula here: https://www.math24.net/higher-order-derivatives/#example2
        x = AD(np.e, N=2)
        f = el.ln(x)
        assert (f.val, f.der[-1]) == (1, -1 / (np.e ** 2))

        x = AD(np.e ** 2, N=5)
        f = el.ln(x)
        assert (f.val, f.der[-1]) == (2, 24 / ((np.e ** 2) ** 5))
コード例 #22
0
    def test_cos(self):
        # N > 1
        x = AD(10, N=5)
        f = el.cos(x)
        assert (f.val, np.round(f.der[-1], 6)) == (np.cos(10), np.round(-np.sin(10), 6))

        x = AD(2, N=2)
        f = el.cos(x)
        assert (f.val, np.round(f.der[-1], 6)) == (np.cos(2), np.round(-np.cos(2), 6))

        f = el.cos(x) + 3
        assert (f.val, np.round(f.der[-1], 6)) == (np.cos(2) + 3, np.round(-np.cos(2), 6))

        f = el.cos(x) - 2
        assert (f.val, np.round(f.der[-1], 6)) == (np.cos(2) - 2, np.round(-np.cos(2), 6))

        f = el.cos(x) * 4
        assert (f.val, np.round(f.der[-1], 6)) == (4 * np.cos(2), np.round(-4 * np.cos(2), 6))
コード例 #23
0
    def test_arctan(self):
        x = AD(0.5)
        f = el.arctan(x)
        assert (f.val, f.der) == (np.arctan(0.5), 1 / (1 + 0.5 ** 2))

        x = 0.5
        f = el.arctan(x)
        assert (f.val, f.der) == (np.arctan(0.5), 0)

        with pytest.raises(AttributeError):
            x = "AD(0.5)"
            f = el.arctan(x)
コード例 #24
0
    def test_sub(self):
        # N > 1
        x = AD(5, N=3)
        f = el.sin(x)
        g = 10
        h = f - g
        assert (h.val, np.round(h.der[-1]), 6) == (np.sin(5) - 10, np.round(-np.cos(5)), 6)

        f = el.sin(x)
        g = x ** 2
        h = f - g
        assert (h.val, np.round(h.der[-1]), 6) == (np.sin(5) - 25, np.round(-np.cos(5)), 6)
コード例 #25
0
    def test_mul(self):
        x = AD(4)
        f = el.log(x, 2) * 3 ** x
        assert (np.round(f.val, 6), np.round(f.der[-1], 6)) == (
        162, np.round(81 / (4 * np.log(2)) + 162 * np.log(3), 6))

        f = 3 ** x * el.log(x, 2)
        assert (np.round(f.val, 6), np.round(f.der[-1], 6)) == (
        162, np.round(81 / (4 * np.log(2)) + 162 * np.log(3), 6))

        with pytest.raises(AttributeError):
            f = x * "5"
コード例 #26
0
    def test_cosh(self):
        x = AD(0.5)
        f = el.cosh(x)
        assert (f.val, f.der) == (np.cosh(0.5), np.sinh(0.5))

        x = 0.5
        f = el.cosh(x)
        assert (f.val, f.der) == (np.cosh(0.5), 0)

        with pytest.raises(AttributeError):
            x = "0.5"
            f = el.cosh(x)
コード例 #27
0
    def test_tanh(self):
        x = AD(0.5)
        f = el.tanh(x)
        assert (f.val, f.der) == (np.tanh(0.5), 1 - np.tanh(0.5) ** 2)

        x = 0.5
        f = el.tanh(x)
        assert (f.val, f.der) == (np.tanh(0.5), 0)

        with pytest.raises(AttributeError):
            x = "0.5"
            f = el.tanh(x)
コード例 #28
0
    def test_logistic(self):
        x = AD(100)
        f = el.logistic(x, x0=0, L=1, k=1)
        assert (f.val, np.round(f.der[-1], 10)) == (1 / (1 + np.exp(-100)),
                                                    np.round(np.exp(100) / (1 + np.exp(100)) ** 2, 6))
        # assert (f.val, f.der[-1]) == (1/(1+np.exp(-100)), np.exp(100)/(1 + np.exp(100))**2)

        f = el.logistic(x, x0=10, L=2, k=3)
        assert (f.val, f.der[-1]) == (2 * np.exp(270) / (1 + np.exp(270)), (6 * np.exp(270)) / (1 + np.exp(270)) ** 2)

        x = AD(100, N=2)
        f = el.logistic(x, x0=0, L=1, k=1)
        assert (f.val, np.round(f.der[-1], 6)) == (
        1 / (1 + np.exp(-100)), np.round((np.exp(100) - np.exp(200)) / (1 + np.exp(100)) ** 3, 6))

        x = 100
        f = el.logistic(x, x0=0, L=1, k=1)
        assert (f.val, f.der) == (1 / (1 + np.exp(-100)), 0)

        with pytest.raises(AttributeError):
            x = "AD(-3)"
            f = el.logistic(x, x0=0, L=1, k=1)
コード例 #29
0
    def test_ln(self):
        # N = 1
        f = el.ln(np.e)
        assert (f.val, f.der) == (1, 0)

        x = AD(np.e)
        f = el.ln(x)
        assert (f.val, f.der) == (1, 1 / np.e)

        # N > 1
        # See explicit formula here: https://www.math24.net/higher-order-derivatives/#example2
        x = AD(np.e, N=2)
        f = el.ln(x)
        assert (f.val, f.der[-1]) == (1, -1 / (np.e ** 2))

        x = AD(np.e ** 2, N=5)
        f = el.ln(x)
        assert (f.val, f.der[-1]) == (2, 24 / ((np.e ** 2) ** 5))

        # composite x
        x = AD(np.e, N=4)
        f = el.ln(x * 3 + 1)
        assert (f.val, f.der[-1]) == (np.log(np.e * 3 + 1), (-6 * 3 ** 4) / ((np.e * 3 + 1) ** 4))
コード例 #30
0
    def test_power(self):
        x = AD(5)
        f = el.power(x, 2)
        assert (f.val, f.der) == (25, 10)

        x = 10
        f = el.power(x, 3)
        assert (f.val, f.der) == (1000, 0)

        x = 10.0
        f = el.power(x, 3)
        assert (f.val, f.der) == (1000, 0)

        with pytest.raises(AttributeError):
            x = "s"
            f = el.power(x, 3)