test_simplification.py 2.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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.

import unittest
from itertools import product
from test.common import expand

import nifty5 as ift
from numpy.testing import assert_allclose, assert_equal


class Simplification_Tests(unittest.TestCase):
    def test_simplification(self):
        from nifty5.operators.operator import _ConstantOperator
        f1 = ift.Field.full(ift.RGSpace(10),2.)
        op = ift.FFTOperator(f1.domain)
        _, op2 = op.simplify_for_constant_input(f1)
        assert_equal(isinstance(op2, _ConstantOperator), True)
        assert_allclose(op(f1).local_data, op2(f1).local_data)

        dom = {"a": ift.RGSpace(10)}
        f1 = ift.full(dom,2.)
        op = ift.FFTOperator(f1.domain["a"]).ducktape("a")
        _, op2 = op.simplify_for_constant_input(f1)
        assert_equal(isinstance(op2, _ConstantOperator), True)
        assert_allclose(op(f1).local_data, op2(f1).local_data)

        dom = {"a": ift.RGSpace(10), "b": ift.RGSpace(5)}
        f1 = ift.full(dom,2.)
        pdom = {"a": ift.RGSpace(10)}
        f2 = ift.full(pdom,2.)
        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)
        assert_equal(isinstance(op2._op1, _ConstantOperator), True)
        assert_allclose(op(f1)["a"].local_data, op2(f1)["a"].local_data)
        assert_allclose(op(f1)["b"].local_data, op2(f1)["b"].local_data)