operator_tests.py 2.86 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
20
from __future__ import absolute_import, division, print_function
from ..compat import *
21
import numpy as np
Martin Reinecke's avatar
step 1  
Martin Reinecke committed
22
from ..sugar import from_random
Martin Reinecke's avatar
Martin Reinecke committed
23
from ..field import Field
Philipp Arras's avatar
Move it  
Philipp Arras committed
24

25
__all__ = ["consistency_check"]
Philipp Arras's avatar
Move it  
Philipp Arras committed
26
27


Martin Reinecke's avatar
Martin Reinecke committed
28
29
30
31
32
33
34
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
35

36
37
38
39
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
40
41
42
    f1 = from_random("normal", op.domain, dtype=domain_dtype)
    f2 = from_random("normal", op.target, dtype=target_dtype)
    res1 = f1.vdot(op.adjoint_times(f2))
Philipp Arras's avatar
Fixups  
Philipp Arras committed
43
    res2 = op.times(f1).vdot(f2)
Philipp Arras's avatar
Move it  
Philipp Arras committed
44
45
46
    np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol)


47
48
49
50
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
51
52
    foo = from_random("normal", op.target, dtype=target_dtype)
    res = op(op.inverse_times(foo))
Martin Reinecke's avatar
Martin Reinecke committed
53
    _assert_allclose(res, foo, atol=atol, rtol=rtol)
Philipp Arras's avatar
Move it  
Philipp Arras committed
54

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

59

60
61
62
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)
63
64


65
66
67
68
69
70
71
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)