Esempio n. 1
0
 def xi(self, tau):
     from mpmath import sqrt, polyval
     xi_r = self.__rr_xi
     retval = xi_r**2 + polyval(self.__fp_xi, xi_r**2 / 2) / (
         2 * (self.__w_xi.P(tau - self.tau_xi) -
              polyval(self.__fpp_xi, xi_r**2 / 2) / 24))
     return sqrt(retval)
Esempio n. 2
0
 def eta(self, tau):
     from mpmath import sqrt, polyval
     eta_r = self.__rr_eta
     retval = eta_r**2 + polyval(self.__fp_eta, eta_r**2 / 2) / (
         2 * (self.__w_eta.P(tau - self.tau_eta) -
              polyval(self.__fpp_eta, eta_r**2 / 2) / 24))
     return sqrt(retval)
Esempio n. 3
0
    def t(self, tau):
        from mpmath import polyval
        xi_r, eta_r = self.__rr_xi, self.__rr_eta
        fp_xi, fp_eta = self.__fp_xi, self.__fp_eta
        fpp_xi, fpp_eta = self.__fpp_xi, self.__fpp_eta
        w_xi, w_eta = self.__w_xi, self.__w_eta
        tau_xi, tau_eta = self.__tau_xi, self.__tau_eta
        # These are the roots at the denominator.
        e_xi, e_eta = polyval(fpp_xi, xi_r**2 / 2) / 24, polyval(
            fpp_eta, eta_r**2 / 2) / 24

        # Link the roots to the half-periods.
        def e_to_omega(wp, e):
            return min([p / 2 for p in list(wp.periods) + [sum(wp.periods)]],
                       key=lambda p: abs(wp.P(p) - e))

        om_xi, om_eta = e_to_omega(w_xi, e_xi), e_to_omega(w_eta, e_eta)
        g2_xi, g2_eta = w_xi.invariants[0], w_eta.invariants[0]
        print(abs(w_xi.P(om_xi) - e_xi))
        print(abs(w_eta.P(om_eta) - e_eta))
        print(om_xi, om_eta)
        retval = tau * (xi_r**2 + eta_r**2) + \
         polyval(fp_xi,xi_r**2/2) / (2 * (g2_xi/4 - 3*e_xi**2)) * (tau*e_xi+w_xi.zeta(tau-tau_xi-om_xi)-w_xi.zeta(-tau_xi-om_xi)) + \
         polyval(fp_eta,eta_r**2/2) / (2 * (g2_eta/4 - 3*e_eta**2)) * (tau*e_eta+w_eta.zeta(tau-tau_eta-om_eta)-w_eta.zeta(-tau_eta-om_eta))
        return retval
Esempio n. 4
0
def getNthKFibonacciNumber( n, k ):
    if real( n ) < 0:
        raise ValueError( 'non-negative argument expected' )

    if real( k ) < 2:
        raise ValueError( 'argument <= 2 expected' )

    if n < k - 1:
        return 0

    nth = int( n ) + 4

    precision = int( fdiv( fmul( n, k ), 8 ) )

    if ( mp.dps < precision ):
        mp.dps = precision

    poly = [ 1 ]
    poly.extend( [ -1 ] * int( k ) )

    roots = polyroots( poly )
    nthPoly = getNthFibonacciPolynomial( k )

    result = 0
    exponent = fsum( [ nth, fneg( k ), -2 ] )

    for i in range( 0, int( k ) ):
        result += fdiv( power( roots[ i ], exponent ), polyval( nthPoly, roots[ i ] ) )

    return floor( fadd( re( result ), fdiv( 1, 2 ) ) )
Esempio n. 5
0
    def polish_one_root(self, coeffs, root, n_iters):
        """
        Use Newton-Raphson method to polish root.
        TODO: add termination condition based on dx

        Parameters
        ----------
        coeffs: list of mpf
            Coefficient to use in mp.polyval. NOTE that these are in the reverse order of numpy.polyval!
        root : mp.mpf
            Initial guess for roots.
        n_iters : int

        Returns
        -------
        polished_root : mp.mpf
        """
        
        prevdx=np.inf
        for i in range(n_iters):
            p, pp = mp.polyval(coeffs, root, derivative=1)
            dx = p/pp
            if abs(dx)>abs(prevdx) and abs(dx)>1e-2:
                raise Exception(prevdx,dx)
            root -= dx
            prevdx = dx
        return root
