test_tensor_dot.py 7.12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2021 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import numpy as np
import pytest

import nifty8 as ift

Neel Shah's avatar
Neel Shah committed
23
from ..common import list2fixture
24
25
26
27
28
29
30
31
32

dtuple = ift.DomainTuple.make((ift.RGSpace(2, 0.2), ift.RGSpace(3, 0.3), 
                                ift.RGSpace(4, 0.4), ift.RGSpace(5, 0.5)))

pmp = pytest.mark.parametrize

domain = list2fixture([dtuple])
spaces = list2fixture((None, (2,), (1, 3), (1, 2, 3), (0, 1, 2, 3)))

33
def test_tensor_dot_endomorphic(domain, spaces, n_tests=4):
34
35
36
37
38
39
40
41
42
43
44
    mat_shape = ()
    if spaces != None:
        for i in spaces:
            mat_shape += domain[i].shape
    else:
        mat_shape += domain.shape
    mat_shape = mat_shape*2
    
    for i in range(n_tests):
        mat = ift.random.current_rng().standard_normal(mat_shape)
        mat = mat + 1j*ift.random.current_rng().standard_normal(mat_shape)
45
        op = ift.TensorDotOperator(domain, mat, spaces=spaces)
46
47
        ift.extra.check_linear_operator(op)

48
def test_tensor_dot_spaces(domain, spaces, n_tests=4):
49
50
51
52
53
54
55
56
57
58
    mat_shape = (7, 8)
    if spaces != None:
        for i in spaces:
            mat_shape += domain[i].shape
    else:
            mat_shape += domain.shape
        
    for i in range(n_tests):
        mat = ift.random.current_rng().standard_normal(mat_shape)
        mat = mat + 1j*ift.random.current_rng().standard_normal(mat_shape)
59
        op = ift.TensorDotOperator(domain, mat, spaces=spaces)
60
61
        ift.extra.check_linear_operator(op)

62
def test_tensor_dot_flatten(domain, n_tests=4):
63
64
65
66
67
    appl_shape = (ift.utilities.my_product(domain.shape),)
    mat_shape = appl_shape * 2
    for i in range(n_tests):
        mat = ift.random.current_rng().standard_normal(mat_shape)
        mat = mat + 1j*ift.random.current_rng().standard_normal(mat_shape)
68
        op = ift.TensorDotOperator(domain, mat, spaces=None, flatten=True)
69
70
71
        ift.extra.check_linear_operator(op)

# the below function demonstrates the only error that cannot be caught
72
# when the operator is initialized. It is caused due to the tensor having
73
74
# too few dimensions to stand in the places of summed over axes of the domain
# as explained in the operator's documentation.
75
def test_tensor_dot_invalid_shapes(domain):
76
77
78
79
80
81
82
83
84
    mat_shape = ()
    spaces = (2,)
    if spaces != None:
        for i in spaces:
            mat_shape += domain[i].shape
    else:
            mat_shape += domain.shape
    with pytest.raises(ValueError):
        mat = ift.random.current_rng().standard_normal(mat_shape)
85
        op = ift.TensorDotOperator(domain, mat, spaces=spaces)
86
87
88
89
90
91
92
93
94
        ift.extra.check_linear_operator(op)
    mat_shape = ()
    spaces = (3,)
    if spaces != None:
        for i in spaces:
            mat_shape += domain[i].shape
    else:
            mat_shape += domain.shape
    mat = ift.random.current_rng().standard_normal(mat_shape)
95
    op = ift.TensorDotOperator(domain, mat, spaces=spaces)
96
97
98
99
100
101
102
103
104
105
    ift.extra.check_linear_operator(op)
    mat_shape = (7,)
    spaces = (1, 2)
    if spaces != None:
        for i in spaces:
            mat_shape += domain[i].shape
    else:
            mat_shape += domain.shape
    with pytest.raises(ValueError):
        mat = ift.random.current_rng().standard_normal(mat_shape)
106
        op = ift.TensorDotOperator(domain, mat, spaces=spaces)
107
108
109
110
111
112
113
114
115
        ift.extra.check_linear_operator(op)
    mat_shape = (7,)
    spaces = (1, 3)
    if spaces != None:
        for i in spaces:
            mat_shape += domain[i].shape
    else:
            mat_shape += domain.shape
    mat = ift.random.current_rng().standard_normal(mat_shape)
