Пример #1
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"
    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))
Пример #3
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)
Пример #4
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)
Пример #5
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
Пример #6
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)
 def test_power(self):
     x = AD(4, N=3)
     f = el.power(x, 4)
     assert (f.val, f.der[-1]) == (256, 96)