def test_pointwise_norm_real(exponent): # 1d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 1) pwnorm = PointwiseNorm(vfspace, exponent) testarr = np.array([[[1, 2], [3, 4]]]) true_norm = np.linalg.norm(testarr, ord=exponent, axis=0) func = vfspace.element(testarr) func_pwnorm = pwnorm(func) assert all_almost_equal(func_pwnorm, true_norm.reshape(-1)) out = fspace.element() pwnorm(func, out=out) assert all_almost_equal(out, true_norm.reshape(-1)) # 3d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 3) pwnorm = PointwiseNorm(vfspace, exponent) testarr = np.array([[[1, 2], [3, 4]], [[0, -1], [0, 1]], [[1, 1], [1, 1]]]) true_norm = np.linalg.norm(testarr, ord=exponent, axis=0) func = vfspace.element(testarr) func_pwnorm = pwnorm(func) assert all_almost_equal(func_pwnorm, true_norm.reshape(-1)) out = fspace.element() pwnorm(func, out=out) assert all_almost_equal(out, true_norm.reshape(-1))
def test_pointwise_norm_init_properties(): # 1d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 1, exponent=1) # Make sure the code runs and test the properties pwnorm = PointwiseNorm(vfspace) assert pwnorm.base_space == fspace assert all_equal(pwnorm.weights, [1]) assert not pwnorm.is_weighted assert pwnorm.exponent == 1.0 repr(pwnorm) pwnorm = PointwiseNorm(vfspace, exponent=2) assert pwnorm.exponent == 2 pwnorm = PointwiseNorm(vfspace, weighting=2) assert all_equal(pwnorm.weights, [2]) assert pwnorm.is_weighted # 3d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 3, exponent=1) # Make sure the code runs and test the properties pwnorm = PointwiseNorm(vfspace) assert pwnorm.base_space == fspace assert all_equal(pwnorm.weights, [1, 1, 1]) assert not pwnorm.is_weighted assert pwnorm.exponent == 1.0 repr(pwnorm) pwnorm = PointwiseNorm(vfspace, exponent=2) assert pwnorm.exponent == 2 pwnorm = PointwiseNorm(vfspace, weighting=[1, 2, 3]) assert all_equal(pwnorm.weights, [1, 2, 3]) assert pwnorm.is_weighted # Bad input with pytest.raises(TypeError): PointwiseNorm(odl.rn(3)) # No power space with pytest.raises(ValueError): PointwiseNorm(vfspace, exponent=0.5) # < 1 not allowed with pytest.raises(ValueError): PointwiseNorm(vfspace, weighting=-1) # < 0 not allowed with pytest.raises(ValueError): PointwiseNorm(vfspace, weighting=[1, 0, 1]) # 0 invalid
def test_pointwise_norm_gradient_real(exponent): # The operator is not differentiable for exponent 'inf' if exponent == float('inf'): fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 1) pwnorm = PointwiseNorm(vfspace, exponent) point = vfspace.one() with pytest.raises(NotImplementedError): pwnorm.derivative(point) return # 1d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 1) pwnorm = PointwiseNorm(vfspace, exponent) point = noise_element(vfspace) direction = noise_element(vfspace) # Computing expected result tmp = pwnorm(point).ufuncs.power(1 - exponent) v_field = vfspace.element() for i in range(len(v_field)): v_field[i] = tmp * point[i] * np.abs(point[i])**(exponent - 2) pwinner = odl.PointwiseInner(vfspace, v_field) expected_result = pwinner(direction) func_pwnorm = pwnorm.derivative(point) assert all_almost_equal(func_pwnorm(direction), expected_result) # 3d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 3) pwnorm = PointwiseNorm(vfspace, exponent) point = noise_element(vfspace) direction = noise_element(vfspace) # Computing expected result tmp = pwnorm(point).ufuncs.power(1 - exponent) v_field = vfspace.element() for i in range(len(v_field)): v_field[i] = tmp * point[i] * np.abs(point[i])**(exponent - 2) pwinner = odl.PointwiseInner(vfspace, v_field) expected_result = pwinner(direction) func_pwnorm = pwnorm.derivative(point) assert all_almost_equal(func_pwnorm(direction), expected_result)
def test_pointwise_norm_weighted(exponent): fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 3) weight = np.array([1.0, 2.0, 3.0]) pwnorm = PointwiseNorm(vfspace, exponent, weighting=weight) testarr = np.array([[[1, 2], [3, 4]], [[0, -1], [0, 1]], [[1, 1], [1, 1]]]) if exponent in (1.0, float('inf')): true_norm = np.linalg.norm(weight[:, None, None] * testarr, ord=exponent, axis=0) else: true_norm = np.linalg.norm(weight[:, None, None]**(1 / exponent) * testarr, ord=exponent, axis=0) func = vfspace.element(testarr) func_pwnorm = pwnorm(func) assert all_almost_equal(func_pwnorm, true_norm.reshape(-1)) out = fspace.element() pwnorm(func, out=out) assert all_almost_equal(out, true_norm.reshape(-1))
def test_pointwise_norm_complex(exponent): fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2), dtype=complex) vfspace = ProductSpace(fspace, 3) pwnorm = PointwiseNorm(vfspace, exponent) testarr = np.array([[[1 + 1j, 2], [3, 4 - 2j]], [[0, -1], [0, 1]], [[1j, 1j], [1j, 1j]]]) true_norm = np.linalg.norm(testarr, ord=exponent, axis=0) func = vfspace.element(testarr) func_pwnorm = pwnorm(func) assert all_almost_equal(func_pwnorm, true_norm) out = fspace.element() pwnorm(func, out=out) assert all_almost_equal(out, true_norm)
def test_pointwise_norm_gradient_real_with_zeros(exponent): # The gradient is only well-defined in points with zeros if the exponent is # >= 2 and < inf if exponent < 2 or exponent == float('inf'): pytest.skip('differential of operator has singularity for this ' 'exponent') # 1d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 1) pwnorm = PointwiseNorm(vfspace, exponent) test_point = np.array([[[0, 0], # This makes the point singular for p < 2 [1, 2]]]) test_direction = np.array([[[1, 2], [4, 5]]]) point = vfspace.element(test_point) direction = vfspace.element(test_direction) func_pwnorm = pwnorm.derivative(point) assert not np.any(np.isnan(func_pwnorm(direction))) # 3d fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2)) vfspace = ProductSpace(fspace, 3) pwnorm = PointwiseNorm(vfspace, exponent) test_point = np.array([[[0, 0], # This makes the point singular for p < 2 [1, 2]], [[3, 4], [0, 0]], # This makes the point singular for p < 2 [[5, 6], [7, 8]]]) test_direction = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]], [[8, 9], [0, 1]]]) point = vfspace.element(test_point) direction = vfspace.element(test_direction) func_pwnorm = pwnorm.derivative(point) assert not np.any(np.isnan(func_pwnorm(direction)))