operator_tests.py 2.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
Philipp Arras's avatar
Move it  
Philipp Arras committed
18

19
import numpy as np
Martin Reinecke's avatar
step 1  
Martin Reinecke committed
20
from ..sugar import from_random
Martin Reinecke's avatar
Martin Reinecke committed
21
from ..field import Field
Philipp Arras's avatar
Move it  
Philipp Arras committed
22

23
__all__ = ["consistency_check"]
Philipp Arras's avatar
Move it  
Philipp Arras committed
24
25


Martin Reinecke's avatar
Martin Reinecke committed
26
27
28
29
30
31
32
def _assert_allclose(f1, f2, atol, rtol):
    if isinstance(f1, Field):
        return np.testing.assert_allclose(f1.local_data, f2.local_data,
                                          atol=atol, rtol=rtol)
    for key, val in f1.items():
        _assert_allclose(val, f2[key], atol=atol, rtol=rtol)

Martin Reinecke's avatar
Martin Reinecke committed
33

34
35
36
37
def adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol):
    needed_cap = op.TIMES | op.ADJOINT_TIMES
    if (op.capability & needed_cap) != needed_cap:
        return
Martin Reinecke's avatar
step 1  
Martin Reinecke committed
38
39
    f1 = from_random("normal", op.domain, dtype=domain_dtype).lock()
    f2 = from_random("normal", op.target, dtype=target_dtype).lock()
Martin Reinecke's avatar
Martin Reinecke committed
40
    res1 = f1.vdot(op.adjoint_times(f2).lock())
Philipp Arras's avatar
Fixups  
Philipp Arras committed
41
    res2 = op.times(f1).vdot(f2)
Philipp Arras's avatar
Move it  
Philipp Arras committed
42
43
44
    np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol)


45
46
47
48
def inverse_implementation(op, domain_dtype, target_dtype, atol, rtol):
    needed_cap = op.TIMES | op.INVERSE_TIMES
    if (op.capability & needed_cap) != needed_cap:
        return
Martin Reinecke's avatar
step 1  
Martin Reinecke committed
49
    foo = from_random("normal", op.target, dtype=target_dtype).lock()
Martin Reinecke's avatar
Martin Reinecke committed
50
    res = op(op.inverse_times(foo).lock())
Martin Reinecke's avatar
Martin Reinecke committed
51
    _assert_allclose(res, foo, atol=atol, rtol=rtol)
Philipp Arras's avatar
Move it  
Philipp Arras committed
52

Martin Reinecke's avatar
step 1  
Martin Reinecke committed
53
    foo = from_random("normal", op.domain, dtype=domain_dtype).lock()
Martin Reinecke's avatar
Martin Reinecke committed
54
    res = op.inverse_times(op(foo).lock())
Martin Reinecke's avatar
Martin Reinecke committed
55
    _assert_allclose(res, foo, atol=atol, rtol=rtol)
Philipp Arras's avatar
Move it  
Philipp Arras committed
56

57

58
59
60
def full_implementation(op, domain_dtype, target_dtype, atol, rtol):
    adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol)
    inverse_implementation(op, domain_dtype, target_dtype, atol, rtol)
61
62


63
64
65
66
67
68
69
def consistency_check(op, domain_dtype=np.float64, target_dtype=np.float64,
                      atol=0, rtol=1e-7):
    full_implementation(op, domain_dtype, target_dtype, atol, rtol)
    full_implementation(op.adjoint, target_dtype, domain_dtype, atol, rtol)
    full_implementation(op.inverse, target_dtype, domain_dtype, atol, rtol)
    full_implementation(op.adjoint.inverse, domain_dtype, target_dtype, atol,
                        rtol)