def BK(xip, nr): r"""Return BK_nr.""" if np.isrealobj(xip): # To keep it real in Laplace-domain [exp(-1j*0) = 1-0j]. return special.kve(nr, xip) else: return np.exp(-1j * np.imag(xip)) * special.kve(nr, xip)
def Rlam(x, lam=None): v1 = kve(lam + 1, x) * np.exp(x) v0 = kve(lam, x) * np.exp(x) val = v1 / v0 if np.isinf(v1) or np.isinf(v0) or v0 == 0 or v1 == 0: lv1 = np.log(sympy.Float(mpmath.besselk(np.abs(lam + 1), x))) lv0 = np.log(sympy.Float(mpmath.besselk(np.abs(lam), x))) val = np.exp(lv1 - lv0) return val
def gigGH(x=None, par=None, invS=None): """ Calculate expectation of W, 1/W and log(W) (E-Step) W is assumed to follow Generalized Inverse Gaussian W~GIG(psi, chi, lambda) Refer to p9 A Mixture of Generalized Hyperbolic Distributions Params: a, v: numerical values b : numpy 2d array # Calculate a, b, c - expectations of w, 1/w, log(w) given x # w : a = a_ig = E[W_ig | x_i, Z_ig = 1] # 1/w : b = b_ig = E[1 / W_ig | x_i, Z_ig = 1] # log(w) : c = c_ig = E[log W_ig | x_i, Z_ig = 1] # abc is a MATRIX # returns a matrix with dim length(a) x 3 stima # a = omega_g + (beta_g)' * sigma_inverse * beta_g # b = omega + delta(x_i, mu_g | sigma_inverse) # kv1 is the denominator BesselK # kv is the numerator BesselK Input x: 2d array data par: ClusterParam object Return: tuple contains 3 values: E[W], E[1/W] and E[log(W)] """ if invS is None: invS = np.linalg.inv(par.sigma) omega = par.omega a = omega + np.matmul(np.matmul(par.alpha, invS), par.alpha) delta = np.array( [mahalanobis(x[i, :], par.mu, invS)**2 for i in range(x.shape[0])]) b = omega + delta v = par.lamda - par.mu.shape[0] / 2 sab = np.sqrt(a * b) kvv = kve(v + 1, sab) / kve(v, sab) sb_a = np.sqrt(b / a) # Expected value of w and 1/w w = kvv * sb_a invw = kvv / sb_a - 2 * v / b logw = np.log(sb_a) + richardson_gradient(log_besselKv, x=v * np.ones(sab.shape), y=sab, eps=1e-8, d=0.0001, r=6, v=2) return (w, invw, logw)
def Fm(x, m, r1, r2, semi=True): ra = r1 rb = r2 if (r2 < r1): rb = r1 ra = r2 if m > 20 and semi: za = x * ra zb = x * rb if (za < 1e-5) and (zb < 1e-5): e = -1 + m * math.log(ra / rb * (m + 1.0) / m) - 0.5 * math.log( (m + 1.0) * m) - math.log(rb / 2 / (m + 1.0)) return -math.exp(2 * e) / 4.0 #return 0.0 sqa1 = math.sqrt(m**2 + za**2) sqa2 = math.sqrt((m + 1)**2 + za**2) sqb1 = math.sqrt(m**2 + zb**2) sqb2 = math.sqrt((m + 1)**2 + zb**2) li1 = sqa1 + m * math.log(za / (m + sqa1)) - 0.5 * math.log(sqa1) li2 = sqa2 + (m + 1) * math.log(za / (m + 1 + sqa2)) - 0.5 * math.log(sqa2) lk1 = -sqb1 - m * math.log(zb / (m + sqb1)) - 0.5 * math.log(sqb1) lk2 = -sqb2 - (m + 1) * math.log(zb / (m + 1 + sqb2)) - 0.5 * math.log(sqb2) L = 2.0 * (li1 + lk1) Li = 2.0 * (li2 - li1) Lk = 2.0 * (lk2 - lk1) return math.exp(L) * x * x * (math.exp(Li) - 1) * (math.exp(Lk) - 1) / 4 #return math.exp(L) * (math.exp(Li) - 1) * (math.exp(Lk) - 1)/4 im1 = special.ive(m, x * ra) km1 = special.kve(m, x * rb) im2 = special.ive(m + 1, x * ra) km2 = special.kve(m + 1, x * rb) e = math.exp(x * (ra - rb)) q1 = im1 * km1 * im1 * km1 q2 = im2 * km2 * im2 * km2 q3 = im1 * km2 * im1 * km2 #q4 = q3 q4 = im2 * km1 * im2 * km1 #q = x * (im1 * km1 - im2 * km2) * e #print r1, r2, m, e, q return x * x * (im1 * im1 - im2 * im2) * (km2 * km2 - km1 * km1) * e * e
def dKo(self,n,z): """odd second-kind radial Mathieu function derivative (DKo) (analogous to K Bessel functions, called Gek in McLachlan p248), for scalar or vector orders or argument n: order (scalar or vector) z: radial argument (scalar or vector)""" from scipy.special import kve,ive n,z,sqrtq,enz,epz,v1,v2,M,EV,OD = self._RadDerivFuncSetup(n,z) dy = np.empty((n.shape[0],z.shape[0]),dtype=complex) ord = self.ord[0:M+2] I = ive(ord[0:M+2,None],v1[None,:])[:,None,:] K = kve(ord[0:M+2,None],v2[None,:])[:,None,:] dI = self._deriv(v1,I[0:M+2,0,:],0)[:,None,:] dK = self._deriv(v2,K[0:M+2,0,:],1)[:,None,:] j = (n[:]-1)//2 dy[EV,:] = (sqrtq/self.B[0,j[EV],0,:]*np.sum(self.B[0:M,j[EV],0,:] * (epz*I[0:M,:,:]*dK[2:M+2,:,:] - enz*dI[0:M,:,:]*K[2:M+2,:,:] - (epz*I[2:M+2,:,:]*dK[0:M,:,:] - enz*dI[2:M+2,:,:]*K[0:M,:,:])),axis=0)) dy[OD,:] = (sqrtq/self.A[0,j[OD],1,:]*np.sum(self.A[0:M,j[OD],1,:] * (epz*I[0:M,:,:]*dK[1:M+1,:,:] - enz*dI[0:M,:,:]*K[1:M+1,:,:] + epz*I[1:M+1,:,:]*dK[0:M,:,:] - enz*dI[1:M+1,:,:]*K[0:M,:,:]),axis=0)) dy *= np.exp(np.abs(v1.real) - v2)[None,:] dy[n==0,:] = np.NaN # dKo_0() invalid return np.squeeze(dy)
def Ko(self,n,z): """odd second-kind radial Mathieu function (Ko) (analogous to K Bessel functions, called Gek in McLachlan p248), for scalar or vector orders or argument n: order (scalar or vector) z: radial argument (scalar or vector)""" from scipy.special import kve,ive n,z,sqrtq,v1,v2,M,EV,OD = self._RadFuncSetup(n,z) y = np.empty((n.shape[0],z.shape[0]),dtype=complex) ord = self.ord[0:M+2] I = ive(ord[0:M+2,None],v1[None,:])[:,None,:] K = kve(ord[0:M+2,None],v2[None,:])[:,None,:] j = (n[:]-1)//2 y[EV,:] = (np.sum(self.B[0:M,j[EV],0,:] * (I[0:M,:,:]*K[2:M+2,:,:] - I[2:M+2,:,:]*K[0:M,:,:]),axis=0)/ self.B[0,j[EV],0,:]) y[OD,:] = (np.sum(self.A[0:M,j[OD],1,:] * (I[0:M,:,:]*K[1:M+1,:,:] + I[1:M+1,:,:]*K[0:M,:,:]),axis=0)/ self.A[0,j[OD],1,:]) y *= np.exp(np.abs(v1.real) - v2)[None,:] y[n==0,:] = np.NaN # Ko_0() invalid return np.squeeze(y)
def Ko(self, n, z): """odd second-kind radial Mathieu function (Ko) (analogous to K Bessel functions, called Gek in McLachlan p248), for scalar or vector orders or argument n: order (scalar or vector) z: radial argument (scalar or vector)""" from scipy.special import kve, ive n, z, sqrtq, v1, v2, M, EV, OD = self._RadFuncSetup(n, z) y = np.empty((n.shape[0], z.shape[0]), dtype=complex) ord = self.ord[0:M + 2] I = ive(ord[0:M + 2, None], v1[None, :])[:, None, :] K = kve(ord[0:M + 2, None], v2[None, :])[:, None, :] j = (n[:] - 1) // 2 y[EV, :] = (np.sum(self.B[0:M, j[EV], 0, :] * (I[0:M, :, :] * K[2:M + 2, :, :] - I[2:M + 2, :, :] * K[0:M, :, :]), axis=0) / self.B[0, j[EV], 0, :]) y[OD, :] = (np.sum(self.A[0:M, j[OD], 1, :] * (I[0:M, :, :] * K[1:M + 1, :, :] + I[1:M + 1, :, :] * K[0:M, :, :]), axis=0) / self.A[0, j[OD], 1, :]) y *= np.exp(np.abs(v1.real) - v2)[None, :] y[n == 0, :] = np.NaN # Ko_0() invalid return np.squeeze(y)
def test_besselk10e(self): xs = [0.1, 1.0, 10.0] for x in xs: sp = kve(10, x) hal = besselkne(10, x) relerr = abs((sp - hal) / sp) self.assertLessEqual(relerr, 0.05)
def testBesselKveZNegativeVLarge(self, dtype, rtol): seed_stream = test_util.test_seed_stream() v = np.linspace(100., 200., num=10, dtype=dtype) z = tf.random.uniform([10], -10., -1., seed=seed_stream(), dtype=dtype) z, bessel_kve = self.evaluate([z, tfp.math.bessel_kve(v, z)]) scipy_kve = scipy_special.kve(v, z) self.assertAllClose(bessel_kve, scipy_kve, rtol=rtol)
def get_prior(E_x, E_invx, k, lamb, rho, tau): #The underlying probability distribution #P(x)=Gamma(x | k, lambda) #Q(x)=GIG(x | k, rho, tau) #The log(x) term is canceled out between logP(x) and logQ(x) #Initialization prior = 0 #Avoid "invalid multiplication" error caused by small tau tau[tau < 1e-100] = 1e-100 #The logP(x) term other than the canceled log(x) prior = prior + (k * np.log(lamb) - sc.gammaln(k)) * E_x.size prior = prior - np.sum(lamb * E_x) #The -logQ(x) term #log2 prior = prior + np.log(2) * E_x.size #-k/2*(log(rho)-log(tau)) prior = prior - (k/2) * np.sum(np.log(rho) - np.log(tau)) #rho*x+tau/x prior = prior + np.sum(rho * E_x + tau * E_invx) #log(bessel(2*sqrt(rho*tau))) prior = prior + np.sum(np.log(sc.kve(k, 2*np.sqrt(rho*tau)))) return prior
def ddghypGH(x, par, log=False, invS=None): mu = par.mu sigma = par.sigma alpha = par.alpha if (len(mu) != len(alpha)): print('mu and alpha do not have the same length.') return d = len(mu) omega = np.exp(np.log(par.omega)) lamda = par.lamda if not invS: invS = np.linalg.pinv(sigma) pa = omega + np.matmul(np.matmul(alpha, invS), alpha) delta = np.array( [mahalanobis(x[i, :], mu, invS)**2 for i in range(x.shape[0])]) mx = omega + delta kx = np.sqrt(mx * pa) xmu = x - mu lvx = np.zeros((x.shape[0], 4)) lvx[:, 0] = (lamda - d / 2) * np.log(kx) lvx[:, 1] = np.log(kve(lamda - d / 2, kx)) - kx lvx[:, 2] = np.matmul(np.matmul(xmu, invS), alpha) lv = np.zeros(6) if np.log(np.linalg.det(sigma)) is np.inf: # need checking sigma = np.eye(len(mu)) lv[0] = -1 / 2 * np.log(np.linalg.det(sigma)) lv[0] -= d / 2 * (np.log(2) + np.log(np.pi)) lv[1] = omega - np.log(kve(lamda, omega)) lv[2] = -lamda / 2 * np.log(1) lv[3] = lamda * np.log(omega) * 0 lv[4] = (d / 2 - lamda) * np.log(pa) val = np.sum(lvx.transpose(), axis=0) + sum(lv) if not log: val = np.exp(val) return val
def Fm(x, m, r1, r2, semi = True): ra = r1 rb = r2 if (r2 < r1): rb = r1 ra = r2 if m > 20 and semi: za = x * ra zb = x * rb if (za < 1e-5) and (zb < 1e-5): e = -1 + m * math.log(ra/rb * (m + 1.0)/m) -0.5 *math.log((m+1.0)*m) - math.log(rb/2/(m + 1.0)) return -math.exp(2*e)/4.0 #return 0.0 sqa1 = math.sqrt(m**2 + za**2) sqa2 = math.sqrt((m + 1)**2 + za**2) sqb1 = math.sqrt(m**2 + zb**2) sqb2 = math.sqrt((m + 1)**2 + zb**2) li1 = sqa1 + m * math.log(za/(m + sqa1)) - 0.5 * math.log(sqa1) li2 = sqa2 + (m + 1) * math.log(za/(m + 1 + sqa2)) - 0.5 * math.log(sqa2) lk1 = -sqb1 - m * math.log(zb/(m + sqb1)) - 0.5 * math.log(sqb1) lk2 = -sqb2 - (m + 1)* math.log(zb/(m + 1 + sqb2)) - 0.5 * math.log(sqb2) L = 2.0*(li1 + lk1) Li = 2.0*(li2 - li1) Lk = 2.0*(lk2 - lk1) return math.exp(L) * x * x * (math.exp(Li) - 1) * (math.exp(Lk) - 1)/4 #return math.exp(L) * (math.exp(Li) - 1) * (math.exp(Lk) - 1)/4 im1 = special.ive(m, x * ra) km1 = special.kve(m, x * rb) im2 = special.ive(m + 1, x * ra) km2 = special.kve(m + 1, x * rb) e = math.exp(x * (ra - rb)) q1 = im1 * km1 * im1 * km1 q2 = im2 * km2 * im2 * km2 q3 = im1 * km2 * im1 * km2 #q4 = q3 q4 = im2 * km1 * im2 * km1 #q = x * (im1 * km1 - im2 * km2) * e #print r1, r2, m, e, q return x * x * (im1 * im1 - im2 * im2) * (km2 * km2 - km1 * km1) * e * e
def estimate(self, context, data): pdf = ScaleMixture() alpha = context.prior.alpha beta = context.prior.beta d = context._d if len(data.shape) == 1: data = data[:, numpy.newaxis] p = -alpha + 0.5 * d b = 2 * beta a = 1e-5 + data.sum(-1) ** 2 s = numpy.clip((numpy.sqrt(b) * kve(p + 1, numpy.sqrt(a * b))) / (numpy.sqrt(a) * kve(p, numpy.sqrt(a * b))), 1e-10, 1e10) pdf.scales = s context.prior.estimate(s) pdf.prior = context.prior return pdf
def get_GIG_expectation(gamma, rho, tau): #Initialize variables if np.isscalar(gamma) == True: gamma = gamma * np.ones_like(rho) #Extend into the same size E_x = np.zeros_like(rho) E_invx = np.zeros_like(rho) #Avoid "invalid multiplication" error caused by small tau tau[tau < 1e-100] = 1e-100 #Compute the scaled bessel function "kve" using the scipy.special rt_rho = np.sqrt(rho) rt_tau = np.sqrt(tau) bessel_gamma = sc.kve(gamma, 2*rt_rho*rt_tau) bessel_gamma_1minus = sc.kve(gamma-1, 2*rt_rho*rt_tau) bessel_gamma_1plus = sc.kve(gamma+1, 2*rt_rho*rt_tau) #Compute expectations of x and inverse x under the GIG E_x = (bessel_gamma_1plus * rt_tau) / (rt_rho * bessel_gamma) E_invx = (bessel_gamma_1minus * rt_rho) / (rt_tau * bessel_gamma) return E_x, E_invx
def compute_gig_expectations(alpha, beta, gamma): if np.asarray(alpha).size == 1: alpha = alpha * np.ones_like(beta) Ex, Exinv = np.zeros_like(beta), np.zeros_like(beta) # For very small values of gamma and positive values of alpha, the GIG # distribution becomes a gamma distribution, and its expectations are both # cheaper and more stable to compute that way. gig_inds = (gamma > 1e-200) gam_inds = (gamma <= 1e-200) if np.any(alpha[gam_inds] < 0): raise ValueError("problem with arguments.") # Compute expectations for GIG distribution. sqrt_beta = np.sqrt(beta[gig_inds]) sqrt_gamma = np.sqrt(gamma[gig_inds]) # Note that we're using the *scaled* version here, since we're just # computing ratios and it's more stable. bessel_alpha_minus = special.kve(alpha[gig_inds] - 1, 2 * sqrt_beta * sqrt_gamma) bessel_alpha = special.kve(alpha[gig_inds], 2 * sqrt_beta * sqrt_gamma) bessel_alpha_plus = special.kve(alpha[gig_inds] + 1, 2 * sqrt_beta * sqrt_gamma) sqrt_ratio = sqrt_gamma / sqrt_beta Ex[gig_inds] = bessel_alpha_plus * sqrt_ratio / bessel_alpha Exinv[gig_inds] = bessel_alpha_minus / (sqrt_ratio * bessel_alpha) # Compute expectations for gamma distribution where we can get away with # it. Ex[gam_inds] = alpha[gam_inds] / beta[gam_inds] Exinv[gam_inds] = beta[gam_inds] / (alpha[gam_inds] - 1) Exinv[Exinv < 0] = np.inf return (Ex, Exinv)
def k012e(x): """Returns k0e(x), k1e(x) and k2e(x) for real or complex x. For real x, the fast exponentially scaled K_n Bessel functions, k0e end k1e are defined in scipy.special, but not ke2, which is computed here from the other two using the recurion: K_n(x) = K_{n-2}(x) + 2(n-1)/z * K_{n-1}(x) For complex x, kve(0,x) and kve(1,x) and the recursion are used. Returns the outputs all three functions, evaluated at x. """ if np.iscomplexobj(x): k0 = special.kve(0,x) k1 = special.kve(1,x) k2 = k0 + 2.0*k1/x else: k0 = special.k0e(x) k1 = special.k1e(x) k2 = k0 + 2.0*k1/x return k0, k1, k2 #def ddxlogK1e(z): """
def logpyt(self, theta, t): alpha = np.exp(theta['log_alpha']) lam = np.exp(theta['log_lambda']) abs_y = np.maximum(np.abs(self.data[t]), tol) # Numerical issues when computing the modified Bessel function #print("Value of (y, alpha, lambda) = ({}, {}, {})".format(abs_y, alpha, lam)) #print("Bessel function = {}".format(kv(lam - 0.5, alpha * abs_y))) if kv(lam - 0.5, alpha * abs_y) > 0: return 2 * lam * np.log(alpha) + (lam - 0.5) * np.log(abs_y) + \ np.log(kv(lam - 0.5, alpha*abs_y)) - 0.5 * np.log(np.pi) - \ gammaln(lam) - (lam - 0.5)*np.log(2*alpha) return 2 * lam * np.log(alpha) + (lam - 0.5) * np.log(abs_y) + \ np.log(kve(lam - 0.5, alpha*abs_y)) - abs_y - 0.5 * np.log(np.pi) - \ gammaln(lam) - (lam - 0.5) * np.log(2 * alpha)
def logpyt(self, theta, t): eta = np.exp(theta['log_eta']) c = np.exp(theta['log_c']) alpha = np.sqrt(2 * c) lam = eta abs_y = np.maximum(np.abs(self.data[t]), tol) # Numerical issues when computing the modified Bessel function if kv(lam - 0.5, alpha * abs_y) > 0: return 2 * lam * np.log(alpha) + (lam - 0.5) * np.log(abs_y) + \ np.log(kv(lam - 0.5, alpha*abs_y)) - 0.5 * np.log(np.pi) - \ gammaln(lam) - (lam - 0.5)*np.log(2*alpha) return 2 * lam * np.log(alpha) + (lam - 0.5) * np.log(abs_y) + \ np.log(kve(lam - 0.5, alpha*abs_y)) - abs_y - 0.5 * np.log(np.pi) - \ gammaln(lam) - (lam - 0.5) * np.log(2 * alpha)
def gig_gamma_term(Ex, Exinv, rho, tau, a, b): score = 0 cut_off = 1e-200 zero_tau = (tau <= cut_off) non_zero_tau = (tau > cut_off) score += Ex.size * (a * log(b) - special.gammaln(a)) score -= np.sum((b - rho) * Ex) score -= np.sum(non_zero_tau) * log(0.5) score += np.sum(tau[non_zero_tau] * Exinv[non_zero_tau]) score -= 0.5 * a * np.sum(np.log(rho[non_zero_tau]) - np.log(tau[non_zero_tau])) # It's numerically safer to use scaled version of besselk score += np.sum(np.log(special.kve(a, 2 * np.sqrt(rho[non_zero_tau] * tau[non_zero_tau]))) - 2 * np.sqrt(rho[non_zero_tau] * tau[non_zero_tau])) score += np.sum(-a * np.log(rho[zero_tau]) + special.gammaln(a)) return score
def gig_gamma_term(Ex, Exinv, rho, tau, a, b): score = 0 cut_off = 1e-200 zero_tau = (tau <= cut_off) non_zero_tau = (tau > cut_off) score += Ex.size * (a * log(b) - special.gammaln(a)) score -= np.sum((b - rho) * Ex) score -= np.sum(non_zero_tau) * log(0.5) score += np.sum(tau[non_zero_tau] * Exinv[non_zero_tau]) score -= 0.5 * a * np.sum( np.log(rho[non_zero_tau]) - np.log(tau[non_zero_tau])) # It's numerically safer to use scaled version of besselk score += np.sum( np.log( special.kve(a, 2 * np.sqrt(rho[non_zero_tau] * tau[non_zero_tau]))) - 2 * np.sqrt(rho[non_zero_tau] * tau[non_zero_tau])) score += np.sum(-a * np.log(rho[zero_tau]) + special.gammaln(a)) return score
def dKo(self, n, z): """odd second-kind radial Mathieu function derivative (DKo) (analogous to K Bessel functions, called Gek in McLachlan p248), for scalar or vector orders or argument n: order (scalar or vector) z: radial argument (scalar or vector)""" from scipy.special import kve, ive n, z, sqrtq, enz, epz, v1, v2, M, EV, OD = self._RadDerivFuncSetup( n, z) dy = np.empty((n.shape[0], z.shape[0]), dtype=complex) ord = self.ord[0:M + 2] I = ive(ord[0:M + 2, None], v1[None, :])[:, None, :] K = kve(ord[0:M + 2, None], v2[None, :])[:, None, :] dI = self._deriv(v1, I[0:M + 2, 0, :], 0)[:, None, :] dK = self._deriv(v2, K[0:M + 2, 0, :], 1)[:, None, :] j = (n[:] - 1) // 2 dy[EV, :] = (sqrtq / self.B[0, j[EV], 0, :] * np.sum(self.B[0:M, j[EV], 0, :] * (epz * I[0:M, :, :] * dK[2:M + 2, :, :] - enz * dI[0:M, :, :] * K[2:M + 2, :, :] - (epz * I[2:M + 2, :, :] * dK[0:M, :, :] - enz * dI[2:M + 2, :, :] * K[0:M, :, :])), axis=0)) dy[OD, :] = (sqrtq / self.A[0, j[OD], 1, :] * np.sum( self.A[0:M, j[OD], 1, :] * (epz * I[0:M, :, :] * dK[1:M + 1, :, :] - enz * dI[0:M, :, :] * K[1:M + 1, :, :] + epz * I[1:M + 1, :, :] * dK[0:M, :, :] - enz * dI[1:M + 1, :, :] * K[0:M, :, :]), axis=0)) dy *= np.exp(np.abs(v1.real) - v2)[None, :] dy[n == 0, :] = np.NaN # dKo_0() invalid return np.squeeze(dy)
def Mtil(m, epr, mur, bet, nu, b): def produto(A,B): C = _np.zeros((A.shape[0],B.shape[1],A.shape[2]),dtype=complex) for i in range(A.shape[0]): for j in range(B.shape[1]): for k in range(A.shape[1]): C[i,j,:] = C[i,j,:] + A[i,k,:]*B[k,j,:] return C for i in range(len(b)): # lembrando que length(b) = número de camadas - 1 x = nu[i+1,:] * b[i] y = nu[i ,:] * b[i] Mt = _np.zeros((4,4,w.shape[0]),dtype=complex) if i<len(b)-1: D = _np.zeros((4,4,nu.shape[1]),dtype=complex) z = nu[i+1,:]*b[i+1] if not any(z.real<0): ind = (z.real<60) A = _scyspe.iv(m,z[ind]) B = _scyspe.kv(m,z[ind]) C = _scyspe.iv(m,x[ind]) E = _scyspe.kv(m,x[ind]) D[0,0,:] = 1 D[1,1,ind] = - B*C/(A*E) D[1,1,~ind] = - _np.exp(-2*(z[~ind]-x[~ind])) D[2,2,:] = 1 D[3,3,ind] = - B*C/(A*E) D[3,3,~ind] = - _np.exp(-2*(z[~ind]-x[~ind])) Mt[0,0,:] = -nu[i+1,:]**2*b[i]/epr[i+1,:]*( epr[i+1,:]/nu[i+1,:]*(-_scyspe.kve(m-1,x)/_scyspe.kve(m,x) - m/x) - epr[i,:]/nu[i,:] *( _scyspe.ive(m-1,y)/_scyspe.ive(m,y) - m/y)) Mt[0,1,:] = -nu[i+1,:]**2*b[i]/epr[i+1,:]*( epr[i+1,:]/nu[i+1,:]*(-_scyspe.kve(m-1,x)/_scyspe.kve(m,x) - m/x) - epr[i,:]/nu[i,:] *(-_scyspe.kve(m-1,y)/_scyspe.kve(m,y) - m/y)) Mt[1,0,:] = -nu[i+1,:]**2*b[i]/epr[i+1,:]*( epr[i+1,:]/nu[i+1,:]*( _scyspe.ive(m-1,x)/_scyspe.ive(m,x) - m/x) - epr[i,:]/nu[i,:] *( _scyspe.ive(m-1,y)/_scyspe.ive(m,y) - m/y)) Mt[1,1,:] = -nu[i+1,:]**2*b[i]/epr[i+1,:]*( epr[i+1,:]/nu[i+1,:]*( _scyspe.ive(m-1,x)/_scyspe.ive(m,x) - m/x) - epr[i,:]/nu[i,:] *(-_scyspe.kve(m-1,y)/_scyspe.kve(m,y) - m/y)) Mt[0,2,:] = (nu[i+1,:]**2/nu[i,:]**2 - 1)*m/(bet*epr[i+1,:]) Mt[0,3,:] = Mt[0,2,:] Mt[1,2,:] = Mt[0,2,:] Mt[1,3,:] = Mt[0,2,:] Mt[2,2,:] = -nu[i+1,:]**2*b[i]/mur[i+1,:]*( mur[i+1,:]/nu[i+1,:]*(-_scyspe.kve(m-1,x)/_scyspe.kve(m,x) - m/x) - mur[i,:]/nu[i,:] *( _scyspe.ive(m-1,y)/_scyspe.ive(m,y) - m/y)) Mt[2,3,:] = -nu[i+1,:]**2*b[i]/mur[i+1,:]*( mur[i+1,:]/nu[i+1,:]*(-_scyspe.kve(m-1,x)/_scyspe.kve(m,x) - m/x) - mur[i,:]/nu[i,:] *(-_scyspe.kve(m-1,y)/_scyspe.kve(m,y) - m/y)) Mt[3,2,:] = -nu[i+1,:]**2*b[i]/mur[i+1,:]*( mur[i+1,:]/nu[i+1,:]*( _scyspe.ive(m-1,x)/_scyspe.ive(m,x) - m/x) - mur[i,:]/nu[i,:] *( _scyspe.ive(m-1,y)/_scyspe.ive(m,y) - m/y)) Mt[3,3,:] = -nu[i+1,:]**2*b[i]/mur[i+1,:]*( mur[i+1,:]/nu[i+1,:]*( _scyspe.ive(m-1,x)/_scyspe.ive(m,x) - m/x) - mur[i,:]/nu[i,:] *(-_scyspe.kve(m-1,y)/_scyspe.kve(m,y) - m/y)) Mt[2,0,:] = (nu[i+1,:]**2/nu[i,:]**2 - 1)*m/(bet*mur[i+1,:]) Mt[2,1,:] = Mt[2,0,:] Mt[3,0,:] = Mt[2,0,:] Mt[3,1,:] = Mt[2,0,:] if len(b) == 1: M = Mt else: if (i ==0): M = produto(D,Mt) elif i < len(b)-1: M = produto(D,produto(Mt,M)) else: M = produto(Mt,M) return M
def rel_thermal_beta(mu): # computes thermal velocity/c for mu return np.sqrt(1.e0 - (kve(1, mu) / kve(2, mu) + 3.e0 / mu)**(-2))
def BK(xip, nr): """Return BK_nr.""" return np.exp(-1j * np.imag(xip)) * kve(nr, xip)
def BK(xip, nr): r"""Return BK_nr.""" return np.exp(-1j * np.imag(xip)) * special.kve(nr, xip)
def besselkve(self, v, x): if v == 1: return special.k1e(x) else: return special.kve(v, x)
def VerifyBesselKve(self, v, z, rtol): bessel_kve, v, z = self.evaluate([tfp.math.bessel_kve(v, z), v, z]) scipy_kve = scipy_special.kve(v, z) self.assertAllClose(bessel_kve, scipy_kve, rtol=rtol)
def log_besselKv(nu, y): return np.log(kve(nu, y)) - np.log(y)
def Mtil(m, epr, mur, bet, nu, b): def produto(A, B): C = _np.zeros((A.shape[0], B.shape[1], A.shape[2]), dtype=complex) for i in range(A.shape[0]): for j in range(B.shape[1]): for k in range(A.shape[1]): C[i, j, :] = C[i, j, :] + A[i, k, :] * B[k, j, :] return C for i in range( len(b)): # lembrando que length(b) = número de camadas - 1 x = nu[i + 1, :] * b[i] y = nu[i, :] * b[i] Mt = _np.zeros((4, 4, w.shape[0]), dtype=complex) if i < len(b) - 1: D = _np.zeros((4, 4, nu.shape[1]), dtype=complex) z = nu[i + 1, :] * b[i + 1] if not any(z.real < 0): ind = (z.real < 60) A = _scyspe.iv(m, z[ind]) B = _scyspe.kv(m, z[ind]) C = _scyspe.iv(m, x[ind]) E = _scyspe.kv(m, x[ind]) D[0, 0, :] = 1 D[1, 1, ind] = -B * C / (A * E) D[1, 1, ~ind] = -_np.exp(-2 * (z[~ind] - x[~ind])) D[2, 2, :] = 1 D[3, 3, ind] = -B * C / (A * E) D[3, 3, ~ind] = -_np.exp(-2 * (z[~ind] - x[~ind])) Mt[0, 0, :] = -nu[i + 1, :]**2 * b[i] / epr[i + 1, :] * ( epr[i + 1, :] / nu[i + 1, :] * (-_scyspe.kve(m - 1, x) / _scyspe.kve(m, x) - m / x) - epr[i, :] / nu[i, :] * (_scyspe.ive(m - 1, y) / _scyspe.ive(m, y) - m / y)) Mt[0, 1, :] = -nu[i + 1, :]**2 * b[i] / epr[i + 1, :] * ( epr[i + 1, :] / nu[i + 1, :] * (-_scyspe.kve(m - 1, x) / _scyspe.kve(m, x) - m / x) - epr[i, :] / nu[i, :] * (-_scyspe.kve(m - 1, y) / _scyspe.kve(m, y) - m / y)) Mt[1, 0, :] = -nu[i + 1, :]**2 * b[i] / epr[i + 1, :] * ( epr[i + 1, :] / nu[i + 1, :] * (_scyspe.ive(m - 1, x) / _scyspe.ive(m, x) - m / x) - epr[i, :] / nu[i, :] * (_scyspe.ive(m - 1, y) / _scyspe.ive(m, y) - m / y)) Mt[1, 1, :] = -nu[i + 1, :]**2 * b[i] / epr[i + 1, :] * ( epr[i + 1, :] / nu[i + 1, :] * (_scyspe.ive(m - 1, x) / _scyspe.ive(m, x) - m / x) - epr[i, :] / nu[i, :] * (-_scyspe.kve(m - 1, y) / _scyspe.kve(m, y) - m / y)) Mt[0, 2, :] = (nu[i + 1, :]**2 / nu[i, :]**2 - 1) * m / (bet * epr[i + 1, :]) Mt[0, 3, :] = Mt[0, 2, :] Mt[1, 2, :] = Mt[0, 2, :] Mt[1, 3, :] = Mt[0, 2, :] Mt[2, 2, :] = -nu[i + 1, :]**2 * b[i] / mur[i + 1, :] * ( mur[i + 1, :] / nu[i + 1, :] * (-_scyspe.kve(m - 1, x) / _scyspe.kve(m, x) - m / x) - mur[i, :] / nu[i, :] * (_scyspe.ive(m - 1, y) / _scyspe.ive(m, y) - m / y)) Mt[2, 3, :] = -nu[i + 1, :]**2 * b[i] / mur[i + 1, :] * ( mur[i + 1, :] / nu[i + 1, :] * (-_scyspe.kve(m - 1, x) / _scyspe.kve(m, x) - m / x) - mur[i, :] / nu[i, :] * (-_scyspe.kve(m - 1, y) / _scyspe.kve(m, y) - m / y)) Mt[3, 2, :] = -nu[i + 1, :]**2 * b[i] / mur[i + 1, :] * ( mur[i + 1, :] / nu[i + 1, :] * (_scyspe.ive(m - 1, x) / _scyspe.ive(m, x) - m / x) - mur[i, :] / nu[i, :] * (_scyspe.ive(m - 1, y) / _scyspe.ive(m, y) - m / y)) Mt[3, 3, :] = -nu[i + 1, :]**2 * b[i] / mur[i + 1, :] * ( mur[i + 1, :] / nu[i + 1, :] * (_scyspe.ive(m - 1, x) / _scyspe.ive(m, x) - m / x) - mur[i, :] / nu[i, :] * (-_scyspe.kve(m - 1, y) / _scyspe.kve(m, y) - m / y)) Mt[2, 0, :] = (nu[i + 1, :]**2 / nu[i, :]**2 - 1) * m / (bet * mur[i + 1, :]) Mt[2, 1, :] = Mt[2, 0, :] Mt[3, 0, :] = Mt[2, 0, :] Mt[3, 1, :] = Mt[2, 0, :] if len(b) == 1: M = Mt else: if (i == 0): M = produto(D, Mt) elif i < len(b) - 1: M = produto(D, produto(Mt, M)) else: M = produto(Mt, M) return M
def k1e(x): if np.iscomplexobj(x): return special.kve(1,x) else: return special.k1e(x)
def k0e(x): if np.iscomplexobj(x): return special.kve(0,x) else: return special.k0e(x)