sugar.py 2.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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/>.
18
19
20
21
22
23
24
25
26

from nifty import PowerSpace,\
                  Field,\
                  DiagonalOperator,\
                  FFTOperator

__all__ = ['create_power_operator']


27
28
def create_power_operator(domain, power_spectrum, dtype=None,
                          distribution_strategy='not'):
Theo Steininger's avatar
Theo Steininger committed
29
30
    """ Creates a diagonal operator with the given power spectrum.

31
    Constructs a diagonal operator that lives over the specified domain, or
Theo Steininger's avatar
Theo Steininger committed
32
33
    its default harmonic codomain in case it is not harmonic.

34
35
36
37
    Parameters
    ----------
    domain : DomainObject
        Domain over which the power operator shall live. If this is not a
Theo Steininger's avatar
Theo Steininger committed
38
        harmonic domain, it will return an operator for its harmonic codomain
39
        instead.
Theo Steininger's avatar
Theo Steininger committed
40
    power_spectrum : (array-like, method)
41
42
        An array-like object, or a method that implements the square root
        of a power spectrum as a function of k.
Theo Steininger's avatar
Theo Steininger committed
43
    dtype : type *optional*
44
        dtype that the field holding the power spectrum shall use
Theo Steininger's avatar
Theo Steininger committed
45
46
47
        (default : None).
        if dtype == None: the dtype of `power_spectrum` will be used.
    distribution_strategy : string *optional*
48
        Distributed strategy to be used by the underlying d2o objects.
Theo Steininger's avatar
Theo Steininger committed
49
50
        (default : 'not')

51
52
    Returns
    -------
Theo Steininger's avatar
Theo Steininger committed
53
54
    DiagonalOperator : An operator that implements the given power spectrum.

55
    """
Theo Steininger's avatar
Theo Steininger committed
56

57
58
59
60
    if not domain.harmonic:
        fft = FFTOperator(domain)
        domain = fft.target[0]

61
62
    power_domain = PowerSpace(domain,
                              distribution_strategy=distribution_strategy)
63
64

    fp = Field(power_domain,
65
               val=power_spectrum, dtype=dtype,
66
               distribution_strategy=distribution_strategy)
67
    fp **= 2
68

69
    f = fp.power_synthesize(mean=1, std=0, real_signal=False)
70

71
    power_operator = DiagonalOperator(domain, diagonal=f, bare=True)
72

73
    return power_operator