Esempio n. 1
0
def generate_2D(N, corners):
    if corners:
        print("Generating %dx%d 2-D adjacency system with corners..." %
              (N**2, N**2))
        A = np.zeros((N**2, N**2)) + 8 * np.eye(N**2)
    else:
        print("Generating %dx%d 2-D adjacency system without corners..." %
              (N**2, N**2))
        A = np.zeros((N**2, N**2)) + 4 * np.eye(N**2)
    # These are the same for both cases
    off_one = np.full(N**2 - 1, -1, dtype=np.float64)
    A += np.diag(off_one, k=1)
    A += np.diag(off_one, k=-1)
    off_N = np.full(N * (N - 1), -1, dtype=np.float64)
    A += np.diag(off_N, k=N)
    A += np.diag(off_N, k=-N)
    # If we have corners then we have four more cases
    if corners:
        off_N_plus = np.full(N * (N - 1) - 1, -1, dtype=np.float64)
        A += np.diag(off_N_plus, k=N + 1)
        A += np.diag(off_N_plus, k=-(N + 1))
        off_N_minus = np.full(N * (N - 1) + 1, -1, dtype=np.float64)
        A += np.diag(off_N_minus, k=N - 1)
        A += np.diag(off_N_minus, k=-(N - 1))
    # Then we can generate a random b matrix
    b = np.random.rand(N**2)
    return A, b
Esempio n. 2
0
def generate_random(N):
    print("Generating %dx%d system..." % (N, N))
    # Generate a random matrix
    A = np.random.rand(N, N)
    # Make sure that it is diagonally dominate
    A = A + N * np.eye(N)
    # Generate a random vector
    b = np.random.rand(N)
    return A, b
Esempio n. 3
0
def generate_random(N):
    print("Generating %dx%d symmetric positive definite system..." % (N, N))
    # Generate a random matrix
    A = np.random.rand(N, N)
    # Make sure the matrix is symmetric
    A = 0.5 * (A + A.T)
    # Now make sure that it is positive definite
    A = A + N * np.eye(N)
    b = np.random.rand(N)
    return A, b
