예제 #1
0
파일: operators.py 프로젝트: danglive/pymor
def test_selection_op():
    p1 = MonomOperator(1)
    select_rhs_functional = GenericParameterFunctional(
        lambda x: round(float(x["nrrhs"])), ParameterType({"nrrhs": ()}))
    s1 = SelectionOperator(operators=[p1],
                           boundaries=[],
                           parameter_functional=select_rhs_functional,
                           name="foo")
    x = np.linspace(-1., 1., num=3)
    vx = p1.source.make_array(x[:, np.newaxis])
    assert np.allclose(
        p1.apply(vx, mu=0).to_numpy(),
        s1.apply(vx, mu=0).to_numpy())

    s2 = SelectionOperator(operators=[p1, p1, p1, p1],
                           boundaries=[-3, 3, 7],
                           parameter_functional=select_rhs_functional,
                           name="Bar")

    assert s2._get_operator_number({"nrrhs": -4}) == 0
    assert s2._get_operator_number({"nrrhs": -3}) == 0
    assert s2._get_operator_number({"nrrhs": -2}) == 1
    assert s2._get_operator_number({"nrrhs": 3}) == 1
    assert s2._get_operator_number({"nrrhs": 4}) == 2
    assert s2._get_operator_number({"nrrhs": 7}) == 2
    assert s2._get_operator_number({"nrrhs": 9}) == 3
예제 #2
0
def test_selection_op():
    p1 = MonomOperator(1)
    select_rhs_functional = GenericParameterFunctional(
        lambda x: round(float(x["nrrhs"])), 
        ParameterType({"nrrhs" : tuple()})
    )
    s1 = SelectionOperator(
        operators = [p1], 
        boundaries = [], 
        parameter_functional = select_rhs_functional,
        name = "foo"
    )
    x = np.linspace(-1., 1., num=3)
    vx = NumpyVectorArray(x[:, np.newaxis])
    assert np.allclose(p1.apply(vx,mu=0).data, s1.apply(vx,mu=0).data)

    s2 = SelectionOperator(
        operators = [p1,p1,p1,p1],
        boundaries = [-3, 3, 7],
        parameter_functional = select_rhs_functional,
        name = "Bar"
    )

    assert s2._get_operator_number({"nrrhs":-4}) == 0
    assert s2._get_operator_number({"nrrhs":-3}) == 0
    assert s2._get_operator_number({"nrrhs":-2}) == 1
    assert s2._get_operator_number({"nrrhs":3}) == 1
    assert s2._get_operator_number({"nrrhs":4}) == 2
    assert s2._get_operator_number({"nrrhs":7}) == 2
    assert s2._get_operator_number({"nrrhs":9}) == 3
예제 #3
0
def misc_operator_with_arrays_and_products_factory(n):
    if n == 0:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 10, 4, 3, n)
        op = ComponentProjection(np.random.randint(0, 100, 10), U.space)
        return op, _, U, V, sp, rp
    elif n == 1:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 0, 4, 3, n)
        op = ComponentProjection([], U.space)
        return op, _, U, V, sp, rp
    elif n == 2:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 3, 4, 3, n)
        op = ComponentProjection([3, 3, 3], U.space)
        return op, _, U, V, sp, rp
    elif n == 3:
        from pymor.operators.constructions import AdjointOperator
        op, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 20, 4, 3, n)
        op = AdjointOperator(op, with_apply_inverse=True)
        return op, _, V, U, rp, sp
    elif n == 4:
        from pymor.operators.constructions import AdjointOperator
        op, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            100, 20, 4, 3, n)
        op = AdjointOperator(op, with_apply_inverse=False)
        return op, _, V, U, rp, sp
    elif 5 <= n <= 7:
        from pymor.operators.constructions import SelectionOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(
            30, 30, 4, 3, n)
        op1 = NumpyMatrixOperator(np.random.random((30, 30)))
        op2 = NumpyMatrixOperator(np.random.random((30, 30)))
        op = SelectionOperator([op0, op1, op2],
                               ProjectionParameterFunctional('x'), [0.3, 0.6])
        return op, op.parameters.parse((n - 5) / 2), V, U, rp, sp
    elif n == 8:
        from pymor.operators.block import BlockDiagonalOperator
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 20, 4, 3, n + 1)
        op2, _, U2, V2, sp2, rp2 = numpy_matrix_operator_with_arrays_and_products_factory(
            30, 30, 4, 3, n + 2)
        op = BlockDiagonalOperator([op0, op1, op2])
        sp = BlockDiagonalOperator([sp0, sp1, sp2])
        rp = BlockDiagonalOperator([rp0, rp1, rp2])
        U = op.source.make_array([U0, U1, U2])
        V = op.range.make_array([V0, V1, V2])
        return op, _, U, V, sp, rp
    elif n == 9:
        from pymor.operators.block import BlockDiagonalOperator, BlockOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0a, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op0b, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op0c, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 20, 4, 3, n + 1)
        op2a, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2b, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op0 = (op0a * ProjectionParameterFunctional('p', 3, 0) +
               op0b * ProjectionParameterFunctional('p', 3, 1) +
               op0c * ProjectionParameterFunctional('p', 3, 1))
        op2 = (op2a * ProjectionParameterFunctional('p', 3, 0) +
               op2b * ProjectionParameterFunctional('q', 1))
        op = BlockOperator([[op0, op2], [None, op1]])
        mu = op.parameters.parse({'p': [1, 2, 3], 'q': 4})
        sp = BlockDiagonalOperator([sp0, sp1])
        rp = BlockDiagonalOperator([rp0, rp1])
        U = op.source.make_array([U0, U1])
        V = op.range.make_array([V0, V1])
        return op, mu, U, V, sp, rp
    elif n == 10:
        from pymor.operators.block import BlockDiagonalOperator, BlockColumnOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 20, 4, 3, n + 1)
        op2a, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2b, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2 = (op2a * ProjectionParameterFunctional('p', 3, 0) +
               op2b * ProjectionParameterFunctional('q', 1))
        op = BlockColumnOperator([op2, op1])
        mu = op.parameters.parse({'p': [1, 2, 3], 'q': 4})
        sp = sp1
        rp = BlockDiagonalOperator([rp0, rp1])
        U = U1
        V = op.range.make_array([V0, V1])
        return op, mu, U, V, sp, rp
    elif n == 11:
        from pymor.operators.block import BlockDiagonalOperator, BlockRowOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(
            10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 20, 4, 3, n + 1)
        op2a, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2b, _, _, _, _, _ = numpy_matrix_operator_with_arrays_and_products_factory(
            20, 10, 4, 3, n + 2)
        op2 = (op2a * ProjectionParameterFunctional('p', 3, 0) +
               op2b * ProjectionParameterFunctional('q', 1))
        op = BlockRowOperator([op0, op2])
        mu = op.parameters.parse({'p': [1, 2, 3], 'q': 4})
        sp = BlockDiagonalOperator([sp0, sp1])
        rp = rp0
        U = op.source.make_array([U0, U1])
        V = V0
        return op, mu, U, V, sp, rp
    else:
        assert False
