operator_tests.py 2.73 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
Philipp Arras's avatar
Move it  
Philipp Arras committed
20
from ..field import Field
21
from .. import dobj
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


26
27
28
29
30
31
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
    f1 = Field.from_random("normal", op.domain, dtype=domain_dtype)
    f2 = Field.from_random("normal", op.target, dtype=target_dtype)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
32
33
    res1 = f1.vdot(op.adjoint_times(f2))
    res2 = op.times(f1).vdot(f2)
Philipp Arras's avatar
Move it  
Philipp Arras committed
34
35
36
    np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol)


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

47
48
49
50
51
    foo = Field.from_random("normal", op.domain, dtype=domain_dtype)
    res = op.inverse_times(op(foo))
    np.testing.assert_allclose(dobj.to_global_data(res.val),
                               dobj.to_global_data(foo.val),
                               atol=atol, rtol=rtol)
Philipp Arras's avatar
Move it  
Philipp Arras committed
52

53

54
55
56
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)
57
58


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