test_diagonal_operator.py 5.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
import unittest

import numpy as np
from numpy.testing import assert_equal,\
    assert_allclose,\
    assert_approx_equal

from nifty import Field,\
    DiagonalOperator

11
from test.common import generate_spaces
12
13
14
15
16

from itertools import product
from test.common import expand

class DiagonalOperator_Tests(unittest.TestCase):
17
    spaces = generate_spaces()
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

    @expand(product(spaces, [True, False], [True, False]))
    def test_property(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag)
        if D.domain[0] != space:
            raise TypeError
        if D.unitary != True:
            raise TypeError
        if D.self_adjoint != True:
            raise TypeError

    @expand(product(spaces, [True, False], [True, False]))
    def test_times_adjoint(self, space, bare, copy):
        rand1 = Field.from_random('normal', domain=space)
        rand2 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        tt1 = rand1.dot(D.times(rand2))
        tt2 = rand2.dot(D.times(rand1))
        assert_approx_equal(tt1, tt2)

    @expand(product(spaces, [True, False], [True, False]))
    def test_times_inverse(self, space, bare, copy):
        rand1 = Field.from_random('normal', domain=space)
        rand2 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        tt1 = D.times(D.inverse_times(rand1))
        assert_allclose(rand1, tt1)
        tt2 = D.inverse_times(D.times(rand2))
        assert_allclose(rand2, tt2)

    @expand(product(spaces, [True, False], [True, False]))
    def test_times(self, space, bare, copy):
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        tt = D.times(rand1)
        assert_equal(tt.domain, space)

    @expand(product(spaces, [True, False], [True, False]))
    def test_adjoint_times(self, space, bare, copy):
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        tt = D.adjoint_times(rand1)
        assert_equal(tt.domain, space)

    @expand(product(spaces, [True, False], [True, False]))
    def test_inverse_times(self, space, bare, copy):
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        tt = D.inverse_times(rand1)
        assert_equal(tt.domain, space)

    @expand(product(spaces, [True, False], [True, False]))
    def test_adjoint_inverse_times(self, space, bare, copy):
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        tt = D.adjoint_inverse_times(rand1)
        assert_equal(tt.domain, space)

    @expand(product(spaces, [True, False], [True, False]))
    def test_diagonal(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        diag_op = D.diagonal(bare=bare, copy=copy)
        assert_allclose(diag.val.get_full_data(), diag_op.val.get_full_data())

    @expand(product(spaces, [True, False], [True, False]))
    def test_inverse(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        diag_op = D.inverse_diagonal(bare=bare)
        assert_allclose(1./diag.val.get_full_data(), diag_op.val.get_full_data())

    @expand(product(spaces, [True, False], [True, False]))
    def test_trace(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        trace_op = D.trace(bare=bare)
        assert_allclose(trace_op, diag.sum())

    @expand(product(spaces, [True, False], [True, False]))
    def test_inverse_trace(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        trace_op = D.inverse_trace(bare=bare)
        assert_allclose(trace_op, 1./diag.sum())

    @expand(product(spaces, [True, False], [True, False]))
    def test_trace_log(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        trace_log = D.trace_log()
        assert_allclose(trace_log, diag.apply_scalar_function(np.log).sum())

    @expand(product(spaces, [True, False], [True, False]))
    def test_determinant(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        det = D.determinant()
        assert_allclose(det, diag.val.get_full_data.prod())

    @expand(product(spaces, [True, False], [True, False]))
    def test_inverse_determinant(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        inv_det = D.inverse_determinant()
        assert_allclose(inv_det, 1./D.determinant())

    @expand(product(spaces, [True, False], [True, False]))
    def test_log_determinant(self, space, bare, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
        log_det = D.log_determinant()
        assert_allclose(log_det, np.log(D.determinant()))