def test_computeDiscreteGaussianVariance(sigma_sq): # Alternative calculation of the variance, # based on Lemma 22 in Cannone et al. (aka Eqn. (41) in the form based on Fourier transforms (page 23) # Also, the numerator and denominator sums in the definition are expressible via Jacobi Theta functions, implemented in mpmath # Definition sums are the fastest, then FT sums, then calculation of jtheta which mpmath does with arbitrary precision, # hence the definition sums are chosen to be the working implementation and the others are tests SQRT2P = np.sqrt(2. * np.pi) sigma_sq_fl = float(sigma_sq) sigma = np.sqrt(sigma_sq_fl) bound = np.floor(100. * sigma) t = np.arange(-bound, bound + 1) pi_t = np.pi * t fac2 = np.exp(-2. * pi_t * pi_t * sigma_sq_fl) f_hat_fac1 = SQRT2P * sigma * sigma * sigma f_hat_fac3 = 1. - 4. * pi_t * pi_t * sigma_sq_fl g_hat_fac1 = SQRT2P * sigma sum_f_hat = np.sum(f_hat_fac1 * fac2 * f_hat_fac3) sum_g_hat = np.sum(g_hat_fac1 * fac2) var_ft = sum_f_hat / sum_g_hat mpmath.MPContext.THETA_Q_LIM = 0.99999999999999999 mpmath.mp.prec = 80 q = mpmath.exp(-1 / (2 * sigma_sq)) var_sp_fun = -mpmath.jtheta(3, 0, q, 2) / (4 * mpmath.jtheta(3, 0, q)) var = primitives.computeDiscreteGaussianVariance(sigma_sq) #print(var_ft, var, 2 * abs(var_ft - var) / (var_ft + var) ) assert 2 * abs(var_ft - var) / (var_ft + var) < 1e-10 assert 2 * abs(var_sp_fun - var) / (var_sp_fun + var) < 1e-10
def calc_mu(k, x1, x2, x3, zeta, abel): mu = [] for i in range(0, 4, 1): mu.append(complex( 0.25 *pi* ((jtheta(1, abel[i]*pi, qfrom(k=k), 1) / (jtheta(1, abel[i]*pi, qfrom(k=k), 0)) ) + (jtheta(3, abel[i]*pi, qfrom(k=k), 1) / (jtheta(3, abel[i]*pi, qfrom(k=k), 0)) )) \ - x3 - (x2 + complex(0,1) *x1) * zeta[i])) return mu
def testIdentity2(self): """eta(t)**3 = theta_2(0,q(t))theta_3(0,q(t))theta_4(0,q(t))/2""" # eta at omega, a third root of unity omega = (-1 + mpmath.sqrt(3) * 1j) / 2 eta3cubed = ecpp.dedekind(omega, 15)**3 triple = (mpmath.jtheta(2, 0, q(omega)) * mpmath.jtheta(3, 0, q(omega)) * mpmath.jtheta(4, 0, q(omega)) / 2) self.assertTrue(mpmath.almosteq(triple, eta3cubed), triple - eta3cubed)
def DedekindEtaA4(tau): ''' Compute the derivative of the Dedekind Eta function for imaginary argument tau. Numerically. ''' try: import mpmath as mp mpmath_loaded = True except ImportError: mpmath_loaded = False return mp.cbrt(0.5*mp.jtheta(2,0,mp.exp(-mp.pi*tau))*mp.jtheta(3,0,mp.exp(-mp.pi*tau))*mp.jtheta(4,0,mp.exp(-mp.pi*tau)))
def dThetaRatio(z,q,n): ''' Compute the ratio of the nth derivative of the third Jacobi theta function with itself.''' try: import mpmath as mp mpmath_loaded = True except ImportError: mpmath_loaded = False #return (mp.djtheta(3,z,q,n)/mp.jtheta(3,z,q)) return (mp.jtheta(3,z,q,n)/mp.jtheta(3,z,q))
def w(v,s,b): """ conformal map for double splitting quench (Equation 5.1). In this code v means \nu. We can interpret the convention for elliptic theta function, \theta_1(\nu,\tau) in Equation 5.3 = jtheta(1,pi*nu,exp(pi*j*tau)). s : moduli parameter. The Hawking Page phase transition point is s=1. b : quench point. We consider double splitting at x=b,-b. s and b must be positive number to get right answer. """ return -j*b*( jtheta(1,pi*v,exp(-pi*s),1)/jtheta(1,pi*v,exp(-pi*s),0) + jtheta(1,pi*(v+j*s/2),exp(-pi*s),1)/jtheta(1,pi*(v+j*s/2),exp(-pi*s),0) + j )
def sigma(self,z): # A+S 18.10. from mpmath import pi, jtheta, exp, mpc, sqrt, sin Delta = self.Delta e1, _, _ = self.__roots om = self.__periods[0] / 2 omp = self.__periods[1] / 2 if self.__ng3: z = mpc(0,1) * z if Delta > 0: tau = omp / om q = (exp(mpc(0,1) * pi() * tau)).real eta = -(pi()**2 * jtheta(n=1,z=0,q=q,derivative=3)) / (12 * om * jtheta(n=1,z=0,q=q,derivative=1)) v = (pi() * z) / (2 * om) retval = (2 * om) / pi() * exp((eta * z**2)/(2 * om)) * jtheta(n=1,z=v,q=q)/jtheta(n=1,z=0,q=q,derivative=1) elif Delta < 0: om2 = om + omp om2p = omp - om tau2 = om2p / (2 * om2) q = mpc(0,(mpc(0,1) * exp(mpc(0,1) * pi() * tau2)).imag) eta2 = -(pi()**2 * jtheta(n=1,z=0,q=q,derivative=3)) / (12 * om2 * jtheta(n=1,z=0,q=q,derivative=1)) v = (pi() * z) / (2 * om2) retval = (2 * om2) / pi() * exp((eta2 * z**2)/(2 * om2)) * jtheta(n=1,z=v,q=q)/jtheta(n=1,z=0,q=q,derivative=1) else: g2, g3 = self.__invariants if g2 == 0 and g3 == 0: retval = z else: c = e1 / 2 A = sqrt(3 * c) retval = (1 / A) * sin(A*z) * exp((c*z**2) / 2) if self.__ng3: return mpc(0,-1) * retval else: return retval
def z_eta(u, m): """Jacobi eta function (eq 16.31.3, [Abramowitz]_).""" q = qfrom(m=m) DM = ellipk(m) z = mp.pi * u / (2 * DM) eta = jtheta(n=1, z=z, q=q) return eta
def PlotPDF(): mu = 0.0 # sigma_list = np.sqrt([1.0/8.0, 1.0/4.0, 1.0/2.0, 1.0, 2.0, 1000.0]) sigma_list = np.sqrt([1.0 / 8.0, 1.0 / 4.0, 1.0 / 2.0, 1.0, 2.0]) data = [] x = np.linspace(-np.pi, np.pi, 100) for sigma in sigma_list: tau = 1.0j * sigma**2 / (2.0 * np.pi) q = np.exp(1.0j * np.pi * tau) mapping = np.frompyfunc(lambda *a: float(mpmath.jtheta(*a)), 3, 1) y = 1 / (2.0 * np.pi) * mapping(3, x / 2.0, q) data.append(([x, y, "$\sigma = {0}$".format(sigma * sigma)])) # TODO(chanwcom) Process the infinity standard deviation case. y = 1 / (2.0 * np.pi) * np.ones(np.shape(x)) data.append(([x, y, "$\sigma = \infty$"])) data_list_plot.PlotContinuousData(data) plt.axis([-np.pi, np.pi, 0.0, 1.2]) plt.xlabel('$x$') plt.ylabel('Probability Density Function (PDF)') fig = plt.gcf() fig.savefig("./wrapped_gaussian_pdf.eps")
def testIdentity1(self): """eta(t) = 1/sqrt(3) * theta_2(pi/6, q(t/3))""" # eta at omega, a third root of unity omega = (-1 + mpmath.sqrt(3) * 1j) / 2 eta3 = ecpp.dedekind(omega, 18) theta3 = mpmath.jtheta(2, mpmath.pi / 6, q( omega / 3)) / mpmath.sqrt(3) self.assertTrue(mpmath.almosteq(theta3, eta3), eta3 - theta3)
def DedekindEtaA1(tau): ''' Compute the derivative of the Dedekind Eta function for imaginary argument tau. Numerically. ''' try: import mpmath as mp mpmath_loaded = True except ImportError: mpmath_loaded = False return 1.0/mp.sqrt(3.0)*mp.jtheta(2,mp.pi/6,mp.exp(-(mp.pi/3.0)*tau))
def DedekindEtaA2(tau): ''' Compute the derivative of the Dedekind Eta function for imaginary argument tau. Numerically. ''' try: import mpmath as mp mpmath_loaded = True except ImportError: mpmath_loaded = False return mp.exp(-mp.pi/12.0)*mp.jtheta(3,mp.pi*(mp.j*tau+1.0)/2.0,mp.exp(-3.0*mp.pi))
def gaussian_total(self,offset): factor = mpmath.sqrt(1.0/(self._period*self._tau)) factor_exponent = -1.0/(2*self._period) exponent_factor = mpmath.mpf(offset)*offset exponent_interval = self._tau*self._tau exponent_offset = 2*self._tau*offset factor_full = factor*mpmath.exp(exponent_factor*factor_exponent) q = mpmath.exp(factor_exponent*exponent_interval) z = factor_exponent*exponent_offset/(2*mpmath.j) theta = mpmath.jtheta(3,z,q).real return factor_full*theta
def sum_gaussian_theta(variance, offset, interval): factor = mpmath.sqrt(1.0/(variance*tau)) factor_exponent=mpmath.mpf(-1.0)/(2*variance) exponent_factor = mpmath.mpf(offset)*offset factor_full = factor*mpmath.exp(exponent_factor*factor_exponent) exponent_interval = interval*interval exponent_offset = 2*interval*offset q = mpmath.exp(factor_exponent*exponent_interval) z = factor_exponent*exponent_offset/(2*mpmath.j) theta = mpmath.jtheta(3,z,q) return factor_full*theta
def DedekindEtaA(tau): ''' Alternative definition of the Dedekind Eta function in terms of the Jacobi theta function. 10x faster computation. http://functions.wolfram.com/EllipticFunctions/DedekindEta/27/01/02/''' try: import mpmath as mp mpmath_loaded = True except ImportError: mpmath_loaded = False return mp.cbrt(0.5*mp.jtheta(1,0,mp.exp(-mp.pi*tau),1))
def Ptheta(self,z): from mpmath import pi, jtheta, exp, mpc e1,e2,e3 = self.__roots om,omp,om2,om2p = self.__omegas Delta = self.__Delta if Delta > 0: tau = omp/om q = exp(mpc(0,1)*pi*tau) v = (pi * z) / (2*om) omega = om e = e1 else: tau2 = om2p/(2*om2) q = mpc(0,1)*exp(mpc(0,1)*pi*tau2) v = (pi * z) / (2*om2) omega = om2 e = e2 retval = e+pi**2/(4*omega*omega)*( jtheta(n=1,z=0,q=q,derivative=1) * jtheta(n=2,z=v,q=q,derivative=0) / ( jtheta(n=2,z=0,q=q,derivative=0) * jtheta(n=1,z=v,q=q,derivative=0) ) )**2 return retval
def z_eta(u, m): r""" A function evaluate Jacobi eta function (eq 16.31.3, [Abramowitz]_). :param u: Argument u :param m: m is the elliptic parameter (not the modulus k and not the nome q) :rtype: Returns a float, Jacobi eta function evaluated for the argument `u` and parameter `m` """ q = qfrom(m=m) DM = ellipk(m) z = mp.pi * u / (2 * DM) eta = jtheta(n=1, z=z, q=q) return eta
def zeta(self, z): # A+S 18.10. from mpmath import pi, jtheta, exp, mpc, cot, sqrt Delta = self.Delta e1, _, _ = self.__roots om = self.__periods[0] / 2 omp = self.__periods[1] / 2 if self.__ng3: z = mpc(0, 1) * z if Delta > 0: tau = omp / om # NOTE: here q has to be real. q = (exp(mpc(0, 1) * pi() * tau)).real eta = -(pi()**2 * jtheta(n=1, z=0, q=q, derivative=3)) / ( 12 * om * jtheta(n=1, z=0, q=q, derivative=1)) v = (pi() * z) / (2 * om) retval = (eta * z) / om + (pi() * jtheta(n=1, z=v, q=q, derivative=1)) / ( 2 * om * jtheta(n=1, z=v, q=q)) elif Delta < 0: om2 = om + omp om2p = omp - om tau2 = om2p / (2 * om2) # NOTE: here q will be pure imaginary. q = mpc(0, (mpc(0, 1) * exp(mpc(0, 1) * pi() * tau2)).imag) eta2 = -(pi()**2 * jtheta(n=1, z=0, q=q, derivative=3)) / ( 12 * om2 * jtheta(n=1, z=0, q=q, derivative=1)) v = (pi() * z) / (2 * om2) retval = (eta2 * z) / om2 + (pi() * jtheta( n=1, z=v, q=q, derivative=1)) / (2 * om2 * jtheta(n=1, z=v, q=q)) else: g2, g3 = self.__invariants if g2 == 0 and g3 == 0: retval = 1 / z else: c = e1 / 2 A = sqrt(3 * c) retval = c * z + A * cot(A * z) if self.__ng3: return mpc(0, 1) * retval else: return retval
def genconsts(n=128): p = random.choice(list(filter(lambda x: x >= n * n, prime(2 * n * n)))) #eps = (3.45678910111213141516) #m = (1 + eps)*(n + 1)*math.log(p) m = 5 * n alp = lambda n: 1 / (math.sqrt(n) * math.log(n)) theta = lambda r: jtheta(3, -math.pi * r, math.exp(-alp(n) * alp(n) * math.pi)) theta_quad = lambda i: quad(theta, [(i - 0.5) / p, (i + 0.5) / p]) print("Generating distribution....") x_dist = [] i = 0 while True: if i % (p // 100) == 0: print("%s/%s" % (i, p - 1)) quad_result = theta_quad(i) x_dist.append(quad_result) if quad_result < 1e-10: break i += 1 print("%s/%s" % (p - 1, p - 1)) x_dist = x_dist + [0] * (p - 2 * len(x_dist) + 1) + list( reversed(x_dist[1::])) ### priv key s = [random.randint(0, p - 1) for i in range(n)] ### public key a = [[random.randint(0, p - 1) for i in range(n)] for i in range(m)] e = random.choices(list(range(p)), weights=x_dist, k=m) b = [(dot(a[i], s, p) + e[i]) % p for i in range(m)] return (m, a, b, p, s)
def zeta(self,z): # A+S 18.10. from mpmath import pi, jtheta, exp, mpc, cot, sqrt Delta = self.Delta e1, _, _ = self.__roots om = self.__periods[0] / 2 omp = self.__periods[1] / 2 if self.__ng3: z = mpc(0,1) * z if Delta > 0: tau = omp / om # NOTE: here q has to be real. q = (exp(mpc(0,1) * pi() * tau)).real eta = -(pi()**2 * jtheta(n=1,z=0,q=q,derivative=3)) / (12 * om * jtheta(n=1,z=0,q=q,derivative=1)) v = (pi() * z) / (2 * om) retval = (eta * z) / om + (pi() * jtheta(n=1,z=v,q=q,derivative=1)) / (2 * om * jtheta(n=1,z=v,q=q)) elif Delta < 0: om2 = om + omp om2p = omp - om tau2 = om2p / (2 * om2) # NOTE: here q will be pure imaginary. q = mpc(0,(mpc(0,1) * exp(mpc(0,1) * pi() * tau2)).imag) eta2 = -(pi()**2 * jtheta(n=1,z=0,q=q,derivative=3)) / (12 * om2 * jtheta(n=1,z=0,q=q,derivative=1)) v = (pi() * z) / (2 * om2) retval = (eta2 * z) / om2 + (pi() * jtheta(n=1,z=v,q=q,derivative=1)) / (2 * om2 * jtheta(n=1,z=v,q=q)) else: g2, g3 = self.__invariants if g2 == 0 and g3 == 0: retval = 1 / z else: c = e1 / 2 A = sqrt(3 * c) retval = c*z + A * cot(A*z) if self.__ng3: return mpc(0,1) * retval else: return retval
return factor_full*theta # In[81]: sum_gaussian(1.0,0.7,tau,100) # In[82]: sum_gaussian_theta(1.0,0.7,tau) # In[3]: mpmath.jtheta(3,0,mpmath.exp(-1)) # In[5]: mpmath.sqrt(mpmath.pi) # In[63]: def von_Mises_entropy_max(): return mpmath.ln(2*mpmath.pi) def von_Mises_entropy_fraction(kappa): I0_kappa = mpmath.besseli(0,kappa) I1_kappa = mpmath.besseli(1,kappa) kappa_entropy = mpmath.ln(2*mpmath.pi*I0_kappa) - kappa*(I1_kappa/I0_kappa)
def jt1d(nu, tau): return jtheta(1, pi * nu, exp(pi * j * tau), 1)
def calc_eta_by_theta(k, z): return 0.25 * complex(0, 1) * pi * ((jtheta(2, 0, qfrom(k=k), 0)) ** 2) \ * ((jtheta(4, 0, qfrom(k=k), 0)) ** 2) \ * (jtheta(3, 0, qfrom(k=k), 0)) \ * (jtheta(3, 2*z*pi, qfrom(k=k), 0)) \ / ( ((jtheta(1, z*pi, qfrom(k=k), 0)) ** 2) * ((jtheta(3, z*pi, qfrom(k=k), 0)) ** 2) )
def arFpDensity(Da, r0a, La, ta): D, r0, L, t = (m.mpmathify(str(x)) for x in (Da, r0a, La, ta)) pref = m.exp(-r0 ** 2 / (4 * D * t)) * D / (4 * m.sqrt(m.pi) * (D * t) ** 1.5) z = (1j * L * r0) / (2 * D * t) q = m.exp(-L ** 2 / (D * t)) return float(m.re(2 * pref * (r0 * m.jtheta(4, z, q) - 0 * 1j * L * m.djtheta(4, z, q))))