def test_pointwise_inner_init_properties(): fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 3, exponent=2) # Make sure the code runs and test the properties pwinner = PointwiseInner(vfspace, vfspace.one()) assert pwinner.base_space == fspace assert all_equal(pwinner.weights, [1, 1, 1]) assert not pwinner.is_weighted repr(pwinner) pwinner = PointwiseInner(vfspace, vfspace.one(), weight=[1, 2, 3]) assert all_equal(pwinner.weights, [1, 2, 3]) assert pwinner.is_weighted # Bad input with pytest.raises(TypeError): PointwiseInner(odl.Rn(3), odl.Rn(3).one()) # No power space # TODO: Does not raise currently, although bad_vecfield not in vfspace! """ bad_vecfield = ProductSpace(fspace, 3, exponent=1).one() with pytest.raises(TypeError): PointwiseInner(vfspace, bad_vecfield) """ with pytest.raises(ValueError): PointwiseInner(vfspace, vfspace.one(), weight=-1) # < 0 not allowed with pytest.raises(ValueError): PointwiseInner(vfspace, vfspace.one(), weight=[1, 0, 1]) # 0 invalid
def test_custom_funcs(): # Checking the standard 1-norm and standard inner product, just to # see that the functions are handled correctly. r2 = odl.Rn(2) r2x = r2.element([1, -1]) r2y = r2.element([-2, 3]) # inner = -5, dist = 5, norms = (sqrt(2), sqrt(13)) r3 = odl.Rn(3) r3x = r3.element([3, 4, 4]) r3y = r3.element([1, -2, 1]) # inner = -1, dist = 7, norms = (sqrt(41), sqrt(6)) pspace_2 = odl.ProductSpace(r2, r3, exponent=2.0) x = pspace_2.element((r2x, r3x)) y = pspace_2.element((r2y, r3y)) pspace_custom = odl.ProductSpace(r2, r3, inner=custom_inner) xc = pspace_custom.element((r2x, r3x)) yc = pspace_custom.element((r2y, r3y)) assert almost_equal(x.inner(y), xc.inner(yc)) pspace_1 = odl.ProductSpace(r2, r3, exponent=1.0) x = pspace_1.element((r2x, r3x)) y = pspace_1.element((r2y, r3y)) pspace_custom = odl.ProductSpace(r2, r3, norm=custom_norm) xc = pspace_custom.element((r2x, r3x)) assert almost_equal(x.norm(), xc.norm()) pspace_custom = odl.ProductSpace(r2, r3, dist=custom_dist) xc = pspace_custom.element((r2x, r3x)) yc = pspace_custom.element((r2y, r3y)) assert almost_equal(x.dist(y), xc.dist(yc)) with pytest.raises(TypeError): odl.ProductSpace(r2, r3, a=1) # extra keyword argument with pytest.raises(ValueError): odl.ProductSpace(r2, r3, norm=custom_norm, inner=custom_inner) with pytest.raises(ValueError): odl.ProductSpace(r2, r3, dist=custom_dist, inner=custom_inner) with pytest.raises(ValueError): odl.ProductSpace(r2, r3, norm=custom_norm, dist=custom_dist) with pytest.raises(ValueError): odl.ProductSpace(r2, r3, norm=custom_norm, exponent=1.0) with pytest.raises(ValueError): odl.ProductSpace(r2, r3, norm=custom_norm, weight=2.0) with pytest.raises(ValueError): odl.ProductSpace(r2, r3, dist=custom_dist, weight=2.0) with pytest.raises(ValueError): odl.ProductSpace(r2, r3, inner=custom_inner, weight=2.0)
def test_getitem_fancy(): r1 = odl.Rn(1) r2 = odl.Rn(2) r3 = odl.Rn(3) H = odl.ProductSpace(r1, r2, r3) assert H[[0, 2]] == odl.ProductSpace(r1, r3) assert H[[0, 2]][0] is r1 assert H[[0, 2]][1] is r3
def test_vector_getitem_fancy(): H = odl.ProductSpace(odl.Rn(1), odl.Rn(2), odl.Rn(3)) x1 = H[0].element([0]) x2 = H[1].element([1, 2]) x3 = H[2].element([3, 4, 5]) x = H.element([x1, x2, x3]) assert x[[0, 2]].space == H[[0, 2]] assert x[[0, 2]][0] is x1 assert x[[0, 2]][1] is x3
def test_getitem_slice(): r1 = odl.Rn(1) r2 = odl.Rn(2) r3 = odl.Rn(3) H = odl.ProductSpace(r1, r2, r3) assert H[:2] == odl.ProductSpace(r1, r2) assert H[:2][0] is r1 assert H[:2][1] is r2 assert H[3:] == odl.ProductSpace(field=r1.field)
def test_getitem_single(): r1 = odl.Rn(1) r2 = odl.Rn(2) H = odl.ProductSpace(r1, r2) assert H[-2] is r1 assert H[-1] is r2 assert H[0] is r1 assert H[1] is r2 with pytest.raises(IndexError): H[-3] H[2]
def test_vector_getitem_single(): H = odl.ProductSpace(odl.Rn(1), odl.Rn(2)) x1 = H[0].element([0]) x2 = H[1].element([1, 2]) x = H.element([x1, x2]) assert x[-2] is x1 assert x[-1] is x2 assert x[0] is x1 assert x[1] is x2 with pytest.raises(IndexError): x[-3] x[2]
def test_vector_equals(): H = odl.ProductSpace(odl.Rn(1), odl.Rn(2)) x = H.element([[0], [1, 2]]) assert x != 0 # test == not always true assert x == x x_2 = H.element([[0], [1, 2]]) assert x == x_2 x_3 = H.element([[3], [1, 2]]) assert x != x_3 x_4 = H.element([[0], [1, 3]]) assert x != x_4
def test_metric(): H = odl.Rn(2) v11 = H.element([1, 2]) v12 = H.element([5, 3]) v21 = H.element([1, 2]) v22 = H.element([8, 9]) # 1-norm HxH = odl.ProductSpace(H, H, exponent=1.0) w1 = HxH.element([v11, v12]) w2 = HxH.element([v21, v22]) assert almost_equal(HxH.dist(w1, w2), H.dist(v11, v21) + H.dist(v12, v22)) # 2-norm HxH = odl.ProductSpace(H, H, exponent=2.0) w1 = HxH.element([v11, v12]) w2 = HxH.element([v21, v22]) assert almost_equal(HxH.dist(w1, w2), (H.dist(v11, v21)**2 + H.dist(v12, v22)**2)**(1 / 2.0)) # inf norm HxH = odl.ProductSpace(H, H, exponent=float('inf')) w1 = HxH.element([v11, v12]) w2 = HxH.element([v21, v22]) assert almost_equal(HxH.dist(w1, w2), max(H.dist(v11, v21), H.dist(v12, v22)))
def test_pspace_op_weighted_init(): r3 = odl.Rn(3) ran = odl.ProductSpace(r3, 2, weight=[1, 2]) I = odl.IdentityOperator(r3) with pytest.raises(NotImplementedError): odl.ProductSpaceOperator([[I], [0]], range=ran)
def test_comp_proj_indices(): r3 = odl.Rn(3) r33 = odl.ProductSpace(r3, 3) x = r33.element([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) proj = odl.ComponentProjection(r33, [0, 2]) assert x[[0, 2]] == proj(x) assert x[[0, 2]] == proj(x, out=proj.range.element())