Beispiel #1
0
def test_la_roots():
    root_func = lambda a: lambda n, mu: orth.la_roots(n, a, mu)
    eval_func = lambda a: lambda n, x: orth.eval_genlaguerre(n, a, x)

    verify_gauss_quad(root_func(-0.5), eval_func(-0.5), 5)
    verify_gauss_quad(root_func(-0.5), eval_func(-0.5), 25, atol=1e-13)
    verify_gauss_quad(root_func(-0.5), eval_func(-0.5), 100, atol=1e-12)

    verify_gauss_quad(root_func(0.1), eval_func(0.1), 5)
    verify_gauss_quad(root_func(0.1), eval_func(0.1), 25, atol=1e-13)
    verify_gauss_quad(root_func(0.1), eval_func(0.1), 100, atol=1e-13)

    verify_gauss_quad(root_func(1), eval_func(1), 5)
    verify_gauss_quad(root_func(1), eval_func(1), 25, atol=1e-13)
    verify_gauss_quad(root_func(1), eval_func(1), 100, atol=1e-13)

    verify_gauss_quad(root_func(10), eval_func(10), 5)
    verify_gauss_quad(root_func(10), eval_func(10), 25, atol=1e-13)
    verify_gauss_quad(root_func(10), eval_func(10), 100, atol=1e-12)

    verify_gauss_quad(root_func(50), eval_func(50), 5)
    verify_gauss_quad(root_func(50), eval_func(50), 25, atol=1e-13)
    verify_gauss_quad(root_func(50), eval_func(50), 100, atol=1e-13)

    x, w = orth.la_roots(5, 2, False)
    y, v, m = orth.la_roots(5, 2, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    assert_raises(ValueError, orth.la_roots, 0, 2)
    assert_raises(ValueError, orth.la_roots, 3.3, 2)
    assert_raises(ValueError, orth.la_roots, 3, -1.1)
Beispiel #2
0
def test_la_roots():
    root_func = lambda a: lambda n, mu: orth.la_roots(n, a, mu)
    eval_func = lambda a: lambda n, x: orth.eval_genlaguerre(n, a, x)

    verify_gauss_quad(root_func(-0.5), eval_func(-0.5), 5)
    verify_gauss_quad(root_func(-0.5), eval_func(-0.5), 25, atol=1e-13)
    verify_gauss_quad(root_func(-0.5), eval_func(-0.5), 100, atol=1e-12)

    verify_gauss_quad(root_func(0.1), eval_func(0.1), 5)
    verify_gauss_quad(root_func(0.1), eval_func(0.1), 25, atol=1e-13)
    verify_gauss_quad(root_func(0.1), eval_func(0.1), 100, atol=1e-13)

    verify_gauss_quad(root_func(1), eval_func(1), 5)
    verify_gauss_quad(root_func(1), eval_func(1), 25, atol=1e-13)
    verify_gauss_quad(root_func(1), eval_func(1), 100, atol=1e-13)

    verify_gauss_quad(root_func(10), eval_func(10), 5)
    verify_gauss_quad(root_func(10), eval_func(10), 25, atol=1e-13)
    verify_gauss_quad(root_func(10), eval_func(10), 100, atol=1e-12)

    verify_gauss_quad(root_func(50), eval_func(50), 5)
    verify_gauss_quad(root_func(50), eval_func(50), 25, atol=1e-13)
    verify_gauss_quad(root_func(50), eval_func(50), 100, atol=1e-13)

    x, w = orth.la_roots(5, 2, False)
    y, v, m = orth.la_roots(5, 2, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    assert_raises(ValueError, orth.la_roots, 0, 2)
    assert_raises(ValueError, orth.la_roots, 3.3, 2)
    assert_raises(ValueError, orth.la_roots, 3, -1.1)
 def quadrature(self,add=10): 
     """base points and weights
     number of points = size of basis + add
     """
     b,w =so.la_roots(self.n+add,0.)
     for i in range(len(b)): w[i]=w[i]*cmath.exp(b[i])
     return b/(2.*self.s)+self.o, w
def test_la_roots():
    rootf = lambda a: lambda n, mu: orth.la_roots(n, a, mu)
    evalf = lambda a: lambda n, x: orth.eval_genlaguerre(n, a, x)
    weightf = lambda a: lambda x: x**a * np.exp(-x)

    vgq = verify_gauss_quad
    vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 5)
    vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 100, atol=1e-12)

    vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 5)
    vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 100, atol=1e-13)

    vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 5)
    vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 100, atol=1e-13)

    vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 5)
    vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 100, atol=1e-12)

    vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 5)
    vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(50),
        evalf(50),
        weightf(50),
        0.,
        np.inf,
        100,
        rtol=1e-14,
        atol=2e-13)

    x, w = orth.la_roots(5, 2, False)
    y, v, m = orth.la_roots(5, 2, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    muI, muI_err = integrate.quad(weightf(2.), 0., np.inf)
    assert_allclose(m, muI, rtol=muI_err)

    assert_raises(ValueError, orth.la_roots, 0, 2)
    assert_raises(ValueError, orth.la_roots, 3.3, 2)
    assert_raises(ValueError, orth.la_roots, 3, -1.1)
Beispiel #5
0
def test_la_roots():
    rootf = lambda a: lambda n, mu: orth.la_roots(n, a, mu)
    evalf = lambda a: lambda n, x: orth.eval_genlaguerre(n, a, x)
    weightf = lambda a: lambda x: x**a * np.exp(-x)

    vgq = verify_gauss_quad
    vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 5)
    vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 100, atol=1e-12)

    vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 5)
    vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 100, atol=1e-13)

    vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 5)
    vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 100, atol=1e-13)

    vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 5)
    vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 100, atol=1e-12)

    vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 5)
    vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 25, atol=1e-13)
    vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 100, rtol=1e-14, atol=2e-13)

    x, w = orth.la_roots(5, 2, False)
    y, v, m = orth.la_roots(5, 2, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    muI, muI_err = integrate.quad(weightf(2.), 0., np.inf)
    assert_allclose(m, muI, rtol=muI_err)

    assert_raises(ValueError, orth.la_roots, 0, 2)
    assert_raises(ValueError, orth.la_roots, 3.3, 2)
    assert_raises(ValueError, orth.la_roots, 3, -1.1)
Beispiel #6
0
def la_roots(n, alpha=0, method='newton'):
    '''
    Returns the roots (x) of the nth order generalized (associated) Laguerre
    polynomial, L^(alpha)_n(x), and weights (w) to use in Gaussian quadrature
    over [0,inf] with weighting function exp(-x) x**alpha with alpha > -1.

    Parameters
    ----------
    n : integer
        number of roots
    method : 'newton' or 'eigenvalue'
        uses Newton Raphson to find zeros of the Laguerre polynomial (Fast)
        or eigenvalue of the jacobi matrix (Slow) to obtain the nodes and
        weights, respectively.

    Returns
    -------
    x : ndarray
        roots
    w : ndarray
        weights

    Example
    -------
    >>> import numpy as np
    >>> [x,w] = h_roots(10)
    >>> np.sum(x*w)
    1.3352627380516791e-17

    See also
    --------
    qrule, gaussq

    References
    ----------
    [1]  Golub, G. H. and Welsch, J. H. (1969)
    'Calculation of Gaussian Quadrature Rules'
    Mathematics of Computation, vol 23,page 221-230,

    [2]. Stroud and Secrest (1966), 'gaussian quadrature formulas',
      prentice-hall, Englewood cliffs, n.j.
    '''

    if alpha <= -1:
        raise ValueError('alpha must be greater than -1')

    if not method.startswith('n'):
        return ort.la_roots(n, alpha)
    return _la_roots_newton(n, alpha)
Beispiel #7
0
def RadialKinetic(N,L,h=1.0):
    T = zeros((N,N),'d')
    a = 2*L+2
    h2 = h*h
    #q = Points(N)
    q,w = la_roots(N,a)
    q = q.real
    for i in range(N):
        for j in range(N):
            if i == j:
                T[i,i] = p2jj(N,a,q[i])-L*(L+1)*qm2jj(N,a,q[i])
            else:
                T[i,j] = pow(-1,i+j)*(p2jk(N,a,q[i],q[j])\
                                      -L*(L+1)*qm2jk(N,a,q[i],q[j]))
    return -0.5*T/h2
Beispiel #8
0
def RadialKinetic_BH0(N,L):
    # Baye + Heenen eq 3.17
    a = 2*L+2
    x,w = la_roots(N,a)
    x = x.real
    T = zeros((N,N),'d')
    for i in range(N):
        xi = x[i]
        Sii = Laguerre_S(i,i,x)
        T[i,i] = 0.25*pow((a+1.)/xi,2)+Sii
        for j in range(i):
            xj = x[j]
            Sij = Laguerre_S(i,j,x)
            T[i,j] = T[j,i] = pow(-1,i-j)*(0.5*(a+1.)*(xi+xj)/pow(xi*xj,1.5)
                                           +Sij)
    return x,0.5*T
    def __init__(self, order, a=-0.5, options={}):
        r"""Initialize a new quadrature rule.

        :param order: The order :math:`k` of the Gauss-Laguerre quadrature.
                      From theory we know that a Gauss quadrature rule
                      of order :math:`k` is exact for polynomials up to
                      degree :math:`2 k - 1`.
        :param a: The parameter :math:`a > -1` of the generalized Gauss-Laguerre quadrature.
                  This value defaults to `0` resulting in classical Gauss-Laguerre quadrature.

        :raise: :py:class:`ValueError` if order ``order`` is not 1 or above.

        .. warning::

		This quadrature is made specifically for our needs. Therefore the default
		values of :math:`\alpha` is not 0 but set to :math:`-\frac{1}{2}`. There
		is hope that this will give less confusion and hidden errors.
        """
        # The space dimension of the quadrature rule.
        self._dimension = 1

        # The order of the Gauss-Laguerre quadrature.
        self._order = order
        self._a = a

        # Quadrature has to have at least a single (node,weight) pair.
        if not self._order > 0:
            raise ValueError("Quadrature rule has to be of order 1 at least.")

        # Set the options
        self._options = options

        nodes, weights = la_roots(self._order, self._a)

        # The number of nodes in this quadrature rule
        self._number_nodes = nodes.size

        # The quadrature nodes \gamma.
        self._nodes = real(nodes).reshape((1,self._number_nodes))
        # The quadrature weights \omega.
        self._weights = real(weights).reshape((1,self._number_nodes))
Beispiel #10
0
    def __init__(self, order, a=-0.5, options={}):
        r"""Initialize a new quadrature rule.

        :param order: The order :math:`k` of the Gauss-Laguerre quadrature.
                      From theory we know that a Gauss quadrature rule
                      of order :math:`k` is exact for polynomials up to
                      degree :math:`2 k - 1`.
        :param a: The parameter :math:`a > -1` of the generalized Gauss-Laguerre quadrature.
                  This value defaults to `0` resulting in classical Gauss-Laguerre quadrature.

        :raise: :py:class:`ValueError` if order ``order`` is not 1 or above.

        .. warning:: This quadrature is made specifically for our needs. Therefore the default
                     values of :math:`\alpha` is not 0 but set to :math:`-\frac{1}{2}`. There
                     is hope that this will give less confusion and hidden errors.
        """
        # Quadrature has to have at least a single (node,weight) pair.
        if not order > 0:
            raise ValueError("Quadrature rule has to be of order 1 at least.")

        # The space dimension of the quadrature rule.
        self._dimension = 1

        # The order of the Gauss-Laguerre quadrature.
        self._order = order
        self._a = a

        # Set the options
        self._options = options

        nodes, weights = la_roots(self._order, self._a)

        # The number of nodes in this quadrature rule
        self._number_nodes = nodes.size

        # The quadrature nodes \gamma.
        self._nodes = real(nodes).reshape((1, self._number_nodes))

        # The quadrature weights \omega.
        self._weights = real(weights).reshape((1, self._number_nodes))
Beispiel #11
0
def la_roots(n, alpha=0, method='newton'):
    '''
    Returns the roots (x) of the nth order generalized (associated) Laguerre
    polynomial, L^(alpha)_n(x), and weights (w) to use in Gaussian quadrature over
    [0,inf] with weighting function exp(-x) x**alpha with alpha > -1.

    Parameters
    ----------
    n : integer
        number of roots
    method : 'newton' or 'eigenvalue'
        uses Newton Raphson to find zeros of the Laguerre polynomial (Fast)
        or eigenvalue of the jacobi matrix (Slow) to obtain the nodes and
        weights, respectively.

    Returns
    -------
    x : ndarray
        roots
    w : ndarray
        weights

    Example
    -------
    >>> import numpy as np
    >>> [x,w] = h_roots(10)
    >>> np.sum(x*w)
    -5.2516042729766621e-19

    See also
    --------
    qrule, gaussq

    References
    ----------
    [1]  Golub, G. H. and Welsch, J. H. (1969)
    'Calculation of Gaussian Quadrature Rules'
    Mathematics of Computation, vol 23,page 221-230,

    [2]. Stroud and Secrest (1966), 'gaussian quadrature formulas',
      prentice-hall, Englewood cliffs, n.j.
    '''

    if alpha <= -1:
        raise ValueError('alpha must be greater than -1')

    if not method.startswith('n'):
        return ort.la_roots(n, alpha)
    else:
        max_iter = 10
        releps = 3e-14
        C = [9.084064e-01, 5.214976e-02, 2.579930e-03, 3.986126e-03]

        # Initial approximations to the roots go into z.
        anu = 4.0 * n + 2.0 * alpha + 2.0
        rhs = np.arange(4 * n - 1, 2, -4) * np.pi / anu
        r3 = rhs ** (1. / 3)
        r2 = r3 ** 2
        theta = r3 * (C[0] + r2 * (C[1] + r2 * (C[2] + r2 * C[3])))
        z = anu * np.cos(theta) ** 2

        dz = zeros(len(z))
        L = zeros((3, len(z)))
        Lp = zeros((1, len(z)))
        pp = zeros((1, len(z)))
        k0 = 0
        kp1 = 1
        k = slice(len(z))
        for _its in xrange(max_iter):
            #%Newton's method carried out simultaneously on the roots.
            L[k0, k] = 0.
            L[kp1, k] = 1.

            for jj in xrange(1, n + 1):
                # Loop up the recurrence relation to get the Laguerre
                # polynomials evaluated at z.
                km1 = k0
                k0 = kp1
                kp1 = np.mod(kp1 + 1, 3)

                L[kp1, k] = ((2 * jj - 1 + alpha - z[k]) * L[k0, k] - (jj - 1 + alpha) * L[km1, k]) / jj
            #end
            #%L now contains the desired Laguerre polynomials.
            #%We next compute pp, the derivatives with a standard
            #% relation involving the polynomials of one lower order.

            Lp[k] = L[k0, k]
            pp[k] = (n * L[kp1, k] - (n + alpha) * Lp[k]) / z[k]

            dz[k] = L[kp1, k] / pp[k]
            z[k] = z[k] - dz[k]# % Newton?s formula.
            #%k = find((abs(dz) > releps.*z))


            if not np.any(abs(dz) > releps):
                break
        else:
            warnings.warn('too many iterations!')

        x = z
        w = -np.exp(sp.gammaln(alpha + n) - sp.gammaln(n)) / (pp * n * Lp)
        return x, w
Beispiel #12
0
def la_roots(n, alpha=0, method='newton'):
    '''
    Returns the roots (x) of the nth order generalized (associated) Laguerre
    polynomial, L^(alpha)_n(x), and weights (w) to use in Gaussian quadrature
    over [0,inf] with weighting function exp(-x) x**alpha with alpha > -1.

    Parameters
    ----------
    n : integer
        number of roots
    method : 'newton' or 'eigenvalue'
        uses Newton Raphson to find zeros of the Laguerre polynomial (Fast)
        or eigenvalue of the jacobi matrix (Slow) to obtain the nodes and
        weights, respectively.

    Returns
    -------
    x : ndarray
        roots
    w : ndarray
        weights

    Example
    -------
    >>> import numpy as np
    >>> [x,w] = h_roots(10)
    >>> np.sum(x*w)
    -5.2516042729766621e-19

    See also
    --------
    qrule, gaussq

    References
    ----------
    [1]  Golub, G. H. and Welsch, J. H. (1969)
    'Calculation of Gaussian Quadrature Rules'
    Mathematics of Computation, vol 23,page 221-230,

    [2]. Stroud and Secrest (1966), 'gaussian quadrature formulas',
      prentice-hall, Englewood cliffs, n.j.
    '''

    if alpha <= -1:
        raise ValueError('alpha must be greater than -1')

    if not method.startswith('n'):
        return ort.la_roots(n, alpha)
    else:
        max_iter = 10
        releps = 3e-14
        C = [9.084064e-01, 5.214976e-02, 2.579930e-03, 3.986126e-03]

        # Initial approximations to the roots go into z.
        anu = 4.0 * n + 2.0 * alpha + 2.0
        rhs = np.arange(4 * n - 1, 2, -4) * np.pi / anu
        r3 = rhs ** (1. / 3)
        r2 = r3 ** 2
        theta = r3 * (C[0] + r2 * (C[1] + r2 * (C[2] + r2 * C[3])))
        z = anu * np.cos(theta) ** 2

        dz = zeros(len(z))
        L = zeros((3, len(z)))
        Lp = zeros((1, len(z)))
        pp = zeros((1, len(z)))
        k0 = 0
        kp1 = 1
        k = slice(len(z))
        for _its in xrange(max_iter):
            # Newton's method carried out simultaneously on the roots.
            L[k0, k] = 0.
            L[kp1, k] = 1.

            for jj in xrange(1, n + 1):
                # Loop up the recurrence relation to get the Laguerre
                # polynomials evaluated at z.
                km1 = k0
                k0 = kp1
                kp1 = np.mod(kp1 + 1, 3)

                L[kp1, k] = ((2 * jj - 1 + alpha - z[k]) * L[
                             k0, k] - (jj - 1 + alpha) * L[km1, k]) / jj
            # end
            # L now contains the desired Laguerre polynomials.
            # We next compute pp, the derivatives with a standard
            #  relation involving the polynomials of one lower order.

            Lp[k] = L[k0, k]
            pp[k] = (n * L[kp1, k] - (n + alpha) * Lp[k]) / z[k]

            dz[k] = L[kp1, k] / pp[k]
            z[k] = z[k] - dz[k]  # % Newton?s formula.
            # k = find((abs(dz) > releps.*z))

            if not np.any(abs(dz) > releps):
                break
        else:
            warnings.warn('too many iterations!')

        x = z
        w = -np.exp(sp.gammaln(alpha + n) - sp.gammaln(n)) / (pp * n * Lp)
        return x, w
Beispiel #13
0
def Vmat(N,L):
    q = la_roots(N,2*L+2)[0].real
    return diag([-1/qi for qi in q])
    def quadrature(self,n): return so.la_roots(n,0)

class ChebyshevPolynomial(OrthogonalPolynomial):