Example #1
0
    def testSVDExecution(self):
        data = np.random.randn(18, 6) + 1j * np.random.randn(18, 6)

        a = tensor(data, chunk_size=(9, 6))
        U, s, V = svd(a)
        t = U.dot(diag(s).dot(V))

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.allclose(res, data))

        a = tensor(data, chunk_size=(18, 6))
        U, s, V = svd(a)
        t = U.dot(diag(s).dot(V))

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.allclose(res, data))

        a = tensor(data, chunk_size=(2, 6))
        U, s, V = svd(a)
        t = U.dot(diag(s).dot(V))

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.allclose(res, data))

        data = np.random.randn(6, 18) + 1j * np.random.randn(6, 18)

        a = tensor(data)
        U, s, V = svd(a)
        t = U.dot(diag(s).dot(V))

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.allclose(res, data))
Example #2
0
    def testSVDExecution(self):
        data = np.random.randn(18, 6) + 1j * np.random.randn(18, 6)

        a = tensor(data, chunk_size=(9, 6))
        U, s, V = svd(a)
        t = U.dot(diag(s).dot(V))

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.allclose(res, data))

        a = tensor(data, chunk_size=(18, 6))
        U, s, V = svd(a)
        t = U.dot(diag(s).dot(V))

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.allclose(res, data))

        a = tensor(data, chunk_size=(2, 6))
        U, s, V = svd(a)
        t = U.dot(diag(s).dot(V))

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.allclose(res, data))

        data = np.random.randn(6, 18) + 1j * np.random.randn(6, 18)

        a = tensor(data)
        U, s, V = svd(a)
        t = U.dot(diag(s).dot(V))

        res = self.executor.execute_tensor(t, concat=True)[0]
        self.assertTrue(np.allclose(res, data))

        # test for matrix of ones
        data = np.ones((20, 10))

        a = tensor(data, chunk_size=10)
        s = svd(a)[1]
        res = self.executor.execute_tensor(s, concat=True)[0]
        expected = np.linalg.svd(a)[1]
        np.testing.assert_array_almost_equal(res, expected)
Example #3
0
    def testDiagExecution(self):
        # 2-d  6 * 6
        a = arange(36, chunk_size=2).reshape(6, 6)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-5)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=-5)
        np.testing.assert_equal(res, expected)

        # 2-d  6 * 6 sparse, no tensor
        a = sps.rand(6, 6, density=.1)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray())
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=1)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=3)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=-2)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-5)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(a.toarray(), k=-5)
        np.testing.assert_equal(res.toarray(), expected)

        # 2-d  6 * 6 sparse, from tensor
        raw_a = sps.rand(6, 6, density=.1)
        a = tensor(raw_a, chunk_size=2)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray())
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=1)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=3)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=-2)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-5)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(raw_a.toarray(), k=-5)
        np.testing.assert_equal(res.toarray(), expected)

        # 2-d  4 * 9
        a = arange(36, chunk_size=2).reshape(4, 9)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=-3)
        np.testing.assert_equal(res, expected)

        # 2-d  4 * 9 sparse, no tensor
        a = sps.rand(4, 9, density=.1)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray())
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=1)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=3)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(a.toarray(), k=-2)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(a.toarray(), k=-3)
        np.testing.assert_equal(res.toarray(), expected)

        # 2-d  4 * 9 sparse, from tensor
        raw_a = sps.rand(4, 9, density=.1)
        a = tensor(raw_a, chunk_size=2)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray())
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=1)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=3)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(raw_a.toarray(), k=-2)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(raw_a.toarray(), k=-3)
        np.testing.assert_equal(res.toarray(), expected)

        # 1-d
        a = arange(5, chunk_size=2)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-3)
        np.testing.assert_equal(res, expected)

        d = diag(a, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5))
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=2, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-3, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-3)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)
Example #4
0
    def testDiagExecution(self):
        # 2-d  6 * 6
        a = arange(36, chunks=2).reshape(6, 6)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-5)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(np.arange(36).reshape(6, 6), k=-5)
        np.testing.assert_equal(res, expected)

        # 2-d  4 * 9
        a = arange(36, chunks=2).reshape(4, 9)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d)[0]
        expected = np.diag(np.arange(36).reshape(4, 9), k=-3)
        np.testing.assert_equal(res, expected)

        # 1-d
        a = arange(5, chunks=2)

        d = diag(a)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5))
        np.testing.assert_equal(res, expected)

        d = diag(a, k=1)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=1)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=3)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-2)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-2)
        np.testing.assert_equal(res, expected)

        d = diag(a, k=-3)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-3)
        np.testing.assert_equal(res, expected)

        d = diag(a, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5))
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=1, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=1)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=2, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-2, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-2)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)

        d = diag(a, k=-3, sparse=True)
        res = self.executor.execute_tensor(d, concat=True)[0]
        expected = np.diag(np.arange(5), k=-3)
        self.assertIsInstance(res, SparseNDArray)
        np.testing.assert_equal(res.toarray(), expected)