def whittW(k, m, z): """Evaluates the Whitaker function W(k, m, z) as defined in Abramowitz & Stegun, Section 13.1. """ from scipy.special import hyperu if k is int or m is int: return np.exp(-0.5 * z) * np.power(z, 0.5 + float(m)) * hyperu(0.5 + float(m - k), 1 + 2 * m, z) elif k is int and m is int: return np.exp(-0.5 * z) * np.power(z, 0.5 + float(m)) * hyperu(0.5 + float(m - k), 1 + 2 * m, z) else: return np.exp(-0.5 * z) * np.power(z, 0.5 + m) * hyperu(0.5 + m - k, 1.0 + 2.0 * m, z)
def _K_ds_n(self,n,k,lmax=8,power=0): ksq = k**2 ret = 0 if power == 0 or power == 1: for ll in range(lmax): ret += self.Hlfac**ll * self._H0_l(ll,k) * hyperu(-ll,n-ll+1,-ksq*self.Bfac_ds) elif power == 2: B = ksq * self.Bfac_ds for ll in range(lmax): ret += self.Hlfac**ll * self._H0_l(ll,k) * (hyperu(-ll,n-ll+1,-B) \ + n/B*hyperu(-ll,n-ll,-B)) return ret/self.sqrtpi # put the numerical factor here to avoid many evaluations
def _K_ds_n(self, n, k, lmax=10, power=0): ksq = k**2 ret = 0 if power == 0 or power == 1: for ll in range(lmax): ret += self.Hlfac**ll * self._H0_l(ll, k) * hyperu( -ll, n - ll + 1, -ksq * self.Bfac_ds) elif power == 2: B = ksq * self.Bfac_ds for ll in range(lmax): ret += self.Hlfac**ll * self._H0_l(ll,k) * (hyperu(-ll,n-ll+1,-B) \ + n/B*hyperu(-ll,n-ll,-B)) return ret
def test_negative_x(self): a, b, x = np.meshgrid( [-1, -0.5, 0, 0.5, 1], [-1, -0.5, 0, 0.5, 1], np.linspace(-100, -1, 10), ) assert np.all(np.isnan(sc.hyperu(a, b, x)))
def pdf_algebraic_gaussian( returns: np.ndarray, K: float, l: float, N: float, Lambda: float ) -> np.ndarray: """Computes de one dimensional Algebraic-Gaussian PDF. :param returns: numpy array with the returns values. :param K: number of companies. :param l: shape parameter. :param N: strength of the fluctuations around the mean. :param Lambda: variance of the returns. :return: numpy array with the pdf values. """ m: np.float = 2 * l - K - 2 numerator: np.float = gamma(l - (K - 1) / 2) * gamma(l - (K - N) / 2) denominator: np.float = ( gamma(l - K / 2) * gamma(N / 2) * np.sqrt(2 * np.pi * Lambda * m / N) ) frac: np.float = numerator / denominator function: np.ndarray = hyperu( l - (K - 1) / 2, (1 - N) / 2 + 1, (N * returns ** 2) / (2 * m * Lambda) ) return frac * function
def WhittakerW(k: float, m: float, x: float) -> float: """Gives the Whittaker function W_{k,m}(x). Args: k, m: parameters. x : point of evaluation. Returns: Value of Whittaker function W_{k,m} function at point x. """ return np.exp(-x / 2) * pow(x, m + 0.5) * special.hyperu(0.5 + m - k, 1 + 2 * m, x)
def test_special_cases(self): assert sc.hyperu(0, 1, 1) == 1.0
def __call__(self, z, q=False, funct=None): if funct is None: funct = self.__call__ if q: return self.__call__(cmath.log(z) / complex(0.0, 2 * math.pi), funct=funct) try: y = z.imag() except TypeError: y = z.imag if 0 < abs(z) < 1: z = -1 / z return (z**self.weight()) * ( self.weilrep()._evaluate(0, -1, 1, 0) * funct(z)) else: try: y = z.imag() except TypeError: y = z.imag if y <= 0: raise ValueError('Not in the upper half plane.') elif y < 0.5: try: x = z.real() except TypeError: x = z.real if abs(x) > 0.5: try: f = x.round() except AttributeError: f = round(x) return self.weilrep()._evaluate(1, f, 0, 1) * funct(z - f) return (z**self.weight()) * ( self.weilrep()._evaluate(0, -1, 1, 0) * funct(z)) two_pi = 2 * math.pi exp = cmath.exp try: zbar = z.conjugate() except TypeError: zbar = z.conjugate f = self.holomorphic_part().n().__call__(z) four_pi_y = 2 * two_pi * y k = self.weight() psi = lambda n: hyperu(k, k, four_pi_y * n) * (2 * two_pi * n)**(k - 1) d = self.xi().coefficients() dsdict = self.weilrep().ds_dict() normlist = self.weilrep().norm_list() z = complex(0.0, 0.0) def correction(): s = vector([z for _ in f]) for g, a in d.items(): i = dsdict[tuple(g[:-1])] n = g[-1] if n: s[i] += psi(n) * exp(-complex(0.0, two_pi) * zbar * n) * a else: s[i] += y**(1 - k) * a / (k - 1) return s try: s = correction() except TypeError: y = y.n() four_pi_y = 2 * two_pi * y psi = lambda n: hyperu(k, k, four_pi_y * n) * (2 * two_pi * n)**( k - 1) s = correction() a = f - s * self.__shadow_multiplier_n return a
def calc_power_rsd(self, ki, q, P, front, exponent_k_squared, zero_lag_1, kmax, alpha_0, alpha_1, C, xi, kappa, gamma, sigma, rsd_exponent_1, rsd_exponent_2, sigma_psi, A, f_val, mu_k): # Function to calculate the redshift space power spectrum Power = 0.0 # Calculate the low k approximation if ki<5e-4 if ki < 5e-5: Power = calc_low_k_approx_rsd(ki, P, sigma_psi, A, kmax, f_val, mu_k) else: for n in range(0, 32): K_n = 0.0 I = ZeldovichPowerIntegral(q, n) if n > 0: for l in range(0, 1): K_n += np.exp( -0.5 * ki**2 * rsd_exponent_1 - 0.5 * ki**2 * rsd_exponent_2) * calc_F_l( l, alpha_0, alpha_1, -0.5 * ki**2 * C, xi, kappa, gamma, sigma) * (-1)**l * np.power( alpha_1, n) * np.power(kappa, n - l) * np.power( gamma**2 / sigma, l) * hyperu( -l, n - l + 1.0, 0.5 * ki**2 * C * kappa * alpha_1) f = (ki * front)**n * np.exp( -0.5 * ki**2 * exponent_k_squared) * K_n else: for l in range(0, 1): K_n += np.exp( -0.5 * ki**2 * rsd_exponent_1 - 0.5 * ki**2 * rsd_exponent_2) * calc_F_l( l, alpha_0, alpha_1, -0.5 * ki**2 * C, xi, kappa, gamma, sigma) * (-1)**l * np.power( kappa, -l) * np.power( gamma**2 / sigma, l) * hyperu( -l, -l + 1.0, 0.5 * ki**2 * C * kappa * alpha_1) f = (np.exp(-0.5 * ki**2 * exponent_k_squared) - np.exp(-ki**2 * zero_lag_1)) * K_n kk, this_Pzel = I(f, extrap=False) Power += spline(kk, this_Pzel)(ki) return Power
def test_nan_inputs(self, a, b, x): assert np.isnan(sc.hyperu(a, b, x)) == np.any(np.isnan([a, b, x]))