Esempio n. 6
0
 def calc_term(z, nudist, p, n):
     pans = mp.polyval(coeffs, z)
     ans = z - p * pans
     ans = (1.0 - p) / ans
     ans = mp.power(ans, n)
     ans *= (pans - z) / (z - p * pans)
     return ans
Esempio n. 7
0
def evaluatePolynomial( a, b ):
    if not isinstance( a, list ):
        a = [ a ]

    if isinstance( b, list ):
        return [ evaluatePolynomial( a, i ) for i in b ]
    else:
        return polyval( a, b )
Esempio n. 8
0
def evaluatePolynomial( a, b ):
    '''Evaluates an arbitrary polynomial a for value b.'''
    if not isinstance( a, list ):
        a = [ a ]

    if isinstance( b, list ):
        return [ evaluatePolynomial( a, i ) for i in b ]
    else:
        return polyval( a, b )
Esempio n. 9
0
	def phi(self,tau):
		from mpmath import polyval
		phi0 = self.__init_coordinates[2]
		pphi = self.__init_momenta[2]
		fp_xi, fp_eta = self.__fp_xi, self.__fp_eta
		fpp_xi, fpp_eta = self.__fpp_xi, self.__fpp_eta
		xi_r, eta_r = self.__rr_xi, self.__rr_eta
		w_xi, w_eta = self.__w_xi, self.__w_eta
		tau_xi, tau_eta = self.__tau_xi, self.__tau_eta
		beta_xi, beta_eta = -polyval(fpp_xi,xi_r**2/2) / 24, -polyval(fpp_eta,eta_r**2/2) / 24
		gamma_xi, gamma_eta = 2*xi_r**2, 2*eta_r**2
		delta_xi, delta_eta = polyval(fp_xi,xi_r**2/2) + 2*xi_r**2*beta_xi, polyval(fp_eta,eta_r**2/2) + 2*eta_r**2*beta_eta
		u_xi, u_eta = w_xi.Pinv(-delta_xi/gamma_xi), w_eta.Pinv(-delta_eta/gamma_eta)
		retval = phi0 + 2*pphi*(tau * (1 / gamma_xi + 1 / gamma_eta) + \
			(delta_xi - beta_xi*gamma_xi) / (gamma_xi**2 * w_xi.Pprime(u_xi)) * \
			(stark.__ln_sigma(w_xi,tau-tau_xi+u_xi) - stark.__ln_sigma(w_xi,-tau+tau_xi+u_xi) - stark.__ln_sigma(w_xi,-tau_xi+u_xi) + stark.__ln_sigma(w_xi,tau_xi+u_xi) - 2*tau*w_xi.zeta(u_xi)) + \
			(delta_eta - beta_eta*gamma_eta) / (gamma_eta**2 * w_eta.Pprime(u_eta)) * \
			(stark.__ln_sigma(w_eta,tau-tau_eta+u_eta) - stark.__ln_sigma(w_eta,-tau+tau_eta+u_eta) - stark.__ln_sigma(w_eta,-tau_eta+u_eta) + stark.__ln_sigma(w_eta,tau_eta+u_eta) - 2*tau*w_eta.zeta(u_eta)))
		return retval
Esempio n. 10
0
def polyval(l,x):
    from mpmath import polyval, mpf, mpc
    if any([isinstance(_,(mpf,mpc)) for _ in l + [x]]):
        return polyval(l,x)
    retval = 0.
    tmp = 1.
    for c in l[-1::-1]:
        retval += c*tmp;
        tmp *= x
    return retval
