test_diagonal_operator.py 3.66 KB
Newer Older
 Martin Reinecke committed Jun 30, 2017 1 ``````from __future__ import division `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 2 3 4 5 6 7 8 9 10 11 ``````import unittest import numpy as np from numpy.testing import assert_equal,\ assert_allclose,\ assert_approx_equal from nifty import Field,\ DiagonalOperator `````` Pumpe, Daniel (dpumpe) committed May 11, 2017 12 ``````from test.common import generate_spaces `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 13 14 15 16 17 `````` from itertools import product from test.common import expand class DiagonalOperator_Tests(unittest.TestCase): `````` Pumpe, Daniel (dpumpe) committed May 11, 2017 18 `````` spaces = generate_spaces() `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 19 20 21 22 23 24 25 `````` @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 `````` Pumpe, Daniel (dpumpe) committed May 11, 2017 26 `````` if D.unitary != False: `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 27 28 29 30 31 32 33 34 35 36 `````` 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) `````` Martin Reinecke committed Jun 20, 2017 37 38 `````` tt1 = rand1.vdot(D.times(rand2)) tt2 = rand2.vdot(D.times(rand1)) `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 39 40 41 42 43 44 45 46 `````` 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) diag = Field.from_random('normal', domain=space) D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy) tt1 = D.times(D.inverse_times(rand1)) `````` Martin Reinecke committed Aug 31, 2017 47 `````` assert_allclose(rand1.val, tt1.val) `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 48 49 50 51 52 53 54 `````` @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) `````` Pumpe, Daniel (dpumpe) committed May 11, 2017 55 `````` assert_equal(tt.domain[0], space) `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 56 57 58 59 60 61 62 `````` @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) `````` Pumpe, Daniel (dpumpe) committed May 11, 2017 63 `````` assert_equal(tt.domain[0], space) `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 64 65 66 67 68 69 70 `````` @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) `````` Pumpe, Daniel (dpumpe) committed May 11, 2017 71 `````` assert_equal(tt.domain[0], space) `````` Pumpe, Daniel (dpumpe) committed May 10, 2017 72 73 74 75 76 77 78 `````` @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) `````` Pumpe, Daniel (dpumpe) committed May 11, 2017 79 `````` assert_equal(tt.domain[0], space) `````` Theo Steininger committed Jun 21, 2017 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 committed Aug 31, 2017 86 `````` assert_allclose(diag.val, diag_op.val) `````` Theo Steininger committed Jun 21, 2017 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 committed Aug 31, 2017 93 `` assert_allclose(1./diag.val, diag_op.val)``