Пример #1
0
def test_low_rank_updated_apply_inverse_adjoint():
    A, L, C, R, U, _ = construct_operators_and_vectorarrays(5, 5, 2, 3)

    LR = LowRankOperator(L, C, R)
    op = LowRankUpdatedOperator(A, LR, 1, 1)
    V = op.apply_inverse_adjoint(U)
    mat = A.matrix + L.to_numpy().T @ C @ R.to_numpy()
    assert np.allclose(V.to_numpy().T, spla.solve(mat.T, U.to_numpy().T))

    LR = LowRankOperator(L, C, R, inverted=True)
    op = LowRankUpdatedOperator(A, LR, 1, 1)
    V = op.apply_inverse_adjoint(U)
    mat = A.matrix + L.to_numpy().T @ spla.solve(C, R.to_numpy())
    assert np.allclose(V.to_numpy().T, spla.solve(mat.T, U.to_numpy().T))
Пример #2
0
def test_to_matrix_LowRankOperator():
    np.random.seed(0)
    m = 6
    n = 5
    r = 2
    L = np.random.randn(m, r)
    Lva = NumpyVectorSpace.make_array(L.T)
    C = np.random.randn(r, r)
    R = np.random.randn(n, r)
    Rva = NumpyVectorSpace.make_array(R.T)

    LR = LowRankOperator(Lva, C, Rva)
    assert_type_and_allclose(L @ C @ R.T, LR, 'dense')

    LR = LowRankOperator(Lva, C, Rva, inverted=True)
    assert_type_and_allclose(L @ spla.solve(C, R.T), LR, 'dense')
Пример #3
0
 def action_merge_low_rank_operators(self, ops):
     low_rank = []
     not_low_rank = []
     for op, coeff in zip(ops, self.coefficients):
         if isinstance(op, LowRankOperator):
             low_rank.append((op, coeff))
         else:
             not_low_rank.append((op, coeff))
     inverted = [op.inverted for op, _ in low_rank]
     if len(inverted) >= 2 and any(inverted) and any(not _ for _ in inverted):
         return None
     inverted = inverted[0]
     left = cat_arrays([op.left for op, _ in low_rank])
     right = cat_arrays([op.right for op, _ in low_rank])
     core = []
     for op, coeff in low_rank:
         core.append(op.core)
         if inverted:
             core[-1] /= coeff
         else:
             core[-1] *= coeff
     core = spla.block_diag(*core)
     new_low_rank_op = LowRankOperator(left, core, right, inverted=inverted)
     if len(not_low_rank) == 0:
         return new_low_rank_op
     else:
         new_ops, new_coeffs = zip(*not_low_rank)
         return assemble_lincomb(chain(new_ops, [new_low_rank_op]), chain(new_coeffs, [1]),
                                 solver_options=self.solver_options, name=self.name)
Пример #4
0
def create_cl_fom(Re=110, level=2, palpha=1e-3, control='bc'):
    """Create model which is used to evaluate the H2-Gap norm."""
    setup_str = 'lvl_' + str(level) + ('_' + control if control is not None else '') \
                + '_re_' + str(Re) + ('_palpha_' + str(palpha) if control == 'bc' else '')

    fom = load_fom(Re, level, palpha, control)

    Bra = fom.B.as_range_array()
    Cva = fom.C.as_source_array()

    Z = solve_ricc_lrcf(fom.A, fom.E, Bra, Cva, trans=False)
    K = fom.E.apply(Z).lincomb(Z.dot(Cva).T)

    KC = LowRankOperator(K, np.eye(len(K)), Cva)
    mKB = cat_arrays([-K, Bra]).to_numpy().T
    mKBop = NumpyMatrixOperator(mKB)

    mKBop_proj = LerayProjectedOperator(mKBop,
                                        fom.A.source.G,
                                        fom.A.source.E,
                                        projection_space='range')

    cl_fom = LTIModel(fom.A - KC, mKBop_proj, fom.C, None, fom.E)

    with open(setup_str + '/cl_fom', 'wb') as cl_fom_file:
        pickle.dump({'cl_fom': cl_fom}, cl_fom_file)