Esempio n. 11
0
    def bisection(self, coeffs, a, b, tol=1e-6, n_iters=10):
        """Use bisection to find roots of function. Then polish off with Newton-Raphson method.

        Parameters
        ----------
        coeffs : list of mp.mpf
            This will be passed to mpmath.polyval. Remember that the order of coeffs go from highest to lowest
            degree polynomial!
        a : mp.mpf
            Lower bound on bracket.
        b : mp.mpf
            Upper bound on bracket.
        tol : float,1e-10
            Difference b-a when the Newton-Raphson method is called.

        Returns
        -------
        root : mp.mpf
            Estimate of root.
        """

        signa=np.sign(mp.polyval(coeffs, a))
        signb=np.sign(mp.polyval(coeffs, b))
        if signa==signb:
            raise BisectionError("Bisection will fail to find root.")
        assert a<b

        found=False
        # keep bisecting the interval
        while not found:
            # using the fact that the sign of the function must change when crossing the root, we can
            # repeatedly bisect and know which side the root must be on
            signa=np.sign(mp.polyval(coeffs, a))
            signmid=np.sign(mp.polyval(coeffs, (a+b)/2))
            if signa==signmid:
                a=(a+b)/2
            else:
                b=(a+b)/2
            if (b-a)<tol:
                found=True
                root=(a+b)/2
        return self.polish_one_root(coeffs, root, n_iters)
Esempio n. 12
0
	def t(self,tau):
		from mpmath import polyval
		xi_r, eta_r = self.__rr_xi, self.__rr_eta
		fp_xi, fp_eta = self.__fp_xi, self.__fp_eta
		fpp_xi, fpp_eta = self.__fpp_xi, self.__fpp_eta
		w_xi, w_eta = self.__w_xi, self.__w_eta
		tau_xi, tau_eta = self.__tau_xi, self.__tau_eta
		# These are the roots at the denominator.
		e_xi, e_eta = polyval(fpp_xi,xi_r**2/2)/24, polyval(fpp_eta,eta_r**2/2)/24
		# Link the roots to the half-periods.
		def e_to_omega(wp,e):
			return min([p/2 for p in list(wp.periods) + [sum(wp.periods)]],key = lambda p: abs(wp.P(p) - e))
		om_xi, om_eta = e_to_omega(w_xi,e_xi), e_to_omega(w_eta,e_eta)
		g2_xi, g2_eta = w_xi.invariants[0], w_eta.invariants[0]
		print(abs(w_xi.P(om_xi)-e_xi))
		print(abs(w_eta.P(om_eta)-e_eta))
		print(om_xi,om_eta)
		retval = tau * (xi_r**2 + eta_r**2) + \
			polyval(fp_xi,xi_r**2/2) / (2 * (g2_xi/4 - 3*e_xi**2)) * (tau*e_xi+w_xi.zeta(tau-tau_xi-om_xi)-w_xi.zeta(-tau_xi-om_xi)) + \
			polyval(fp_eta,eta_r**2/2) / (2 * (g2_eta/4 - 3*e_eta**2)) * (tau*e_eta+w_eta.zeta(tau-tau_eta-om_eta)-w_eta.zeta(-tau_eta-om_eta))
		return retval
Esempio n. 13
0
 def phi(self, tau):
     from mpmath import polyval
     phi0 = self.__init_coordinates[2]
     pphi = self.__init_momenta[2]
     fp_xi, fp_eta = self.__fp_xi, self.__fp_eta
     fpp_xi, fpp_eta = self.__fpp_xi, self.__fpp_eta
     xi_r, eta_r = self.__rr_xi, self.__rr_eta
     w_xi, w_eta = self.__w_xi, self.__w_eta
     tau_xi, tau_eta = self.__tau_xi, self.__tau_eta
     beta_xi, beta_eta = -polyval(fpp_xi, xi_r**2 / 2) / 24, -polyval(
         fpp_eta, eta_r**2 / 2) / 24
     gamma_xi, gamma_eta = 2 * xi_r**2, 2 * eta_r**2
     delta_xi, delta_eta = polyval(
         fp_xi, xi_r**2 / 2) + 2 * xi_r**2 * beta_xi, polyval(
             fp_eta, eta_r**2 / 2) + 2 * eta_r**2 * beta_eta
     u_xi, u_eta = w_xi.Pinv(-delta_xi / gamma_xi), w_eta.Pinv(-delta_eta /
                                                               gamma_eta)
     retval = phi0 + 2*pphi*(tau * (1 / gamma_xi + 1 / gamma_eta) + \
      (delta_xi - beta_xi*gamma_xi) / (gamma_xi**2 * w_xi.Pprime(u_xi)) * \
      (stark.__ln_sigma(w_xi,tau-tau_xi+u_xi) - stark.__ln_sigma(w_xi,-tau+tau_xi+u_xi) - stark.__ln_sigma(w_xi,-tau_xi+u_xi) + stark.__ln_sigma(w_xi,tau_xi+u_xi) - 2*tau*w_xi.zeta(u_xi)) + \
      (delta_eta - beta_eta*gamma_eta) / (gamma_eta**2 * w_eta.Pprime(u_eta)) * \
      (stark.__ln_sigma(w_eta,tau-tau_eta+u_eta) - stark.__ln_sigma(w_eta,-tau+tau_eta+u_eta) - stark.__ln_sigma(w_eta,-tau_eta+u_eta) + stark.__ln_sigma(w_eta,tau_eta+u_eta) - 2*tau*w_eta.zeta(u_eta)))
     return retval
