Exemplo n.º 1
0
def test_ext(extension_alg):
    size = 5
    ident = np.identity(size)
    current = ident[0]
    for i in range(1, size):
        c = NumpyVectorSpace.from_data(current)
        n, _ = extension_alg(c, NumpyVectorSpace.from_data(ident[i]))
        assert np.allclose(n.data, ident[0:i+1])
        current = ident[0:i+1]
Exemplo n.º 2
0
def test_complex():
    np.random.seed(0)
    I = np.eye(5)
    A = np.random.randn(5, 5)
    B = np.random.randn(5, 5)
    C = np.random.randn(3, 5)

    Iop = NumpyMatrixOperator(I)
    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cva = NumpyVectorSpace.from_data(C)

    # assemble_lincomb
    assert not np.iscomplexobj(
        Aop.assemble_lincomb((Iop, Bop), (1, 1))._matrix)
    assert not np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1, 1))._matrix)
    assert np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1 + 0j, 1 + 0j))._matrix)
    assert np.iscomplexobj(Aop.assemble_lincomb((Aop, Bop), (1j, 1))._matrix)
    assert np.iscomplexobj(Aop.assemble_lincomb((Bop, Aop), (1, 1j))._matrix)

    # apply_inverse
    assert not np.iscomplexobj(Aop.apply_inverse(Cva).data)
    assert np.iscomplexobj((Aop * 1j).apply_inverse(Cva).data)
    assert np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1, 1j)).apply_inverse(Cva).data)
    assert np.iscomplexobj(Aop.apply_inverse(Cva * 1j).data)

    # append
    for rsrv in (0, 10):
        for o_ind in (slice(None), [0]):
            va = NumpyVectorSpace(5).empty(reserve=rsrv)
            va.append(Cva)
            D = np.random.randn(1, 5) + 1j * np.random.randn(1, 5)
            Dva = NumpyVectorSpace.from_data(D)

            assert not np.iscomplexobj(va.data)
            assert np.iscomplexobj(Dva.data)
            va.append(Dva[o_ind])
            assert np.iscomplexobj(va.data)

    # scal
    assert not np.iscomplexobj(Cva.data)
    assert np.iscomplexobj((Cva * 1j).data)
    assert np.iscomplexobj((Cva * (1 + 0j)).data)

    # axpy
    assert not np.iscomplexobj(Cva.data)
    Cva[0].axpy(1, Dva)
    assert np.iscomplexobj(Cva.data)

    Cva = NumpyVectorSpace.from_data(C)
    assert not np.iscomplexobj(Cva.data)
    Cva[0].axpy(1j, Dva)
    assert np.iscomplexobj(Cva.data)
Exemplo n.º 3
0
def test_axpy():
    x = NumpyVectorSpace.from_data(np.array([1.]))
    y = NumpyVectorSpace.from_data(np.array([1.]))
    y.axpy(1 + 1j, x)
    assert y.data[0, 0] == 2 + 1j

    x = NumpyVectorSpace.from_data(np.array([1 + 1j]))
    y = NumpyVectorSpace.from_data(np.array([1.]))
    y.axpy(-1, x)
    assert y.data[0, 0] == -1j
Exemplo n.º 4
0
def test_axpy():
    x = NumpyVectorSpace.from_data(np.array([1.]))
    y = NumpyVectorSpace.from_data(np.array([1.]))
    y.axpy(1 + 1j, x)
    assert y.data[0, 0] == 2 + 1j

    x = NumpyVectorSpace.from_data(np.array([1 + 1j]))
    y = NumpyVectorSpace.from_data(np.array([1.]))
    y.axpy(-1, x)
    assert y.data[0, 0] == -1j
