def test_svd_execution(setup): rs = np.random.RandomState() data = rs.randn(18, 6) + 1j * rs.randn(18, 6) a = tensor(data, chunk_size=(9, 6)) U, s, V = svd(a) t = U.dot(diag(s).dot(V)) res = t.execute().fetch() np.testing.assert_array_almost_equal(res, data) a = tensor(data, chunk_size=(18, 6)) U, s, V = svd(a) t = U.dot(diag(s).dot(V)) res = t.execute().fetch() np.testing.assert_array_almost_equal(res, data) a = tensor(data, chunk_size=(2, 6)) U, s, V = svd(a) t = U.dot(diag(s).dot(V)) res = t.execute().fetch() np.testing.assert_array_almost_equal(res, data) data = rs.randn(6, 18) + 1j * rs.randn(6, 18) a = tensor(data) U, s, V = svd(a) t = U.dot(diag(s).dot(V)) res = t.execute().fetch() np.testing.assert_array_almost_equal(res, data) # test for matrix of ones data = np.ones((20, 10)) a = tensor(data, chunk_size=10) s = svd(a)[1] res = s.execute().fetch() expected = np.linalg.svd(a)[1] np.testing.assert_array_almost_equal(res, expected)
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)
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) self.assertTrue(res.flags['C_CONTIGUOUS']) self.assertFalse(res.flags['F_CONTIGUOUS']) 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)
def test_diag_execution(setup): # 2-d 6 * 6 a = arange(36, chunk_size=5).reshape(6, 6) d = diag(a) res = d.execute().fetch() expected = np.diag(np.arange(36).reshape(6, 6)) np.testing.assert_equal(res, expected) d = diag(a, k=1) res = d.execute().fetch() expected = np.diag(np.arange(36).reshape(6, 6), k=1) np.testing.assert_equal(res, expected) d = diag(a, k=3) res = d.execute().fetch() expected = np.diag(np.arange(36).reshape(6, 6), k=3) np.testing.assert_equal(res, expected) d = diag(a, k=-2) res = d.execute().fetch() expected = np.diag(np.arange(36).reshape(6, 6), k=-2) np.testing.assert_equal(res, expected) d = diag(a, k=-5) res = d.execute().fetch() 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 = d.execute().fetch() expected = np.diag(a.toarray()) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=1) res = d.execute().fetch() expected = np.diag(a.toarray(), k=1) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=3) res = d.execute().fetch() expected = np.diag(a.toarray(), k=3) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-2) res = d.execute().fetch() expected = np.diag(a.toarray(), k=-2) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-5) res = d.execute().fetch() 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 = d.execute().fetch() expected = np.diag(raw_a.toarray()) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=1) res = d.execute().fetch() expected = np.diag(raw_a.toarray(), k=1) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=3) res = d.execute().fetch() expected = np.diag(raw_a.toarray(), k=3) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-2) res = d.execute().fetch() expected = np.diag(raw_a.toarray(), k=-2) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-5) res = d.execute().fetch() 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 = d.execute().fetch() expected = np.diag(np.arange(36).reshape(4, 9)) np.testing.assert_equal(res, expected) d = diag(a, k=1) res = d.execute().fetch() expected = np.diag(np.arange(36).reshape(4, 9), k=1) np.testing.assert_equal(res, expected) d = diag(a, k=3) res = d.execute().fetch() expected = np.diag(np.arange(36).reshape(4, 9), k=3) np.testing.assert_equal(res, expected) d = diag(a, k=-2) res = d.execute().fetch() expected = np.diag(np.arange(36).reshape(4, 9), k=-2) np.testing.assert_equal(res, expected) d = diag(a, k=-3) res = d.execute().fetch() 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 = d.execute().fetch() expected = np.diag(a.toarray()) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=1) res = d.execute().fetch() expected = np.diag(a.toarray(), k=1) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=3) res = d.execute().fetch() expected = np.diag(a.toarray(), k=3) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-2) res = d.execute().fetch() expected = np.diag(a.toarray(), k=-2) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-3) res = d.execute().fetch() 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 = d.execute().fetch() expected = np.diag(raw_a.toarray()) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=1) res = d.execute().fetch() expected = np.diag(raw_a.toarray(), k=1) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=3) res = d.execute().fetch() expected = np.diag(raw_a.toarray(), k=3) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-2) res = d.execute().fetch() expected = np.diag(raw_a.toarray(), k=-2) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-3) res = d.execute().fetch() 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 = d.execute().fetch() expected = np.diag(np.arange(5)) np.testing.assert_equal(res, expected) assert res.flags['C_CONTIGUOUS'] is True assert res.flags['F_CONTIGUOUS'] is False d = diag(a, k=1) res = d.execute().fetch() expected = np.diag(np.arange(5), k=1) np.testing.assert_equal(res, expected) d = diag(a, k=3) res = d.execute().fetch() expected = np.diag(np.arange(5), k=3) np.testing.assert_equal(res, expected) d = diag(a, k=-2) res = d.execute().fetch() expected = np.diag(np.arange(5), k=-2) np.testing.assert_equal(res, expected) d = diag(a, k=-3) res = d.execute().fetch() expected = np.diag(np.arange(5), k=-3) np.testing.assert_equal(res, expected) d = diag(a, sparse=True) res = d.execute().fetch() expected = np.diag(np.arange(5)) assert isinstance(res, SparseNDArray) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=1, sparse=True) res = d.execute().fetch() expected = np.diag(np.arange(5), k=1) assert isinstance(res, SparseNDArray) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=2, sparse=True) res = d.execute().fetch() expected = np.diag(np.arange(5), k=2) assert isinstance(res, SparseNDArray) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-2, sparse=True) res = d.execute().fetch() expected = np.diag(np.arange(5), k=-2) assert isinstance(res, SparseNDArray) np.testing.assert_equal(res.toarray(), expected) d = diag(a, k=-3, sparse=True) res = d.execute().fetch() expected = np.diag(np.arange(5), k=-3) assert isinstance(res, SparseNDArray) np.testing.assert_equal(res.toarray(), expected)