Esempio n. 4
0
def test():
    assert lg.count_nonzero(lg.array([])) == 0
    assert lg.count_nonzero(lg.array([], dtype="?")) == 0
    assert_equal(lg.nonzero([]), np.nonzero([]))

    assert lg.count_nonzero(lg.array([[], []])) == 0
    assert lg.count_nonzero(lg.array([[], []], dtype="?")) == 0
    assert_equal(lg.nonzero([[], []]), np.nonzero([[], []]))

    assert lg.count_nonzero(lg.array([0])) == 0
    assert lg.count_nonzero(lg.array([0], dtype="?")) == 0
    assert_equal(lg.nonzero(lg.array([0])), ([],))

    assert lg.count_nonzero(lg.array([1])) == 1
    assert lg.count_nonzero(lg.array([1], dtype="?")) == 1
    assert_equal(lg.nonzero([1]), np.nonzero([1]))

    assert lg.count_nonzero(lg.array(0)) == 0
    assert lg.count_nonzero(lg.array(0, dtype="?")) == 0
    assert_equal(lg.nonzero(0), np.nonzero(0))

    assert lg.count_nonzero(lg.array(1)) == 1
    assert lg.count_nonzero(lg.array(1, dtype="?")) == 1
    assert_equal(lg.nonzero(1), np.nonzero(1))

    x = lg.array([1, 0, 2, -1, 0, 0, 8])
    x_np = np.array([1, 0, 2, -1, 0, 0, 8])
    assert lg.count_nonzero(x) == 4
    assert_equal(lg.nonzero(x), np.nonzero(x_np))

    # TBD: this will work once nonzero returns output in row-major output
    # x = lg.array([[0, 1, 0], [2, 0, 3]])
    # x_np = np.array([[0, 1, 0], [2, 0, 3]])
    # assert (lg.count_nonzero(x) == 3)
    # assert_equal(lg.nonzero(x), np.nonzero(x_np))

    x_lg = lg.eye(3)
    x_np = np.eye(3)
    assert lg.count_nonzero(x_lg) == np.count_nonzero(x_np)
    assert_equal(lg.nonzero(x_lg), np.nonzero(x_np))

    # TBD: this will work once nonzero returns output in row-major output
    # x = lg.array([[[0, 1], [1, 1], [7, 0], [1, 0], [0, 1]], [[3, 0], [0, 3], [0, 0], [2, 2], [0, 19]]]) # noqa E501
    # x_np = np.array([[[0, 1], [1, 1], [7, 0], [1, 0], [0, 1]], [[3, 0], [0, 3], [0, 0], [2, 2], [0, 19]]]) # noqa E501
    # assert (lg.count_nonzero(x) == np.count_nonzero(x_np))
    # assert_equal(lg.count_nonzero(x, axis=0), np.count_nonzero(x_np, axis=0))
    # assert_equal(lg.count_nonzero(x, axis=1), np.count_nonzero(x_np, axis=1))
    # assert_equal(lg.count_nonzero(x, axis=2), np.count_nonzero(x_np, axis=2))
    # assert (lg.count_nonzero(x, axis=(0, 1, 2)) == np.count_nonzero(x_np, axis=(0, 1, 2))) # noqa E501
    # assert_equal(lg.nonzero(x), np.nonzero(x_np))

    # x_np = np.concatenate((x_np,) * 2000, axis=1)
    # x = lg.array(x_np)
    # assert (lg.count_nonzero(x) == np.count_nonzero(x_np))
    # assert_equal(lg.count_nonzero(x, axis=0), np.count_nonzero(x_np, axis=0))
    # assert_equal(lg.count_nonzero(x, axis=1), np.count_nonzero(x_np, axis=1))
    # assert_equal(lg.count_nonzero(x, axis=2), np.count_nonzero(x_np, axis=2))
    # assert (lg.count_nonzero(x, axis=(0, 1, 2)) == np.count_nonzero(x_np, axis=(0, 1, 2))) # noqa E501
    # assert_equal(lg.nonzero(x), np.nonzero(x_np))

    # lg_nonzero = lg.nonzero(x)
    # np_nonzero = np.nonzero(x_np)
    # assert_equal(lg_nonzero, np_nonzero)

    assert_equal(lg.nonzero(0), ([],))
    assert_equal(lg.nonzero(1), ([0],))

    x_np = np.random.randn(100)
    indices = np.random.choice(
        np.arange(x_np.size), replace=False, size=int(x_np.size * 0.2)
    )
    x_np[indices] = 0
    x = lg.array(x_np)
    assert lg.count_nonzero(x) == np.count_nonzero(x_np)
    lg_nonzero = lg.nonzero(x)
    np_nonzero = np.nonzero(x_np)
    assert_equal(lg_nonzero, np_nonzero)

    # x_np = x_np.reshape(10, 10)
    # x = lg.array(x_np)
    # assert (lg.count_nonzero(x) == np.count_nonzero(x_np))
    # lg_nonzero = lg.nonzero(x)
    # np_nonzero = np.nonzero(x_np)
    # assert_equal(lg_nonzero, np_nonzero)

    return
