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