Exemplo n.º 5
0
def test_complex():
    np.random.seed(0)
    I = np.eye(5)
    A = np.random.randn(5, 5)
    B = np.random.randn(5, 5)
    C = np.random.randn(3, 5)

    Iop = NumpyMatrixOperator(I)
    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cva = NumpyVectorSpace.from_data(C)

    # assemble_lincomb
    assert not np.iscomplexobj(Aop.assemble_lincomb((Iop, Bop), (1, 1))._matrix)
    assert not np.iscomplexobj(Aop.assemble_lincomb((Aop, Bop), (1, 1))._matrix)
    assert not np.iscomplexobj(Aop.assemble_lincomb((Aop, Bop), (1 + 0j, 1 + 0j))._matrix)
    assert np.iscomplexobj(Aop.assemble_lincomb((Aop, Bop), (1j, 1))._matrix)
    assert np.iscomplexobj(Aop.assemble_lincomb((Bop, Aop), (1, 1j))._matrix)

    # apply_inverse
    assert not np.iscomplexobj(Aop.apply_inverse(Cva).data)
    assert np.iscomplexobj((Aop * 1j).apply_inverse(Cva).data)
    assert np.iscomplexobj(Aop.assemble_lincomb((Aop, Bop), (1, 1j)).apply_inverse(Cva).data)
    assert np.iscomplexobj(Aop.apply_inverse(Cva * 1j).data)

    # append
    for rsrv in (0, 10):
        for o_ind in (slice(None), [0]):
            va = NumpyVectorSpace(5).empty(reserve=rsrv)
            va.append(Cva)
            D = np.random.randn(1, 5) + 1j * np.random.randn(1, 5)
            Dva = NumpyVectorSpace.from_data(D)

            assert not np.iscomplexobj(va.data)
            assert np.iscomplexobj(Dva.data)
            va.append(Dva[o_ind])
            assert np.iscomplexobj(va.data)

    # scal
    assert not np.iscomplexobj(Cva.data)
    assert np.iscomplexobj((Cva * 1j).data)
    assert np.iscomplexobj((Cva * (1 + 0j)).data)

    # axpy
    assert not np.iscomplexobj(Cva.data)
    Cva[0].axpy(1, Dva)
    assert np.iscomplexobj(Cva.data)

    Cva = NumpyVectorSpace.from_data(C)
    assert not np.iscomplexobj(Cva.data)
    Cva[0].axpy(1j, Dva)
    assert np.iscomplexobj(Cva.data)
Exemplo n.º 6
0
def test_scal():
    v = np.array([[1, 2, 3], [4, 5, 6]], dtype=float)
    v = NumpyVectorSpace.from_data(v)
    v.scal(1j)

    k = 0
    for i in range(2):
        for j in range(3):
            k += 1
            assert v.data[i, j] == k * 1j
Exemplo n.º 7
0
def test_scal():
    v = np.array([[1, 2, 3],
                  [4, 5, 6]], dtype=float)
    v = NumpyVectorSpace.from_data(v)
    v.scal(1j)

    k = 0
    for i in range(2):
        for j in range(3):
            k += 1
            assert v.data[i, j] == k * 1j
Exemplo n.º 8
0
def test_blk_diag_apply_inverse():
    np.random.seed(0)

    A = np.random.randn(2, 2)
    B = np.random.randn(3, 3)
    C = spla.block_diag(A, B)
    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cop = BlockDiagonalOperator((Aop, Bop))

    v1 = np.random.randn(2)
    v2 = np.random.randn(3)
    v = np.hstack((v1, v2))
    v1va = NumpyVectorSpace.from_data(v1)
    v2va = NumpyVectorSpace.from_data(v2)
    vva = BlockVectorSpace.make_array((v1va, v2va))

    wva = Cop.apply_inverse(vva)
    w = np.hstack((wva.block(0).data, wva.block(1).data))
    assert np.allclose(spla.solve(C, v), w)
Exemplo n.º 9
0
def test_vtkio(rect_or_tria_grid):
    grid = rect_or_tria_grid
    steps = 4
    for dim in range(1, 2):
        for codim, data in enumerate((NumpyVectorSpace.from_data(np.zeros((steps, grid.size(c)))) for c in range(grid.dim+1))):
            with SafeTemporaryFileName('wb') as out_name:
                if codim == 1:
                    with pytest.raises(NotImplementedError):
                        write_vtk(grid, data, out_name, codim=codim)
                else:
                    write_vtk(grid, data, out_name, codim=codim)
Exemplo n.º 10
0
def test_vtkio(rect_or_tria_grid):
    grid = rect_or_tria_grid
    steps = 4
    for dim in range(1, 2):
        for codim, data in enumerate((NumpyVectorSpace.from_data(np.zeros((steps, grid.size(c)))) for c in range(grid.dim+1))):
            with NamedTemporaryFile('wb') as out:
                if codim == 1:
                    with pytest.raises(NotImplementedError):
                        write_vtk(grid, data, out.name, codim=codim)
                else:
                    write_vtk(grid, data, out.name, codim=codim)
Exemplo n.º 11
0
def test_blk_diag_apply_inverse():
    np.random.seed(0)

    A = np.random.randn(2, 2)
    B = np.random.randn(3, 3)
    C = spla.block_diag(A, B)
    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cop = BlockDiagonalOperator((Aop, Bop))

    v1 = np.random.randn(2)
    v2 = np.random.randn(3)
    v = np.hstack((v1, v2))
    v1va = NumpyVectorSpace.from_data(v1)
    v2va = NumpyVectorSpace.from_data(v2)
    vva = BlockVectorSpace.make_array((v1va, v2va))

    wva = Cop.apply_inverse(vva)
    w = np.hstack((wva.block(0).data, wva.block(1).data))
    assert np.allclose(spla.solve(C, v), w)
