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
def test_lincomb_op(): p1 = MonomOperator(1) p2 = MonomOperator(2) p12 = p1 + p2 p0 = p1 - p1 x = np.linspace(-1., 1., num=3) vx = NumpyVectorArray(x[:, np.newaxis]) assert np.allclose(p0.apply(vx).data, [0.]) assert np.allclose(p12.apply(vx).data, (x * x + x)[:, np.newaxis]) assert np.allclose((p1 * 2.).apply(vx).data, (x * 2.)[:, np.newaxis]) assert p2.jacobian(vx).apply(vx).almost_equal(p1.apply(vx) * 2.).all() assert p0.jacobian(vx).apply(vx).almost_equal(vx * 0.).all() with pytest.raises(TypeError): p2.as_vector() p1.as_vector() assert p1.as_vector().almost_equal(p1.apply(NumpyVectorArray(1.))) basis = NumpyVectorArray([1.]) for p in (p1, p2, p12): projected = p.projected(basis, basis) pa = projected.apply(vx) assert pa.almost_equal(p.apply(vx)).all()