Esempio n. 14
0
def main():
    func = lambda x: mpmath.exp(mpmath.power(x, 2))
    precision = sys.argv[1].split('**')
    precision = math.pow(int(precision[0]), int(precision[1]))
    x = mpmath.mpf(float(sys.argv[2]))

    print "expected value = %f" % mpmath.quad(func, [0, x])
    print "precision = %f" % precision
    print "x = %f" % x
    print "max Taylor degree to try = %s" % sys.argv[3]
    print ""

    upperbound = int(sys.argv[3])
    lowerbound = 0
    lowestn = 0

    # find the degree logarithmically, this is usually faster than trying 0..n
    while lowerbound < upperbound:
        n = (lowerbound + upperbound) / 2

        # estimate the remainder
        diff = mpmath.diff(func, x, n)
        rn = diff / mpmath.factorial(n + 1)
        rn = rn * mpmath.power(x, n + 1)

        # is it good enough?
        if rn < precision:
            upperbound = n
            lowestn = n
        else:
            lowerbound = n + 1

    if lowestn:
        print "lowest Taylor degree needed = %d" % lowestn
        coefficients = []

        # find the coefficients of our Taylor polynomial
        for k in reversed(range(lowestn + 1)):
            if k > 0:
                coefficients.append(mpmath.diff(func, 0, k - 1) / mpmath.factorial(k))

        # compute the value of the polynomial (add 0 for the free variable, the value of the indefinite integral at 0)
        p = mpmath.polyval(coefficients + [0], x)
        print "computed value = %f" % p
    else:
        print "max n is too low"
Esempio n. 15
0
def getNthPolygonalNumber(n, k):
    coeff = fdiv(fsub(k, 2), 2)  # validateRealInt isn't happy, why?
    return polyval([coeff, fneg(fsub(coeff, 1)), 0], int(n))
Esempio n. 16
0
def getNthTetrahedralNumberOperator(n):
    return polyval([fdiv(1, 6), fdiv(1, 2), fdiv(1, 3), 0], n)
Esempio n. 17
0
	def xi(self,tau):
		from mpmath import sqrt, polyval
		xi_r = self.__rr_xi
		retval = xi_r**2 + polyval(self.__fp_xi,xi_r**2/2) / (2 * (self.__w_xi.P(tau - self.tau_xi) - polyval(self.__fpp_xi,xi_r**2/2) / 24))
		return sqrt(retval)
Esempio n. 18
0
def getNthPentatopeNumber( n ):
    return fdiv( polyval( [ 1, 6, 11, 6, 0 ], real_int( n ) ), 24 )
Esempio n. 19
0
 def retval_p(eta):
     return polyval([4*c4,3*c3,2*c2,c1],eta)
Esempio n. 20
0
 def retval_pp(xi):
     return polyval([4*3*c4,3*2*c3,2*c2],xi)
Esempio n. 21
0
 def retval(xi):
     return polyval([c4,c3,c2,c1,c0],xi)
Esempio n. 22
0
def getNthTruncatedOctahedralNumberOperator(n):
    return polyval([16, -33, 24, -6], n)
Esempio n. 23
0
def getNthCenteredIcosahedralNumberOperator(n):
    arg = fsub(n, 1)
    return fmul(fadd(fmul(arg, 2), 1), fdiv(polyval([5, 5, 3], arg), 3))