예제 #4
0
파일: operator.py 프로젝트: simon-ca/pymor
def misc_operator_with_arrays_and_products_factory(n):
    if n == 0:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 10, 4, 3, n)
        op = ComponentProjection(np.random.randint(0, 100, 10), U.space)
        return op, _, U, V, sp, rp
    elif n == 1:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 0, 4, 3, n)
        op = ComponentProjection([], U.space)
        return op, _, U, V, sp, rp
    elif n == 2:
        from pymor.operators.constructions import ComponentProjection
        _, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 3, 4, 3, n)
        op = ComponentProjection([3, 3, 3], U.space)
        return op, _, U, V, sp, rp
    elif n == 3:
        from pymor.operators.constructions import AdjointOperator
        op, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 20, 4, 3, n)
        op = AdjointOperator(op, with_apply_inverse=True)
        return op, _, V, U, rp, sp
    elif n == 4:
        from pymor.operators.constructions import AdjointOperator
        op, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(100, 20, 4, 3, n)
        op = AdjointOperator(op, with_apply_inverse=False)
        return op, _, V, U, rp, sp
    elif 5 <= n <= 7:
        from pymor.operators.constructions import SelectionOperator
        from pymor.parameters.functionals import ProjectionParameterFunctional
        op0, _, U, V, sp, rp = numpy_matrix_operator_with_arrays_and_products_factory(30, 30, 4, 3, n)
        op1 = NumpyMatrixOperator(np.random.random((30, 30)))
        op2 = NumpyMatrixOperator(np.random.random((30, 30)))
        op = SelectionOperator([op0, op1, op2], ProjectionParameterFunctional('x', ()), [0.3, 0.6])
        return op, op.parse_parameter((n-5)/2), V, U, rp, sp
    elif n == 8:
        from pymor.operators.block import BlockDiagonalOperator
        from pymor.vectorarrays.block import BlockVectorArray
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(20, 20, 4, 3, n+1)
        op2, _, U2, V2, sp2, rp2 = numpy_matrix_operator_with_arrays_and_products_factory(30, 30, 4, 3, n+2)
        op = BlockDiagonalOperator([op0, op1, op2])
        sp = BlockDiagonalOperator([sp0, sp1, sp2])
        rp = BlockDiagonalOperator([rp0, rp1, rp2])
        U = BlockVectorArray([U0, U1, U2])
        V = BlockVectorArray([V0, V1, V2])
        return op, _, U, V, sp, rp
    elif n == 9:
        from pymor.operators.block import BlockDiagonalOperator, BlockOperator
        from pymor.vectorarrays.block import BlockVectorArray
        op0, _, U0, V0, sp0, rp0 = numpy_matrix_operator_with_arrays_and_products_factory(10, 10, 4, 3, n)
        op1, _, U1, V1, sp1, rp1 = numpy_matrix_operator_with_arrays_and_products_factory(20, 20, 4, 3, n+1)
        op2, _, U2, V2, sp2, rp2 = numpy_matrix_operator_with_arrays_and_products_factory(20, 10, 4, 3, n+2)
        op = BlockOperator([[op0, op2],
                            [None, op1]])
        sp = BlockDiagonalOperator([sp0, sp1])
        rp = BlockDiagonalOperator([rp0, rp1])
        U = BlockVectorArray([U0, U1])
        V = BlockVectorArray([V0, V1])
        return op, None, U, V, sp, rp
    else:
        assert False