test_simplification.py 2.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2019 Max-Planck-Society
15
#
Martin Reinecke's avatar
Martin Reinecke committed
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17

Martin Reinecke's avatar
Martin Reinecke committed
18
from numpy.testing import assert_allclose, assert_equal
19

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
20
import nifty6 as ift
21
from ..common import setup_function, teardown_function
22
23


Martin Reinecke's avatar
Martin Reinecke committed
24
def test_simplification():
Philipp Arras's avatar
Fixups    
Philipp Arras committed
25
    from nifty6.operators.simplify_for_const import ConstantOperator
Martin Reinecke's avatar
Martin Reinecke committed
26
    f1 = ift.Field.full(ift.RGSpace(10), 2.)
Martin Reinecke's avatar
Martin Reinecke committed
27
28
    op = ift.FFTOperator(f1.domain)
    _, op2 = op.simplify_for_constant_input(f1)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
29
    assert_equal(isinstance(op2, ConstantOperator), True)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
30
    assert_allclose(op(f1).val, op2(f1).val)
31

Martin Reinecke's avatar
Martin Reinecke committed
32
    dom = {"a": ift.RGSpace(10)}
Martin Reinecke's avatar
Martin Reinecke committed
33
    f1 = ift.full(dom, 2.)
Martin Reinecke's avatar
Martin Reinecke committed
34
35
    op = ift.FFTOperator(f1.domain["a"]).ducktape("a")
    _, op2 = op.simplify_for_constant_input(f1)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
36
    assert_equal(isinstance(op2, ConstantOperator), True)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
37
    assert_allclose(op(f1).val, op2(f1).val)
38

Martin Reinecke's avatar
Martin Reinecke committed
39
    dom = {"a": ift.RGSpace(10), "b": ift.RGSpace(5)}
Martin Reinecke's avatar
Martin Reinecke committed
40
    f1 = ift.full(dom, 2.)
Martin Reinecke's avatar
Martin Reinecke committed
41
    pdom = {"a": ift.RGSpace(10)}
Martin Reinecke's avatar
Martin Reinecke committed
42
    f2 = ift.full(pdom, 2.)
Martin Reinecke's avatar
Martin Reinecke committed
43
44
45
46
47
    o1 = ift.FFTOperator(f1.domain["a"])
    o2 = ift.FFTOperator(f1.domain["b"])
    op = (o1.ducktape("a").ducktape_left("a") +
          o2.ducktape("b").ducktape_left("b"))
    _, op2 = op.simplify_for_constant_input(f2)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
48
    assert_equal(isinstance(op2._op1, ConstantOperator), True)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
49
50
    assert_allclose(op(f1)["a"].val, op2(f1)["a"].val)
    assert_allclose(op(f1)["b"].val, op2(f1)["b"].val)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
51
    lin = ift.Linearization.make_var(ift.MultiField.full(op2.domain, 2.), True)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
52
53
54
55
    assert_allclose(op(lin).val["a"].val,
                    op2(lin).val["a"].val)
    assert_allclose(op(lin).val["b"].val,
                    op2(lin).val["b"].val)