def XG_plusBA(n, RA, RB, rh, l, pm1, Om, lam, tau0, width, deltaphi, isP): bA = mp.sqrt(RA**2 - rh**2) / l bB = mp.sqrt(RB**2 - rh**2) / l f = lambda y: 2/Om/(bB-bA) * fp.exp(-fp.j*Om/2*(bB+bA)*y)\ * h_n2(y,n,RA,RB,rh,l,pm1,deltaphi,isP)\ * fp.sin( Om/2*(bB-bA)*(2*(tau0+width)-y)) return fp.quad(f, [2 * tau0 + width, 2 * (tau0 + width)])
def lourenco_dist_mp( mgamma, z, n, m, sigma, Ne, Ne_scal, scal_fac=1): # not sure if I do it right with the scale factor... s = mgamma / 2. / Ne_scal * scal_fac if s == 0: return numpy.nan #s = mgamma # prob = mp.power(2, (1-m)/2.)*mp.power(Ne, 0.5)*mp.power(mp.fabs(s), (m-1)/2.)*(1+1/(Ne*mp.power(sigma, 2.)))*mp.exp(-Ne*s) / (mp.power(mp.pi,0.5)*mp.power(sigma, m)*mp.gamma(m/2.)) * mp.besselk((m-1)/2., Ne*mp.fabs(s)*mp.power(1+1/(Ne*mp.power(sigma, 2.)),0.5)) prob = mp.power(2, -(m + 1) / 2) * mp.exp( -n * s / 4 / mp.power(z, 2.)) * mp.sqrt(n) * mp.power( (1 + 4. * mp.power(z, 2.) / n / mp.power(sigma, 2.)) / mp.power(s, 2.), (1 - m) / 4) * mp.power(sigma, -m) / ( mp.sqrt(mp.pi) * z * mp.gamma(m / 2.)) * mp.besselk( (m - 1) / 2., 1 / (4. * mp.sqrt( mp.power(z, 2.) / (n * mp.power(s, 2.) * (n / mp.power(z, 2.) + 4. / mp.power(sigma, 2.)))))) return float(prob / 2 / Ne_scal * scal_fac)
def jac_ortho_basis_at_mp(self, p, q, r): a = 2 * (1 + p) / (1 - q) - 1 if q != 1 else -1 b = q c = r f = jacobi(self.order - 1, 0, 0, a) df = jacobi_diff(self.order - 1, 0, 0, a) pab = [] for i, (fi, dfi) in enumerate(zip(f, df)): g = jacobi(self.order - i - 1, 2 * i + 1, 0, b) dg = jacobi_diff(self.order - i - 1, 2 * i + 1, 0, b) for j, (gj, dgj) in enumerate(zip(g, dg)): cij = mp.sqrt((2 * i + 1) * (2 * i + 2 * j + 2)) / 2 ** (i + 1) tmp = (1 - b) ** (i - 1) if i > 0 else 1 pij = 2 * tmp * dfi * gj qij = tmp * (-i * fi + (1 + a) * dfi) * gj + (1 - b) ** i * fi * dgj rij = (1 - b) ** i * fi * gj pab.append([cij * pij, cij * qij, cij * rij]) sk = [mp.sqrt(k + 0.5) for k in range(self.order)] hc = [s * jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, c))] dhc = [s * jp for s, jp in zip(sk, jacobi_diff(self.order - 1, 0, 0, c))] return [[pij * hk, qij * hk, rij * dhk] for pij, qij, rij in pab for hk, dhk in zip(hc, dhc)]
def XG_minusAB(n, RA, RB, rh, l, pm1, Om, lam, tau0, width, deltaphi, isP): bA = mp.sqrt(RA**2 - rh**2) / l bB = mp.sqrt(RB**2 - rh**2) / l f = lambda y: 2/Om/(bB-bA) * fp.exp(-fp.j*Om/2*(bB+bA)*y)\ * h_n1(y,n,RA,RB,rh,l,pm1,deltaphi,isP)\ * fp.sin( Om/2*(bB-bA)*(y-2*tau0)) return fp.quad(f, [2 * tau0, 2 * tau0 + width])
def ortho_basis_at_mp(self, p, q, r): r = r if r != 1 else r + mp.eps a = 2*p/(1 - r) b = 2*q/(1 - r) c = r sk = [mp.mpf(2)**(-k - 0.25)*mp.sqrt(k + 0.5) for k in xrange(self.order)] pa = [s*jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, a))] pb = [s*jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, b))] ob = [] for i, pi in enumerate(pa): for j, pj in enumerate(pb): cij = (1 - c)**(i + j) pij = pi*pj pc = jacobi(self.order - max(i, j) - 1, 2*(i + j + 1), 0, c) for k, pk in enumerate(pc): ck = mp.sqrt(2*(k + j + i) + 3) ob.append(cij*ck*pij*pk) return ob
def integrandof_Xn_plus(y, n, RA, RB, rh, l, pm1, Om, lam, tau0, width, deltaphi, isP): bA = mp.sqrt(RA**2 - rh**2) / l bB = mp.sqrt(RB**2 - rh**2) / l return fp.j*4 * fp.exp(-fp.j*Om*(bB+bA)*(tau0+width/2))/(Om*(bB+bA))\ * fp.cos( Om/2*(bB+bA)*(y-width) ) * fp.cos(Om/2*(bB-bA)*y)\ * g_n2(y,n,RA,RB,rh,l,pm1,deltaphi,isP)
def jac_ortho_basis_at_mp(self, p, q, r): a = 2 * (1 + p) / (1 - q) - 1 if q != 1 else -1 b = q c = r f = jacobi(self.order - 1, 0, 0, a) df = jacobi_diff(self.order - 1, 0, 0, a) pab = [] for i, (fi, dfi) in enumerate(zip(f, df)): g = jacobi(self.order - i - 1, 2 * i + 1, 0, b) dg = jacobi_diff(self.order - i - 1, 2 * i + 1, 0, b) for j, (gj, dgj) in enumerate(zip(g, dg)): cij = mp.sqrt((2 * i + 1) * (2 * i + 2 * j + 2)) / 2**(i + 1) tmp = (1 - b)**(i - 1) if i > 0 else 1 pij = 2 * tmp * dfi * gj qij = tmp * (-i * fi + (1 + a) * dfi) * gj + (1 - b)**i * fi * dgj rij = (1 - b)**i * fi * gj pab.append([cij * pij, cij * qij, cij * rij]) sk = [mp.sqrt(k + 0.5) for k in range(self.order)] hc = [s * jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, c))] dhc = [ s * jp for s, jp in zip(sk, jacobi_diff(self.order - 1, 0, 0, c)) ] return [[pij * hk, qij * hk, rij * dhk] for pij, qij, rij in pab for hk, dhk in zip(hc, dhc)]
def ortho_basis_at_mp(self, p, q, r): a = 2 * p / (1 - r) if r != 1 else 0 b = 2 * q / (1 - r) if r != 1 else 0 c = r sk = [ mp.mpf(2)**(-k - 0.25) * mp.sqrt(k + 0.5) for k in range(self.order) ] pa = [s * jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, a))] pb = [s * jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, b))] ob = [] for i, pi in enumerate(pa): for j, pj in enumerate(pb): cij = (1 - c)**(i + j) pij = pi * pj pc = jacobi(self.order - max(i, j) - 1, 2 * (i + j + 1), 0, c) for k, pk in enumerate(pc): ck = mp.sqrt(2 * (k + j + i) + 3) ob.append(cij * ck * pij * pk) return ob
def test_svd_test_case(): # a test case from Golub and Reinsch # (see wilkinson/reinsch: handbook for auto. comp., vol ii-linear algebra, 134-151(1971).) eps = mp.exp(0.8 * mp.log(mp.eps)) a = [[22, 10, 2, 3, 7], [14, 7, 10, 0, 8], [-1, 13, -1, -11, 3], [-3, -2, 13, -2, 4], [ 9, 8, 1, -2, 4], [ 9, 1, -7, 5, -1], [ 2, -6, 6, 5, 1], [ 4, 5, 0, -2, 2]] a = mp.matrix(a) b = mp.matrix([mp.sqrt(1248), 20, mp.sqrt(384), 0, 0]) S = mp.svd_r(a, compute_uv = False) S -= b assert mp.mnorm(S) < eps S = mp.svd_c(a, compute_uv = False) S -= b assert mp.mnorm(S) < eps
def test_gauss_quadrature_dynamic(verbose = False): n = 5 A = mp.randmatrix(2 * n, 1) def F(x): r = 0 for i in xrange(len(A) - 1, -1, -1): r = r * x + A[i] return r def run(qtype, FW, R, alpha = 0, beta = 0): X, W = mp.gauss_quadrature(n, qtype, alpha = alpha, beta = beta) a = 0 for i in xrange(len(X)): a += W[i] * F(X[i]) b = mp.quad(lambda x: FW(x) * F(x), R) c = mp.fabs(a - b) if verbose: print(qtype, c, a, b) assert c < 1e-5 run("legendre", lambda x: 1, [-1, 1]) run("legendre01", lambda x: 1, [0, 1]) run("hermite", lambda x: mp.exp(-x*x), [-mp.inf, mp.inf]) run("laguerre", lambda x: mp.exp(-x), [0, mp.inf]) run("glaguerre", lambda x: mp.sqrt(x)*mp.exp(-x), [0, mp.inf], alpha = 1 / mp.mpf(2)) run("chebyshev1", lambda x: 1/mp.sqrt(1-x*x), [-1, 1]) run("chebyshev2", lambda x: mp.sqrt(1-x*x), [-1, 1]) run("jacobi", lambda x: (1-x)**(1/mp.mpf(3)) * (1+x)**(1/mp.mpf(5)), [-1, 1], alpha = 1 / mp.mpf(3), beta = 1 / mp.mpf(5) )
def w(sigma, t, T0dash): wterm1 = 1 + (sigma**2) / (T0dash**2) wterm2 = 1 + ((1 - sigma)**2) / (T0dash**2) wterm3 = (sigma - 1) * mp.log(wterm1) / 4.0 + nonnegative( (T0dash / 2.0) * mp.atan(sigma / T0dash) - sigma / 2.0) + 1 / (12.0 * (T0dash - 0.33)) return mp.sqrt(wterm1) * mp.sqrt(wterm2) * mp.exp(wterm3)
def createP_sigma(lx, ly, band): C = table.C_Sigma(ly / (2 / mp.sqrt(3)), lx / (mp.sqrt(3) / 2), band) #Values tabulated on a rectangular grid, flipped and compensated for. C = [ C[0], C[1], C[2], C[3] / (3**(1 / 2)), C[4] / (3**(1 / 2)), C[5] / (3**(1 / 2)) ] #For sqrt(3) normalisation, see paper for reference. f = lambda kx, ky, kz: C[0] * fi_As(lx, ly, kx, ky, kz) + C[1] * fi_Ax( lx, ly, kx, ky, kz) + C[2] * fi_Ay(lx, ly, kx, ky, kz) + C[3] * fi_Bs( lx, ly, kx, ky, kz) + C[4] * fi_Bx(lx, ly, kx, ky, kz) + C[ 5] * fi_By(lx, ly, kx, ky, kz) nrm = 1 / mp.sqrt( mp.conj(C[0]) * C[0] + mp.conj(C[1]) * C[1] + mp.conj(C[2]) * C[2] + 3 * (mp.conj(C[3]) * C[3] + mp.conj(C[4]) * C[4] + mp.conj(C[5]) * C[5]) + 2 * mp.re((mp.conj(C[0]) * C[3] * pss + mp.conj(C[0]) * C[4] * psx + mp.conj(C[0]) * C[5] * psy + mp.conj(C[1]) * C[3] * psx + mp.conj(C[1]) * C[4] * pxx + mp.conj(C[1]) * C[5] * pxy + mp.conj(C[2]) * C[3] * psy + mp.conj(C[2]) * C[4] * pxy + mp.conj(C[2]) * C[5] * pyy) * (mp.exp(1j * mp.fdot([lx, ly, 0], R_1)) + mp.exp(1j * mp.fdot([lx, ly, 0], R_2)) + mp.exp(1j * mp.fdot([lx, ly, 0], R_3))))) return lambda kx, ky, kz: f(kx, ky, kz) * nrm
def jac_ortho_basis_at_mp(self, p, q, r): a = 2 * p / (1 - r) if r != 1 else 0 b = 2 * q / (1 - r) if r != 1 else 0 c = r sk = [mp.mpf(2) ** (-k - 0.25) * mp.sqrt(k + 0.5) for k in range(self.order)] fc = [s * jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, a))] gc = [s * jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, b))] dfc = [s * jp for s, jp in zip(sk, jacobi_diff(self.order - 1, 0, 0, a))] dgc = [s * jp for s, jp in zip(sk, jacobi_diff(self.order - 1, 0, 0, b))] ob = [] for i, (fi, dfi) in enumerate(zip(fc, dfc)): for j, (gj, dgj) in enumerate(zip(gc, dgc)): h = jacobi(self.order - max(i, j) - 1, 2 * (i + j + 1), 0, c) dh = jacobi_diff(self.order - max(i, j) - 1, 2 * (i + j + 1), 0, c) for k, (hk, dhk) in enumerate(zip(h, dh)): ck = mp.sqrt(2 * (k + j + i) + 3) tmp = (1 - c) ** (i + j - 1) if i + j > 0 else 1 pijk = 2 * tmp * dfi * gj * hk qijk = 2 * tmp * fi * dgj * hk rijk = ( tmp * (a * dfi * gj + b * fi * dgj - (i + j) * fi * gj) * hk + (1 - c) ** (i + j) * fi * gj * dhk ) ob.append([ck * pijk, ck * qijk, ck * rijk]) return ob
def xdipole_wigner3j_recurrence(l, wig_1llp1_000, wig_1llm1_000, wig_1llp1_1m10, wig_1llm1_1m10): #routine for computing successive Wigner3j symbols for x-polarized dipole based on recurrence #wig_1llp1_000 stores wigner symbols of the form (1 l l+1 ; 0 0 0) #wig_1llm1_000 stores wigner symbols of the form (1 l l-1 ; 0 0 0) mp.dps = mp.dps * 2 if len(wig_1llp1_000) < 2: #the m1=m2=m3=0 wigner3j symbols wig_1llp1_000.clear() wig_1llm1_000.clear() wig_1llp1_000.extend([ mp.mpf(wigner_3j(1, 1, 2, 0, 0, 0).evalf(mp.dps)), mp.mpf(wigner_3j(1, 2, 3, 0, 0, 0).evalf(mp.dps)) ]) wig_1llm1_000.extend([ mp.mpf(wigner_3j(1, 1, 0, 0, 0, 0).evalf(mp.dps)), mp.mpf(wigner_3j(1, 2, 1, 0, 0, 0).evalf(mp.dps)) ]) #the m1=1, m2=-1, m3=0 wigner3j symbols wig_1llp1_1m10.clear() wig_1llm1_1m10.clear() wig_1llp1_1m10.extend([ mp.mpf(wigner_3j(1, 1, 2, 1, -1, 0).evalf(mp.dps)), mp.mpf(wigner_3j(1, 2, 3, 1, -1, 0).evalf(mp.dps)) ]) wig_1llm1_1m10.extend([ mp.mpf(wigner_3j(1, 1, 0, 1, -1, 0).evalf(mp.dps)), mp.mpf(wigner_3j(1, 2, 1, 1, -1, 0).evalf(mp.dps)) ]) i = len(wig_1llp1_000) while i < l: i = i + 1 mp_i = mp.mpf(i) wig_1llm1_000.append(-mp.sqrt(mp_i * (2 * mp_i - 3) / ((mp_i - 1) * (2 * mp_i + 1))) * wig_1llp1_000[i - 3]) wig_1llp1_000.append(-mp.sqrt( (2 * mp_i - 1) * (mp_i + 1) / (mp_i * (2 * mp_i + 3))) * wig_1llm1_000[i - 1]) wig_1llm1_1m10.append( (wig3j_j2rec_j2m1_factor(1, i, i - 1, 1, -1, 0) * wig3j_j2rec_j2m1_factor(1, i - 1, i - 1, 1, -1, 0) + wig3j_j2rec_j2m2_factor(1, i, i - 1, 1, -1, 0)) * wig_1llp1_1m10[i - 1 - 2]) wig_1llp1_1m10.append( (wig3j_j3rec_j3m1_factor(1, i, i + 1, 1, -1, 0) * wig3j_j3rec_j3m1_factor(1, i, i, 1, -1, 0) + wig3j_j3rec_j3m2_factor(1, i, i + 1, 1, -1, 0)) * wig_1llm1_1m10[i - 1]) mp.dps = mp.dps // 2
def XBTZ_n(n, RA, RB, rh, l, pm1, Om, lam, tau0, width, deltaphi=0, isP=False): bA = mp.sqrt(RA**2 - rh**2) / l bB = mp.sqrt(RB**2 - rh**2) / l tau0 *= bA width *= bA return -lam**2*bA*bB * (Xn_minus(n,RA,RB,rh,l,pm1,Om,lam,tau0,width,deltaphi,isP)\ - pm1*Xn_plus(n,RA,RB,rh,l,pm1,Om,lam,tau0,width,deltaphi,isP))
def check_xdipole_spherical_expansion(k, R, xp, yp, zp, dist): print("original xdipole field function") print(xdipole_field(k, 0, 0, -R - dist, xp, yp, zp)) print("spherical wave expansion dipole field") print(xdipole_field_from_spherical_wave(k, 0, 0, -R - dist, xp, yp, zp)) field = lambda x, y, z: xdipole_field(k, 0, 0, -R - dist, x, y, z) fnormsqr = get_field_normsqr(R, field) print(fnormsqr) wig_1llp1_000 = [] wig_1llm1_000 = [] wig_1llp1_1m10 = [] wig_1llm1_1m10 = [] #setup wigner3j lists xdipole_wigner3j_recurrence(2, wig_1llp1_000, wig_1llm1_000, wig_1llp1_1m10, wig_1llm1_1m10) cs = [] cnormsqr = 0.0 l = 0 while (fnormsqr - cnormsqr) / fnormsqr > 1e-4: l += 1 #cl = get_real_RgNM_l_coeffs_for_xdipole_field(l,k,R+dist, wig_1llp1_000,wig_1llm1_000,wig_1llp1_1m10,wig_1llm1_1m10) cl = get_normalized_real_RgNM_l_coeffs_for_xdipole_field( l, k, R, dist, wig_1llp1_000, wig_1llm1_000, wig_1llp1_1m10, wig_1llm1_1m10) cs.extend(cl) # rhoM = mp_rho_M(l,k*R) # rhoN = mp_rho_N(l,k*R) # rhol = [rhoM, rhoM, rhoN, rhoN] #cnormsqr += mp.re( np.sum(np.conjugate(cl)*cl * rhol / (2*k**3)) )#factor of 2 since here m!=0 cnormsqr += mp.re(np.sum(np.conjugate(cl) * cl)) # print(cnormsqr) expfield1 = np.array([mp.zero, mp.zero, mp.zero]) for i in range(1, l + 1): RgMe_field = get_rgM(k, xp, yp, zp, i, 1, 0) RgMo_field = get_rgM(k, xp, yp, zp, i, 1, 1) RgNe_field = get_rgN(k, xp, yp, zp, i, 1, 0) RgNo_field = get_rgN(k, xp, yp, zp, i, 1, 1) # expfield1 += (RgMe_field*cs[4*i-4] + RgMo_field*cs[4*i-3] + # RgNe_field*cs[4*i-2] + RgNo_field*cs[4*i-1]) rhoM = mp_rho_M(i, k * R) rhoN = mp_rho_N(i, k * R) normM = mp.sqrt(rhoM / (2 * k**3)) normN = mp.sqrt(rhoN / (2 * k**3)) expfield1 += (RgMe_field * cs[4 * i - 4] / normM + RgMo_field * cs[4 * i - 3] / normM + RgNe_field * cs[4 * i - 2] / normN + RgNo_field * cs[4 * i - 1] / normN) print("expansion field via real spherical waves is") print(expfield1) print(cs)
def _CalculateLeastUpperBoundInoperativeInterval(x0, x1, v0, v1, vm, am): # All input must already be of mp.mpf type d = x1 - x0 temp1 = Prod([ number('2'), Neg(Sqr(am)), Sub(Prod([number('2'), am, d]), Add(Sqr(v0), Sqr(v1))) ]) if temp1 < zero: T0 = number('-1') T1 = number('-1') else: term1 = mp.fdiv(Add(v0, v1), am) term2 = mp.fdiv(mp.sqrt(temp1), Sqr(am)) T0 = Add(term1, term2) T1 = Sub(term1, term2) temp2 = Prod([ number('2'), Sqr(am), Add(Prod([number('2'), am, d]), Add(Sqr(v0), Sqr(v1))) ]) if temp2 < zero: T2 = number('-1') T3 = number('-1') else: term1 = Neg(mp.fdiv(Add(v0, v1), am)) term2 = mp.fdiv(mp.sqrt(temp2), Sqr(am)) T2 = Add(term1, term2) T3 = Sub(term1, term2) newDuration = max(max(T0, T1), max(T2, T3)) if newDuration > zero: dStraight = Prod([pointfive, Add(v0, v1), newDuration]) if Sub(d, dStraight) > 0: amNew = am vmNew = vm else: amNew = -am vmNew = -vm # import IPython; IPython.embed() vp = Mul(pointfive, Sum([Mul(newDuration, amNew), v0, v1])) # the peak velocity if (Abs(vp) > vm): dExcess = mp.fdiv(Sqr(Sub(vp, vmNew)), am) assert (dExcess > 0) deltaTime = mp.fdiv(dExcess, vm) newDuration = Add(newDuration, deltaTime) newDuration = Mul(newDuration, number('1.01')) # add 1% safety bound return newDuration else: log.debug('Unable to calculate the least upper bound: T0 = {0}; T1 = {1}; T2 = {2}; T3 = {3}'.\ format(mp.nstr(T0, n=_prec), mp.nstr(T1, n=_prec), mp.nstr(T2, n=_prec), mp.nstr(T3, n=_prec))) return number('-1')
def L_transform(j1,j2): d = mp.mpf(10**(-mp.dps+1)) z0,z1 = j1*j2+1/(j1*j2), j1/j2+j2/j1 jp = d + mp.sqrt(z1)/mp.sqrt(z0) dF = mp.log(mp.sqrt(z1)*mp.sqrt(z0)) return jp,dF
def RSZ_upto_c1(x): x = mp.mpf(x.real) tau = mp.sqrt(x/(2*PI)) N = int(tau.real) p = tau-N running_sum=0 for n in range(1,N+1): running_sum += mp.cos(RStheta(x) - x*mp.log(n))/mp.sqrt(n) return (2*running_sum + mp.power(-1,N-1)*mp.power(tau,-0.5)*(c0(p) - (1/tau)*c1(p))).real
def RSZ_plain(x): x = mp.mpf(x.real) tau = mp.sqrt(x / (2 * mp.pi())) N = int(tau.real) z = 2 * (x - N) - 1 running_sum = 0 for n in range(1, N + 1): running_sum += mp.cos(RStheta(x) - (x * mp.log(n))) / mp.sqrt(n) return (2 * running_sum).real
def wig3j_j2rec_j2m2_factor(j1, j2, j3, m1, m2, m3): num = -j2 * mp.sqrt( (j2 - m2 - 1) * (j2 + m2 - 1) * (j1 - j2 + j3 + 2) * (-j1 + j2 + j3 - 1) * (j1 + j2 - j3 - 1) * (j1 + j2 + j3)) denom = (j2 - 1) * mp.sqrt( (j2 - m2) * (j2 + m2) * (j1 - j2 + j3 + 1) * (-j1 + j2 + j3) * (j1 + j2 - j3) * (j1 + j2 + j3 + 1)) return num / denom
def wig3j_j3rec_j3m2_factor(j1, j2, j3, m1, m2, m3): num = -j3 * mp.sqrt( (j3 + m3 - 1) * (j3 - m3 - 1) * (-j1 + j2 + j3 - 1) * (j1 - j2 + j3 - 1) * (j1 + j2 - j3 + 2) * (j1 + j2 + j3)) denom = (j3 - 1) * mp.sqrt( (j3 - m3) * (j3 + m3) * (-j1 + j2 + j3) * (j1 - j2 + j3) * (j1 + j2 - j3 + 1) * (j1 + j2 + j3 + 1)) return num / denom
def gauss_hermite(n): d = mp.matrix([mp.mpf('0.0') for _ in range(n)]) e = [mp.sqrt(k / 2) for k in mp.arange(1, n)] e.append(mp.mpf('0.0')) e = mp.matrix(e) z = mp.eye(n)[0, :] tridiag_eigen(mp, d, e, z) z = mp.sqrt(mp.pi) * z.apply(lambda x: x**2) return d, z.T
def integrand_deltaPdot_n(s, tau, n, R, rh, l, pm1, Om, lam, sig, deltaphi): Zm = mp.mpf(rh**2/(R**2-rh**2)\ *(R**2/rh**2 * fp.cosh(rh/l * (deltaphi - 2*fp.pi*(n+1/2))) - 1)) Zp = mp.mpf(rh**2/(R**2-rh**2)\ *(R**2/rh**2 * fp.cosh(rh/l * (deltaphi - 2*fp.pi*(n+1/2))) + 1)) K = lam**2 * rh / (2 * fp.pi * fp.sqrt(2) * l * fp.sqrt(R**2 - rh**2)) * fp.exp(-tau**2 / 2 / sig**2) return K * fp.exp(-(tau-s)**2/2/sig**2) * fp.exp(-fp.j*Om*s)\ * ( 1/fp.sqrt(Zm + mp.cosh(rh/l/mp.sqrt(R**2-rh**2) * s))\ - pm1*1/fp.sqrt(Zp + mp.cosh(rh/l/mp.sqrt(R**2-rh**2) * s)) )
def Ht_Effective(z, t): """ This uses the effective approximation of H_t from Terry's blog :param z: point at which H_t is computed :param t: the "time" parameter :return: H_t as a sum of two terms that are analogous to A and B, but now also with an efffective error bound (returned as percentage of |H_t| """ z, t = mp.mpc(z), mp.mpc(t) sigma = (1 - z.imag) / 2.0 T = (z.real) / 2.0 Tdash = T + t * mp.pi() / 8.0 s1 = sigma + 1j * T s2 = 1 - sigma + 1j * T N = int((mp.sqrt(Tdash / (2 * mp.pi()))).real) alph1 = alpha1(s1) alph2 = alpha1(s2).conjugate() A0_expo = (t / 4.0) * alph1 * alph1 B0_expo = (t / 4.0) * alph2 * alph2 H01_est1 = H01(s1) H01_est2 = H01(s2).conjugate() #begin main estimate block A0 = mp.exp(A0_expo) * H01_est1 B0 = mp.exp(B0_expo) * H01_est2 A_sum = 0.0 B_sum = 0.0 for n in range(1, N + 1): A_sum += 1 / mp.power(n, s1 + (t / 2.0) * alph1 - (t / 4.0) * mp.log(n)) B_sum += 1 / mp.power( n, 1 - s1 + (t / 2.0) * alph2 - (t / 4.0) * mp.log(n)) A = A0 * A_sum B = B0 * B_sum H = (A + B) / 8.0 #end main estimate block #begin error block A0_err_expo = (t / 4.0) * (abs(alph1)**2) #A0_expo.real may also work B0_err_expo = (t / 4.0) * (abs(alph2)**2) #B0_expo.real may also work epserr_1 = mp.exp(A0_err_expo) * abs(H01_est1) * abs(eps_err(s1, t)) / ( (T - 3.33) * 8.0) epserr_2 = mp.exp(B0_err_expo) * abs(H01_est2) * abs(eps_err(s2, t)) / ( (T - 3.33) * 8.0) epserr = epserr_1 + epserr_2 C0 = mp.sqrt(mp.pi()) * mp.exp(-1 * (t / 64.0) * (mp.pi()**2)) * mp.power( Tdash, 1.5) * mp.exp(-1 * mp.pi() * T / 4.0) C = C0 * vwf_err(s1, t) / 8.0 toterr = epserr + C #print(epserr_1, epserr_2, C0, vwf_err(s1, t), C, toterr.real) #end error block if z.imag == 0: return (H.real, toterr.real / abs(H.real)) else: return (H, toterr.real / abs(H))
def __init__(self, npts): if not mp.isint(mp.sqrt(npts)): raise ValueError('Invalid number of points for quad rule') rulecls = subclass_where(BaseLineQuadRule, name=self.name) rule = rulecls(int(mp.sqrt(npts))) self.points = [(i, j) for j in rule.points for i in rule.points] if hasattr(rule, 'weights'): self.weights = [i*j for j in rule.weights for i in rule.weights]
def XGEON_integrand_nBA(y, n, RA, RB, rh, l, pm1, Om, lam, sig, deltaphi): bA = mp.sqrt(RA**2 - rh**2) / l bB = mp.sqrt(RB**2 - rh**2) / l K = 1 alp2 = bA**2 * bB**2 / 2 / (bA**2 + bB**2) / sig**2 bet2 = (bA + bB) * bA * bB / (bA**2 + bB**2) E = (bB - bA) / fp.sqrt(2) / fp.sqrt(bB**2 + bA**2) * ( (bB + bA) * y / 2 / sig + fp.j * sig * Om) return K*mp.exp(-alp2*y**2)*mp.exp(-fp.j*bet2*Om*y) *fp.erfc(E) \ * XGEON_denoms_n(y,n,RA,RB,rh,l,pm1,Om,lam,sig,deltaphi)
def _CalculateLeastUpperBoundInoperativeInterval(x0, x1, v0, v1, vm, am): # All input must already be of mp.mpf type d = x1 - x0 temp1 = Prod([number('2'), Neg(Sqr(am)), Sub(Prod([number('2'), am, d]), Add(Sqr(v0), Sqr(v1)))]) if temp1 < zero: T0 = number('-1') T1 = number('-1') else: term1 = mp.fdiv(Add(v0, v1), am) term2 = mp.fdiv(mp.sqrt(temp1), Sqr(am)) T0 = Add(term1, term2) T1 = Sub(term1, term2) temp2 = Prod([number('2'), Sqr(am), Add(Prod([number('2'), am, d]), Add(Sqr(v0), Sqr(v1)))]) if temp2 < zero: T2 = number('-1') T3 = number('-1') else: term1 = Neg(mp.fdiv(Add(v0, v1), am)) term2 = mp.fdiv(mp.sqrt(temp2), Sqr(am)) T2 = Add(term1, term2) T3 = Sub(term1, term2) newDuration = max(max(T0, T1), max(T2, T3)) if newDuration > zero: dStraight = Prod([pointfive, Add(v0, v1), newDuration]) if Sub(d, dStraight) > 0: amNew = am vmNew = vm else: amNew = -am vmNew = -vm # import IPython; IPython.embed() vp = Mul(pointfive, Sum([Mul(newDuration, amNew), v0, v1])) # the peak velocity if (Abs(vp) > vm): dExcess = mp.fdiv(Sqr(Sub(vp, vmNew)), am) assert(dExcess > 0) deltaTime = mp.fdiv(dExcess, vm) newDuration = Add(newDuration, deltaTime) log.debug('Calculation successful: T0 = {0}; T1 = {1}; T2 = {2}; T3 = {3}'.format(mp.nstr(T0, n=_prec), mp.nstr(T1, n=_prec), mp.nstr(T2, n=_prec), mp.nstr(T3, n=_prec))) newDuration = Mul(newDuration, number('1.01')) # add 1% safety bound return newDuration else: if (FuzzyEquals(x0, x1, epsilon) and FuzzyZero(v0, epsilon) and FuzzyZero(v1, epsilon)): # t = 0 is actually a correct solution newDuration = 0 return newDuration log.debug('Unable to calculate the least upper bound: T0 = {0}; T1 = {1}; T2 = {2}; T3 = {3}'.\ format(mp.nstr(T0, n=_prec), mp.nstr(T1, n=_prec), mp.nstr(T2, n=_prec), mp.nstr(T3, n=_prec))) return number('-1')
def w(sigma, s, t): T = s.imag T0 = T T0dash = T0 + mp.pi() * t / 8.0 Tdash = T + mp.pi() * t / 8.0 wterm1 = 1 + (sigma**2) / (T0dash**2) wterm2 = 1 + ((1 - sigma)**2) / (T0dash**2) wterm3 = (sigma - 1) * mp.log(wterm1) / 4.0 + nonnegative( (T0dash / 2.0) * mp.atan(sigma / T0dash) - sigma / 2.0) + 1 / (12.0 * (Tdash - 0.33)) return mp.sqrt(wterm1) * mp.sqrt(wterm2) * mp.exp(wterm3)
def z_x123_frm_m(N, m): """Function to get x1, x2 and x3 (eq 3, 5 and 6, [McNamara93]_).""" M = -ellipk(m) / N snMM = ellipfun('sn', u= -M, m=m) snM = ellipfun('sn', u=M, m=m) cnM = ellipfun('cn', u=M, m=m) dnM = ellipfun('dn', u=M, m=m) znM = z_zn(M, m) x3 = snMM x1 = x3 * mp.sqrt(1 - m) / dnM x2 = x3 * mp.sqrt(1 - (cnM * znM) / (snM * dnM)) return x1, x2, x3
def z_x123_frm_m(N, m): """Function to get x1, x2 and x3 (eq 3, 5 and 6, [McNamara93]_).""" M = -ellipk(m) / N snMM = ellipfun('sn', u=-M, m=m) snM = ellipfun('sn', u=M, m=m) cnM = ellipfun('cn', u=M, m=m) dnM = ellipfun('dn', u=M, m=m) znM = z_zn(M, m) x3 = snMM x1 = x3 * mp.sqrt(1 - m) / dnM x2 = x3 * mp.sqrt(1 - (cnM * znM) / (snM * dnM)) return x1, x2, x3
def XGEON_denoms_n(y, n, RA, RB, rh, l, pm1, Om, lam, sig, deltaphi): bA = mp.sqrt(RA**2 - rh**2) / l bB = mp.sqrt(RB**2 - rh**2) / l Zm = rh**2 / l**2 / bA / bB * ( RA * RB / rh**2 * mp.cosh(rh / l * (deltaphi - 2 * mp.pi * (n + 1 / 2))) - 1) # Z-minus Zp = rh**2 / l**2 / bA / bB * ( RA * RB / rh**2 * mp.cosh(rh / l * (deltaphi - 2 * mp.pi * (n + 1 / 2))) + 1) # Z-plus return 1 / mp.sqrt(Zm + mp.cosh(y)) - pm1 / mp.sqrt(Zp + mp.cosh(y))
def test_laguerre_mpmath(): mp.dps = 51 scheme = quadpy.e1r.gauss_laguerre(2, mode="mpmath") tol = 1.0e-50 x1 = 2 - mp.sqrt(2) x2 = 2 + mp.sqrt(2) assert (abs(scheme.points - [x1, x2]) < tol).all() w1 = (2 + mp.sqrt(2)) / 4 w2 = (2 - mp.sqrt(2)) / 4 assert (abs(scheme.weights - [w1, w2]) < tol).all()
def f02(y, n, R, rh, l, pm1, Om, lam, sig): K = lam**2 * sig / 2 / fp.sqrt(2 * fp.pi) a = (R**2 - rh**2) * l**2 / 4 / sig**2 / rh**2 b = fp.sqrt(R**2 - rh**2) * Om * l / rh Zp = mp.mpf((R**2 + rh**2) / (R**2 - rh**2)) if Zp - mp.cosh(y) > 0: return fp.mpf(K * mp.exp(-a * y**2) * mp.cos(b * y) / mp.sqrt(Zp - mp.cosh(y))) elif Zp - mp.cosh(y) < 0: return fp.mpf(-K * mp.exp(-a * y**2) * mp.sin(b * y) / mp.sqrt(mp.cosh(y) - Zp)) else: return 0
def test_laguerre_mpmath(): scheme = quadpy.e1r.GaussLaguerre(2, mode="mpmath", decimal_places=51) tol = 1.0e-50 x1 = 2 - mp.sqrt(2) x2 = 2 + mp.sqrt(2) assert (abs(scheme.points - [x1, x2]) < tol).all() w1 = (2 + mp.sqrt(2)) / 4 w2 = (2 - mp.sqrt(2)) / 4 assert (abs(scheme.weights - [w1, w2]) < tol).all() return
def fn1(y, n, R, rh, l, pm1, Om, lam, sig): K = lam**2 * sig / 2 / fp.sqrt(2 * fp.pi) a = (R**2 - rh**2) * l**2 / 4 / sig**2 / rh**2 b = fp.sqrt(R**2 - rh**2) * Om * l / rh Zm = mp.mpf(rh**2 / (R**2 - rh**2) * (R**2 / rh**2 * fp.cosh(2 * fp.pi * rh / l * n) - 1)) if Zm == mp.cosh(y): return 0 elif Zm - fp.cosh(y) > 0: return fp.mpf(K * mp.exp(-a * y**2) * mp.cos(b * y) / mp.sqrt(Zm - mp.cosh(y))) else: return fp.mpf(-K * mp.exp(-a * y**2) * mp.sin(b * y) / mp.sqrt(mp.cosh(y) - Zm))
def ortho_basis_at_mp(self, p, q, r): sk = [mp.sqrt(k + 0.5) for k in range(self.order)] pa = [c * jp for c, jp in zip(sk, jacobi(self.order - 1, 0, 0, p))] pb = [c * jp for c, jp in zip(sk, jacobi(self.order - 1, 0, 0, q))] pc = [c * jp for c, jp in zip(sk, jacobi(self.order - 1, 0, 0, r))] return [pi * pj * pk for pi in pa for pj in pb for pk in pc]
def _hex_orthob_at(order, p, q, r): sk = [mp.sqrt(k + 0.5) for k in xrange(order)] pa = [c*jp for c, jp in zip(sk, jacobi(order - 1, 0, 0, p))] pb = [c*jp for c, jp in zip(sk, jacobi(order - 1, 0, 0, q))] pc = [c*jp for c, jp in zip(sk, jacobi(order - 1, 0, 0, r))] return [pi*pj*pk for pi in pa for pj in pb for pk in pc]
def _pri_orthob_at(order, p, q, r): a = 2*(1 + p)/(1 - q) - 1 if q != 1 else 0 b = q c = r pab = [] for i, pi in enumerate(jacobi(order - 1, 0, 0, a)): ci = (1 - b)**i / 2**(i + 1) for j, pj in enumerate(jacobi(order - i - 1, 2*i + 1, 0, b)): cij = mp.sqrt((2*i + 1)*(2*i + 2*j + 2))*ci pab.append(cij*pi*pj) sk = [mp.sqrt(k + 0.5) for k in xrange(order)] pc = [s*jp for s, jp in zip(sk, jacobi(order - 1, 0, 0, c))] return [pij*pk for pij in pab for pk in pc]
def jac_ortho_basis_at_mp(self, p, q): sk = [mp.sqrt(k + 0.5) for k in range(self.order)] pa = [c * jp for c, jp in zip(sk, jacobi(self.order - 1, 0, 0, p))] pb = [c * jp for c, jp in zip(sk, jacobi(self.order - 1, 0, 0, q))] dpa = [c * jp for c, jp in zip(sk, jacobi_diff(self.order - 1, 0, 0, p))] dpb = [c * jp for c, jp in zip(sk, jacobi_diff(self.order - 1, 0, 0, q))] return [[dpi * pj, pi * dpj] for pi, dpi in zip(pa, dpa) for pj, dpj in zip(pb, dpb)]
def z_x123_frm_m(N, m): r""" Function to get x1, x2 and x3 (eq 3, 5 and 6, [McNamara93]_). :param N: Order of the Zolotarev polynomial :param m: m is the elliptic parameter (not the modulus k and not the nome q) :rtype: Returns [x1,x2,x3] ... where x1, x2, and x3 are defined in Fig. 1, [McNamara93]_ """ M = -ellipk(m) / N snMM = ellipfun('sn', u= -M, m=m) snM = ellipfun('sn', u=M, m=m) cnM = ellipfun('cn', u=M, m=m) dnM = ellipfun('dn', u=M, m=m) znM = z_zn(M, m) x3 = snMM x1 = x3 * mp.sqrt(1 - m) / dnM x2 = x3 * mp.sqrt(1 - (cnM * znM) / (snM * dnM)) return x1, x2, x3
def ortho_basis_at_mp(self, p, q, r): q = q if q != 1 else q + mp.eps a = 2*(1 + p)/(1 - q) - 1 b = q c = r pab = [] for i, pi in enumerate(jacobi(self.order - 1, 0, 0, a)): ci = (1 - b)**i / 2**(i + 1) for j, pj in enumerate(jacobi(self.order - i - 1, 2*i + 1, 0, b)): cij = mp.sqrt((2*i + 1)*(2*i + 2*j + 2))*ci pab.append(cij*pi*pj) sk = [mp.sqrt(k + 0.5) for k in xrange(self.order)] pc = [s*jp for s, jp in zip(sk, jacobi(self.order - 1, 0, 0, c))] return [pij*pk for pij in pab for pk in pc]
def _tet_orthob_at(order, p, q, r): a = -2*(1 + p)/(q + r) - 1 if q + r != 0 else 0 b = 2*(1 + q)/(1 - r) - 1 if r != 1 else 0 c = r ob = [] for i, pi in enumerate(jacobi(order - 1, 0, 0, a)): ci = mp.mpf(2)**(-2*i - 1.5)*mp.sqrt(4*i + 2)*(1 - b)**i for j, pj in enumerate(jacobi(order - i - 1, 2*i + 1, 0, b)): cj = mp.sqrt(i + j + 1)*2**-j*(1 - c)**(i + j) cij = ci*cj pij = pi*pj jp = jacobi(order - i - j - 1, 2*(i + j + 1), 0, c) for k, pk in enumerate(jp): ck = mp.sqrt(2*(k + j + i) + 3) ob.append(cij*ck*pij*pk) return ob
def ortho_basis_at_mp(self, p, q, r): a = -2 * (1 + p) / (q + r) - 1 if r != -q else -1 b = 2 * (1 + q) / (1 - r) - 1 if r != 1 else -1 c = r ob = [] for i, pi in enumerate(jacobi(self.order - 1, 0, 0, a)): ci = mp.mpf(2) ** (-2 * i - 1) * mp.sqrt(2 * i + 1) * (1 - b) ** i for j, pj in enumerate(jacobi(self.order - i - 1, 2 * i + 1, 0, b)): cj = mp.sqrt(i + j + 1) * 2 ** -j * (1 - c) ** (i + j) cij = ci * cj pij = pi * pj jp = jacobi(self.order - i - j - 1, 2 * (i + j + 1), 0, c) for k, pk in enumerate(jp): ck = mp.sqrt(2 * (k + j + i) + 3) ob.append(cij * ck * pij * pk) return ob
def jac_ortho_basis_at_mp(self, p, q, r): a = -2 * (1 + p) / (q + r) - 1 if r != -q else -1 b = 2 * (1 + q) / (1 - r) - 1 if r != 1 else -1 c = r f = jacobi(self.order - 1, 0, 0, a) df = jacobi_diff(self.order - 1, 0, 0, a) ob = [] for i, (fi, dfi) in enumerate(zip(f, df)): ci = mp.mpf(2) ** (-2 * i - 1) * mp.sqrt(2 * i + 1) g = jacobi(self.order - i - 1, 2 * i + 1, 0, b) dg = jacobi_diff(self.order - i - 1, 2 * i + 1, 0, b) for j, (gj, dgj) in enumerate(zip(g, dg)): cj = mp.sqrt(i + j + 1) * 2 ** -j cij = ci * cj h = jacobi(self.order - i - j - 1, 2 * (i + j + 1), 0, c) dh = jacobi_diff(self.order - i - j - 1, 2 * (i + j + 1), 0, c) for k, (hk, dhk) in enumerate(zip(h, dh)): ck = mp.sqrt(2 * (k + j + i) + 3) cijk = cij * ck tmp1 = (1 - c) ** (i + j - 1) if i + j > 0 else 1 tmp2 = tmp1 * (1 - b) ** (i - 1) if i > 0 else 1 pijk = 4 * tmp2 * dfi * gj * hk qijk = 2 * (tmp2 * (-i * fi + (1 + a) * dfi) * gj + tmp1 * (1 - b) ** i * fi * dgj) * hk rijk = ( 2 * (1 + a) * tmp2 * dfi * gj * hk + (1 + b) * tmp1 * (1 - b) ** i * fi * dgj * hk + (1 - c) ** (i + j) * (1 - b) ** i * fi * gj * dhk - (i * (1 + b) * tmp2 + (i + j) * tmp1 * (1 - b) ** i) * fi * gj * hk ) ob.append([cijk * pijk, cijk * qijk, cijk * rijk]) return ob
def _Interpolate1DNoVelocityLimit(x0, x1, v0, v1, am): # Check types if type(x0) is not mp.mpf: x0 = mp.mpf("{:.15e}".format(x0)) if type(x1) is not mp.mpf: x1 = mp.mpf("{:.15e}".format(x1)) if type(v0) is not mp.mpf: v0 = mp.mpf("{:.15e}".format(v0)) if type(v1) is not mp.mpf: v1 = mp.mpf("{:.15e}".format(v1)) if type(am) is not mp.mpf: am = mp.mpf("{:.15e}".format(am)) # Check inputs assert(am > zero) # Check for an appropriate acceleration direction of the first ramp d = Sub(x1, x0) dv = Sub(v1, v0) difVSqr = Sub(v1**2, v0**2) if Abs(dv) < epsilon: if Abs(d) < epsilon: # Stationary ramp ramp0 = Ramp(zero, zero, zero, x0) return ParabolicCurve([ramp0]) else: dStraight = zero else: dStraight = mp.fdiv(difVSqr, Prod([2, mp.sign(dv), am])) if IsEqual(d, dStraight): # With the given distance, v0 and v1 can be directly connected using max/min # acceleration. Here the resulting profile has only one ramp. a0 = mp.sign(dv) * am ramp0 = Ramp(v0, a0, mp.fdiv(dv, a0), x0) return ParabolicCurve([ramp0]) sumVSqr = Add(v0**2, v1**2) sigma = mp.sign(Sub(d, dStraight)) a0 = sigma * am # acceleration of the first ramp vp = sigma * mp.sqrt(Add(Mul(pointfive, sumVSqr), Mul(a0, d))) t0 = mp.fdiv(Sub(vp, v0), a0) t1 = mp.fdiv(Sub(vp, v1), a0) ramp0 = Ramp(v0, a0, t0, x0) assert(IsEqual(ramp0.v1, vp)) # check soundness ramp1 = Ramp(vp, Neg(a0), t1) curve = ParabolicCurve([ramp0, ramp1]) assert(IsEqual(curve.d, d)) # check soundness return curve
def ortho_basis_at_mp(self, p, q): a = 2 * (1 + p) / (1 - q) - 1 if q != 1 else -1 b = q ob = [] for i, pi in enumerate(jacobi(self.order - 1, 0, 0, a)): pa = pi * (1 - b) ** i for j, pj in enumerate(jacobi(self.order - i - 1, 2 * i + 1, 0, b)): cij = mp.sqrt((2 * i + 1) * (2 * i + 2 * j + 2)) / 2 ** (i + 1) ob.append(cij * pa * pj) return ob
def _tri_orthob_at(order, p, q): a = 2*(1 + p)/(1 - q) - 1 if q != 1 else 0 b = q ob = [] for i, pi in enumerate(jacobi(order - 1, 0, 0, a)): pa = pi*(1 - b)**i for j, pj in enumerate(jacobi(order - i - 1, 2*i + 1, 0, b)): cij = mp.sqrt((2*i + 1)*(2*i + 2*j + 2)) / 2**(i + 1) ob.append(cij*pa*pj) return ob
def z_Zolotarev(N, x, m): """Function to evaluate the Zolotarev polynomial (eq 1, [McNamara93]_).""" M = -ellipk(m) / N x3 = ellipfun('sn', u= -M, m=m) xbar = x3 * mp.sqrt((x ** 2 - 1) / (x ** 2 - x3 ** 2)) # rearranged eq 21, [Levy70]_ u = ellipf(mp.asin(xbar), m) # rearranged eq 20, [Levy70]_, asn(x) = F(asin(x)|m) f = mp.cosh((N / 2) * mp.log(z_eta(M + u, m) / z_eta(M - u, m))) if (f.imag / f.real > 1e-10): print "imaginary part of the Zolotarev function is not negligible!" print "f_imaginary = ", f.imag else: if (x > 0): # no idea why I am doing this ... anyhow, it seems working f = -f.real else: f = f.real return f
def SolveQuartic(a, b, c, d, e): """ SolveQuartic solves a quartic (fouth order) equation of the form ax^4 + bx^3 + cx^2 + dx + e = 0. For the detail of formulae presented here, see https://en.wikipedia.org/wiki/Quartic_function """ # Check types if type(a) is not mp.mpf: a = mp.mpf("{:.15e}".format(a)) if type(b) is not mp.mpf: b = mp.mpf("{:.15e}".format(b)) if type(c) is not mp.mpf: c = mp.mpf("{:.15e}".format(c)) if type(d) is not mp.mpf: d = mp.mpf("{:.15e}".format(d)) if type(e) is not mp.mpf: e = mp.mpf("{:.15e}".format(e)) """ # Working code (more readable but probably less precise) p = (8*a*c - 3*b*b)/(8*a*a) q = (b**3 - 4*a*b*c + 8*a*a*d)/(8*a*a*a) delta0 = c*c - 3*b*d + 12*a*e delta1 = 2*(c**3) - 9*b*c*d + 27*b*b*e + 27*a*d*d - 72*a*c*e Q = mp.nthroot(pointfive*(delta1 + mp.sqrt(delta1*delta1 - 4*mp.power(delta0, 3))), 3) S = pointfive*mp.sqrt(-mp.fdiv(mp.mpf('2'), mp.mpf('3'))*p + (one/(3*a))*(Q + delta0/Q)) x1 = -b/(4*a) - S + pointfive*mp.sqrt(-4*S*S - 2*p + q/S) x2 = -b/(4*a) - S - pointfive*mp.sqrt(-4*S*S - 2*p + q/S) x3 = -b/(4*a) + S + pointfive*mp.sqrt(-4*S*S - 2*p - q/S) x4 = -b/(4*a) + S - pointfive*mp.sqrt(-4*S*S - 2*p - q/S) """ p = mp.fdiv(Sub(Prod([number('8'), a, c]), Mul(number('3'), mp.power(b, 2))), Mul(number('8'), mp.power(a, 2))) q = mp.fdiv(Sum([mp.power(b, 3), Prod([number('-4'), a, b, c]), Prod([number('8'), mp.power(a, 2), d])]), Mul(8, mp.power(a, 3))) delta0 = Sum([mp.power(c, 2), Prod([number('-3'), b, d]), Prod([number('12'), a, e])]) delta1 = Sum([Mul(2, mp.power(c, 3)), Prod([number('-9'), b, c, d]), Prod([number('27'), mp.power(b, 2), e]), Prod([number('27'), a, mp.power(d, 2)]), Prod([number('-72'), a, c, e])]) Q = mp.nthroot(Mul(pointfive, Add(delta1, mp.sqrt(Add(mp.power(delta1, 2), Mul(number('-4'), mp.power(delta0, 3)))))), 3) S = Mul(pointfive, mp.sqrt(Mul(mp.fdiv(mp.mpf('-2'), mp.mpf('3')), p) + Mul(mp.fdiv(one, Mul(number('3'), a)), Add(Q, mp.fdiv(delta0, Q))))) # log.debug("p = {0}".format(mp.nstr(p, n=_prec))) # log.debug("q = {0}".format(mp.nstr(q, n=_prec))) # log.debug("delta0 = {0}".format(mp.nstr(delta0, n=_prec))) # log.debug("delta1 = {0}".format(mp.nstr(delta1, n=_prec))) # log.debug("Q = {0}".format(mp.nstr(Q, n=_prec))) # log.debug("S = {0}".format(mp.nstr(S, n=_prec))) x1 = Sum([mp.fdiv(b, Mul(number('-4'), a)), Neg(S), Mul(pointfive, mp.sqrt(Sum([Mul(number('-4'), mp.power(S, 2)), Mul(number('-2'), p), mp.fdiv(q, S)])))]) x2 = Sum([mp.fdiv(b, Mul(number('-4'), a)), Neg(S), Neg(Mul(pointfive, mp.sqrt(Sum([Mul(number('-4'), mp.power(S, 2)), Mul(number('-2'), p), mp.fdiv(q, S)]))))]) x3 = Sum([mp.fdiv(b, Mul(number('-4'), a)), S, Mul(pointfive, mp.sqrt(Sum([Mul(number('-4'), mp.power(S, 2)), Mul(number('-2'), p), Neg(mp.fdiv(q, S))])))]) x4 = Sum([mp.fdiv(b, Mul(number('-4'), a)), S, Neg(Mul(pointfive, mp.sqrt(Sum([Mul(number('-4'), mp.power(S, 2)), Mul(number('-2'), p), Neg(mp.fdiv(q, S))]))))]) return [x1, x2, x3, x4]
def jac_ortho_basis_at_mp(self, p, q): a = 2 * (1 + p) / (1 - q) - 1 if q != 1 else -1 b = q f = jacobi(self.order - 1, 0, 0, a) df = jacobi_diff(self.order - 1, 0, 0, a) ob = [] for i, (fi, dfi) in enumerate(zip(f, df)): g = jacobi(self.order - i - 1, 2 * i + 1, 0, b) dg = jacobi_diff(self.order - i - 1, 2 * i + 1, 0, b) for j, (gj, dgj) in enumerate(zip(g, dg)): cij = mp.sqrt((2 * i + 1) * (2 * i + 2 * j + 2)) / 2 ** (i + 1) tmp = (1 - b) ** (i - 1) if i > 0 else 1 pij = 2 * tmp * dfi * gj qij = tmp * (-i * fi + (1 + a) * dfi) * gj + (1 - b) ** i * fi * dgj ob.append([cij * pij, cij * qij]) return ob
def test_levin_3(): mp.dps = 17 z=mp.mpf(2) eps = mp.mpf(mp.eps) with mp.extraprec(7*mp.prec): # we need copious amount of precision to sum this highly divergent series L = mp.levin(method = "levin", variant = "t") n, s = 0, 0 while 1: s += (-z)**n * mp.fac(4 * n) / (mp.fac(n) * mp.fac(2 * n) * (4 ** n)) n += 1 v, e = L.step_psum(s) if e < eps: break if n > 1000: raise RuntimeError("iteration limit exceeded") eps = mp.exp(0.8 * mp.log(eps)) exact = mp.quad(lambda x: mp.exp( -x * x / 2 - z * x ** 4), [0,mp.inf]) * 2 / mp.sqrt(2 * mp.pi) # there is also a symbolic expression for the integral: # exact = mp.exp(mp.one / (32 * z)) * mp.besselk(mp.one / 4, mp.one / (32 * z)) / (4 * mp.sqrt(z * mp.pi)) err = abs(v - exact) assert err < eps w = mp.nsum(lambda n: (-z)**n * mp.fac(4 * n) / (mp.fac(n) * mp.fac(2 * n) * (4 ** n)), [0, mp.inf], method = "levin", levin_variant = "t", workprec = 8*mp.prec, steps = [2] + [1 for x in xrange(1000)]) err = abs(v - w) assert err < eps
def norm_q(prob): r""" A multi-precision calculation of the standard normal quantile function: .. math:: \int_{-\infty}^{q(p)} \frac{e^{-z^2/2}}{\sqrt{2\pi}} \; dz = p where $p$ is `prob`. Parameters ---------- prob : float Returns ------- quantile : float """ return np.array(mp.erfinv(2*prob-1)*mp.sqrt(2))
def z_Zolotarev(N, x, m): r""" Function to evaluate the Zolotarev polynomial (eq 1, [McNamara93]_). :param N: Order of the Zolotarev polynomial :param x: The argument at which one would like to evaluate the Zolotarev polynomial :param m: m is the elliptic parameter (not the modulus k and not the nome q) :rtype: Returns a float, the value of Zolotarev polynomial at x """ M = -ellipk(m) / N x3 = ellipfun('sn', u= -M, m=m) xbar = x3 * mp.sqrt((x ** 2 - 1) / (x ** 2 - x3 ** 2)) # rearranged eq 21, [Levy70]_ u = ellipf(mp.asin(xbar), m) # rearranged eq 20, [Levy70]_, asn(x) = F(asin(x)|m) f = mp.cosh((N / 2) * mp.log(z_eta(M + u, m) / z_eta(M - u, m))) if (f.imag / f.real > 1e-10): print "imaginary part of the Zolotarev function is not negligible!" print "f_imaginary = ", f.imag else: if (x > 0): # no idea why I am doing this ... anyhow, it seems working f = -f.real else: f = f.real return f
Yp = mp.matrix(f.size, 1) Zc = mp.matrix(f.size, 1) A = np.ndarray(f.size) R = np.ndarray(f.size) I = np.ndarray(f.size) Gr = np.ndarray(f.size) Gi = np.ndarray(f.size) for n in range(f.size): Zap[n] = ZapA[n]*( i0(gammac[n]*a) / i1(gammac[n]*a) ) Zbp[n] = ZbpA[n] * ((i0(gammac[n]*b) * k1(gammac[n]*c) + k0(gammac[n]*b)*i1(gammac[n]*c)) / ( i1(gammac[n]*c)*k1(gammac[n]*b) - i1(gammac[n]*b)*k1(gammac[n]*c) ) ) ZL[n] = 1j*omega[n]*Lp YC[n] = 1j*omega[n]*Cp Zp[n] = Zap[n] + Zbp[n] + ZL[n] Yp[n] = YC[n] # + G[n] Zc[n] = mp.sqrt(Zp[n]/Yp[n]) A[n] = mp.fabs(Zc[n]) R[n] = mp.re(Zc[n]) I[n] = mp.im(Zc[n]) Gr[n] = mp.re(mp.sqrt(Zp[n]*Yp[n])) Gi[n] = mp.im(mp.sqrt(Zp[n]*Yp[n])) fig = plt.figure() plt.plot(f/10**6, A, label='Magnitude', color='red') plt.xlabel('Frequency [MHz]') plt.ylabel('Magnitude of Z$_{c}$ [$\Omega$]') plt.legend() plt.xlim([-0.3,100]) #plt.show() fig2 = plt.figure() plt.plot(f/10**6, R, label='Real Part', color='red')
def dcurve(a, b, c): return a + b + c + 2 * mp.sqrt(a * b + b * c + c * a)
# * Description: AC. # * http://en.wikipedia.org/wiki/Descartes%27_theorem # ********************************/ from mpmath import mp def dcurve(a, b, c): return a + b + c + 2 * mp.sqrt(a * b + b * c + c * a) def dfs(a, b, c, depth): if depth == 10: return 0 a, b, c = sorted([a, b, c]) d = dcurve(a, b, c) if a == b == c: return 1 / d**2 + 3 * dfs(a, a, d, depth + 1) elif a == b != c: return 1 / d**2 + dfs(a, a, d, depth + 1) + 2 * dfs(b, c, d, depth + 1) elif a != b == c: return 1 / d**2 + dfs(b, c, d, depth + 1) + 2 * dfs(a, b, d, depth + 1) elif a != b != c: return 1 / d**2 + dfs(a, b, d, depth + 1) + dfs(a, c, d, depth + 1) + dfs(b, c, d, depth + 1) else: print("ERROR") k0 = 1 + 2 / mp.sqrt(3) print("Answer is %.8f" % (1 - (dfs(k0, k0, k0, 0) + 3 * dfs(k0, k0, -1, 0) + 3 / k0**2)))
def _quad_orthob_at(order, p, q): sk = [mp.sqrt(k + 0.5) for k in xrange(order)] pa = [c*jp for c, jp in zip(sk, jacobi(order - 1, 0, 0, p))] pb = [c*jp for c, jp in zip(sk, jacobi(order - 1, 0, 0, q))] return [pi*pj for pi in pa for pj in pb]