test_diagonal_operator.py 3.46 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
from __future__ import division
2
3
4
5
6
7
8
import unittest

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

Martin Reinecke's avatar
Martin Reinecke committed
9
from nifty2go import Field,\
10
11
    DiagonalOperator

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

from itertools import product
from test.common import expand

class DiagonalOperator_Tests(unittest.TestCase):
18
    spaces = generate_spaces()
19

Martin Reinecke's avatar
Martin Reinecke committed
20
21
    @expand(product(spaces, [True, False]))
    def test_property(self, space, copy):
22
23
24
25
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag)
        if D.domain[0] != space:
            raise TypeError
26
        if D.unitary != False:
27
28
29
30
            raise TypeError
        if D.self_adjoint != True:
            raise TypeError

Martin Reinecke's avatar
Martin Reinecke committed
31
32
    @expand(product(spaces, [True, False]))
    def test_times_adjoint(self, space, copy):
33
34
35
        rand1 = Field.from_random('normal', domain=space)
        rand2 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
36
        D = DiagonalOperator(space, diagonal=diag, copy=copy)
Martin Reinecke's avatar
Martin Reinecke committed
37
38
        tt1 = rand1.vdot(D.times(rand2))
        tt2 = rand2.vdot(D.times(rand1))
39
40
        assert_approx_equal(tt1, tt2)

Martin Reinecke's avatar
Martin Reinecke committed
41
42
    @expand(product(spaces, [True, False]))
    def test_times_inverse(self, space, copy):
43
44
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
45
        D = DiagonalOperator(space, diagonal=diag, copy=copy)
46
        tt1 = D.times(D.inverse_times(rand1))
Martin Reinecke's avatar
Martin Reinecke committed
47
        assert_allclose(rand1.val, tt1.val)
48

Martin Reinecke's avatar
Martin Reinecke committed
49
50
    @expand(product(spaces, [True, False]))
    def test_times(self, space, copy):
51
52
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
53
        D = DiagonalOperator(space, diagonal=diag, copy=copy)
54
        tt = D.times(rand1)
55
        assert_equal(tt.domain[0], space)
56

Martin Reinecke's avatar
Martin Reinecke committed
57
58
    @expand(product(spaces, [True, False]))
    def test_adjoint_times(self, space, copy):
59
60
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
61
        D = DiagonalOperator(space, diagonal=diag, copy=copy)
62
        tt = D.adjoint_times(rand1)
63
        assert_equal(tt.domain[0], space)
64

Martin Reinecke's avatar
Martin Reinecke committed
65
66
    @expand(product(spaces, [True, False]))
    def test_inverse_times(self, space, copy):
67
68
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
69
        D = DiagonalOperator(space, diagonal=diag, copy=copy)
70
        tt = D.inverse_times(rand1)
71
        assert_equal(tt.domain[0], space)
72

Martin Reinecke's avatar
Martin Reinecke committed
73
74
    @expand(product(spaces, [True, False]))
    def test_adjoint_inverse_times(self, space, copy):
75
76
        rand1 = Field.from_random('normal', domain=space)
        diag = Field.from_random('normal', domain=space)
Martin Reinecke's avatar
Martin Reinecke committed
77
        D = DiagonalOperator(space, diagonal=diag, copy=copy)
78
        tt = D.adjoint_inverse_times(rand1)
79
        assert_equal(tt.domain[0], space)
80
81
82
83
84
85

    @expand(product(spaces, [True, False]))
    def test_diagonal(self, space, copy):
        diag = Field.from_random('normal', domain=space)
        D = DiagonalOperator(space, diagonal=diag, copy=copy)
        diag_op = D.diagonal()
Martin Reinecke's avatar
Martin Reinecke committed
86
        assert_allclose(diag.val, diag_op.val)
87
88
89
90
91
92

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