Exemple #1
0
def gauss_lobatto_points(p):
    """
    Returns the list of Gauss-Lobatto points of the order 'p'.
    """
    x = Symbol("x")
    print "creating"
    e = legendre(p, x).diff(x)
    e = Poly(e, x)
    print "polydone"
    if e == 0:
        return []
    print "roots"
    if e == 1:
        r = []
    else:
        with workdps(40):
            r, err = polyroots(e.all_coeffs(), error=True)
        #if err > 1e-40:
        #    raise Exception("Internal Error: Root is not precise")
    print "done"
    p = []
    p.append("-1.0")
    for x in r:
        if abs(x) < 1e-40: x = 0
        p.append(str(x))
    p.append("1.0")
    return p
Exemple #2
0
def gauss_lobatto_points(p):
    """
    Returns the list of Gauss-Lobatto points of the order 'p'.
    """
    x = Symbol("x")
    print "creating"
    e = legendre(p, x).diff(x)
    e = Poly(e, x)
    print "polydone"
    if e == 0:
        return []
    print "roots"
    if e == 1:
        r = []
    else:
        with workdps(40):
            r, err = polyroots(e.all_coeffs(), error=True)
        #if err > 1e-40:
        #    raise Exception("Internal Error: Root is not precise")
    print "done"
    p = []
    p.append("-1.0")
    for x in r:
        if abs(x) < 1e-40: x = 0
        p.append(str(x))
    p.append("1.0")
    return p
Exemple #3
0
def calc_roots(c):
    '''
    Calculataion of roots based on Sympy's arbitrary precision routine.
    Algorithm: Durand-Kerner
    Ref: http://en.wikipedia.org/wiki/Durand-Kerner_method
    '''
    coeffs = c[-1:0:-1]
    #Return all roots, both real and complex. The roots are returned as a sorted
    #list, with the real roots first.
    allroots = mpmath.polyroots(coeffs,
                                maxsteps=50,
                                cleanup=True,
                                extraprec=10,
                                error=False)
    #Parse the roots to find only real positive roots
    roots = {}
    realroots = [x for x in allroots if x.imag == 0.0]
    posroots = [x for x in realroots if x > 0.0]
    while posroots:
        aroot = posroots.pop()
        if aroot in roots.keys():
            roots[aroot] += 1
        else:
            roots[aroot] = 1
    for key in roots.keys():
        roots[float(key)] = roots.pop(key)
    return roots
def raizes():
                #polyroots retorna uma lista
                if(const[5] != 0):
                                roots = mpmath.polyroots([const[5], const[4], const[3], const[2], const[1], const[0]])
                           # print roots[0], roots[1], roots[2], roots[3], roots[4]
                elif((const[5] == 0) and (const[4] !=0)):
                                roots = mpmath.polyroots([const[4], const[3], const[2], const[1], const[0]])
                           # print roots[0], roots[1], roots[2], roots[3]
                elif((const[4] == 0) and (const[3] !=0)):
                                roots = mpmath.polyroots([const[3], const[2], const[1], const[0]])
                           # print roots[0], roots[1], roots[2]
                elif((const[3] == 0) and (const[2] !=0)):
                                roots = mpmath.polyroots([const[2], const[1], const[0]])
                           # print roots[0], roots[1]
                elif((const[2] == 0) and (const[1] !=0)):
                                roots = mpmath.polyroots([const[1], const[0]])
                           # print roots[0]

                Respostas[0] = roots
