def angular_gaussian_integral(alpha): """ angular gaussian integral Formula: int[G[alpha, theta]*Sin(theta) dtheta dphi, {dtheta, 0, Pi}, {dphi, 0, 2Pi}] """ factor1 = (np.exp(-1 / (4 * alpha)) * np.sqrt(np.pi)) / (4 * np.sqrt(alpha)) factor2 = 0 factor2 += 2 * erfi(1 / (2 * np.sqrt(alpha))) inside_term_1 = 1 - (np.pi * alpha * (0 + 2j)) inside_term_2 = 1 + (np.pi * alpha * (0 + 2j)) factor2 -= erfi(inside_term_1 / (2 * np.sqrt(alpha))) factor2 -= erfi(inside_term_2 / (2 * np.sqrt(alpha))) return np.real(factor1 * factor2)
def irwav(q, n): """ Irreg wave function """ if n == 0: return PI**(3 / 4) * np.exp(-(q**2) / 2) * special.erfi(q) else: return (1 / np.sqrt(2 * (n - 1) + 2)) * (q * irwav(q, n - 1) - dirwav(q, n - 1))
def ep_xg_angular3(alpha, z): factor = 7 * np.exp(-4.0 / alpha) * np.sqrt( np.pi) * np.cos(z) / (256.0 * np.sqrt(alpha)) factor *= (-3 + 5 * np.power(np.cos(z), 2.0)) it1 = (2.0j + alpha * np.pi) / np.sqrt(alpha) it2 = 1 / np.sqrt(alpha) it3 = 2 / np.sqrt(alpha) it4 = (1 - 1.0j * alpha * np.pi) / np.sqrt(alpha) it5 = (1 + 1.0j * alpha * np.pi) / np.sqrt(alpha) it6 = 2.0 + (1.0j * alpha * np.pi) sum1 = 5.0j * erf(it1) sum1 -= (4.0 * np.exp(3 / alpha)) * erfi(it2) - 10.0 * erfi(it3) sum1 += 2 * np.exp(3 / alpha) * (erfi(it4) + erfi(it5)) sum1 -= 5.0 * erfi(it6) return factor * sum1
def func(f, amp, f_mean, f_sigma, time): # the 2.pi goes with 'time' because we are in the frequency domain and not in the angular frequency domain # time is 'ts-t0' return -amp * np.exp(-np.asarray(f - f_mean)**2 / (2 * abs(f_sigma)**2)) * np.imag( special.erfi( (np.asarray(f - f_mean) + 1j * (abs(f_sigma)**2) * time * 2 * math.pi) / np.sqrt(2 * abs(f_sigma)**2)))
def Moments(SL, SR, A, EL, ER): # complex variables epsilon = ER - EL gamma = A * epsilon**2 delta_S = SR - SL # calculate methods if A < 0: weight = (np.sqrt(np.pi) * np.exp(SL) * np.exp( (gamma - delta_S)**2 / 4 * gamma) * (erf( (delta_S + gamma) / (2 * np.sqrt(gamma))) - erf( (delta_S - gamma) / (2 * np.sqrt(gamma))))) / (2 * np.sqrt(gamma)) avg_E = epsilon * (((delta_S + gamma) / (2 * gamma)) - ((1 - np.exp(-delta_S)) / (erf((delta_S + gamma) / 2 * np.sqrt(gamma)) - erf( (delta_S + gamma) / 2 * np.sqrt(gamma))))) + EL theta = ((np.sqrt(np.pi)/8*gamma**(5/2))((delta_S + gamma)**2+2*gamma))*(-np.exp((delta_S + gamma)**2/4*gamma + delta_S))*erf((delta_S + gamma)/2*np.sqrt(gamma)) + \ np.exp(((delta_S - gamma)**2)/4*gamma)*erf((delta_S - gamma)/2*np.sqrt(gamma)) + \ - ((3*gamma - delta_S)-np.exp(delta_S)*(gamma - delta_S))/(4*gamma**2) avg_E_2 = (epsilon * np.exp(SL) * theta) / weight + 3 * EL**2 - 2 * EL * avg_E stdev_E = np.sqrt(avg_E**2 - avg_E_2) return [weight, avg_E, avg_E_2, stdev_E] elif A > 0: weight = (np.sqrt(np.pi) * np.exp(SL) * np.exp( (gamma - delta_S)**2 / 4 * gamma) * (erfi( (delta_S + gamma) / (2 * np.sqrt(gamma))) - erfi( (delta_S - gamma) / (2 * np.sqrt(gamma))))) / (2 * np.sqrt(gamma)) avg_E = epsilon * (((delta_S + gamma) / (2 * gamma)) - ( (1 - np.exp(-delta_S)) / (erfi( (delta_S + gamma) / 2 * np.sqrt(gamma)) - erfi( (delta_S + gamma) / 2 * np.sqrt(gamma))))) + EL theta = ((np.sqrt(np.pi)/8*gamma**(5/2))((delta_S + gamma)**2+2*gamma))*(-np.exp((delta_S + gamma)**2/4*gamma + delta_S))*erfi((delta_S + gamma)/2*np.sqrt(gamma)) + \ np.exp(((delta_S - gamma)**2)/4*gamma)*erfi((delta_S - gamma)/2*np.sqrt(gamma)) + \ - ((3*gamma - delta_S)-np.exp(delta_S)*(gamma - delta_S))/(4*gamma**2) avg_E_2 = (epsilon * np.exp(SL) * theta) / weight + 3 * EL**2 - 2 * EL * avg_E stdev_E = np.sqrt(avg_E**2 - avg_E_2) return [weight, avg_E, avg_E_2, stdev_E] else: return "A == 0"
def Efn(e_min, e_max, e_ideal, sd, T): erfn_min = erfi((e_min - e_ideal) / sd - sd / (2. * R * T)) erfn_max = erfi((e_max - e_ideal) / sd - sd / (2. * R * T)) fnexp = np.exp(-(sd**2 + 4 * e_ideal * (R * T)) / (4 * (R * T)**2)) e_min = 1 / 4 * sd * (2 * sd * np.exp( (e_ideal - e_min)**2 / sd**2 - e_min / (R * T)) + (np.sqrt(np.pi) * (sd**2 + 2 * e_ideal * (R * T)) * fnexp * erfn_min) / (R * T)) e_max = 1 / 4 * sd * (2 * sd * np.exp( (e_ideal - e_max)**2 / sd**2 - e_max / (R * T)) + (np.sqrt(np.pi) * (sd**2 + 2 * e_ideal * (R * T)) * fnexp * erfn_max) / (R * T)) Z = (sd * np.sqrt(np.pi) * (erfn_max - erfn_min) * fnexp / 2) return (e_max - e_min) / Z
def int_exact(self, x): '''Integral of dawson function: analytical solution''' # Evaluated with arbitrary precision arithmetic # 50 times faster than direct integration; can still suffer from numeric overflow y = np.zeros(x.size) i = 0 for t in x: # run a loop since mpm doesn't support vectorization y[i] = 0.5 * t * t * float(mpm.hyp2f2( 1, 1, 3 / 2, 2, t * t)) + 0.25 * np.pi * erfi(t) i += 1 return y
def calcAve(input_index, A, B, C): #Other imported info: bins[], bstep x = input_index E0 = bins[x] - bstep * 0.5 E1 = bins[x] + bstep * 0.5 if C < 0: F3 = B / (2 * np.sqrt(-C)) F1 = special.erf(np.sqrt(-C) * E1 - F3) - special.erf(np.sqrt(-C) * E0 - F3) comp1 = np.exp(B * E0 + C * E0 * E0) - np.exp(B * E1 + C * E1 * E1) calcAve = 1 / (np.sqrt(-C * np.pi) * np.exp(F3**2)) * comp1 / F1 + F3 / np.sqrt(-C) else: F3 = B / (2 * np.sqrt(C)) F1 = special.erfi(np.sqrt(C) * E1 + F3) - special.erfi(np.sqrt(C) * E0 + F3) comp1 = np.exp(B * E0 + C * E0 * E0) - np.exp(B * E1 + C * E1 * E1) calcAve = -1 / (np.sqrt(C * np.pi)) * np.exp( F3**2) * comp1 / F1 - F3 / np.sqrt(C) return calcAve
def dirwav(q, n): """ Derivative of irreg wave functions """ if n == 0: return PI**(3 / 4) * np.exp(-(q**2) / 2) * ( (2 / np.sqrt(PI)) * np.exp(q**2) - q * special.erfi(q)) else: wf = irwav(q, n - 1) return (1 / np.sqrt(2 * (n - 1) + 2)) * ( (2 * (n - 1) + 2) * wf + q * dirwav(q, n - 1) - (q**2) * wf)
def dipole_gaussian(G, u, alpha): """ Obtains the first moment of function: rho[d] = (u d Exp[-G d - alpha * (theta^2)]) """ factor1 = (np.exp(-1 / alpha) * np.sqrt(np.pi)) / (8 * np.sqrt(alpha)) factor2 = 0 factor2 += 2 * erfi(1 / np.sqrt(alpha)) inside_term_1 = 1 - (np.pi * alpha * (0 + 1j)) inside_term_2 = 1 + (np.pi * alpha * (0 + 1j)) factor2 -= erfi(inside_term_1 / (np.sqrt(alpha))) factor2 -= erfi(inside_term_2 / (np.sqrt(alpha))) factor3 = 12 * np.pi / (G ** 4) * u return np.real(factor1 * factor2 * factor3)
def v_func_gb(f, f_0, f_dot, T, ts): """ function of frequency giving the Fourier transform of exp(-j*Phi(t)), where Phi(t) is the phase of the gravitatonal wave Parameters ---------- f : array_like vector frequencies (size n_data) where to evalutate the function w(f) f_0 : scalar float wave frequency f_dot : scalar float wave frequency derivative T : scalar float integration time ts : scalar float sampling time (cadence) Returns ------- v : numpy array vector of values of v (size n_data) calculated at given frequencies """ if f_dot != 0: f_shift = f + f_0 a = np.pi * f_dot coeff = np.exp(3j * np.pi / 4) / np.sqrt(a) common_factor = - np.exp(1j*np.pi/4)/(2*np.sqrt(a))*np.sqrt(np.pi) \ * np.exp(1j*np.pi**2 * f_shift**2 / a) erfi_0 = special.erfi(coeff * (np.pi*f_shift)) erfi_T = special.erfi(coeff * (a*T+np.pi*f_shift)) return common_factor*(erfi_T - erfi_0)/ts else: return np.exp(-1j*np.pi*(f_0+f)*T)*np.sinc((f_0+f)*T)*T/ts
def Nesc_Triaxial(sigr,sigphi,beta,v_esc): # sigr, sigphi = radial and azimuthal dispersions # beta = anisotropy parameter # v_esc = escape velocity N_esc = erf(v_esc/(sqrt(2.0)*sigr)) - sqrt((1.0-beta)/beta)\ *exp(-v_esc**2.0/(2.0*sigphi**2.0))\ *erfi(v_esc/(sqrt(2)*sigr)*sqrt(beta/(1-beta))) # Make sure the N_esc doesn't blow up (which it does for some streams) if (abs(N_esc)==inf) or isnan(N_esc): N_esc = 1.0 return N_esc
def int_exact(self, X): q = np.zeros(X.size) i = 0 fun1 = lambda x: np.power(erfcx(-x), 2) * dawsn(x) fun2 = lambda x: np.exp(-x * x) * np.power(erfcx(-x), 2) for x in X: y1, _ = quad(fun1, -np.inf, x) y2, _ = quad(fun2, -np.inf, x) q[i] = -np.pi / 4 * y1 + np.power(np.sqrt(np.pi) / 2, 3) * erfi(x) * y2 i += 1 return q
def dipole_gaussian(G, u, alpha): """ :param G: :param u: :param alpha: :return: """ factor1 = (np.exp(-1 / alpha) * np.sqrt(np.pi)) / (8 * np.sqrt(alpha)) factor2 = 0 factor2 += 2 * erfi(1 / np.sqrt(alpha)) inside_term_1 = 1 - (np.pi * alpha * (0 + 1j)) inside_term_2 = 1 + (np.pi * alpha * (0 + 1j)) factor2 -= erfi(inside_term_1 / (np.sqrt(alpha))) factor2 -= erfi(inside_term_2 / (np.sqrt(alpha))) factor3 = 12 * np.pi / (G**4) * u return np.real(factor1 * factor2 * factor3)
def P_2(a): from numpy import pi, sqrt, exp from scipy.special import erfi # Double precision e^x overflows so do a Taylor expansion around infinity # for large values of a. With five terms it is accurate to 12 digits # at a = 700, and 7 digits at a = 75. if a <= 700: r = exp(a) / sqrt(pi * a) / erfi(sqrt(a)) else: r = 1 / ((((6.5525 / a + 1.875) / a + 0.75) / a + 0.5) / a + 1) return 1.5 * r - 0.75 / a - 0.5
def int_nnk(e, L, nnk, l1, m1, l2, m2, alpha): nk = norm(nnk) twopibyL = 2. * math.pi / L k = nk * twopibyL gamma = gam(e, k) x2 = xx2(e, L, k) #hhk = hh(e, k) # TB: put hhk in C x_term = x2**(-(l1 + l2) / 2) # TB #q2s = (x2*twopibyL**2)**(-(l1+l2)/2) # FRL if (l1 != l2 or m1 != m2): return 0. elif (l1 == l2 == 0): factor1 = -sqrt(math.pi / alpha) * 0.5 * np.exp(alpha * (x2)) factor2 = 0.5 * math.pi * sqrt(x2) * erfi(sqrt(alpha * x2)) #out = q2s*hhk*4*math.pi*gamma*(factor1 + factor2) # FRL # out = x_term*4*math.pi*gamma*(factor1 + factor2) #TB out = 4 * math.pi * gamma * (factor1 + factor2) #TB, no q elif (l1 == l2 == 2): factor1 = -np.sqrt(math.pi / alpha**5) * ( 3 + 2 * alpha * x2 + 4 * alpha**2 * x2**2) * np.exp(alpha * (x2)) / 8 factor2 = 0.5 * math.pi * sqrt(x2**5) * erfi(sqrt(alpha * x2)) #out = q2s*hhk*4*math.pi*gamma*(factor1 + factor2) # FRL #out = x_term*4*math.pi*gamma*(factor1 + factor2) #TB out = (2 * pi / L)**4 * 4 * math.pi * gamma * (factor1 + factor2 ) #TB, no q else: return 0. if abs(out.imag) > 1e-15: print('Error in int_nnk: imaginary part in output') else: out = out.real return out
def int_nnk(e, L, nnk, l1, m1, l2, m2, alpha): if (l1 != l2 or m1 != m2): return 0. elif (l1 == l2 == 0): nk = norm(nnk) twopibyL = 2. * math.pi / L k = nk * twopibyL gamma = gam(e, k) x2 = xx2(e, L, k) x_term = x2**(-(l1 + l2) / 2) # TB factor1 = -npsqrt(math.pi / alpha) * 0.5 * exp(alpha * (x2)) factor2 = 0.5 * math.pi * sqrt(x2) * erfi(sqrt(alpha * x2)) out = 4 * math.pi * gamma * (factor1 + factor2) #TB, no q elif (l1 == l2 == 2): nk = norm(nnk) twopibyL = 2. * math.pi / L k = nk * twopibyL gamma = gam(e, k) x2 = xx2(e, L, k) x_term = x2**(-(l1 + l2) / 2) # TB factor1 = -npsqrt(math.pi / alpha**5) * ( 3 + 2 * alpha * x2 + 4 * alpha**2 * x2**2) * exp(alpha * (x2)) / 8 factor2 = 0.5 * math.pi * sqrt(x2**5) * erfi(sqrt(alpha * x2)) out = (2 * pi / L)**4 * 4 * math.pi * gamma * (factor1 + factor2 ) #TB, no q else: return 0. # if abs(out.imag)>1e-15: # print('Error in int_nnk: imaginary part in output') # else: # out = out.real return out.real
def calcAve2(input_index, A, B, C): #Other imported info: bins[], bstep x = input_index E0 = bins[x] - bstep * 0.5 E1 = bins[x] + bstep * 0.5 if C < 0: F3 = B / (2 * np.sqrt(-C)) F1 = special.erf(np.sqrt(-C) * E1 - F3) - special.erf(np.sqrt(-C) * E0 - F3) comp2 = np.exp(B * E0 + C * E0 * E0) * ( B - 2 * C * E0) - np.exp(B * E1 + C * E1 * E1) * (B - 2 * C * E1) calcAve2 = 1 / (2 * (-C)**1.5 * np.pi**0.5 * np.exp( F3**2)) * comp2 / F1 + (B**2 - 2 * C) / (4 * C**2) else: F3 = B / (2 * np.sqrt(C)) F1 = special.erfi(np.sqrt(C) * E1 + F3) - special.erfi(np.sqrt(C) * E0 + F3) comp2 = np.exp(B * E0 + C * E0 * E0) * ( B - 2 * C * E0) - np.exp(B * E1 + C * E1 * E1) * (B - 2 * C * E1) calcAve2 = 1 / (2 * (C)**1.5 * np.pi**0.5) * np.exp( F3**2) * comp2 / F1 + (B**2 - 2 * C) / (4 * C**2) return calcAve2
def _the_integral_variance_per_unit_mut_input(self, x): if x <= self._a: return 0 if self._a < x < self._SCAL / 2.0: numeratori = 2.0 * self._N * ( 2 * (self._rs * dawsn(self._rs / 2.0) - 1 + np.exp(-self._S * x * (1 - x)) * (1 - self._rs * dawsn(self._rs / 2.0 * (1.0 - 2.0 * x))))) else: if x > self._b: x = self._b integtilldenom = 2.0 * self._N * ( 2 * (self._rs * dawsn(self._rs / 2.0) - 1 + np.exp(-self._S * self._DENOMINATOR * (1 - self._DENOMINATOR)) * (1 - self._rs * dawsn(self._rs / 2.0 * (1.0 - 2.0 * self._DENOMINATOR))))) numeratori = 2.0 * self._rs * np.sqrt(np.pi) * np.exp( -self._S / 4.0) * (erfi(self._rs / 2.0 * (1.0 - self._SCAL)) - erfi(self._rs / 2.0 * (1.0 - 2.0 * x))) numeratori += integtilldenom return numeratori
def mean_firstpassage_time(self, x0, a): r""" Return the mean first-passage time for the 1D Ornstein-Uhlenbeck process (exact formula). Parameters ---------- x0: float Initial position a: float Threshold Return ------ tau: float Mean first-passage time Notes ----- The first passage time is defined by :math:`\tau_a(x_0)=\inf \{t>0, X_t>a | X_0=x_0\}`. It is a random variable. Here, we compute only its expectation value, for which an analytical formula is known. General methods for first-passage time conputations are avaiblable in the `stochrare.firstpassage` module. """ if self.mu != 0: raise NotImplementedError( "The theoretical formula has not been checked for nonzero mu") if x0 > a: tau = 0 else: k = np.sqrt(self.theta / (2 * self.D0)) u = np.linspace(k * x0, k * a) integral = np.sqrt(np.pi) / self.theta * np.trapz(erfcx(u), u) tau = np.pi / self.theta * (erfi(k * a) - erfi(k * x0)) - integral return tau
def int_exact(self, X): '''Integral of the 2nd order Dawson function (with a change of order of integration)''' q = np.zeros(X.size) i = 0 fun1 = lambda x: np.power(erfcx(-x), 2) * dawsn(x) fun2 = lambda x: np.exp(-x * x) * np.power(erfcx(-x), 2) for x in X: if x < -25: #== -np.inf: q[i] = self.int_asym_neginf(x) else: y1, _ = quad(fun1, -np.inf, x) y2, _ = quad(fun2, -np.inf, x) q[i] = -np.pi / 4 * y1 + np.power(np.sqrt(np.pi) / 2, 3) * erfi(x) * y2 i += 1 return q
def WatsonHinderedDiffusionCoeff(self, dPar, dPerp, kappa): dw = np.zeros((2, 1)) dParMdPerp = dPar - dPerp if kappa < 1e-5: dParP2dPerp = dPar + 2 * dPerp k2 = kappa * kappa dw[0] = dParP2dPerp / 3 + 4 * dParMdPerp * kappa / 45 \ + 8 * dParMdPerp * k2 / 945 dw[1] = dParP2dPerp / 3 - 2 * dParMdPerp * kappa / 45 \ - 4 * dParMdPerp * k2 / 945 else: sk = np.sqrt(kappa) dawsonf = 0.5 * np.exp(-kappa) * np.sqrt(np.pi) * special.erfi(sk) factor = sk / dawsonf dw[0] = (-dParMdPerp + 2 * dPerp * kappa + dParMdPerp * factor) / (2 * kappa) dw[1] = (dParMdPerp + 2 * (dPar + dPerp) * kappa - dParMdPerp * factor) / (4 * kappa) return dw
def int_fast(self, x): '''fast approximation''' region1 = -np.abs(x) < self.coef.cheb_xmin_for_G region2 = ~region1 region2_pos = x > 0 y = np.zeros(x.size) y[region1] = self.int_asym_neginf(-np.abs(x[region1])) y[region2] = Chebyshev.chebval_no_transform( -np.abs(x[region2]), self.coef.cheb_G_neg, xmin=self.coef.cheb_xmin_for_G, xmax=0, num_subdiv=self.coef.div, degree_cheb=self.coef.deg) y[region2_pos] += np.pi / 2 * erfi(x[region2_pos]) return y
def __init__(self, rho_0, v_LSR, sig_v, v_esc, v_pec, beta, eta): self.LocalDensity = rho_0 self.RotationSpeed = v_LSR self.Dispersion = sig_v self.EscapeSpeed = v_esc self.PeculiarVelocity = v_pec self.Normalisation = erf(v_esc/(sqrt(2.0)*sig_v))-\ sqrt(2.0/pi)*(v_esc/sig_v)*\ exp(-v_esc**2.0/(2.0*sig_v**2.0)) self.SausageEta = eta if eta > 0.0: self.SausageBeta = beta sigr = sqrt(3 * v_LSR**2.0 / (2.0 * (3 - 2.0 * beta))) sigphi = sqrt(3 * v_LSR**2.0 * (1 - beta) / (2.0 * (3 - 2.0 * beta))) sigz = sqrt(3 * v_LSR**2.0 * (1 - beta) / (2.0 * (3 - 2.0 * beta))) self.SausageDispersionTensor = array([sigr, sigphi, sigz]) self.Normalisation = erf(v_esc/(sqrt(2.0)*sigr)) \ - sqrt((1.0-beta)/beta)\ *exp(-v_esc**2.0/(2.0*sigphi**2.0))\ *erfi(v_esc/(sqrt(2)*sigr)*sqrt(beta/(1-beta)))
def dawson2(self, x): """ 2nd order Dawson function (fast approximation) with transformation and subdivision """ region1 = x < -10.0 region3 = x > 10.0 region2 = ~(region1 | region3) region2_pos = region2 & (x > 0) y = np.zeros(x.size) y[region1] = self.asym_neginf(x[region1]) y[region3] = self.asym_posinf(x[region3]) y[region2] = Chebyshev.chebval_neg(-np.abs(x[region2]), self.coef.cheb_neg, num_subdiv=self.coef.div, degree_cheb=self.coef.deg) x_pos = x[region2_pos] G = self.dawson1_int(-x_pos) y[region2_pos] = np.sqrt(np.pi)*np.exp(x_pos*x_pos)*\ (0.5*np.log(2)+2*G+np.pi/2*erfi(x_pos)) \ - y[region2_pos] return y
def Z(xi): return np.sqrt(np.pi)*np.exp(-xi**2)*(1j - sp.erfi(xi))
def WatsonSHCoeff(self, k): # The maximum order of SH coefficients (2n) n = 6 # Computing the SH coefficients C = np.zeros((n + 1)) # 0th order is a constant C[0] = 2 * np.sqrt(np.pi) # Precompute the special function values sk = np.sqrt(k) sk2 = sk * k sk3 = sk2 * k sk4 = sk3 * k sk5 = sk4 * k sk6 = sk5 * k # sk7 = sk6 * k[exact] k2 = k**2 k3 = k2 * k k4 = k3 * k k5 = k4 * k k6 = k5 * k # k7 = k6 * k erfik = special.erfi(sk) ierfik = 1 / erfik ek = np.exp(k) dawsonk = 0.5 * np.sqrt(np.pi) * erfik / ek if k > 0.1: # for large enough kappa C[1] = 3 * sk - (3 + 2 * k) * dawsonk C[1] = np.sqrt(5) * C[1] * ek C[1] = C[1] * ierfik / k C[2] = (105 + 60 * k + 12 * k2) * dawsonk C[2] = C[2] - 105 * sk + 10 * sk2 C[2] = .375 * C[2] * ek / k2 C[2] = C[2] * ierfik C[3] = -3465 - 1890 * k - 420 * k2 - 40 * k3 C[3] = C[3] * dawsonk C[3] = C[3] + 3465 * sk - 420 * sk2 + 84 * sk3 C[3] = C[3] * np.sqrt(13 * np.pi) / 64 / k3 C[3] = C[3] / dawsonk C[4] = 675675 + 360360 * k + 83160 * k2 + 10080 * k3 + 560 * k4 C[4] = C[4] * dawsonk C[4] = C[4] - 675675 * sk + 90090 * sk2 - 23100 * sk3 + 744 * sk4 C[4] = np.sqrt(17) * C[4] * ek C[4] = C[4] / 512 / k4 C[4] = C[4] * ierfik C[5] = -43648605 - 22972950 * k - 5405400 * k2 - 720720 * k3 \ - 55440 * k4 - 2016 * k5 C[5] = C[5] * dawsonk C[5] = C[5] + 43648605 * sk - 6126120 * sk2 + 1729728 * sk3 \ - 82368 * sk4 + 5104 * sk5 C[5] = np.sqrt(21 * np.pi) * C[5] / 4096 / k5 C[5] = C[5] / dawsonk C[6] = 7027425405 + 3666482820 * k + 872972100 * k2 \ + 122522400 * k3 + 10810800 * k4 + 576576 * k5 + 14784 * k6 C[6] = C[6] * dawsonk C[6] = C[6] - 7027425405 * sk + 1018467450 * sk2 \ - 302630328 * sk3 + 17153136 * sk4 - 1553552 * sk5 \ + 25376 * sk6 C[6] = 5 * C[6] * ek C[6] = C[6] / 16384 / k6 C[6] = C[6] * ierfik if k > 30: # for very large kappa lnkd = np.log(k) - np.log(30) lnkd2 = lnkd * lnkd lnkd3 = lnkd2 * lnkd lnkd4 = lnkd3 * lnkd lnkd5 = lnkd4 * lnkd lnkd6 = lnkd5 * lnkd C[1] = 7.52308 + 0.411538 * lnkd - 0.214588 * lnkd2 \ + 0.0784091 * lnkd3 - 0.023981 * lnkd4 + 0.00731537 * lnkd5 \ - 0.0026467 * lnkd6 C[2] = 8.93718 + 1.62147 * lnkd - 0.733421 * lnkd2 \ + 0.191568 * lnkd3 - 0.0202906 * lnkd4 - 0.00779095 * lnkd5 \ + 0.00574847*lnkd6 C[3] = 8.87905 + 3.35689 * lnkd - 1.15935 * lnkd2 \ + 0.0673053 * lnkd3 + 0.121857 * lnkd4 - 0.066642 * lnkd5 \ + 0.0180215 * lnkd6 C[4] = 7.84352 + 5.03178 * lnkd - 1.0193 * lnkd2 \ - 0.426362 * lnkd3 + 0.328816 * lnkd4 - 0.0688176 * lnkd5 \ - 0.0229398 * lnkd6 C[5] = 6.30113 + 6.09914 * lnkd - 0.16088 * lnkd2 \ - 1.05578 * lnkd3 + 0.338069 * lnkd4 + 0.0937157 * lnkd5 \ - 0.106935 * lnkd6 C[6] = 4.65678 + 6.30069 * lnkd + 1.13754 * lnkd2 \ - 1.38393 * lnkd3 - 0.0134758 * lnkd4 + 0.331686 * lnkd5 \ - 0.105954 * lnkd6 if k <= 0.1: # for small kappa C[1] = 4 / 3 * k + 8 / 63 * k2 C[1] = C[1] * np.sqrt(np.pi / 5) C[2] = 8 / 21 * k2 + 32 / 693 * k3 C[2] = C[2] * (np.sqrt(np.pi) * 0.2) C[3] = 16 / 693 * k3 + 32 / 10395 * k4 C[3] = C[3] * np.sqrt(np.pi / 13) C[4] = 32 / 19305 * k4 C[4] = C[4] * np.sqrt(np.pi / 17) C[5] = 64 * np.sqrt(np.pi / 21) * k5 / 692835 C[6] = 128 * np.sqrt(np.pi) * k6 / 152108775 return C
xold = x0 xnews = (x0,) for t in steps[1:]: guess = xold xnew = mynewton(guess) # SOLVE fstep for xnew it, xnew, f_xroot = mynewton(guess) # Store xnew in list xnews = xnews + (xnew,) xold = xnew # Using Wolfram Alpha to determine the analytical solution we find: # x = np.exp(t**2/2)/np.sqrt(np.sqrt(np.pi*np.erfi(t)+1)) t_an = steps x_an = np.exp(t_an**2/2)/np.sqrt(np.sqrt(np.pi)*sp.erfi(t_an)+1) # Plot results fig = plt.figure() axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) axes.set_xlabel('t') axes.set_ylabel('x(t), x0 = 1') axes.plot(steps,x_an,color = "blue",label = "analytical") axes.plot(steps,list(xnews),color = "red",label = "numerical") axes.legend() plt.show()
def eye(z): z = np.asarray(z, float) return (z**4 / 6 * hyp2f2(1.0, 1.0, 2.5, 3, z * z / 2) + np.pi * (1 - z * z) * erfi(z / np.sqrt(2)) - 3 * z * z + np.sqrt(2 * np.pi) * np.exp(z * z / 2) * z + 2.0)
x4 = np.linspace(a, b, n) for p in [0.66, 0.95]: k = 1 / (b - a) + m / 2 * (b - a - np.sqrt(8 * p / m)) x4 = np.linspace((a + b) / 2 + (k / m + 1 / (a - b) / m), b, n) x2 = sigma2 * (-np.log(k) + np.log(1 / (b - a) + m * (x4 - (b + a) / 2))) axes['x2']['x4'].plot(x2, x4, ls) axes['x2']['x4'].plot(np.zeros_like(x4), x4, ls) axes['x2']['x4'].plot(x2, b * np.ones_like(x2), ls) from scipy.special import erf, erfi from scipy.optimize import root for p in [0.66, 0.95]: k = root( lambda k: (1 / (b - a)**2 * (2 + (b - a)**2 * m)**2 * erf( np.sqrt(np.log( (2 + (b - a)**2 * m) / 2 / (b - a) / k))) - 4 * k**2 * erfi( np.sqrt(np.log((2 + (b - a)**2 * m) / 2 / (b - a) / k)))) / 8 / m - p, 0.5).x[0] x1 = np.sqrt(2 * np.log((2 + (a - b)**2 * m) / 2 / (b - a) / k)) x1 = np.linspace(-x1, x1, n) x4 = (a + b) / 2 + np.exp(x1**2 / 2) * k / m - 1 / (b - a) / m x1 *= sigma1 axes['x1']['x4'].plot(x1, x4, ls) axes['x0']['x4'].plot(x1, x4, ls) axes['x1']['x4'].plot(x1, b * np.ones_like(x1), ls) axes['x0']['x4'].plot(x1, b * np.ones_like(x1), ls)
def dawson(x): # Abramowitz and Stegun (1972) a = 0.5 * np.sqrt(np.pi) * np.exp(-np.power(x, 2)) * special.erfi(x) return (a)
def _pdf_angle(self, q, *args): # Sample random values for: q = cos(theta) # P(q) = erfi(sqrt(k)*p*q)/erfi(sqrt(k)*p) a, R = args return R - erfi(a*q)/erfi(a)