Example #1
0
def pk2corrfunc(k, pk, r, h=0.001):
    # compute correlation function for a given matter power spectrum
    #r = np.arange(rmin,rmax,dr)
    # power spectrum
    pkfunc = spline(k, k**0.5 / (2**1.5 * np.pi**1.5) * pk)
    # setup hankel transform
    ht = hankel.HankelTransform(nu=0.5, h=h)
    # compute correlation function
    XI = ht.transform(pkfunc, r, False, inverse=True) / np.sqrt(r)
    return spline(r, XI)
Example #2
0
def IntegPJ0(k, pk0, h=.001, verbose=False):
    # construct a function: F(r) = int[kP(k)*J_0(kr)/2pi]
    r = basic.cosmofuncs.dist_comoving(np.linspace(1e-4, 5., 2000)) * .05
    if verbose: print('compute using r between', min(r), max(r), r[1] - r[0])
    # setup hankel transform
    ht = hankel.HankelTransform(nu=0., h=h)
    # not use Hankel transform for r = 0 due to numerical accuracy
    PJ0 = integrate.quad(spline(k, k * pk0), 1e-4, 4.)[0] / (2 * np.pi)
    # for r /= 0
    PJR = ht.transform(spline(k, pk0), r, False, inverse=True) / (2 * np.pi)
    # replace r=0 with the exact result
    PJR[0] = PJ0
    return spline(r, PJR)
Example #3
0
def IntegPJ(k, zs, pk, h=.001):
    # construct a function: F(r) = int[kP(k)*J_0(kr)/2pi]
    r = basic.cosmofuncs.dist_comoving(np.linspace(1e-4, 5., 2000)) * .05
    # setup hankel transform
    ht = hankel.HankelTransform(nu=0., h=h)
    PJR = np.zeros((len(zs), len(r)))
    for zi, z in enumerate(zs):
        # for r /= 0
        PJR[zi, :] = ht.transform(spline(k, pk[zi]), r, False,
                                  inverse=True) / (2 * np.pi)
        # replace r=0 with the exact result
        PJR[zi, 0] = integrate.quad(spline(k, k * pk[zi]), 1e-4,
                                    4.)[0] / (2 * np.pi)
    return interp2d(r, zs, PJR, kind='cubic')
