operator_tests.py 2.65 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
Philipp Arras's avatar
Move it  
Philipp Arras committed
21

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


25
26
27
28
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
29
30
    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
31
    res1 = f1.vdot(op.adjoint_times(f2).lock())
Philipp Arras's avatar
Fixups    
Philipp Arras committed
32
    res2 = op.times(f1).vdot(f2)
Philipp Arras's avatar
Move it  
Philipp Arras committed
33
34
35
    np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol)


36
37
38
39
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
40
    foo = from_random("normal", op.target, dtype=target_dtype).lock()
Martin Reinecke's avatar
Martin Reinecke committed
41
42
    res = op(op.inverse_times(foo).lock())
    np.testing.assert_allclose(res.to_global_data(), res.to_global_data(),
43
                               atol=atol, rtol=rtol)
Philipp Arras's avatar
Move it  
Philipp Arras committed
44

Martin Reinecke's avatar
step 1    
Martin Reinecke committed
45
    foo = from_random("normal", op.domain, dtype=domain_dtype).lock()
Martin Reinecke's avatar
Martin Reinecke committed
46
47
    res = op.inverse_times(op(foo).lock())
    np.testing.assert_allclose(res.to_global_data(), foo.to_global_data(),
48
                               atol=atol, rtol=rtol)
Philipp Arras's avatar
Move it  
Philipp Arras committed
49

50

51
52
53
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)
54
55


56
57
58
59
60
61
62
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)