Esempio n. 24
0
def getNthCenteredDodecahedralNumberOperator(n):
    arg = fsub(n, 1)
    return fmul(fadd(fmul(arg, 2), 1), polyval([5, 5, 1], arg))
Esempio n. 25
0
def getNthStellaOctangulaNumberOperator(n):
    return polyval([2, 0, -1, 0], n)
Esempio n. 26
0
def getCenteredPolygonalNumber( n, k ):
    if real_int( k ) < 3:
        raise ValueError( 'the number of sides of the polygon cannot be less than 3,' )

    coefficient = fdiv( k, 2 )
    return polyval( [ coefficient, fneg( coefficient ), 1 ], real( n ) )
Esempio n. 27
0
	def __init__(self,eps,x0,v0):
		from numpy import dot
		from mpmath import polyroots, mpf, mpc, sqrt, atan2, polyval
		from weierstrass_elliptic import weierstrass_elliptic as we
		if eps <= 0:
			raise ValueError('thrust must be strictly positive')
		eps = mpf(eps)
		# Unitary grav. parameter.
		mu = mpf(1.)
		x,y,z = [mpf(x) for x in x0]
		vx,vy,vz = [mpf(v) for v in v0]
		r = sqrt(x**2 + y**2 + z**2)
		xi = sqrt(r+z)
		eta = sqrt(r-z)
		phi = atan2(y,x)
		vr = dot(v0,x0) / r
		vxi = (vr + vz) / (2 * sqrt(r+z))
		veta = (vr - vz) / (2 * sqrt(r-z))
		vphi = (vy*x - vx*y) / (x**2 + y**2)
		pxi = (xi**2 + eta**2) * vxi
		peta = (xi**2 + eta**2) * veta
		pphi = xi**2*eta**2*vphi
		if pphi == 0:
			raise ValueError('bidimensional case')
		# Energy constant.
		h = (pxi**2 + peta**2) / (2*(xi**2 + eta**2)) + pphi**2 / (2*xi**2*eta**2) - (2 * mu) / (xi**2+eta**2) - eps * (xi**2 - eta**2) / 2
		# Alpha constants.
		alpha1 = -eps * xi**4 / 2 - h * xi**2 + pxi**2 / 2 + pphi**2 / (2*xi**2)
		alpha2 = eps * eta**4 / 2 - h * eta**2 + peta**2 / 2 + pphi**2 / (2*eta**2)
		# Analysis of the cubic polynomials in the equations for pxi and peta.
		roots_xi, _ = polyroots([8*eps,8*h,4*alpha1,-pphi**2],error=True,maxsteps=100)
		roots_eta, _ = polyroots([-8*eps,8*h,4*alpha2,-pphi**2],error=True,maxsteps=100)
		# NOTE: these are all paranoia checks that could go away if we used the exact cubic formula.
		if not (all([isinstance(x,mpf) for x in roots_xi]) or (isinstance(roots_xi[0],mpf) and isinstance(roots_xi[1],mpc) and isinstance(roots_xi[2],mpc))):
			raise ValueError('invalid xi roots detected: ' + str(roots_xi))
		if not (all([isinstance(x,mpf) for x in roots_eta]) or (isinstance(roots_eta[0],mpf) and isinstance(roots_eta[1],mpc) and isinstance(roots_eta[2],mpc))):
			raise ValueError('invalid eta roots detected: ' + str(roots_eta))
		# For xi we need to understand which of the real positive roots will be or was reached
		# given the initial condition.
		rp_roots_extract = lambda x: isinstance(x,mpf) and x > 0
		rp_roots_xi = [sqrt(2 * _) for _ in filter(rp_roots_extract,roots_xi)]
		rp_roots_eta = [sqrt(2. * _) for _ in filter(rp_roots_extract,roots_eta)]
		# Paranoia.
		if not len(rp_roots_xi) in [1,3]:
			raise ValueError('invalid xi roots detected: ' + str(roots_xi))
		if len(rp_roots_eta) != 2:
			raise ValueError('invalid eta roots detected: ' + str(roots_eta))
		# We choose as reachable/reached roots always those corresponding to the "pericentre"
		# for the two coordinates.
		if len(rp_roots_xi) == 1:
			# Here there's really no choice, only 1 root available.
			rr_xi = rp_roots_xi[0]
		else:
			# If motion is unbound, take the only root, otherwise take the smallest of the
			# two roots of the bound motion.
			rr_xi = rp_roots_xi[-1] if xi >= rp_roots_xi[-1] else rp_roots_xi[0]
		# No choice to be made here.
		rr_eta = rp_roots_eta[0]
		# Store parameters and constants.
		self.__init_coordinates = [xi,eta,phi]
		self.__init_momenta = [pxi,peta,pphi]
		self.__eps = eps
		self.__h = h
		self.__alpha1 = alpha1
		self.__alpha2 = alpha2
		self.__rp_roots_xi = rp_roots_xi
		self.__rp_roots_eta = rp_roots_eta
		self.__rr_xi = rr_xi
		self.__rr_eta = rr_eta
		self.__roots_xi = roots_xi
		self.__roots_eta = roots_eta
		# Create the Weierstrass objects for xi and eta.
		a1, a2, a3, a4 = 2*eps, (4 * h)/3, alpha1, -pphi**2
		g2 = -4 * a1 * a3 + 3 * a2**2
		g3 = 2 * a1 * a2 * a3 - a2**3 - a1**2*a4
		self.__f_xi = [4*a1,6*a2,4*a3,a4]
		self.__fp_xi = [12*a1,12*a2,4*a3]
		self.__fpp_xi = [24*a1,12*a2]
		self.__w_xi = we(g2,g3)
		# Eta.
		a1,a3 = -a1,alpha2
		g2 = -4 * a1 * a3 + 3 * a2**2
		g3 = 2 * a1 * a2 * a3 - a2**3 - a1**2*a4
		self.__f_eta = [4*a1,6*a2,4*a3,a4]
		self.__fp_eta = [12*a1,12*a2,4*a3]
		self.__fpp_eta = [24*a1,12*a2]
		self.__w_eta = we(g2,g3)
		# Compute the taus.
		tau_xi = self.__compute_tau_xi()
		tau_eta = self.__compute_tau_eta()
		self.__tau_xi = tau_xi
		self.__tau_eta = tau_eta
		# Store the real periods.
		self.__T_xi = self.__w_xi.periods[0]
		self.__T_eta = self.__w_eta.periods[0]
		# Delta bound (for debugging).
		xi_roots = self.__w_xi.roots
		# Determine the root corresponding to the real half-period.
		e_R = min(xi_roots,key = lambda x: abs(self.__w_xi.P(self.__T_xi/2) - x))
		self.__Dbound = e_R - polyval(self.__fpp_xi,xi**2/2) / 24
