test_multi_field.py 2.36 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Martin Reinecke's avatar
Martin Reinecke committed
17

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

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

Martin Reinecke's avatar
Martin Reinecke committed
24
25
dom = ift.makeDomain({"d1": ift.RGSpace(10)})

26

Philipp Arras's avatar
Philipp Arras committed
27
28
29
def test_vdot():
    f1 = ift.from_random("normal", domain=dom, dtype=np.complex128)
    f2 = ift.from_random("normal", domain=dom, dtype=np.complex128)
Martin Reinecke's avatar
Martin Reinecke committed
30
    assert_allclose(f1.s_vdot(f2), np.conj(f2.s_vdot(f1)))
Philipp Arras's avatar
Philipp Arras committed
31
32
33
34
35


def test_func():
    f1 = ift.from_random("normal", domain=dom, dtype=np.complex128)
    assert_allclose(
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
36
        ift.log(ift.exp((f1)))["d1"].val, f1["d1"].val)
Philipp Arras's avatar
Philipp Arras committed
37

Martin Reinecke's avatar
Martin Reinecke committed
38

Reimar H Leike's avatar
Reimar H Leike committed
39
40
def test_multifield_field_consistency():
    f1 = ift.full(dom, 27)
Martin Reinecke's avatar
Martin Reinecke committed
41
    f2 = ift.makeField(dom['d1'], f1['d1'].val)
Martin Reinecke's avatar
Martin Reinecke committed
42
    assert_equal(f1.s_sum(), f2.s_sum())
Reimar H Leike's avatar
fix    
Reimar H Leike committed
43
    assert_equal(f1.size, f2.size)
Reimar H Leike's avatar
Reimar H Leike committed
44

Philipp Arras's avatar
Philipp Arras committed
45
46
47

def test_dataconv():
    f1 = ift.full(dom, 27)
Martin Reinecke's avatar
Martin Reinecke committed
48
    f2 = ift.makeField(dom, f1.val)
Philipp Arras's avatar
Philipp Arras committed
49
    for key, val in f1.items():
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
50
        assert_equal(val.val, f2[key].val)
Martin Reinecke's avatar
Martin Reinecke committed
51
    if "d1" not in f2:
Reimar H Leike's avatar
Reimar H Leike committed
52
53
54
55
56
57
        raise KeyError()
    assert_equal({"d1": f1}, f2.to_dict())
    f3 = ift.full(dom, 27+1.j)
    f4 = ift.full(dom, 1.j)
    assert_equal(f2, f3.real)
    assert_equal(f4, f3.imag)
Philipp Arras's avatar
Philipp Arras committed
58
59
60


def test_blockdiagonal():
61
    op = ift.BlockDiagonalOperator(
62
        dom, {"d1": ift.ScalingOperator(dom["d1"], 20.)})
Philipp Arras's avatar
Philipp Arras committed
63
64
65
66
67
    op2 = op(op)
    ift.extra.consistency_check(op2)
    assert_equal(type(op2), ift.BlockDiagonalOperator)
    f1 = op2(ift.full(dom, 1))
    for val in f1.values():
Martin Reinecke's avatar
Martin Reinecke committed
68
        assert_equal((val == 400).s_all(), True)
Philipp Arras's avatar
Philipp Arras committed
69
70
71
72
    op2 = op + op
    assert_equal(type(op2), ift.BlockDiagonalOperator)
    f1 = op2(ift.full(dom, 1))
    for val in f1.values():
Martin Reinecke's avatar
Martin Reinecke committed
73
        assert_equal((val == 40).s_all(), True)