def P(self, z): # A+S 18.9. from mpmath import sqrt, mpc, sin, ellipfun, mpf Delta = self.Delta e1, e2, e3 = self.__roots if self.__ng3: z = mpc(0, 1) * z if Delta > 0: zs = sqrt(e1 - e3) * z m = (e2 - e3) / (e1 - e3) retval = e3 + (e1 - e3) / ellipfun('sn', u=zs, m=m)**2 elif Delta < 0: H2 = (sqrt((e2 - e3) * (e2 - e1))).real assert (H2 > 0) m = mpf(1) / mpf(2) - 3 * e2 / (4 * H2) zp = 2 * z * sqrt(H2) retval = e2 + H2 * (1 + ellipfun('cn', u=zp, m=m)) / ( 1 - ellipfun('cn', u=zp, m=m)) else: g2, g3 = self.__invariants if g2 == 0 and g3 == 0: retval = 1 / (z**2) else: c = e1 / 2 retval = -c + 3 * c / (sin(sqrt(3 * c) * z))**2 if self.__ng3: return -retval else: return retval
def P(self,z): # A+S 18.9. from mpmath import sqrt, mpc, sin, ellipfun, mpf Delta = self.Delta e1, e2, e3 = self.__roots if self.__ng3: z = mpc(0,1) * z if Delta > 0: zs = sqrt(e1 - e3) * z m = (e2 - e3) / (e1 - e3) retval = e3 + (e1 - e3) / ellipfun('sn',u=zs,m=m)**2 elif Delta < 0: H2 = (sqrt((e2 - e3) * (e2 - e1))).real assert(H2 > 0) m = mpf(1) / mpf(2) - 3 * e2 / (4 * H2) zp = 2 * z * sqrt(H2) retval = e2 + H2 * (1 + ellipfun('cn',u=zp,m=m)) / (1 - ellipfun('cn',u=zp,m=m)) else: g2, g3 = self.__invariants if g2 == 0 and g3 == 0: retval = 1 / (z**2) else: c = e1 / 2 retval = -c + 3 * c / (sin(sqrt(3 * c) * z))**2 if self.__ng3: return -retval else: return retval
def do_approximation(self): epsilonp = np.sqrt(np.power(10, self.Ap / 10) - 1) gp = np.power(10, -self.Ap / 20) k1 = np.sqrt((np.power(10, self.Ap / 10) - 1) / (np.power(10, self.Ao / 10) - 1)) k = 1 / self.wan a = mp.asin(1j / epsilonp) vo = mp.ellipf(1j / epsilonp, k1) / (1j * self.n) self.n = np.ceil( special.ellipk(np.sqrt(1 - np.power(k1, 2))) * special.ellipk(k) / (special.ellipk(k1) * special.ellipk(np.sqrt(1 - np.power(k, 2))))) for i in range(1, int(np.floor(self.n / 2)) + 1): cd = mp.ellipfun('cd', (2 * i - 1) / self.n, k) zero = (1j / (float(k * cd.real) + 1j * float(k * cd.imag))) self.num = self.num * np.poly1d([1 / zero, 1]) self.num = self.num * np.poly1d([1 / np.conj(zero), 1]) cd = mp.ellipfun('cd', (2 * i - 1) / self.n - 1j * vo, k) pole = 1j * (float(cd.real) + 1j * float(cd.imag)) if np.real(pole) <= 0: self.den = self.den * np.poly1d([-1 / pole, 1]) self.den = self.den * np.poly1d([-1 / np.conj(pole), 1]) if np.mod(self.n, 2) == 1: sn = 1j * mp.ellipfun('sn', 1j * vo, k) pole = 1j * (float(sn.real) + 1j * float(sn.imag)) if np.real(pole) <= 0: self.den = self.den * np.poly1d([-1 / pole, 1]) self.den = self.den * np.poly1d([-1 / np.conj(pole), 1]) self.zeroes = np.roots(self.num) self.poles = np.roots(self.den) self.aprox_gain = np.power(gp, 1 - (self.n - 2 * np.floor(self.n / 2))) self.num = self.num * self.aprox_gain self.norm_sys = signal.TransferFunction( self.num, self.den) #Filter system is obtained
def Pprime(self,z): # A+S 18.9. from mpmath import ellipfun, sqrt, cos, sin, mpc, mpf Delta = self.Delta e1, e2, e3 = self.__roots if self.__ng3: z = mpc(0,1) * z if Delta > 0: zs = sqrt(e1 - e3) * z m = (e2 - e3) / (e1 - e3) retval = -2 * sqrt((e1 - e3)**3) * ellipfun('cn',u=zs,m=m) * ellipfun('dn',u=zs,m=m) / (ellipfun('sn',u=zs,m=m)**3) elif Delta < 0: H2 = (sqrt((e2 - e3) * (e2 - e1))).real assert(H2 > 0) m = mpf(1) / mpf(2) - 3 * e2 / (4 * H2) zp = 2 * z * sqrt(H2) retval = -4 * sqrt(H2**3) * ellipfun('sn',u=zp,m=m) * ellipfun('dn',u=zp,m=m) / ((1 - ellipfun('cn',u=zp,m=m))**2) else: g2, g3 = self.__invariants if g2 == 0 and g3 == 0: retval = -2 / (z**3) else: c = e1 / 2 A = sqrt(3 * c) retval = -6 * c * A * cos(A * z) / (sin(A * z))**3 if self.__ng3: return mpc(0,-1) * retval else: return retval
def Zolotarev2(p, q, m): """Another way to generate Zolotarev polynomial. It is not done yet.""" n = p + q u0 = (p / (p + q)) * ellipk(m) wp = 2 * (ellipfun('cd', u0, m)) ** 2 - 1 ws = 2 * (ellipfun('cn', u0, m)) ** 2 - 1 wq = (wp + ws) / 2 sn = ellipfun('sn', u0, m) cn = ellipfun('cn', u0, m) dn = ellipfun('dn', u0, m) wm = ws + 2 * z_zn(u0, m) * ((sn * cn) / (dn)) beta = np.zeros((n + 5, 1)) beta[n] = 1 d = np.zeros((6, 1)) # Implementation of the main recursive algorithm for m1 in range(n + 2, 2, -1): d[0] = (m1 + 2) * (m1 + 1) * wp * ws * wm d[1] = -(m1 + 1) * (m1 - 1) * wp * ws - (m1 + 1) * (2 * m1 + 1) * wm * wq d[2] = wm * (n ** 2 * wm ** 2 - m1 ** 2 * wp * ws) + m1 ** 2 * (wm - wq) + 3 * m1 * (m1 - 1) * wq d[3] = (m1 - 1) * (m1 - 2) * (wp * ws - wm * wq - 1) - 3 * wm * (n ** 2 * wm - (m1 - 1) ** 2 * wq) d[4] = (2 * m1 - 5) * (m1 - 2) * (wm - wq) + 3 * wm * (n ** 2 - (m1 - 2) ** 2) d[5] = n ** 2 - (m1 - 3) ** 2 tmp = 0 for mu in range(0, 5): tmp = tmp + d[mu] * beta[m1 + 3 - (mu + 1)] beta[m1 - 3] = tmp / d[5] tmp = 0 for m1 in range(0, n + 1): tmp = tmp + beta[m1] b = np.zeros((n + 1, 1)) for m1 in range(0, n + 1): b[m1] = (-1) ** p * (beta[m1] / tmp) return b
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 z_am(u, m): """Jacobi amplitude function (eq 16.1.5, [Abramowitz]_).""" snM = ellipfun('sn', u=u, m=m) cnM = ellipfun('cn', u=u, m=m) if (0 <= cnM <= 1): phi = mp.asin(snM) elif (-1 <= cnM < 0): if (snM >= 0): phi = mp.pi - mp.asin(snM) else: phi = mp.asin(snM) - mp.pi else: print("This function only handles real 'phi' values.") return phi
def z_am(u, m): """Jacobi amplitude function (eq 16.1.5, [Abramowitz]_).""" snM = ellipfun('sn', u=u, m=m) cnM = ellipfun('cn', u=u, m=m) if (0 <= cnM <= 1): phi = mp.asin(snM) elif (-1 <= cnM < 0): if (snM >= 0): phi = mp.pi - mp.asin(snM) else: phi = mp.asin(snM) - mp.pi else: print "This function only handles real 'phi' values." return phi
def getaddoa(time): elliptic = mpmath.ellipfun(func) #print time h = 0.0122070471 seconderiv = ((Amp*elliptic(time+h,m_val)) - (2*Amp*elliptic(time,m_val)) + (Amp*elliptic(time-h,m_val)))/(h*h) addoa = seconderiv/(Amp*elliptic(time-h,m_val)) return float(addoa)
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
def test_ellipfun_sn(self): # Oscillating function --- limit range of first argument; the # loss of precision there is an expected numerical feature # rather than an actual bug assert_mpmath_equal(lambda u, m: sc.ellipj(u, m)[0], lambda u, m: mpmath.ellipfun("sn", u=u, m=m), [Arg(-1e6, 1e6), Arg(a=0, b=1)], atol=1e-20)
def Zolotarev2(p, q, m): r""" Another way to generate Zolotarev polynomial. It is not done yet. :param p: :param q: :param m: :rtype: Returns """ n = p + q u0 = (p / (p + q)) * ellipk(m) wp = 2 * (ellipfun('cd', u0, m))**2 - 1 ws = 2 * (ellipfun('cn', u0, m))**2 - 1 wq = (wp + ws) / 2 sn = ellipfun('sn', u0, m) cn = ellipfun('cn', u0, m) dn = ellipfun('dn', u0, m) wm = ws + 2 * z_zn(u0, m) * ((sn * cn) / (dn)) beta = np.zeros((n + 5, 1)) beta[n] = 1 d = np.zeros((6, 1)) # Implementation of the main recursive algorithm for m1 in range(n + 2, 2, -1): d[0] = (m1 + 2) * (m1 + 1) * wp * ws * wm d[1] = -(m1 + 1) * (m1 - 1) * wp * ws - (m1 + 1) * (2 * m1 + 1) * wm * wq d[2] = wm * (n**2 * wm**2 - m1**2 * wp * ws) + m1**2 * ( wm - wq) + 3 * m1 * (m1 - 1) * wq d[3] = (m1 - 1) * (m1 - 2) * (wp * ws - wm * wq - 1) - 3 * wm * (n**2 * wm - (m1 - 1)**2 * wq) d[4] = (2 * m1 - 5) * (m1 - 2) * (wm - wq) + 3 * wm * (n**2 - (m1 - 2)**2) d[5] = n**2 - (m1 - 3)**2 tmp = 0 for mu in range(0, 5): tmp = tmp + d[mu] * beta[m1 + 3 - (mu + 1)] beta[m1 - 3] = tmp / d[5] tmp = 0 for m1 in range(0, n + 1): tmp = tmp + beta[m1] b = np.zeros((n + 1, 1)) for m1 in range(0, n + 1): b[m1] = (-1)**p * (beta[m1] / tmp) return b
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 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 z_am(u, m): r""" A function evaluate Jacobi amplitude function (eq 16.1.5, [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 amplitude function evaluated for the argument `u` and parameter `m` """ snM = ellipfun('sn', u=u, m=m) cnM = ellipfun('cn', u=u, m=m) if (0 <= cnM <= 1): phi = mp.asin(snM) elif (-1 <= cnM < 0): if (snM >= 0): phi = mp.pi - mp.asin(snM) else: phi = mp.asin(snM) - mp.pi else: print("This function only handles real 'phi' values.") return phi
def z_am(u, m): r""" A function evaluate Jacobi amplitude function (eq 16.1.5, [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 amplitude function evaluated for the argument `u` and parameter `m` """ snM = ellipfun('sn', u=u, m=m) cnM = ellipfun('cn', u=u, m=m) if (0 <= cnM <= 1): phi = mp.asin(snM) elif (-1 <= cnM < 0): if (snM >= 0): phi = mp.pi - mp.asin(snM) else: phi = mp.asin(snM) - mp.pi else: print "This function only handles real 'phi' values." return phi
def JacobiElipa(): kspace = numpy.linspace(1,100,10) n_inside = -100/(numpy.amin(kspace)) n_outside = -1/(1000*numpy.amax(kspace)) num_steps = 1000 h = ((n_outside - n_inside)/num_steps) #------------------------------------- dn = mpmath.ellipfun('dn') data = [] xpts = numpy.linspace(n_inside,n_outside,num_steps) for x in xpts: a = dn(x, 1) data.append(a) return data,h
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 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 calc_zq(qz, zp, zm, En, aa): """ function used in computing polar geodesic coordinates Parameters: qz (float) zp (float): polar root zm (float): polar root En (float): energy aa (float): spin Returns: zq (float) """ ktheta = (aa**2 * (1 - En**2) * zm**2) / zp**2 sn = ellipfun("sn") pi = mp.pi return zm * sn((2 * (pi / 2.0 + qz) * ellipk(ktheta)) / pi, ktheta)
def calc_rq(qr, r1, r2, r3, r4): """ function used in computing radial geodesic coordinates Parameters: qr (float) r1 (float): radial root r2 (float): radial root r3 (float): radial root r4 (float): radial root Returns: rq (float) """ pi = mp.pi kr = ((r1 - r2) * (r3 - r4)) / ((r1 - r3) * (r2 - r4)) sn = ellipfun("sn") return (-(r2 * (r1 - r3)) + (r1 - r2) * r3 * sn( (qr * ellipk(kr)) / pi, kr)**2) / (-r1 + r3 + (r1 - r2) * sn( (qr * ellipk(kr)) / pi, kr)**2)
def eq13(_P: float, _r: float, _a: float, M: float, incl: float, n: int = 0, tol=10e-6) -> float: """Relation between radius (where photon was emitted in accretion disk), a and P. P can be converted to b, yielding the polar coordinates (b, a) on the photographic plate""" zinf = zeta_inf(_P, M) Qvar = Q(_P, M) m_ = k2( _P, M ) # modulus of the elliptic integrals. mpmath takes m = k² as argument. ellinf = F(zinf, m_) # Elliptic integral F(zinf, k) g = mpmath.acos(cos_gamma(_a, incl)) # real # Calculate the argument of sn (mod is m = k², same as the original elliptic integral) # WARNING: paper has an error here: \sqrt(P / Q) should be in denominator, not numerator # There's no way that \gamma and \sqrt(P/Q) can end up on the same side of the division if n: # higher order image ellK = K(m_) # calculate complete elliptic integral of mod m = k² ellips_arg = (g - 2. * n * np.pi) / ( 2. * mpmath.sqrt(_P / Qvar)) - ellinf + 2. * ellK else: # direct image ellips_arg = g / (2. * mpmath.sqrt(_P / Qvar)) + ellinf # complex # sn is an Jacobi elliptic function: elliptic sine. ellipfun() takes 'sn' # as argument to specify "elliptic sine" and modulus m=k² sn = mpmath.ellipfun('sn', ellips_arg, m=m_) sn2 = sn * sn # sn2 = float(sn2.real) term1 = -(Qvar - _P + 2. * M) / (4. * M * _P) term2 = ((Qvar - _P + 6. * M) / (4. * M * _P)) * sn2 s = 1. - _r * (term1 + term2) # solve this for zero return s
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
def test_ellipfun_dn(self): assert_mpmath_equal(lambda u, m: sc.ellipj(u, m)[2], lambda u, m: mpmath.ellipfun("dn", u=u, m=m), [Arg(), Arg(a=0, b=1)])
theta0 = 45*pi/180 z = sin(theta0/2) g = 9.81 size = 50 Tspace = np.linspace(0,5,size) L2lengths = np.linspace(0.1,8,size) rangeSpace = np.zeros((size,size)) L2Count = 0 tCount = 0 m2 = 1 m1 = 200 L1 = 1 for t in Tspace: for L2 in L2lengths: w0 = sqrt((g*(m1*L1-m2*L2))/(m1*L1**2 + m2*L2**2)) theta = 2*asin(z*mpmath.ellipfun('sn', w0*t, z)) theta_dot = 2*z*w0*mpmath.ellipfun('cn', w0*t, z) range = (2*L2**2*(theta_dot)**2*sin(theta-pi/2)*cos(theta-pi/2))/g rangeSpace[tCount,L2Count] = range L2Count += 1 L2Count = 0 tCount += 1 X, Y = np.meshgrid(Tspace, L2lengths) fig, ax = newfig(0.5) plt.subplot(1,1,1) i,j = np.unravel_index(rangeSpace.argmax(), rangeSpace.shape) levels = np.linspace(rangeSpace.min(),rangeSpace.max()+0.01,size*2,endpoint = True) patches = [] #subplot(1,1,1)
def test_ellipfun_dn(self): # see comment in ellipfun_sn assert_mpmath_equal(lambda u, m: sc.ellipj(u, m)[2], lambda u, m: mpmath.ellipfun("dn", u=u, m=m), [Arg(-1e6, 1e6), Arg(a=0, b=1)], atol=1e-20)
def weier_to_jacobi_k(e1, e2, e3): return cmath.sqrt((e2 - e3) / (e1 - e3)) def weier_to_jacobi_y(z, e1, e3): return z * cmath.sqrt(e1 - e3) def weierstrass_to_jacobi(z, g2, g3): e_vec = weierstrass_Es(g2, g3) return (weier_to_jacobi_y(z, e_vec[0], e_vec[2]), weier_to_jacobi_k(e_vec[0], e_vec[1], e_vec[2])) sn = ellipfun('sn') cn = ellipfun('cn') dn = ellipfun('dn') results = [] for g2 in frange(1.0, 1.6, 0.1): r = [] for g3 in frange(-0.5, 0.6, 0.1): weier_Es = weierstrass_Es(g2, g3) w1 = omega1(g2, g3, weier_Es[0]) w3 = omega3(g2, g3, weier_Es[2]) k_val = weier_to_jacobi_k(weier_Es[0], weier_Es[1], weier_Es[2]) # print([g2, g3, weier_Es[0], weier_Es[1], weier_Es[2], w1, w3, k_val]) # print("") r.append(
import mpmath import numpy import matplotlib.pyplot as plt dn = mpmath.ellipfun('dn') sn = mpmath.ellipfun('sn') data1, data2, data3 = [],[],[] grads= [] xmin,xmax,xsteps = -5,0.0,100.0 xpts = numpy.linspace(xmin,xmax,xsteps) h = ((xmax-xmin)/xsteps) adashdash = [0,0,0] addova = [None, None, None] addova_analytical = [] for i in range(int(xsteps)): x = xpts[i] a = dn(x,1) b = sn(x,1) data1.append(10*a) data2.append(abs(1/x)) data3.append(10*(b+1)) if i > 2: seconderiv = (data1[i] - 2*data1[i-1] + data1[i-2])/(h*h) adashdash.append(seconderiv) addova.append(adashdash[i]/a) plt.plot(xpts, data1, label = "dn fnc") plt.plot(xpts, adashdash, color = 'm', label = "a''") plt.plot(xpts, addova, color = 'c', label = "a''/a") plt.plot(xpts, data2 , color ='r', label = "1/n")
# Utility def ulp_error(approx, truth): epsilon = numpy.finfo(float).eps error = approx - truth ulp = approx * epsilon return math.log2(max(1, abs(float(error / ulp)))) mp.mp.prec = 256 # Test single values k = 0.4 x = 3.6 print("x = ", x, "k = ", k) truth_v = (mp.ellipfun('sn', x, k=k), mp.ellipfun('cn', x, k=k), mp.ellipfun('dn', x, k=k)) agm_v = sncndn_agm(x, k) landen_v = sncndn_landen(x, k) combine_v = sncndn(x, k) print("combine: ", combine_v) print("landen: ", landen_v) print("agm: ", agm_v) print("mpmath: ", truth_v[0], truth_v[1], truth_v[2]) print("inverse: ", (arcsn(combine_v[0], k), arccn(combine_v[1], k), arcdn(combine_v[2], k))) #exit() # Calculate errors max_period = 77.632480664198080934069420189 # Periodicity at 1-epsilon
# print a_pow # test_w_vs_a(-10,10) else: print ' Jacobi Elliptic selected' jacobifunctions = ['dn'] #when you do something other than 1 odeint messes up AGAIN #m_vals = [0.5,0.75,0.95,1.0] m_vals = [1.0] amplitudes = numpy.logspace(0.01,3,num=10,base=10) # amplitudes = [1.0,12.0,23.0,34.0,45.0,56.0,67.0,78.0,89.0,100.0] #------------------------------------ for p in jacobifunctions: func = p elliptic = mpmath.ellipfun(func) #-------------------------------- for m_val in m_vals: for amp_index, q in enumerate(amplitudes): graphdata = numpy.zeros((len(amplitudes),2)) Amp = q kspace = numpy.linspace(1,100,5) n_inside = -100/(numpy.amin(kspace)) n_outside = -1/(1000*numpy.amax(kspace)) num_steps = 1000 data = numpy.zeros((len(kspace),4)) for i in range(0,len(kspace)): print i k = kspace[i] time = numpy.linspace(n_inside,n_outside,num_steps) xinit = numpy.array([1/(math.sqrt(2*k))*math.cos(k*n_inside), -k/(math.sqrt(2*k))*math.sin(k*n_inside)])
def cn(self, z): return ellipfun('cn', z, self._k**2).__complex__()