test_conjugate_gradient.py 1.99 KB
Newer Older
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
1
2
3

import unittest

Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
4
from numpy.testing import assert_equal, assert_almost_equal
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
5
6
7
8
9
10
11

from nifty import *

from itertools import product
from test.common import expand
from test.common import generate_spaces

Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
12
np.random.seed(42)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


class ConjugateGradient_Tests(unittest.TestCase):
    spaces = generate_spaces()


    @expand(product(spaces, [10,  100, 1000], [1E-3, 1E-4, 1E-5], [2, 3, 4] ))
    def test_property(self, space, iteration_limit, convergence_tolerance, 
                      convergence_level):
        
        x0 = Field.from_random('normal', domain=space)
        A = DiagonalOperator(space, diagonal = 1.)
        b = Field(space, val=0.)
        
        minimizer = ConjugateGradient(iteration_limit=iteration_limit,
                                    convergence_tolerance=convergence_tolerance, 
                                    convergence_level=convergence_level)
                                    
        (position, convergence) = minimizer(A=A, x0=x0, b=b)
        
        if position.domain[0] != space:
            raise TypeError
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
35
        if type(convergence) != int:
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
36
37
38
39
40
41
42
            raise TypeError

    @expand(product(spaces, [10,  100, 1000], [1E-3, 1E-4, 1E-5], [2, 3, 4] ))
    def test_property(self, space, iteration_limit, convergence_tolerance, 
                      convergence_level):
        
        x0 = Field.from_random('normal', domain=space)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
43
        test_x = Field(space, val = 1.)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
44
        A = DiagonalOperator(space, diagonal = 1.)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
45
        b = Field(space, val=1.)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
46
47
48
49
50
51
52
        
        minimizer = ConjugateGradient(iteration_limit=iteration_limit,
                                    convergence_tolerance=convergence_tolerance, 
                                    convergence_level=convergence_level)
                                    
        (position, convergence) = minimizer(A=A, x0=x0, b=b)
        
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
53
        assert_almost_equal(position.val.get_full_data(), 
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
54
                            test_x.val.get_full_data(), decimal=3)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
55
56
57
        assert_equal(convergence, convergence_level+1)