Esempio n. 5
0
def test():
    x = lg.array([])
    r = lg.sum(x)
    assert r == 0

    x = lg.array([1])
    r = lg.sum(x)

    assert r == 1

    x = lg.eye(3)
    r = lg.sum(x)

    assert r == 3

    x = lg.array([1, 2, 3, 4.0])
    r = lg.sum(x)
    r2 = lg.add.reduce(x)

    assert r == r2 == 10

    x = lg.array([1, 2, 3, 4.0, 5.0])
    r = lg.prod(x)
    r2 = lg.multiply.reduce(x)
    assert r == r2 == 120

    asserts.assert_equal(lg.sum([]), np.sum([]))
    asserts.assert_equal(lg.add.reduce([]), np.add.reduce([]))

    asserts.assert_equal(lg.sum([[], []]), np.sum([[], []]))
    asserts.assert_equal(lg.add.reduce([[], []]), np.add.reduce([[], []]))

    asserts.assert_equal(lg.sum(lg.array([0])), np.sum(np.array([0])))
    asserts.assert_equal(
        lg.add.reduce(lg.array([0])), np.add.reduce(np.array([0]))
    )

    asserts.assert_equal(lg.sum([1]), np.sum([1]))
    asserts.assert_equal(lg.add.reduce([1]), np.add.reduce([1]))

    asserts.assert_equal(lg.sum(0), np.sum(0))
    asserts.assert_equal(lg.add.reduce(0), np.add.reduce(0))

    asserts.assert_equal(lg.sum(1), np.sum(1))
    asserts.assert_equal(lg.add.reduce(1), np.add.reduce(1))

    x = lg.array([1, 0, 2, -1, 0, 0, 8])
    x_np = np.array([1, 0, 2, -1, 0, 0, 8])
    asserts.assert_equal(lg.sum(x), np.sum(x_np))
    asserts.assert_equal(lg.add.reduce(x), np.add.reduce(x_np))

    x = lg.array([[0, 1, 0], [2, 0, 3]])
    x_np = np.array([[0, 1, 0], [2, 0, 3]])
    asserts.assert_equal(lg.sum(x), np.sum(x_np))
    asserts.assert_equal(lg.add.reduce(x), np.add.reduce(x_np))

    x = lg.eye(3)
    x_np = np.eye(3)
    asserts.assert_equal(lg.sum(x), np.sum(x_np))
    asserts.assert_equal(lg.add.reduce(x), np.add.reduce(x_np))

    x = lg.array(
        [
            [[0, 1], [1, 1], [7, 0], [1, 0], [0, 1]],
            [[3, 0], [0, 3], [0, 0], [2, 2], [0, 19]],
        ]
    )
    x_np = np.array(
        [
            [[0, 1], [1, 1], [7, 0], [1, 0], [0, 1]],
            [[3, 0], [0, 3], [0, 0], [2, 2], [0, 19]],
        ]
    )
    asserts.assert_equal(lg.sum(x, axis=0), np.sum(x_np, axis=0))
    asserts.assert_equal(lg.sum(x, axis=1), np.sum(x_np, axis=1))
    asserts.assert_equal(lg.sum(x, axis=2), np.sum(x_np, axis=2))
    asserts.assert_equal(lg.add.reduce(x, axis=0), np.add.reduce(x_np, axis=0))
    asserts.assert_equal(lg.add.reduce(x, axis=1), np.add.reduce(x_np, axis=1))
    asserts.assert_equal(lg.add.reduce(x, axis=2), np.add.reduce(x_np, axis=2))
    asserts.assert_equal(lg.sum(x), np.sum(x_np))
    asserts.assert_equal(lg.add.reduce(x), np.add.reduce(x_np))

    x_np = np.concatenate((x_np,) * 2000, axis=1)
    x = lg.array(x_np)
    asserts.assert_equal(lg.sum(x, axis=0), np.sum(x_np, axis=0))
    asserts.assert_equal(lg.sum(x, axis=1), np.sum(x_np, axis=1))
    asserts.assert_equal(lg.sum(x, axis=2), np.sum(x_np, axis=2))
    asserts.assert_equal(lg.sum(x), np.sum(x_np))
    asserts.assert_equal(lg.add.reduce(x, axis=0), np.add.reduce(x_np, axis=0))
    asserts.assert_equal(lg.add.reduce(x, axis=1), np.add.reduce(x_np, axis=1))
    asserts.assert_equal(lg.add.reduce(x, axis=2), np.add.reduce(x_np, axis=2))
    asserts.assert_equal(lg.add.reduce(x), np.add.reduce(x_np))

    x_np = np.random.randn(100)
    indices = np.random.choice(
        np.arange(x_np.size), replace=False, size=int(x_np.size * 0.2)
    )
    x_np[indices] = 0
    x = lg.array(x_np)
    asserts.assert_allclose(lg.sum(x), np.sum(x_np))
    asserts.assert_allclose(lg.add.reduce(x), np.add.reduce(x_np))

    x_np = x_np.reshape(10, 10)
    x = lg.array(x_np)
    asserts.assert_allclose(lg.sum(x), np.sum(x_np))
    asserts.assert_allclose(lg.add.reduce(x), np.add.reduce(x_np))

    return