Example #4
0
    def initialize_postload(self):
        self.covmat = self.fullcov[np.ix_(self.used_indices,
                                          self.used_indices)]
        self.covinv = np.linalg.inv(self.covmat)
        self.data_vector = self.make_vector(self.data_arrays)
        self.errors = copy.deepcopy(self.data_arrays)
        cov_ix = 0
        for i, (type_ix, f1, f2, ix) in enumerate(self.indices):
            self.errors[type_ix][f1, f2][ix] = np.sqrt(self.fullcov[cov_ix,
                                                                    cov_ix])
            cov_ix += 1
        self.theta_bins_radians = self.theta_bins / 60 * np.pi / 180
        # Note hankel assumes integral starts at ell=0
        # (though could change spline to zero at zero).
        # At percent level it matters what is assumed
        if self.use_hankel:  # pragma: no cover
            # noinspection PyUnresolvedReferences
            import hankel
            maxx = self.theta_bins_radians[-1] * self.l_max
            h = 3.2 * np.pi / maxx
            N = int(3.2 / h)
            self.hankel0 = hankel.HankelTransform(nu=0, N=N, h=h)
            self.hankel2 = hankel.HankelTransform(nu=2, N=N, h=h)
            self.hankel4 = hankel.HankelTransform(nu=4, N=N, h=h)
        elif self.binned_bessels:
            # Approximate bessel integral as binned smooth C_L against integrals of
            # bessel in each bin. Here we crudely precompute an approximation to the
            # bessel integral by brute force
            dls = np.diff(
                np.unique((np.exp(
                    np.linspace(np.log(1.), np.log(self.l_max),
                                int(500 * self.acc)))).astype(int)))
            groups = []
            ell = 2  # ell_min
            self.ls_bessel = np.zeros(dls.size)
            for i, dlx in enumerate(dls):
                self.ls_bessel[i] = (2 * ell + dlx - 1) / 2.
                groups.append(np.arange(ell, ell + dlx))
                ell += dlx
            js = np.empty(
                (3, self.ls_bessel.size, len(self.theta_bins_radians)))
            bigell = np.arange(0, self.l_max + 1, dtype=np.float64)
            for i, theta in enumerate(self.theta_bins_radians):
                bigx = bigell * theta
                for ix, nu in enumerate([0, 2, 4]):
                    bigj = special.jn(nu, bigx) * bigell / (2 * np.pi)
                    for j, g in enumerate(groups):
                        js[ix, j, i] = np.sum(bigj[g])
            self.bessel_cache = js[0, :, :], js[1, :, :], js[2, :, :]
        else:  # pragma: no cover
            # get ell for bessel transform in dense array,
            # and precompute bessel function matrices
            # Much slower than binned_bessels as many more sampling points
            dl = 4
            self.ls_bessel = np.arange(2 + dl / 2,
                                       self.l_max + 1,
                                       dl,
                                       dtype=np.float64)

            j0s = np.empty((len(self.ls_bessel), len(self.theta_bins_radians)))
            j2s = np.empty((len(self.ls_bessel), len(self.theta_bins_radians)))
            j4s = np.empty((len(self.ls_bessel), len(self.theta_bins_radians)))
            for i, theta in enumerate(self.theta_bins_radians):
                x = self.ls_bessel * theta
                j0s[:, i] = self.ls_bessel * special.jn(0, x)
                j2s[:, i] = self.ls_bessel * special.jn(2, x)
                j4s[:, i] = self.ls_bessel * special.jn(4, x)
            j0s *= dl / (2 * np.pi)
            j2s *= dl / (2 * np.pi)
            j4s *= dl / (2 * np.pi)
            self.bessel_cache = j0s, j2s, j4s
        # Fine z sampling
        if self.acc > 1:
            self.zs = np.linspace(0.005, self.zmax, int(350 * self.acc))
        else:
            self.zs = self.zmid[self.zmid <= self.zmax]
        # Interpolator z sampling
        assert self.zmax <= 5, "z max too large!"
        self.zs_interp = np.linspace(0, self.zmax, 100)
Example #5
0
import matplotlib.pyplot as plt
import numpy as np
import hankel

# We need the spline interpolation to obtain a smooth function that can be transformed back to the original function
from scipy.interpolate import InterpolatedUnivariateSpline as spline

realspace = np.linspace(0, 100, 300)[1:]  #  Realspace domain
hankelspace = np.logspace(-5, 15, 300)  #  Hankel space domain
ht = hankel.HankelTransform(nu=1, N=500, h=0.0005)

gupFilter = np.genfromtxt('gup1997J147pt.csv', float, delimiter=',').flatten()
print(f"n={len(gupFilter)} Filter loaded")
print(gupFilter)


def abscissaeJ1(r):
    # Returns the lambdas used in the method of Guptasarma and Singh 1997
    # function f(lambda) is evaluated at these lambdas

    # 47 point filter parameters:
    a = -3.05078187595e0
    s = 1.10599010095e-1
    # 140 point filter parameters
    # a = -7.91001919000e0
    # s = 8.79671439570e-2
    rangefunc = np.arange(0, len(gupFilter))

    eval_points = (1 / r[:, np.newaxis]) * np.power(10, a + (rangefunc * s))

    return eval_points
Example #6
0
 def wint(self, rr, nn=100, hh=0.03):
     """Hankel integrator"""
     fpow = self.fmaker(rr)
     h = hankel.HankelTransform(nu=2, N=nn, h=hh)
     val = np.array(h.transform(fpow)) / rr / (2. * np.pi)
     return val