diff --git a/Wrappers/Python/test/test_BlockOperator.py b/Wrappers/Python/test/test_BlockOperator.py index 3e81ab4cad..bff62cd5af 100644 --- a/Wrappers/Python/test/test_BlockOperator.py +++ b/Wrappers/Python/test/test_BlockOperator.py @@ -26,7 +26,7 @@ from cil.framework import ImageGeometry, ImageData import numpy from cil.optimisation.operators import FiniteDifferenceOperator - +from cil.optimisation.operators import LinearOperator initialise_tests() class TestBlockOperator(unittest.TestCase): @@ -187,9 +187,9 @@ def test_FiniteDiffOperator(self): G = FiniteDifferenceOperator(ig, direction=0, bnd_cond = 'Neumann') logging.info("{} {}".format(type(u), str(u.as_array()))) logging.info(str(G.direct(u).as_array())) - + LinearOperator.PowerMethod(G, range_is_domain=False) # Gradient Operator norm, for one direction should be close to 2 - numpy.testing.assert_allclose(G.norm(), numpy.sqrt(4), atol=0.1) + numpy.testing.assert_allclose(LinearOperator.PowerMethod(G, range_is_domain=False), numpy.sqrt(4), atol=0.1) M1, N1, K1 = 200, 300, 2 ig1 = ImageGeometry(voxel_num_x = M1, voxel_num_y = N1, channels = K1) diff --git a/Wrappers/Python/test/test_Operator.py b/Wrappers/Python/test/test_Operator.py index 4d2cf5c1e4..ea756157d6 100644 --- a/Wrappers/Python/test/test_Operator.py +++ b/Wrappers/Python/test/test_Operator.py @@ -306,25 +306,32 @@ def test_PowerMethod(self): numpy.testing.assert_almost_equal(res1,3.1624439599276974, decimal=4) # 2x2 non-diagonalisable nilpotent matrix - with self.assertRaises(ValueError): - - M1=numpy.array([[0.,1.], [0.,0.]]) - M1op = MatrixOperator(M1) - res1 = M1op.PowerMethod(M1op,5) - + M1=numpy.array([[0.,1.], [0.,0.]]) + M1op = MatrixOperator(M1) + res1 = M1op.PowerMethod(M1op,5) + numpy.testing.assert_almost_equal(res1,0, decimal=4) + + # 2x2 non-diagonalisable nilpotent matrix where range_is_domain is False + M1=numpy.array([[0.,1.], [0.,0.]]) + M1op = MatrixOperator(M1) + res1 = M1op.PowerMethod(M1op,5, range_is_domain=False) + numpy.testing.assert_almost_equal(res1,1, decimal=4) + + # 2x2 matrix, max absolute eigenvalue is not unique and initial vector chosen for non-convergence - with self.assertWarns(Warning): - M1=numpy.array([[2.,1.], [0.,-2.]]) - M1op = MatrixOperator(M1) - res1 = M1op.PowerMethod(M1op,100, initial=DataContainer(numpy.array([1.,1.]))) + + M1=numpy.array([[2.,1.], [0.,-2.]]) + M1op = MatrixOperator(M1) + _,_,_,_,convergence = M1op.PowerMethod(M1op,100, initial=DataContainer(numpy.array([1.,1.])), return_all=True) + numpy.testing.assert_equal(convergence,False) # 2x2 matrix, max absolute eigenvalue is not unique and initial vector chosen for convergence M1=numpy.array([[2.,1.,0.],[0.,1.,1.], [0.,0.,1.]]) M1op = MatrixOperator(M1) - res1 = M1op.PowerMethod(M1op,100) + res1,_,_,_,convergence = M1op.PowerMethod(M1op,100, return_all=True) numpy.testing.assert_almost_equal(res1,2., decimal=4) - + numpy.testing.assert_equal(convergence,True) # Gradient Operator (float) ig = ImageGeometry(30,30)