Exemple #1
0
def test_lincomb_op():
    p1 = MonomOperator(1)
    p2 = MonomOperator(2)
    p12 = p1 + p2
    p0 = p1 - p1
    x = np.linspace(-1., 1., num=3)
    vx = p1.source.make_array((x[:, np.newaxis]))
    one = p1.source.make_array([1])
    assert np.allclose(p0.apply(vx).to_numpy(), [0.])
    assert np.allclose(p12.apply(vx).to_numpy(), (x * x + x)[:, np.newaxis])
    assert np.allclose((p1 * 2.).apply(vx).to_numpy(), (x * 2.)[:, np.newaxis])
    with pytest.raises(AssertionError):
        p2.jacobian(vx)
    for i in range(len(vx)):
        assert almost_equal(
            p2.jacobian(vx[i]).apply(one),
            p1.apply(vx[i]) * 2.)
        assert almost_equal(p0.jacobian(vx[i]).apply(one), vx[i] * 0.)
    with pytest.raises(TypeError):
        p2.as_vector()
    p1.as_vector()
    assert almost_equal(p1.as_vector(), p1.apply(p1.source.make_array([1.])))

    basis = p1.source.make_array([1.])
    for p in (p1, p2, p12):
        projected = project(p, basis, basis)
        pa = projected.apply(vx)
        assert almost_equal(pa, p.apply(vx)).all()
Exemple #2
0
def test_lincomb_op_with_zero_coefficients():
    p1 = MonomOperator(1)
    p2 = MonomOperator(2)
    p10 = p1 + 0 * p2
    p0 = 0 * p1 + 0 * p1
    x = np.linspace(-1., 1., num=3)
    vx = p1.source.make_array((x[:, np.newaxis]))

    pc1 = NumpyMatrixOperator(np.eye(p1.source.dim))
    pc2 = NumpyMatrixOperator(2 * np.eye(p1.source.dim))
    pc10 = pc1 + 0 * pc2
    pc0 = 0 * pc1 + 0 * pc2

    assert np.allclose(p0.apply(vx).to_numpy(), [0.])
    assert len(p0.apply(vx)) == len(vx)
    assert almost_equal(p10.apply(vx), p1.apply(vx)).all()

    assert np.allclose(p0.apply2(vx, vx), [0.])
    assert len(p0.apply2(vx, vx)) == len(vx)
    assert np.allclose(p10.apply2(vx, vx), p1.apply2(vx, vx))

    assert np.allclose(p0.pairwise_apply2(vx, vx), [0.])
    assert len(p0.pairwise_apply2(vx, vx)) == len(vx)
    assert np.allclose(p10.pairwise_apply2(vx, vx), p1.pairwise_apply2(vx, vx))

    assert np.allclose(pc0.apply_adjoint(vx).to_numpy(), [0.])
    assert len(pc0.apply_adjoint(vx)) == len(vx)
    assert almost_equal(pc10.apply_adjoint(vx), pc1.apply_adjoint(vx)).all()
Exemple #3
0
def test_selection_op():
    p1 = MonomOperator(1)
    select_rhs_functional = GenericParameterFunctional(
        lambda x: round(x["nrrhs"].item()), {"nrrhs": 1})
    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).to_numpy(),
        s1.apply(vx, mu=s1.parameters.parse(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(s2.parameters.parse({"nrrhs": -4})) == 0
    assert s2._get_operator_number(s2.parameters.parse({"nrrhs": -3})) == 0
    assert s2._get_operator_number(s2.parameters.parse({"nrrhs": -2})) == 1
    assert s2._get_operator_number(s2.parameters.parse({"nrrhs": 3})) == 1
    assert s2._get_operator_number(s2.parameters.parse({"nrrhs": 4})) == 2
    assert s2._get_operator_number(s2.parameters.parse({"nrrhs": 7})) == 2
    assert s2._get_operator_number(s2.parameters.parse({"nrrhs": 9})) == 3
Exemple #4
0
def test_newton(order, error_measure):
    mop = MonomOperator(order)
    U, _ = _newton(mop,
                   atol=1e-15 if error_measure == 'residual' else 1e-7,
                   rtol=0.,
                   error_measure=error_measure)
    assert float_cmp(mop.apply(U).to_numpy(), 0.0)
Exemple #5
0
def test_newton_residual_is_zero(order=5):
    mop = MonomOperator(order)
    U, _ = _newton(mop, initial_value=0.0)
    assert float_cmp(mop.apply(U).to_numpy(), 0.0)
Exemple #6
0
def test_newton_with_line_search():
    mop = MonomOperator(3) - 2 * MonomOperator(1) + 2 * MonomOperator(0)
    U, _ = _newton(mop, initial_value=0.0, atol=1e-15, relax='armijo')
    assert float_cmp(mop.apply(U).to_numpy(), 0.0)