test_diagonal_operator.py 2.8 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
from __future__ import division
2
import unittest
Martin Reinecke's avatar
Martin Reinecke committed
3
from numpy.testing import assert_equal, assert_allclose
Martin Reinecke's avatar
Martin Reinecke committed
4
from nifty2go import Field, DiagonalOperator
5
from test.common import generate_spaces
6
7
from itertools import product
from test.common import expand
Martin Reinecke's avatar
Martin Reinecke committed
8
9
from nifty2go.dobj import to_ndarray as to_np

10
11

class DiagonalOperator_Tests(unittest.TestCase):
12
    spaces = generate_spaces()
13

14
15
    @expand(product(spaces))
    def test_property(self, space):
16
        diag = Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
17
        D = DiagonalOperator(diag)
18
19
        if D.domain[0] != space:
            raise TypeError
Martin Reinecke's avatar
Martin Reinecke committed
20
        if D.unitary:
21
            raise TypeError
Martin Reinecke's avatar
Martin Reinecke committed
22
        if not D.self_adjoint:
23
24
            raise TypeError

25
26
    @expand(product(spaces))
    def test_times_adjoint(self, space):
27
28
29
        rand1 = Field.from_random('normal', domain=space)
        rand2 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
30
        D = DiagonalOperator(diag)
Martin Reinecke's avatar
Martin Reinecke committed
31
32
        tt1 = rand1.vdot(D.times(rand2))
        tt2 = rand2.vdot(D.times(rand1))
Martin Reinecke's avatar
Martin Reinecke committed
33
        assert_allclose(tt1, tt2)
34

35
36
    @expand(product(spaces))
    def test_times_inverse(self, space):
37
38
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
39
        D = DiagonalOperator(diag)
40
        tt1 = D.times(D.inverse_times(rand1))
Martin Reinecke's avatar
Martin Reinecke committed
41
        assert_allclose(to_np(rand1.val), to_np(tt1.val))
42

43
44
    @expand(product(spaces))
    def test_times(self, space):
45
46
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
47
        D = DiagonalOperator(diag)
48
        tt = D.times(rand1)
49
        assert_equal(tt.domain[0], space)
50

51
52
    @expand(product(spaces))
    def test_adjoint_times(self, space):
53
54
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
55
        D = DiagonalOperator(diag)
56
        tt = D.adjoint_times(rand1)
57
        assert_equal(tt.domain[0], space)
58

59
60
    @expand(product(spaces))
    def test_inverse_times(self, space):
61
62
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
63
        D = DiagonalOperator(diag)
64
        tt = D.inverse_times(rand1)
65
        assert_equal(tt.domain[0], space)
66

67
68
    @expand(product(spaces))
    def test_adjoint_inverse_times(self, space):
69
70
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
71
        D = DiagonalOperator(diag)
72
        tt = D.adjoint_inverse_times(rand1)
73
        assert_equal(tt.domain[0], space)
74

75
76
    @expand(product(spaces))
    def test_diagonal(self, space):
77
        diag = Field.from_random('normal', domain=space)
78
        D = DiagonalOperator(diag)
79
        diag_op = D.diagonal()
Martin Reinecke's avatar
Martin Reinecke committed
80
        assert_allclose(to_np(diag.val), to_np(diag_op.val))