コード例 #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_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)
コード例 #3
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))
コード例 #4
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"
コード例 #5
0
    def test_sin(self):
        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)

        with pytest.raises(AttributeError):
            x = "!"
            f = el.sin(x)
コード例 #6
0
    def test_add(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 = 10
        g = el.sin(x)
        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)
コード例 #7
0
    def test_pow(self):
        x = AD(2)
        f = x ** 4
        assert (f.val, f.der[-1]) == (16, 32)

        x = AD(2)
        f = 3 ** x
        assert (np.round(f.val, 8), np.round(f.der[-1], 6)) == (9, np.round(9 * np.log(3), 6))

        x = AD(2)
        f = el.power(x, 2)
        assert (f.val, f.der) == (4, 4)

        x = AD(2)
        f = x ** x
        assert (f.val, f.der[-1]) == (4, 4 * np.log(2) + 4)

        x = AD(2)
        f = (el.power(x, 2)) ** x
        assert (f.val, f.der) == (16, 16 * np.log(4) + 32)

        f = (el.power(x, 2)) ** 3
        assert (f.val, f.der) == (64, 192)

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

        f = (2 ** x) ** x
        assert (f.val, f.der) == (16, 16 * np.log(16))

        f = x ** (2 ** x)
        assert (f.val, f.der) == (16, 32 + 64 * (np.log(2) ** 2))

        x = AD(0)
        f = el.sin(x)
        g = x

        with pytest.raises(ValueError):
            h = f ** g