Exemplo n.º 12
0
def test_real_imag():
    A = np.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j], [9 + 10j, 11 + 12j]])
    Ava = NumpyVectorSpace.from_data(A)
    Bva = Ava.real
    Cva = Ava.imag

    k = 0
    for i in range(3):
        for j in range(2):
            k += 1
            assert Bva.data[i, j] == k
            k += 1
            assert Cva.data[i, j] == k
Exemplo n.º 13
0
def test_apply_transpose():
    np.random.seed(0)

    A11 = np.random.randn(2, 3)
    A12 = np.random.randn(2, 4)
    A21 = np.zeros((5, 3))
    A22 = np.random.randn(5, 4)
    A = np.vstack((np.hstack((A11, A12)), np.hstack((A21, A22))))
    A11op = NumpyMatrixOperator(A11)
    A12op = NumpyMatrixOperator(A12)
    A22op = NumpyMatrixOperator(A22)
    Aop = BlockOperator(np.array([[A11op, A12op], [None, A22op]]))

    v1 = np.random.randn(2)
    v2 = np.random.randn(5)
    v = np.hstack((v1, v2))
    v1va = NumpyVectorSpace.from_data(v1)
    v2va = NumpyVectorSpace.from_data(v2)
    vva = BlockVectorSpace.make_array((v1va, v2va))

    wva = Aop.apply_transpose(vva)
    w = np.hstack((wva.block(0).data, wva.block(1).data))
    assert np.allclose(A.T.dot(v), w)
Exemplo n.º 14
0
def test_real_imag():
    A = np.array([[1 + 2j, 3 + 4j],
                  [5 + 6j, 7 + 8j],
                  [9 + 10j, 11 + 12j]])
    Ava = NumpyVectorSpace.from_data(A)
    Bva = Ava.real
    Cva = Ava.imag

    k = 0
    for i in range(3):
        for j in range(2):
            k += 1
            assert Bva.data[i, j] == k
            k += 1
            assert Cva.data[i, j] == k
Exemplo n.º 15
0
def test_apply_transpose():
    np.random.seed(0)

    A11 = np.random.randn(2, 3)
    A12 = np.random.randn(2, 4)
    A21 = np.zeros((5, 3))
    A22 = np.random.randn(5, 4)
    A = np.vstack((np.hstack((A11, A12)),
                   np.hstack((A21, A22))))
    A11op = NumpyMatrixOperator(A11)
    A12op = NumpyMatrixOperator(A12)
    A22op = NumpyMatrixOperator(A22)
    Aop = BlockOperator(np.array([[A11op, A12op], [None, A22op]]))

    v1 = np.random.randn(2)
    v2 = np.random.randn(5)
    v = np.hstack((v1, v2))
    v1va = NumpyVectorSpace.from_data(v1)
    v2va = NumpyVectorSpace.from_data(v2)
    vva = BlockVectorSpace.make_array((v1va, v2va))

    wva = Aop.apply_transpose(vva)
    w = np.hstack((wva.block(0).data, wva.block(1).data))
    assert np.allclose(A.T.dot(v), w)
Exemplo n.º 16
0
def _newton(order, **kwargs):
    mop = MonomOperator(order)
    rhs = NumpyVectorSpace.from_data([0.0])
    guess = NumpyVectorSpace.from_data([1.0])
    return newton(mop, rhs, initial_guess=guess, **kwargs)
Exemplo n.º 17
0
def test_pairwise_dot():
    x = NumpyVectorSpace.from_data(np.array([1 + 1j]))
    y = NumpyVectorSpace.from_data(np.array([1 - 1j]))
    z = x.pairwise_dot(y)
    assert z == 2j
Exemplo n.º 18
0
def test_pairwise_dot():
    x = NumpyVectorSpace.from_data(np.array([1 + 1j]))
    y = NumpyVectorSpace.from_data(np.array([1 - 1j]))
    z = x.pairwise_dot(y)
    assert z == 2j
Exemplo n.º 19
0
def test_dot():
    x = NumpyVectorSpace.from_data(np.array([1 + 1j]))
    y = NumpyVectorSpace.from_data(np.array([1 - 1j]))
    z = x.dot(y)
    assert z[0, 0] == 2j
Exemplo n.º 20
0
def _newton(order, **kwargs):
    mop = MonomOperator(order)
    rhs = NumpyVectorSpace.from_data([0.0])
    guess = NumpyVectorSpace.from_data([1.0])
    return newton(mop, rhs, initial_guess=guess, **kwargs)
Exemplo n.º 21
0
def numpy_vector_array_factory(length, dim, seed):
    np.random.seed(seed)
    return NumpyVectorSpace.from_data(np.random.random((length, dim)))
Exemplo n.º 22
0
def test_dot():
    x = NumpyVectorSpace.from_data(np.array([1 + 1j]))
    y = NumpyVectorSpace.from_data(np.array([1 - 1j]))
    z = x.dot(y)
    assert z[0, 0] == 2j