def raizes():
                #polyroots retorna uma lista
                if(a5 != 0):
                                roots = mpmath.polyroots([a5, a4, a3, a2, a1, a0])
                           # print roots[0], roots[1], roots[2], roots[3], roots[4]
                elif((a5 == 0) and (a4 !=0)):
                                roots = mpmath.polyroots([a4, a3, a2, a1, a0])
                           # print roots[0], roots[1], roots[2], roots[3]
                elif((a4 == 0) and (a3 !=0)):
                                roots = mpmath.polyroots([a3, a2, a1, a0])
                           # print roots[0], roots[1], roots[2]
                elif((a3 == 0) and (a2 !=0)):
                                roots = mpmath.polyroots([a2, a1, a0])
                           # print roots[0], roots[1]
                elif((a2 == 0) and (a1 !=0)):
                                roots = mpmath.polyroots([a1, a0])
                           # print roots[0]

                Respostas[0] = roots
Exemple #6
0
def lstsq(m, n, w, pn, f, maxsteps=50, extraprec=10):
    """compute Gauss functions to fit function f
    
    f(x) = sum_j c_j x^pn exp(a_jx^2)

    Inputs
    ------
    m : Int
        Number of GTOs to expand function f
    n : Int 
        Number of points to use fitting. 
	require :  n>2m
    w : Scalar
        Paramaeter. Points used for fitting are
        x_k = Sqrt(kw) k = 1,...,n
    pn : Int
        Principle number of GTOs
    f  : lambda 
       Target function

    Returns
    -------
    (cs, zs, {"pn": pn, "f": f})
    cs : [Scalar]
        List of coefficient of GTOs
    zs : [Scalar]
        List of orbital exponents of GTOs
    """

    # grid points
    xn_s = [np.sqrt((k + 1) * w) for k in range(n)]

    # discreted function values
    fn_s = [f(x) / (x**pn) for x in xn_s]

    # equation for determine S
    f_mat = [fn_s[j:j + m] for j in range(n - m)]
    f_vec = [-fn_s[j + m] for j in range(n - m)]
    (sm_s, resi, d1, d2) = np.linalg.lstsq(f_mat, f_vec)

    # solve polynomial of S
    coef_for_v = np.hstack([[1], sm_s[::-1]])
    vm_s = polyroots(coef_for_v, maxsteps, extraprec)

    # calculate orbital exponents
    am_s = np.array([np.log(np.real_if_close(complex(v))) / w for v in vm_s])

    # calculate coefficient
    v_mat = np.array([[v**(k + 1) for v in vm_s] for k in range(n)])
    (cm_s, resi_c, d1, d2) = np.linalg.lstsq(v_mat, fn_s)
    return (cm_s, am_s, {"pn": pn, "f": f, "xn_s": xn_s})
Exemple #7
0
def gauss_lobatto_points(p):
    """
    Returns the list of Gauss-Lobatto points of the order 'p'.
    """
    x = Symbol("x")
    e = (1-x**2)*legendre(p, x).diff(x)
    e = Poly(e, x)
    if e == 0:
        return []
    with workdps(40):
        r, err = polyroots(e.all_coeffs(), error=True)
    if err > 1e-40:
        raise Exception("Internal Error: Root is not precise")
    p = []
    for x in r:
        if abs(x) < 1e-40: x = 0
        p.append(str(x))
    return p
Exemple #8
0
def test_poly():
    output("""\
    poly_:{
        r:{$[prec>=abs(x:reim x)1;x 0;x]} each .qml.poly x;
        r iasc {x:reim x;(abs x 1;neg x 1;x 0)} each r};""")

    for A in poly_subjects:
        A = sp.Poly(A, sp.Symbol("x"))
        if A.degree() <= 3 and all(a.is_real for a in A.all_coeffs()):
            R = []
            for r in sp.roots(A, multiple=True):
                r = sp.simplify(sp.expand_complex(r))
                R.append(r)
            R.sort(key=lambda x: (abs(sp.im(x)), -sp.im(x), sp.re(x)))
        else:
            R = mp.polyroots([mp.mpc(*(a.evalf(mp.mp.dps)).as_real_imag())
                              for a in A.all_coeffs()])
            R.sort(key=lambda x: (abs(x.imag), -x.imag, x.real))
        assert len(R) == A.degree()
        test("poly_", A.all_coeffs(), R, complex_pair=True)