Example #1
0
    def testLUExecution(self):
        np.random.seed(1)

        # square matrix
        data = np.random.randint(1, 10, (6, 6))

        a = tensor(data)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=2)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=(2, 3))
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=4)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        # shape[0] > shape[1]
        data = np.random.randint(1, 10, (10, 6))

        a = tensor(data)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=2)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=(2, 3))
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=4)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l, result_u = self.executor.execute_tensors([L, U])

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        # shape[0] < shape[1]
        data = np.random.randint(1, 10, (6, 10))

        a = tensor(data)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=2)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=(2, 3))
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        a = tensor(data, chunk_size=4)
        P, L, U = lu(a)

        # check lower and upper triangular matrix
        result_l, result_u = self.executor.execute_tensors([L, U])

        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_allclose(res, data)

        # test for sparse
        data = sps.csr_matrix([[2, 0, 0, 0, 5, 2], [0, 6, 1, 0, 0, 6],
                               [8, 0, 9, 0, 0, 2], [0, 6, 0, 8, 7, 3],
                               [7, 0, 6, 1, 7, 0], [0, 0, 0, 7, 0, 8]])

        a = tensor(data)
        P, L, U = lu(a)
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        # check lower and upper triangular matrix
        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)
        self.assertIsInstance(result_l, SparseNDArray)
        self.assertIsInstance(result_u, SparseNDArray)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_array_almost_equal(data.A, res)

        a = tensor(data, chunk_size=2)
        P, L, U = lu(a)
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        # check lower and upper triangular matrix
        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)
        self.assertIsInstance(result_l, SparseNDArray)
        self.assertIsInstance(result_u, SparseNDArray)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_array_almost_equal(data.A, res)

        a = tensor(data, chunk_size=(2, 3))
        P, L, U = lu(a)
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        # check lower and upper triangular matrix
        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)
        self.assertIsInstance(result_l, SparseNDArray)
        self.assertIsInstance(result_u, SparseNDArray)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_array_almost_equal(data.A, res)

        a = tensor(data, chunk_size=4)
        P, L, U = lu(a)
        result_l = self.executor.execute_tensor(L, concat=True)[0]
        result_u = self.executor.execute_tensor(U, concat=True)[0]

        # check lower and upper triangular matrix
        np.testing.assert_allclose(np.tril(result_l), result_l)
        np.testing.assert_allclose(np.triu(result_u), result_u)
        self.assertIsInstance(result_l, SparseNDArray)
        self.assertIsInstance(result_u, SparseNDArray)

        t = P.dot(L).dot(U)
        res = self.executor.execute_tensor(t, concat=True)[0]
        np.testing.assert_array_almost_equal(data.A, res)
Example #2
0
def test_lu_execution(setup):
    rs = np.random.RandomState(0)

    # square matrix
    data = rs.randint(1, 10, (6, 6))

    a = tensor(data)
    P, L, U = lu(a)

    # check lower and upper triangular matrix
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_allclose(res, data)

    a = tensor(data, chunk_size=(3, 4))
    P, L, U = lu(a)

    # check lower and upper triangular matrix
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_allclose(res, data)

    # shape[0] > shape[1]
    data = rs.randint(1, 10, (10, 6))

    a = tensor(data)
    P, L, U = lu(a)

    # check lower and upper triangular matrix
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_allclose(res, data)

    a = tensor(data, chunk_size=5)
    P, L, U = lu(a)

    # check lower and upper triangular matrix
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_allclose(res, data)

    a = tensor(data, chunk_size=(4, 5))
    P, L, U = lu(a)

    # check lower and upper triangular matrix
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_allclose(res, data)

    # shape[0] < shape[1]
    data = rs.randint(1, 10, (6, 10))

    a = tensor(data)
    P, L, U = lu(a)

    # check lower and upper triangular matrix
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_allclose(res, data)

    a = tensor(data, chunk_size=5)
    P, L, U = lu(a)

    # check lower and upper triangular matrix
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_allclose(res, data)

    a = tensor(data, chunk_size=(4, 5))
    P, L, U = lu(a)

    # check lower and upper triangular matrix
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_allclose(res, data)

    # test for sparse
    data = sps.csr_matrix([[2, 0, 0, 0, 5, 2], [0, 6, 1, 0, 0, 6],
                           [8, 0, 9, 0, 0, 2], [0, 6, 0, 8, 7, 3],
                           [7, 0, 6, 1, 7, 0], [0, 0, 0, 7, 0, 8]])

    a = tensor(data)
    P, L, U = lu(a)
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    # check lower and upper triangular matrix
    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)
    assert isinstance(result_l, SparseNDArray)
    assert isinstance(result_u, SparseNDArray)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_array_almost_equal(data.A, res)

    a = tensor(data, chunk_size=5)
    P, L, U = lu(a)
    result_l = L.execute().fetch()
    result_u = U.execute().fetch()

    # check lower and upper triangular matrix
    np.testing.assert_allclose(np.tril(result_l), result_l)
    np.testing.assert_allclose(np.triu(result_u), result_u)
    assert isinstance(result_l, SparseNDArray)
    assert isinstance(result_u, SparseNDArray)

    t = P.dot(L).dot(U)
    res = t.execute().fetch()
    np.testing.assert_array_almost_equal(data.A, res)