Esempio n. 28
0
def getNthCenteredPolygonalNumber(n, k):
    coefficient = fdiv(k, 2)
    return polyval([coefficient, fneg(coefficient), 1], n)
Esempio n. 29
0
 def __init__(self, eps, x0, v0):
     from numpy import dot
     from mpmath import polyroots, mpf, mpc, sqrt, atan2, polyval
     from weierstrass_elliptic import weierstrass_elliptic as we
     if eps <= 0:
         raise ValueError('thrust must be strictly positive')
     eps = mpf(eps)
     # Unitary grav. parameter.
     mu = mpf(1.)
     x, y, z = [mpf(x) for x in x0]
     vx, vy, vz = [mpf(v) for v in v0]
     r = sqrt(x**2 + y**2 + z**2)
     xi = sqrt(r + z)
     eta = sqrt(r - z)
     phi = atan2(y, x)
     vr = dot(v0, x0) / r
     vxi = (vr + vz) / (2 * sqrt(r + z))
     veta = (vr - vz) / (2 * sqrt(r - z))
     vphi = (vy * x - vx * y) / (x**2 + y**2)
     pxi = (xi**2 + eta**2) * vxi
     peta = (xi**2 + eta**2) * veta
     pphi = xi**2 * eta**2 * vphi
     if pphi == 0:
         raise ValueError('bidimensional case')
     # Energy constant.
     h = (pxi**2 + peta**2) / (2 * (xi**2 + eta**2)) + pphi**2 / (
         2 * xi**2 *
         eta**2) - (2 * mu) / (xi**2 + eta**2) - eps * (xi**2 - eta**2) / 2
     # Alpha constants.
     alpha1 = -eps * xi**4 / 2 - h * xi**2 + pxi**2 / 2 + pphi**2 / (2 *
                                                                     xi**2)
     alpha2 = eps * eta**4 / 2 - h * eta**2 + peta**2 / 2 + pphi**2 / (
         2 * eta**2)
     # Analysis of the cubic polynomials in the equations for pxi and peta.
     roots_xi, _ = polyroots([8 * eps, 8 * h, 4 * alpha1, -pphi**2],
                             error=True,
                             maxsteps=100)
     roots_eta, _ = polyroots([-8 * eps, 8 * h, 4 * alpha2, -pphi**2],
                              error=True,
                              maxsteps=100)
     # NOTE: these are all paranoia checks that could go away if we used the exact cubic formula.
     if not (all([isinstance(x, mpf) for x in roots_xi]) or
             (isinstance(roots_xi[0], mpf) and isinstance(roots_xi[1], mpc)
              and isinstance(roots_xi[2], mpc))):
         raise ValueError('invalid xi roots detected: ' + str(roots_xi))
     if not (all([isinstance(x, mpf) for x in roots_eta]) or
             (isinstance(roots_eta[0], mpf) and isinstance(
                 roots_eta[1], mpc) and isinstance(roots_eta[2], mpc))):
         raise ValueError('invalid eta roots detected: ' + str(roots_eta))
     # For xi we need to understand which of the real positive roots will be or was reached
     # given the initial condition.
     rp_roots_extract = lambda x: isinstance(x, mpf) and x > 0
     rp_roots_xi = [sqrt(2 * _) for _ in filter(rp_roots_extract, roots_xi)]
     rp_roots_eta = [
         sqrt(2. * _) for _ in filter(rp_roots_extract, roots_eta)
     ]
     # Paranoia.
     if not len(rp_roots_xi) in [1, 3]:
         raise ValueError('invalid xi roots detected: ' + str(roots_xi))
     if len(rp_roots_eta) != 2:
         raise ValueError('invalid eta roots detected: ' + str(roots_eta))
     # We choose as reachable/reached roots always those corresponding to the "pericentre"
     # for the two coordinates.
     if len(rp_roots_xi) == 1:
         # Here there's really no choice, only 1 root available.
         rr_xi = rp_roots_xi[0]
     else:
         # If motion is unbound, take the only root, otherwise take the smallest of the
         # two roots of the bound motion.
         rr_xi = rp_roots_xi[-1] if xi >= rp_roots_xi[-1] else rp_roots_xi[0]
     # No choice to be made here.
     rr_eta = rp_roots_eta[0]
     # Store parameters and constants.
     self.__init_coordinates = [xi, eta, phi]
     self.__init_momenta = [pxi, peta, pphi]
     self.__eps = eps
     self.__h = h
     self.__alpha1 = alpha1
     self.__alpha2 = alpha2
     self.__rp_roots_xi = rp_roots_xi
     self.__rp_roots_eta = rp_roots_eta
     self.__rr_xi = rr_xi
     self.__rr_eta = rr_eta
     self.__roots_xi = roots_xi
     self.__roots_eta = roots_eta
     # Create the Weierstrass objects for xi and eta.
     a1, a2, a3, a4 = 2 * eps, (4 * h) / 3, alpha1, -pphi**2
     g2 = -4 * a1 * a3 + 3 * a2**2
     g3 = 2 * a1 * a2 * a3 - a2**3 - a1**2 * a4
     self.__f_xi = [4 * a1, 6 * a2, 4 * a3, a4]
     self.__fp_xi = [12 * a1, 12 * a2, 4 * a3]
     self.__fpp_xi = [24 * a1, 12 * a2]
     self.__w_xi = we(g2, g3)
     # Eta.
     a1, a3 = -a1, alpha2
     g2 = -4 * a1 * a3 + 3 * a2**2
     g3 = 2 * a1 * a2 * a3 - a2**3 - a1**2 * a4
     self.__f_eta = [4 * a1, 6 * a2, 4 * a3, a4]
     self.__fp_eta = [12 * a1, 12 * a2, 4 * a3]
     self.__fpp_eta = [24 * a1, 12 * a2]
     self.__w_eta = we(g2, g3)
     # Compute the taus.
     tau_xi = self.__compute_tau_xi()
     tau_eta = self.__compute_tau_eta()
     self.__tau_xi = tau_xi
     self.__tau_eta = tau_eta
     # Store the real periods.
     self.__T_xi = self.__w_xi.periods[0]
     self.__T_eta = self.__w_eta.periods[0]
     # Delta bound (for debugging).
     xi_roots = self.__w_xi.roots
     # Determine the root corresponding to the real half-period.
     e_R = min(xi_roots,
               key=lambda x: abs(self.__w_xi.P(self.__T_xi / 2) - x))
     self.__Dbound = e_R - polyval(self.__fpp_xi, xi**2 / 2) / 24
	def func(x):
		retval = sqrt(polyval([a,b,c,d,e],x))
		if retval.imag == 0:
			return retval
		else:
			return float('nan')
