test_composed_operator.py 1.87 KB
Newer Older
1 2 3 4 5 6
import unittest

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

Martin Reinecke's avatar
Martin Reinecke committed
7
from nifty2go import Field,\
8 9 10 11 12 13 14 15 16 17 18
    DiagonalOperator,\
    ComposedOperator

from test.common import generate_spaces

from itertools import product
from test.common import expand

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

19
    @expand(product(spaces, spaces))
20 21 22
    def test_property(self, space1, space2):
        rand1 = Field.from_random('normal', domain=space1)
        rand2 = Field.from_random('normal', domain=space2)
Martin Reinecke's avatar
Martin Reinecke committed
23 24
        op1 = DiagonalOperator(rand1)
        op2 = DiagonalOperator(rand2)
25
        op = ComposedOperator((op1, op2))
26
        if op.domain != (op1.domain[0], op2.domain[0]):
27 28 29 30
            raise TypeError
        if op.unitary != False:
            raise ValueError

31
    @expand(product(spaces,spaces))
32 33 34
    def test_times_adjoint_times(self, space1, space2):
        diag1 = Field.from_random('normal', domain=space1)
        diag2 = Field.from_random('normal', domain=space2)
Martin Reinecke's avatar
Martin Reinecke committed
35 36
        op1 = DiagonalOperator(diag1)
        op2 = DiagonalOperator(diag2)
37 38 39 40 41 42

        op = ComposedOperator((op1, op2))

        rand1 = Field.from_random('normal', domain=(space1,space2))
        rand2 = Field.from_random('normal', domain=(space1,space2))

Martin Reinecke's avatar
Martin Reinecke committed
43 44
        tt1 = rand2.vdot(op.times(rand1))
        tt2 = rand1.vdot(op.adjoint_times(rand2))
45 46
        assert_approx_equal(tt1, tt2)

47
    @expand(product(spaces, spaces))
48 49 50
    def test_times_inverse_times(self, space1, space2):
        diag1 = Field.from_random('normal', domain=space1)
        diag2 = Field.from_random('normal', domain=space2)
Martin Reinecke's avatar
Martin Reinecke committed
51 52
        op1 = DiagonalOperator(diag1)
        op2 = DiagonalOperator(diag2)
53 54 55 56 57 58

        op = ComposedOperator((op1, op2))

        rand1 = Field.from_random('normal', domain=(space1, space2))
        tt1 = op.inverse_times(op.times(rand1))

Martin Reinecke's avatar
Martin Reinecke committed
59
        assert_allclose(tt1.val, rand1.val)
60