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_pointwise_inner_adjoint(): # 1d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2), dtype=complex) vfspace = ProductSpace(fspace, 1) array = np.array([[[-1, -3], [2, 0]]]) pwinner = PointwiseInner(vfspace, vecfield=array) testarr = np.array([[1 + 1j, 2], [3, 4 - 2j]]) true_inner_adj = testarr[None, :, :] * array testfunc = fspace.element(testarr) testfunc_pwinner_adj = pwinner.adjoint(testfunc) assert all_almost_equal(testfunc_pwinner_adj, true_inner_adj.reshape([1, -1])) out = vfspace.element() pwinner.adjoint(testfunc, out=out) assert all_almost_equal(out, true_inner_adj.reshape([1, -1])) # 3d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2), dtype=complex) vfspace = ProductSpace(fspace, 3) array = np.array([[[-1 - 1j, -3], [2, 2j]], [[-1j, 0], [0, 1]], [[-1, 1 + 2j], [1, 1]]]) pwinner = PointwiseInner(vfspace, vecfield=array) testarr = np.array([[1 + 1j, 2], [3, 4 - 2j]]) true_inner_adj = testarr[None, :, :] * array testfunc = fspace.element(testarr) testfunc_pwinner_adj = pwinner.adjoint(testfunc) assert all_almost_equal(testfunc_pwinner_adj, true_inner_adj.reshape([3, -1])) out = vfspace.element() pwinner.adjoint(testfunc, out=out) assert all_almost_equal(out, true_inner_adj.reshape([3, -1]))
def test_pointwise_inner_adjoint_weighted(): # Weighted product space only fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2), dtype=complex) vfspace = ProductSpace(fspace, 3, weight=[2, 4, 6]) array = np.array([[[-1 - 1j, -3], [2, 2j]], [[-1j, 0], [0, 1]], [[-1, 1 + 2j], [1, 1]]]) pwinner = PointwiseInner(vfspace, vecfield=array) testarr = np.array([[1 + 1j, 2], [3, 4 - 2j]]) true_inner_adj = testarr[None, :, :] * array # same as unweighted case testfunc = fspace.element(testarr) testfunc_pwinner_adj = pwinner.adjoint(testfunc) assert all_almost_equal(testfunc_pwinner_adj, true_inner_adj.reshape([3, -1])) out = vfspace.element() pwinner.adjoint(testfunc, out=out) assert all_almost_equal(out, true_inner_adj.reshape([3, -1])) # Using different weighting in the inner product pwinner = PointwiseInner(vfspace, vecfield=array, weight=[4, 8, 12]) testarr = np.array([[1 + 1j, 2], [3, 4 - 2j]]) true_inner_adj = 2 * testarr[None, :, :] * array # w / v = (2, 2, 2) testfunc = fspace.element(testarr) testfunc_pwinner_adj = pwinner.adjoint(testfunc) assert all_almost_equal(testfunc_pwinner_adj, true_inner_adj.reshape([3, -1])) out = vfspace.element() pwinner.adjoint(testfunc, out=out) assert all_almost_equal(out, true_inner_adj.reshape([3, -1]))
def test_pointwise_inner_real(): # 1d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 1) array = np.array([[[-1, -3], [2, 0]]]) pwinner = PointwiseInner(vfspace, vecfield=array) testarr = np.array([[[1, 2], [3, 4]]]) true_inner = np.sum(testarr * array, axis=0) func = vfspace.element(testarr) func_pwinner = pwinner(func) assert all_almost_equal(func_pwinner, true_inner.reshape(-1)) out = fspace.element() pwinner(func, out=out) assert all_almost_equal(out, true_inner.reshape(-1)) # 3d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 3) array = np.array([[[-1, -3], [2, 0]], [[0, 0], [0, 1]], [[-1, 1], [1, 1]]]) pwinner = PointwiseInner(vfspace, vecfield=array) testarr = np.array([[[1, 2], [3, 4]], [[0, -1], [0, 1]], [[1, 1], [1, 1]]]) true_inner = np.sum(testarr * array, axis=0) func = vfspace.element(testarr) func_pwinner = pwinner(func) assert all_almost_equal(func_pwinner, true_inner.reshape(-1)) out = fspace.element() pwinner(func, out=out) assert all_almost_equal(out, true_inner.reshape(-1))
def test_pointwise_inner_weighted(): fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 3) array = np.array([[[-1, -3], [2, 0]], [[0, 0], [0, 1]], [[-1, 1], [1, 1]]]) weight = np.array([1.0, 2.0, 3.0]) pwinner = PointwiseInner(vfspace, vecfield=array, weight=weight) testarr = np.array([[[1, 2], [3, 4]], [[0, -1], [0, 1]], [[1, 1], [1, 1]]]) true_inner = np.sum(weight[:, None, None] * testarr * array, axis=0) func = vfspace.element(testarr) func_pwinner = pwinner(func) assert all_almost_equal(func_pwinner, true_inner.reshape(-1)) out = fspace.element() pwinner(func, out=out) assert all_almost_equal(out, true_inner.reshape(-1))
def test_pointwise_inner_complex(): fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2), dtype=complex) vfspace = ProductSpace(fspace, 3) array = np.array([[[-1 - 1j, -3], [2, 2j]], [[-1j, 0], [0, 1]], [[-1, 1 + 2j], [1, 1]]]) pwinner = PointwiseInner(vfspace, vecfield=array) testarr = np.array([[[1 + 1j, 2], [3, 4 - 2j]], [[0, -1], [0, 1]], [[1j, 1j], [1j, 1j]]]) true_inner = np.sum(testarr * array.conj(), axis=0) func = vfspace.element(testarr) func_pwinner = pwinner(func) assert all_almost_equal(func_pwinner, true_inner.reshape(-1)) out = fspace.element() pwinner(func, out=out) assert all_almost_equal(out, true_inner.reshape(-1))