Esempio n. 31
0
def getNthCenteredIcosahedralNumber( n ):
    arg = real_int( fsub( n, 1 ) )
    return fmul( fadd( fmul( arg, 2 ), 1 ), fdiv( polyval( [ 5, 5, 3 ], arg ), 3 ) )
Esempio n. 32
0
 def retval_p(xi):
     return polyval([4*c4,3*c3,2*c2,c1],xi)
Esempio n. 33
0
def getNthPolygonalNumber( n, k ):
    if real_int( k ) < 3:
        raise ValueError( 'the number of sides of the polygon cannot be less than 3,' )

    coeff = fdiv( fsub( k, 2 ), 2 )
    return polyval( [ coeff, fneg( fsub( coeff, 1 ) ), 0 ], real( n ) )
Esempio n. 34
0
 def retval(eta):
     return polyval([c4,c3,c2,c1,c0],eta)
Esempio n. 35
0
def getNthRhombicDodecahedralNumber( n ):
    return polyval( [ 4, 6, 4, 1 ], real_int( n ) )
Esempio n. 36
0
 def retval_pp(eta):
     return polyval([4*3*c4,3*2*c3,2*c2],eta)
Esempio n. 37
0
def getNthOctahedralNumberOperator(n):
    return polyval([fdiv(2, 3), 0, fdiv(1, 3), 0], n)