116
    op = ift.TensorDotOperator(domain, mat, spaces=spaces)
117
    ift.extra.check_linear_operator(op)
118

119
def test_tensor_dot_examples(n_tests=4):
Neel Shah's avatar
Neel Shah committed
120
121
122
123
124
    for i in range(n_tests):
        # 1. Demonstrate that multiplying by a unitary matrix doesn't change
        # the norm of a vector
        domain = ift.RGSpace(10, 0.1)
        field = ift.Field.from_random(domain=domain, dtype=np.complex128)
125
        norm = ift.TensorDotOperator(domain, field.conjugate().val).times(field)
Neel Shah's avatar
Neel Shah committed
126
127
128
129
130
131
132
        M = ift.random.current_rng().standard_normal(domain.shape*2)
        H = M + M.transpose()
        O = np.linalg.eig(H)[1]
        O_inv = np.linalg.inv(O)
        Hd = np.matmul(O_inv, np.matmul(H, O))
        Ud = np.diag(np.exp(1j*np.diag(Hd)))
        U = np.matmul(O, np.matmul(Ud, O_inv))
133
        U_matmul = ift.TensorDotOperator(domain, U)
Neel Shah's avatar
Neel Shah committed
134
        field2 = U_matmul.times(field)
135
        norm2 = ift.TensorDotOperator(domain, field2.conjugate().val).times(field2)
Neel Shah's avatar
Neel Shah committed
136
137
138
139
140
141
        ift.extra.assert_allclose(norm, norm2, rtol=1e-14, atol=0)
        
        # 2. Demonstrate using the operator to get complex conjugate of a field
        domain = dtuple
        field = ift.Field.from_random(domain=domain, dtype=np.complex128)
        one = ift.Field.from_raw(ift.DomainTuple.make(None), 1)
142
        op = ift.TensorDotOperator(domain, field.val)
Neel Shah's avatar
Neel Shah committed
143
144
145
146
147
148
        op_conjugate = op.adjoint_times(one)
        ift.extra.assert_equal(op_conjugate, field.conjugate())
        
        # 3. Demonstrate using the operator to take the trace of a square matrix
        domain = ift.DomainTuple.make((ift.RGSpace(10, 0.3), ift.RGSpace(10, 0.2)))
        field = ift.Field.from_random(domain=domain, dtype=np.complex128)
149
        trace_op = ift.TensorDotOperator(domain, np.eye(10))
Neel Shah's avatar
Neel Shah committed
150
151
152
153
154
155
156
157
158
159
        trace = trace_op.times(field)
        np_trace = np.trace(field.val)
        np.testing.assert_allclose(trace.val, np_trace, rtol=1e-14, atol=0)
        
        # 4. Demonstrate the cyclic property of the trace using the matrix
        # product operator for matrix products
        domain = ift.DomainTuple.make((ift.RGSpace(10, 0.3), ift.RGSpace(10, 0.2)))
        A = ift.Field.from_random(domain=domain, dtype=np.complex128)
        B = ift.Field.from_random(domain=domain, dtype=np.complex128)
        C = ift.Field.from_random(domain=domain, dtype=np.complex128)
160
161
162
163
        trace_op = ift.TensorDotOperator(domain, np.eye(10))
        A_matmul = ift.TensorDotOperator(domain, A.val, spaces=(0,))
        B_matmul = ift.TensorDotOperator(domain, B.val, spaces=(0,))
        C_matmul = ift.TensorDotOperator(domain, C.val, spaces=(0,))
Neel Shah's avatar
Neel Shah committed
164
165
166
167
168
169
170
171
172
        ABC = A_matmul.times(B_matmul.times(C))
        BCA = B_matmul.times(C_matmul.times(A))
        CAB = C_matmul.times(A_matmul.times(B))
        trace1 = trace_op.times(ABC)
        trace2 = trace_op.times(BCA)
        trace3 = trace_op.times(CAB)
        ift.extra.assert_allclose(trace1, trace2, rtol=1e-14, atol=0)
        ift.extra.assert_allclose(trace2, trace3, rtol=1e-14, atol=0)
        ift.extra.assert_allclose(trace3, trace1, rtol=1e-14, atol=0)