Пример #1
0
    def testInv(self):
        import scipy.linalg
        np.random.seed(1)

        data = np.random.randint(1, 10, (20, 20))

        A = tensor(data)
        inv_A = inv(A)

        res = self.executor.execute_tensor(inv_A, concat=True)[0]
        self.assertTrue(np.allclose(res, scipy.linalg.inv(data)))
        res = self.executor.execute_tensor(A.dot(inv_A), concat=True)[0]
        self.assertTrue(np.allclose(res, np.eye(data.shape[0], dtype=float)))

        A = tensor(data, chunk_size=5)
        inv_A = inv(A)

        res = self.executor.execute_tensor(inv_A, concat=True)[0]
        self.assertTrue(np.allclose(res, scipy.linalg.inv(data)))
        res = self.executor.execute_tensor(A.dot(inv_A), concat=True)[0]
        self.assertTrue(np.allclose(res, np.eye(data.shape[0], dtype=float)))

        B = A.T.dot(A)
        inv_B = inv(B)
        res = self.executor.execute_tensor(inv_B, concat=True)[0]
        self.assertTrue(np.allclose(res, scipy.linalg.inv(data.T.dot(data))))
        res = self.executor.execute_tensor(B.dot(inv_B), concat=True)[0]
        self.assertTrue(np.allclose(res, np.eye(data.shape[0], dtype=float)))

        # test for not all chunks are square in matrix A
        A = tensor(data, chunk_size=8)
        inv_A = inv(A)

        res = self.executor.execute_tensor(inv_A, concat=True)[0]
        self.assertTrue(np.allclose(res, scipy.linalg.inv(data)))
        res = self.executor.execute_tensor(A.dot(inv_A), concat=True)[0]
        self.assertTrue(np.allclose(res, np.eye(data.shape[0], dtype=float)))

        # test sparse
        data = np.random.randint(1, 10, (20, 20))
        sp_data = sps.csr_matrix(data)

        A = tensor(sp_data, chunk_size=5)
        inv_A = inv(A)

        res = self.executor.execute_tensor(inv_A, concat=True)[0]
        self.assertIsInstance(res, SparseNDArray)
        self.assertTrue(np.allclose(res, scipy.linalg.inv(data)))
        res = self.executor.execute_tensor(A.dot(inv_A), concat=True)[0]
        self.assertTrue(np.allclose(res, np.eye(data.shape[0], dtype=float)))

        # test for not all chunks are square in matrix A
        A = tensor(sp_data, chunk_size=8)
        inv_A = inv(A)

        res = self.executor.execute_tensor(inv_A, concat=True)[0]
        self.assertIsInstance(res, SparseNDArray)
        self.assertTrue(np.allclose(res, scipy.linalg.inv(data)))
        res = self.executor.execute_tensor(A.dot(inv_A), concat=True)[0]
        self.assertTrue(np.allclose(res, np.eye(data.shape[0], dtype=float)))
Пример #2
0
    def testInv(self):
        import scipy.linalg
        np.random.seed(1)

        data = np.random.randint(1, 10, (20, 20))

        A = tensor(data, chunk_size=5)
        inv_A = inv(A)

        res = self.executor.execute_tensor(inv_A, concat=True)[0]
        self.assertTrue(np.allclose(res, scipy.linalg.inv(data)))
        res = self.executor.execute_tensor(A.dot(inv_A), concat=True)[0]
        self.assertTrue(np.allclose(res, np.eye(data.shape[0], dtype=float)))