コード例 #1
0
ファイル: test_ops.py プロジェクト: psychon7/dit
class TestLinear(object):
    def setup_class(self):
        self.ops = LinearOperations()

    def test_add(self):
        X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1])
        Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1])
        Z = np.array([0, 1, 1, -1, -1, 0, 0, 2, -2])
        for x, y, z in zip(X, Y, Z):
            assert self.ops.add(x, y) == pytest.approx(z)
        assert np.allclose(self.ops.add(X, Y), Z)

    def test_add_inplace(self):
        X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1])
        Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1])
        Z = np.array([0, 1, 1, -1, -1, 0, 0, 2, -2])
        self.ops.add_inplace(X, Y)
        assert np.allclose(X, Z)

    def test_add_reduce(self):
        X = np.array([[0, 0, 0], [0, 1, 2], [1, 1, 1], [-1, 0, 1], [2, 0, -2],
                      [-1, -1, -1]])
        Y = np.array([0, 3, 3, 0, 0, -3])
        for x, y in zip(X, Y):
            assert self.ops.add_reduce(x) == pytest.approx(y)

    def test_mult(self):
        X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1, 2, 2, 2])
        Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1, 1, 2, -2])
        Z = np.array([0, 0, 0, 0, 0, -1, -1, 1, 1, 2, 4, -4])
        for x, y, z in zip(X, Y, Z):
            assert self.ops.mult(x, y) == pytest.approx(z)
        assert np.allclose(self.ops.mult(X, Y), Z)

    def test_mult_inplace(self):
        X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1, 2, 2, 2])
        Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1, 1, 2, -2])
        Z = np.array([0, 0, 0, 0, 0, -1, -1, 1, 1, 2, 4, -4])
        self.ops.mult_inplace(X, Y)
        assert np.allclose(X, Z)

    def test_invert(self):
        X = np.array([1, 2, -1, 10], dtype=float)
        Y = np.array([1, 1 / 2, -1, 1 / 10])
        for x, y in zip(X, Y):
            assert self.ops.invert(x) == pytest.approx(y)

    def test_mult_reduce(self):
        prods = [1, 2, 6, 24, 120]
        for i, p in enumerate(prods):
            assert self.ops.mult_reduce(np.arange(1,
                                                  i + 2)) == pytest.approx(p)

    def test_normalize(self):
        X = np.ones(3)
        Y = self.ops.normalize(X)
        Y_ = X / 3
        assert np.allclose(Y, Y_)
コード例 #2
0
ファイル: test_ops.py プロジェクト: chebee7i/dit
class TestLinear(object):
    def setUp(self):
        self.ops = LinearOperations()

    def test_add(self):
        X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1])
        Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1])
        Z = np.array([0, 1, 1, -1, -1, 0, 0, 2, -2])
        for x, y, z in zip(X, Y, Z):
            assert_almost_equal(self.ops.add(x, y), z)
        npt.assert_allclose(self.ops.add(X, Y), Z)

    def test_add_inplace(self):
        X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1])
        Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1])
        Z = np.array([0, 1, 1, -1, -1, 0, 0, 2, -2])
        self.ops.add_inplace(X, Y)
        npt.assert_allclose(X, Z)

    def test_add_reduce(self):
        X = np.array([[0, 0, 0],
                      [0, 1, 2],
                      [1, 1, 1],
                      [-1, 0, 1],
                      [2, 0, -2],
                      [-1, -1, -1]])
        Y = np.array([0, 3, 3, 0, 0, -3])
        for x, y in zip(X, Y):
            assert_almost_equal(self.ops.add_reduce(x), y)

    def test_mult(self):
        X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1, 2, 2, 2])
        Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1, 1, 2, -2])
        Z = np.array([0, 0, 0, 0, 0, -1, -1, 1, 1, 2, 4, -4])
        for x, y, z in zip(X, Y, Z):
            assert_almost_equal(self.ops.mult(x, y), z)
        npt.assert_allclose(self.ops.mult(X, Y), Z)

    def test_mult_inplace(self):
        X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1, 2, 2, 2])
        Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1, 1, 2, -2])
        Z = np.array([0, 0, 0, 0, 0, -1, -1, 1, 1, 2, 4, -4])
        self.ops.mult_inplace(X, Y)
        npt.assert_allclose(X, Z)

    def test_invert(self):
        X = np.array([1, 2, -1, 10], dtype=float)
        Y = np.array([1, 1/2, -1, 1/10])
        for x, y in zip(X, Y):
            assert_almost_equal(self.ops.invert(x), y)

    def test_mult_reduce(self):
        prods = [1, 2, 6, 24, 120]
        for i, p in enumerate(prods):
            assert_almost_equal(self.ops.mult_reduce(np.arange(1, i+2)), p)

    def test_normalize(self):
        X = np.ones(3)
        Y = self.ops.normalize(X)
        Y_ = X / 3
        npt.assert_allclose(Y, Y_)