Esempio n. 38
0
def evaluatePolynomialOperator( a, b ):
    return polyval( a, b )
Esempio n. 39
0
	def eta(self,tau):
		from mpmath import sqrt, polyval
		eta_r = self.__rr_eta
		retval = eta_r**2 + polyval(self.__fp_eta,eta_r**2/2) / (2 * (self.__w_eta.P(tau - self.tau_eta) - polyval(self.__fpp_eta,eta_r**2/2) / 24))
		return sqrt(retval)
Esempio n. 40
0
def getNthCenteredDodecahedralNumber( n ):
    arg = real_int( fsub( n, 1 ) )
    return fmul( fadd( fmul( arg, 2 ), 1 ), polyval( [ 5, 5, 1 ], arg ) )
Esempio n. 41
0
def getNthIcosahedralNumberOperator(n):
    return polyval([fdiv(5, 2), fdiv(-5, 2), 1, 0], n)
Esempio n. 42
0
def getNthDodecahedralNumberOperator(n):
    return polyval([fdiv(9, 2), fdiv(-9, 2), 1, 0], n)
Esempio n. 43
0
def getNthTruncatedOctahedralNumber( n ):
    return polyval( [ 16, -33, 24, 6 ], real_int( n ) )
Esempio n. 44
0
def getNthRhombicDodecahedralNumberOperator(n):
    return polyval([4, 6, 4, 1], fsub(n, 1))
Esempio n. 45
0
def getNthStellaOctangulaNumber( n ):
    return polyval( [ 2, 0, -1, 0 ], real_int( n ) )
Esempio n. 46
0
def getNthPentatopeNumberOperator(n):
    return fdiv(polyval([1, 6, 11, 6, 0], n), 24)
Esempio n. 47
0
 def calc_term(z, nudist, p, n):
     ans = mp.polyval(coeffs, z)
     ans = z - p * ans
     ans = (1.0 - p) / ans
     return mp.power(ans, n)