def test_MatrixOperator(self):

        print('Check MatrixOperator')

        m = 30
        n = 20

        vg = VectorGeometry(n)

        Amat = numpy.random.randn(m, n)
        A = MatrixOperator(Amat)

        b = vg.allocate('random')

        out1 = A.range_geometry().allocate()
        out2 = A.domain_geometry().allocate()

        res1 = A.direct(b)
        res2 = numpy.dot(A.A, b.as_array())
        self.assertNumpyArrayAlmostEqual(res1.as_array(), res2)

        A.direct(b, out=out1)
        self.assertNumpyArrayAlmostEqual(res1.as_array(),
                                         out1.as_array(),
                                         decimal=4)

        res3 = A.adjoint(res1)
        res4 = numpy.dot(A.A.transpose(), res1.as_array())
        self.assertNumpyArrayAlmostEqual(res3.as_array(), res4, decimal=4)

        A.adjoint(res1, out=out2)
        self.assertNumpyArrayAlmostEqual(res3.as_array(),
                                         out2.as_array(),
                                         decimal=4)
예제 #2
0
    def test_GDArmijo2(self):
        from cil.optimisation.functions import Rosenbrock
        from cil.framework import VectorData, VectorGeometry

        f = Rosenbrock(alpha=1., beta=100.)
        vg = VectorGeometry(2)
        x = vg.allocate('random_int', seed=2)
        # x = vg.allocate('random', seed=1)
        x.fill(numpy.asarray([10., -3.]))

        max_iter = 10000
        update_interval = 1000

        alg = GD(x,
                 f,
                 max_iteration=max_iter,
                 update_objective_interval=update_interval,
                 alpha=1e6)

        alg.run(verbose=0)

        print(alg.get_output().as_array(), alg.step_size, alg.kmax, alg.k)

        # this with 10k iterations
        numpy.testing.assert_array_almost_equal(alg.get_output().as_array(),
                                                [0.13463363, 0.01604593],
                                                decimal=5)
예제 #3
0
    def __init__(self, A):
        '''creator

        :param A: numpy ndarray representing a matrix
        '''
        self.A = A
        M_A, N_A = self.A.shape
        domain_geometry = VectorGeometry(N_A)
        range_geometry = VectorGeometry(M_A)
        self.s1 = None  # Largest singular value, initially unknown
        super(MatrixOperator, self).__init__(domain_geometry=domain_geometry,
                                             range_geometry=range_geometry)
예제 #4
0
    def test_Norm2sq_as_OperatorCompositionFunction(self):

        print('Test for OperatorCompositionFunction')

        M, N = 50, 50
        ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N)
        #numpy.random.seed(1)
        b = ig.allocate('random', seed=1)

        print('Check call with IdentityOperator operator... OK\n')
        operator = 3 * IdentityOperator(ig)

        u = ig.allocate('random', seed=50)
        f = 0.5 * L2NormSquared(b=b)
        func1 = OperatorCompositionFunction(f, operator)
        func2 = LeastSquares(operator, b, 0.5)
        print("f.L {}".format(f.L))
        print("0.5*f.L {}".format((0.5 * f).L))
        print("type func1 {}".format(type(func1)))
        print("func1.L {}".format(func1.L))
        print("func2.L {}".format(func2.L))
        print("operator.norm() {}".format(operator.norm()))

        numpy.testing.assert_almost_equal(func1(u), func2(u))

        print('Check gradient with IdentityOperator operator... OK\n')

        tmp1 = ig.allocate()
        tmp2 = ig.allocate()
        res_gradient1 = func1.gradient(u)
        res_gradient2 = func2.gradient(u)
        func1.gradient(u, out=tmp1)
        func2.gradient(u, out=tmp2)

        self.assertNumpyArrayAlmostEqual(res_gradient1.as_array(),
                                         res_gradient2.as_array())
        self.assertNumpyArrayAlmostEqual(tmp1.as_array(), tmp2.as_array())

        print('Check call with MatrixOperator... OK\n')
        mat = np.random.randn(M, N)
        operator = MatrixOperator(mat)
        vg = VectorGeometry(N)
        b = vg.allocate('random')
        u = vg.allocate('random')

        func1 = OperatorCompositionFunction(0.5 * L2NormSquared(b=b), operator)
        func2 = LeastSquares(operator, b, 0.5)

        self.assertNumpyArrayAlmostEqual(func1(u), func2(u))
        numpy.testing.assert_almost_equal(func1.L, func2.L)
예제 #5
0
    def test_VectorGeometry_allocate_complex(self):

        vg = VectorGeometry(3)
        self.complex_allocate_geometry_test(vg)
예제 #6
0
    def test_VectorGeometry_allocate_dtype(self):

        # print("Test VectorGeometry dtype\n")

        vg = VectorGeometry(3)
        self.dtype_allocate_test(vg)