Пример #5
0
def test_low_rank_updated_assemble_apply():
    A, L, C, R, U, _ = construct_operators_and_vectorarrays(5, 5, 2, 3)

    LR = LowRankOperator(L, C, R)
    op = (A + (A + LR).assemble() + LR).assemble()
    V = op.apply(U)
    assert np.allclose(V.to_numpy().T,
                       2 * A.matrix @ U.to_numpy().T
                       + 2 * L.to_numpy().T @ C @ (R.to_numpy() @ U.to_numpy().T))
Пример #6
0
def test_low_rank_updated_assemble():
    A, L, C, R, _, _ = construct_operators_and_vectorarrays(5, 5, 2, 0)
    LR = LowRankOperator(L, C, R)

    op = (A + LR).assemble()
    assert isinstance(op, LowRankUpdatedOperator)

    op = (A + LR + LR).assemble()
    assert isinstance(op, LowRankUpdatedOperator)

    op = (A + (A + LR).assemble() + LR).assemble()
    assert isinstance(op, LowRankUpdatedOperator)
Пример #7
0
def test_low_rank_assemble():
    r1, r2 = 2, 3
    _, L1, C1, R1, _, _ = construct_operators_and_vectorarrays(5, 5, r1, 0)
    _, L2, C2, R2, _, _ = construct_operators_and_vectorarrays(5, 5, r2, 0, seed=1)

    LR1 = LowRankOperator(L1, C1, R1)
    LR2 = LowRankOperator(L2, C2, R2)
    op = assemble_lincomb([LR1, LR2], [1, 1])
    assert isinstance(op, LowRankOperator)
    assert len(op.left) == r1 + r2
    assert not op.inverted

    op = (LR1 + (LR1 + LR2) + LR2).assemble()
    assert isinstance(op, LowRankOperator)

    LR1 = LowRankOperator(L1, C1, R1, inverted=True)
    LR2 = LowRankOperator(L2, C2, R2, inverted=True)
    op = assemble_lincomb([LR1, LR2], [1, 1])
    assert isinstance(op, LowRankOperator)
    assert len(op.left) == r1 + r2
    assert op.inverted

    LR1 = LowRankOperator(L1, C1, R1, inverted=True)
    LR2 = LowRankOperator(L2, C2, R2)
    op = assemble_lincomb([LR1, LR2], [1, 1])
    assert op is None
Пример #8
0
def test_to_matrix_LowRankUpdatedOperator():
    np.random.seed(0)
    m = 6
    n = 5
    r = 2
    A = np.random.randn(m, n)
    Aop = NumpyMatrixOperator(A)
    L = np.random.randn(m, r)
    Lva = NumpyVectorSpace.make_array(L.T)
    C = np.random.randn(r, r)
    R = np.random.randn(n, r)
    Rva = NumpyVectorSpace.make_array(R.T)
    LR = LowRankOperator(Lva, C, Rva)

    op = LowRankUpdatedOperator(Aop, LR, 1, 1)
    assert_type_and_allclose(A + L @ C @ R.T, op, 'dense')
Пример #9
0
def test_low_rank_apply_adjoint():
    _, L, C, R, _, V = construct_operators_and_vectorarrays(6, 5, 2, 3)

    LR = LowRankOperator(L, C, R)
    U = LR.apply_adjoint(V)
    assert np.allclose(U.to_numpy().T, R.to_numpy().T @ C.T @ (L.to_numpy() @ V.to_numpy().T))

    LR = LowRankOperator(L, C, R, inverted=True)
    U = LR.apply_adjoint(V)
    assert np.allclose(U.to_numpy().T,
                       R.to_numpy().T @ spla.solve(C.T, L.to_numpy() @ V.to_numpy().T))
Пример #10
0
def test_low_rank_apply():
    _, L, C, R, U, _ = construct_operators_and_vectorarrays(6, 5, 2, 3)

    LR = LowRankOperator(L, C, R)
    V = LR.apply(U)
    assert np.allclose(V.to_numpy().T, L.to_numpy().T @ C @ (R.to_numpy() @ U.to_numpy().T))

    LR = LowRankOperator(L, C, R, inverted=True)
    V = LR.apply(U)
    assert np.allclose(V.to_numpy().T,
                       L.to_numpy().T @ spla.solve(C, R.to_numpy() @ U.to_numpy().T))