def test_chebyshev1_mpmath(): scheme = quadpy.line_segment.ChebyshevGauss1(4, mode="mpmath", decimal_places=50) tol = 1.0e-50 x1 = mp.cos(3 * mp.pi / 8) x2 = mp.cos(1 * mp.pi / 8) assert (abs(scheme.points - [-x2, -x1, +x1, +x2]) < tol).all() w = mp.pi / 4 tol = 1.0e-49 assert (abs(scheme.weights - [w, w, w, w]) < tol).all() return
def test_chebyshev1_mpmath(): mp.dps = 50 scheme = quadpy.c1.chebyshev_gauss_1(4, mode="mpmath") tol = 1.0e-50 x1 = mp.cos(3 * mp.pi / 8) x2 = mp.cos(1 * mp.pi / 8) assert (abs(scheme.points_symbolic - [+x2, +x1, -x1, -x2]) < tol).all() w = mp.pi / 4 tol = 1.0e-49 assert (abs(scheme.weights_symbolic - [w, w, w, w]) < tol).all()
def test_chebyshev2_mpmath(): scheme = quadpy.line_segment.ChebyshevGauss2(4, mode="mpmath", decimal_places=51) tol = 1.0e-50 x1 = mp.cos(2 * mp.pi / 5) x2 = mp.cos(1 * mp.pi / 5) assert (abs(scheme.points - [-x2, -x1, +x1, +x2]) < tol).all() w1 = mp.pi / 5 * mp.sin(2 * mp.pi / 5) ** 2 w2 = mp.pi / 5 * mp.sin(1 * mp.pi / 5) ** 2 assert (abs(scheme.weights - [w2, w1, w1, w2]) < tol).all() return
def test_chebyshev2_mpmath(): mp.dps = 51 scheme = quadpy.c1.chebyshev_gauss_2(4, mode="mpmath") tol = 1.0e-50 x1 = mp.cos(2 * mp.pi / 5) x2 = mp.cos(1 * mp.pi / 5) assert (abs(scheme.points_symbolic - [+x2, +x1, -x1, -x2]) < tol).all() w1 = mp.pi / 5 * mp.sin(2 * mp.pi / 5)**2 w2 = mp.pi / 5 * mp.sin(1 * mp.pi / 5)**2 assert (abs(scheme.weights_symbolic - [w2, w1, w1, w2]) < tol).all()
def ddTk(x): with mp.extradps(extradps): x = clip(x, -1.0, 1.0) if mp.almosteq(x, mp.one): return pos if mp.almosteq(x, -mp.one): return neg moredps = max( 0, int(-math.log10( min(abs(x - mp.one), abs(x + mp.one))) * 1.5) + 2) moredps = min(moredps, 100) with mp.extradps(moredps): t = mp.acos(x) s = mp.sin(t) return -k**2 * mp.cos(k * t) / s**2 + k * mp.cos( t) * mp.sin(k * t) / s**3
def chebyshev_gauss_2(n, mode="numpy", decimal_places=None): """Chebyshev-Gauss quadrature for \\int_{-1}^1 f(x) * sqrt(1+x^2) dx.""" degree = n if n % 2 == 1 else n + 1 # TODO make explicit for all modes if mode == "numpy": points = numpy.cos(numpy.pi * numpy.arange(1, n + 1) / (n + 1)) weights = ( numpy.pi / (n + 1) * (numpy.sin(numpy.pi * numpy.arange(1, n + 1) / (n + 1))) ** 2 ) elif mode == "sympy": points = numpy.array( [sympy.cos(sympy.Rational(k, n + 1) * sympy.pi) for k in range(1, n + 1)] ) weights = numpy.array( [ sympy.pi / (n + 1) * sympy.sin(sympy.pi * sympy.Rational(k, n + 1)) ** 2 for k in range(1, n + 1) ] ) else: assert mode == "mpmath" points = numpy.array( [mp.cos(mp.mpf(k) / (n + 1) * mp.pi) for k in range(1, n + 1)] ) weights = numpy.array( [ mp.pi / (n + 1) * mp.sin(mp.pi * mp.mpf(k) / (n + 1)) ** 2 for k in range(1, n + 1) ] ) return C1Scheme("Chebyshev-Gauss 2", degree, weights, points)
def two_body_reference(self, t1, num_1 = 0, num_2 = 1): """ reference notations are same as Yoel's notes using a precision of 64 digits for max accuracy """ mp.dps = 64 self.load_data() t1 = mp.mpf(t1) m_1 = mp.mpf(self.planet_lst[num_1].mass) m_2 = mp.mpf(self.planet_lst[num_2].mass) x1_0 = mp.matrix(self.planet_lst[num_1].loc) x2_0 = mp.matrix(self.planet_lst[num_2].loc) v1_0 = mp.matrix(self.planet_lst[num_1].v) v2_0 = mp.matrix(self.planet_lst[num_2].v) M = m_1 + m_2 x_cm = (1/M)*((m_1*x1_0)+(m_2*x2_0)) v_cm = (1/M)*((m_1*v1_0)+(m_2*v2_0)) u1_0 = v1_0 - v_cm w = x1_0 - x_cm r_0 = mp.norm(w) alpha = mp.acos((np.inner(u1_0,w))/(mp.norm(u1_0)*mp.norm(w))) K = mp.mpf(self.G) * (m_2**3)/(M**2) u1_0 = mp.norm(u1_0) L = r_0 * u1_0 * mp.sin(alpha) cosgamma = ((L**2)/(K*r_0)) - 1 singamma = -((L*u1_0*mp.cos(alpha))/K) gamma = mp.atan2(singamma, cosgamma) e = mp.sqrt(((r_0*(u1_0**2)*(mp.sin(alpha)**2)/K)-1)**2 + \ (((r_0**2)*(u1_0**4)*(mp.sin(alpha)**2)*(mp.cos(alpha)**2))/(K**2)) ) r_theta = lambda theta: (L**2)/(K*(1+(e*mp.cos(theta - gamma)))) f = lambda x: r_theta(x)**2/L curr_theta = 0 max_it = 30 it = 1 converged = 0 while ((it < max_it) & (converged != 1)): t_val = mp.quad(f,[0,curr_theta]) - t1 dt_val = f(curr_theta) delta = t_val/dt_val if (abs(delta)<1.0e-6): converged = 1 curr_theta -= delta it += 1 x1_new = mp.matrix([r_theta(curr_theta)*mp.cos(curr_theta), r_theta(curr_theta)*mp.sin(curr_theta)]) # x2_new = -(m_1/m_2)*(x1_new) +(x_cm + v_cm*t1) x1_new = x1_new + (x_cm + v_cm*t1) return x1_new
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 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 Ht_complex_integrand(u, z, t): """ Computes the integrand of H_t(u) in Terry' blog at https://terrytao.wordpress.com/2018/02/02/polymath15-second-thread-generalising-the-riemann-siegel-approximate-functional-equation/ :param u: integration parameter :param z: point at which H_t is computed :param t: the "time" parameter :return: integrand of H_t(z) """ u, z, t = mp.mpc(u), mp.mpc(z), mp.mpc(t) return mp.exp(t * u * u) * phi_decay(u) * mp.cos(z * u)
def Construct_Dp(Phi, Psi, Omega, tt): Cos = mp.matrix([[ mp.cos(eigvalst[i] * tt) if i == j else mp.mpf(0.0) for j in range(L) ] for i in range(L)]) Sin = mp.matrix([[ mp.sin(eigvalst[i] * tt) if i == j else mp.mpf(0.0) for j in range(L) ] for i in range(L)]) OmegaCosSin = Cos * Omega * Sin Dp = -Phi * (OmegaCosSin.T - OmegaCosSin) * Phi.T return (Dp)
def Construct_Dm(Phi, Psi, Omega, tt): Cos = mp.matrix([[ mp.cos(eigvalst[i] * tt) if i == j else mp.mpf(0.0) for j in range(L) ] for i in range(L)]) Sin = mp.matrix([[ mp.sin(eigvalst[i] * tt) if i == j else mp.mpf(0.0) for j in range(L) ] for i in range(L)]) OmegaSinCos = Sin * Omega * Cos Dm = -Psi * (OmegaSinCos - OmegaSinCos.T) * Psi.T return (Dm)
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 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 Construct_K(Phi, Psi, Omega, tt): Cos = mp.matrix([[ mp.cos(eigvalst[i] * tt) if i == j else mp.mpf(0.0) for j in range(L) ] for i in range(L)]) Sin = mp.matrix([[ mp.sin(eigvalst[i] * tt) if i == j else mp.mpf(0.0) for j in range(L) ] for i in range(L)]) OmegaCos = Cos * Omega * Cos OmegaSin = Sin * Omega * Sin Kmatrix = -Phi * (OmegaCos + OmegaSin.T) * Psi.T return (Kmatrix)
def __init__(self, domain=None, name='sin^2'): super(SinSquaredExpression, self).__init__(mp_terms=[ lambda x: mp.sin(x)**2, lambda x: mp.sin(2 * x), lambda x: 2 * mp.cos(2 * x) ], fp_terms=[ lambda x: math.sin(x)**2, lambda x: math.sin(2 * x), lambda x: 2 * math.cos(2 * x) ], desc="sin^2(x)", domain=domain, name=name)
def __init__(self, side, extended, distance): self.distance = mp.mpf(distance) rightPointX = mp.mpf('1.41949302') if extended else mp.sqrt(2) self.side = side # side length of square self.d = side / mp.cos(mp.pi / 12) # length of left edge sin60 = mp.sqrt(3) / 2 self.top = matrix([self.d / 2, sin60 * self.d]) self.right = matrix([side * rightPointX, 0]) self.nleft = matrix([[-sin60, 0.5]]) self.ndown = matrix([[0, -1]]) v = self.right - self.top norm = mp.norm(v) self.nright = matrix([[-v[1] / norm, v[0] / norm]]) self.offset = self.nright * self.right
def verify(self, x0, x1, y0, y1, s, t, x, y): "Verify the transformation given by (s, t, x, y) on pentagon." pentagon = [(x0, 0), (x1, 0), (0, y0), (0, y1), (self.side, self.side)] # reference point in interior of pentagon ref = matrix([x0 + x1 + self.side, y0 + y1 + self.side]) / 5 translation = matrix( [s * ref[0] + t * ref[1] + x, -t * ref[0] + s * ref[1] + y]) # reconstruct rotation angle norm = mp.sqrt(s * s + t * t) if t < 0: angle = -mp.acos(s / norm) else: angle = mp.acos(s / norm) rotation = matrix([[mp.cos(angle), mp.sin(angle)], [-mp.sin(angle), mp.cos(angle)]]) pentagon = list(map(lambda pt: matrix(pt), pentagon)) ipentagon = list( map(lambda pt: rotation * (pt - ref) + translation, pentagon)) # Verify that ipentagon is a congruent image of pentagon eps = mp.mpf(10)**-(mp.dps - 2) # accept error in last two digits for i in range(5): for j in range(i + 1, 5): d1 = mp.norm(pentagon[j] - pentagon[i]) d2 = mp.norm(ipentagon[j] - ipentagon[i]) dd = abs(d1 - d2) assert dd < eps, (pentagon, dd, eps) dists = [] for p in ipentagon: dists.append(self.nleft * p) dists.append(self.ndown * p) dists.append(self.nright * p - self.offset) dist = max(map(lambda m: m[0], dists)) if dist > -self.distance: sys.stderr.write("Pentagon failing slack test: %d %d %d %d\n" % (x0, x1, y0, y1)) return False return True
def mp_cyl(x): f = mp.mpf theta = f(THETA) * mp.pi / f(180) qr = x * f(RADIUS) * mp.sin(theta) qh = x * f(LENGTH) / f(2) * mp.cos(theta) be = f(2) * mp.j1(qr) / qr si = mp.sin(qh) / qh background = f(0) #background = f(1)/f(1000) volume = mp.pi * f(RADIUS)**f(2) * f(LENGTH) contrast = f(5) units = f(1) / f(10000) #return be #return si return units * (volume * contrast * be * si)**f(2) / volume + background
def chebyshev_gauss_1(n, mode="numpy"): """Chebyshev-Gauss quadrature for \\int_{-1}^1 f(x) / sqrt(1+x^2) dx.""" degree = n if n % 2 == 1 else n + 1 if mode == "numpy": points = np.cos((2 * np.arange(1, n + 1) - 1) / (2 * n) * np.pi) weights = np.full(n, np.pi / n) elif mode == "sympy": points = np.array([ sympy.cos(sympy.Rational(2 * k - 1, 2 * n) * sympy.pi) for k in range(1, n + 1) ]) weights = np.full(n, sympy.pi / n) else: assert mode == "mpmath" points = np.array([ mp.cos(mp.mpf(2 * k - 1) / (2 * n) * mp.pi) for k in range(1, n + 1) ]) weights = np.full(n, mp.pi / n) return C1Scheme("Chebyshev-Gauss 1", degree, weights, points)
def createP_pi(lx, ly): fle = fl(lx, ly, 0) phi_l = -mp.atan(mp.im(fle) / mp.re(fle)) f = lambda kx, ky, kz: (1 + 1 / (3**(1 / 2)) * mp.exp(1j * phi_l) * fl( lx + kx / ct.hbar, ly + ky / ct.hbar, kz / ct.hbar)) * w2pz( kx, ky, kz) #analytical #differential vectors, not used in nearest neighbor approximation R_12 = [R_1[0] - R_2[0], R_1[1] - R_2[1], R_1[2] - R_2[2]] R_23 = [R_2[0] - R_3[0], R_2[1] - R_3[1], R_2[2] - R_3[2]] R_31 = [R_3[0] - R_1[0], R_3[1] - R_1[1], R_3[2] - R_1[2]] nrm = mp.sqrt(1 / ( 2 + 2 * s / (3**(1 / 2)) * (mp.cos(phi_l + mp.fdot([lx, ly, 0], R_1)) + mp.cos(phi_l + mp.fdot([lx, ly, 0], R_2)) + mp.cos(phi_l + mp.fdot([lx, ly, 0], R_3))) + 2 * s2 * (mp.cos(mp.fdot([lx, ly, 0], R_12)) + mp.cos(mp.fdot( [lx, ly, 0], R_23)) + mp.cos(mp.fdot([lx, ly, 0], R_31))))) return lambda kx, ky, kz: f(kx, ky, kz) * nrm
def f1(x): return mp.cos(x) * mp.cosh(x) - 1
from mpmath import mp mp.dps = 50 N = long(sys.argv[1]) if len(sys.argv) > 1 else 1000000 inicio = -1*mp.pi fim = 1*mp.pi dx = (fim-inicio)/(N-1) Idx = mp.mpf(1/dx) with open('Cosseno.h', 'w') as f: f.write("\n".join([ 'using namespace std;', '', '#define COS_N {N}', '#define COS_inicio {inicio}', '#define COS_fim {fim}', '', '', ]).format(**locals())) f.write('const double Cosseno[] = {') for i in range(N): value = mp.cos(i*dx + inicio) v = "%.50e" % value f.write( v + ' ,\n') f.write('\n};')
def cosd(x): try: return mp.cos(x * mp.pi / 180) except: return math.cos(x * math.pi / 180)
def __init__(self, npts): # Only points self.points = [mp.cos((i - 1)*mp.pi/(npts - 1)) for i in xrange(npts, 0, -1)]
def c0(p): return mp.cos(2 * mp.pi() * (p * p - p - 1 / 16.0)) / mp.cos(2 * mp.pi() * p)
def psi(p): term1 = 2 * mp.pi() term2 = mp.cos(mp.pi() * (p * p / 2 - p - 1 / 8.0)) term3 = mp.exp(0.5j * mp.pi() * p * p - 5j * mp.pi() / 8.0) term4 = mp.cos(mp.pi() * p) return term1 * term2 * term3 / term4
from mpmath import mp mp.dps = 50 # dokładność print( mp.quad(lambda x: (mp.cos(x) - mp.exp(x)) * mp.csc(x), [-0.9999999999999999, 1])) #Zakres nie zaczyna się od -1 ponieważ wartość funkcji w tym punkcie jest bardzo bliska 0 i program sie wypisuje #Natomiast zastępując liczbe -1 bardzo bliską -1 otrzymujemy wynik bardzo poprawny z dokładnościa większa niz 10 cyfr
import sys from mpmath import mp mp.dps = 50 N = long(sys.argv[1]) if len(sys.argv) > 1 else 20000 inicio = -1*mp.pi fim = 1*mp.pi dx = (fim-inicio)/(N-1) idx = 1 / dx Idx = mp.mpf(1/dx) with open('Cos.h', 'w') as f: f.write("\n".join([ '#define COS_N {N}', '#define COS_Idx {idx}', '#define COS_inicio {inicio}', '#define COS_fim {fim}', '', '', ]).format(**locals())) f.write('const double Cos[] = {') for i in range(N): f.write('' + str(mp.cos(i*dx + inicio)) + ',\n') f.write('\n};')
def dR(self, lx, ly, kr, kt, kp, qmin, qmax, band, fdm, local_mchi): if band == "pi": if not self.lastlx == lx or not self.lastly == ly: self.P_pi = createP_pi(lx, ly) self.lastlx = lx self.lastly = ly self.E_b = E_pi_minus(lx, ly) sqP = lambda qr, qt, qp: mp.re( self.P_pi( qr * mp.sin(qt) * mp.cos(qp) - kr * mp.sin(kt) * mp.cos(kp ), qr * mp.sin(qt) * mp.sin(qp) - kr * mp.sin(kt) * mp.sin(kp ), qr * mp.cos(qt) - kr * mp.cos(kt)))**2 + mp.im( self.P_pi( qr * mp.sin(qt) * mp.cos(qp) - kr * mp.sin(kt) * mp .cos(kp), qr * mp.sin(qt) * mp.sin(qp) - kr * mp.sin( kt) * mp.sin(kp), qr * mp.cos(qt) - kr * mp.cos(kt)))**2 else: if not self.lastlx == lx or not self.lastly == ly: self.P_sigma = createP_sigma(lx, ly, band) self.lastlx = lx self.lastly = ly self.E_b = E_sigma(lx, ly, band) sqP = lambda qr, qt, qp: mp.re( self.P_sigma( qr * mp.sin(qt) * mp.cos(qp) - kr * mp.sin(kt) * mp.cos(kp ), qr * mp.sin(qt) * mp.sin(qp) - kr * mp.sin(kt) * mp.sin(kp ), qr * mp.cos(qt) - kr * mp.cos(kt)))**2 + mp.im( self.P_sigma( qr * mp.sin(qt) * mp.cos(qp) - kr * mp.sin(kt) * mp .cos(kp), qr * mp.sin(qt) * mp.sin(qp) - kr * mp.sin( kt) * mp.sin(kp), qr * mp.cos(qt) - kr * mp.cos(kt)))**2 integrand = lambda qr, qt, qp: (qr) * mp.sin(qt) / ( 4 * mp.pi) * self.eta(qr, kr, self.E_b, local_mchi) * F_DM( qr, fdm) * sqP(qr, qt, qp) result = mint.integrate(integrand, [qmin, qmax], [0, mp.pi], [0, 2 * mp.pi], self.method) return result
def df1(x): return mp.cos(x) * mp.sinh(x) - mp.sin(x) * mp.cosh(x)
def f3(x): return 2**(-x) + mp.e**x + 2 * mp.cos(x) - 6
add_function( name="expm1(x)", mp_function=mp.expm1, np_function=np.expm1, ocl_function=make_ocl("return expm1(q);", "sas_expm1"), limits=(-5., 5.), ) add_function( name="arctan(x)", mp_function=mp.atan, np_function=np.arctan, ocl_function=make_ocl("return atan(q);", "sas_arctan"), ) add_function( name="3 j1(x)/x", mp_function=lambda x: 3 * (mp.sin(x) / x - mp.cos(x)) / (x * x), # Note: no taylor expansion near 0 np_function=lambda x: 3 * (np.sin(x) / x - np.cos(x)) / (x * x), ocl_function=make_ocl("return sas_3j1x_x(q);", "sas_j1c", ["lib/sas_3j1x_x.c"]), ) add_function( name="(1-cos(x))/x^2", mp_function=lambda x: (1 - mp.cos(x)) / (x * x), np_function=lambda x: (1 - np.cos(x)) / (x * x), ocl_function=make_ocl("return (1-cos(q))/q/q;", "sas_1mcosx_x2"), ) add_function( name="(1-sin(x)/x)/x", mp_function=lambda x: 1 / x - mp.sin(x) / (x * x), np_function=lambda x: 1 / x - np.sin(x) / (x * x),