Ejemplo n.º 1
0
 def test_lagroots(self):
     assert_almost_equal(lag.lagroots([1]), [])
     assert_almost_equal(lag.lagroots([0, 1]), [1])
     for i in range(2, 5):
         tgt = np.linspace(0, 3, i)
         res = lag.lagroots(lag.lagfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 2
0
 def test_lagroots(self) :
     assert_almost_equal(lag.lagroots([1]), [])
     assert_almost_equal(lag.lagroots([0, 1]), [1])
     for i in range(2, 5) :
         tgt = np.linspace(0, 3, i)
         res = lag.lagroots(lag.lagfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 3
0
def getAbscissasAndWeights(N=5):
    # Laguerre polynomial roots and weights for Laguerre-Guass quadrature
    coef = np.concatenate([np.zeros(N), [1]])
    roots = laguerre.lagroots(coef)
    weights = []
    for n, root in enumerate(roots):
        n = n + 1
        array = np.concatenate([np.zeros(N + 1), [1]])
        value = laguerre.lagval(root, array)
        weight = root / ((N + 1) * value)**2
        weights.append(weight)

    return roots, weights
Ejemplo n.º 4
0
def GaussLaguerre(N):
    """
    function to return roots and weights for Gauss-Laguerre integration
    with N mesh points
    no multiplication for weights for Laguerre!
    """
    #initialize coeffitents for Laguerre series
    coeff = np.zeros(N+1)
    #find root of N-th Laguerre polynomial
    coeff[N] = 1
    roots = laguerre.lagroots(coeff)
    #reset coeff.; initalize L matrix
    L = np.zeros((N,N), dtype = np.float64)
    coeff = np.zeros(N)
    for i in range(N):
        #fill j-th Laguerre poly with the i-th root.
        for j in range(N):
            coeff[j] = 1
            L[i, j] = laguerre.lagval(roots[i], coeff)
            coeff[j] = 0
    L_inv = inv(L)
    return   L_inv[0,:], roots
Ejemplo n.º 5
0
from numpy.polynomial.laguerre import lagroots, lagfromroots
import numpy as n


def print_roots(p, roots):
    print(p)
    size = roots.size
    for i in range(size):
        print(roots[i])
    print()


p = n.poly1d([243, -486, 783, -990, 558, -28, -72, 16])
p_roots = lagroots(p)

print_roots(p, p_roots)

p2 = n.poly1d([1, 1, 3, 2, -1, -3, -11, -8, -12, -4, -4])
p_roots2 = lagroots(p2)

print_roots(p2, p_roots2)

p3 = n.poly1d([1, complex(0, 1), -1, complex(0, -1), 1])
p_roots3 = lagroots(p3)
print_roots(p3, p_roots3)
def func(x):
    g = np.exp(x) * f(x)
    return g


# Recurrence for L'(n,x):x*L'(n,x)=(x-n-1)*L(n,x)+(n+1)*L(n+1,x)
dl_dx = lambda n, x: ((x - n - 1) * L(n, x) + (n + 1) * L(n + 1, x)) / (x)

n = int(input("enter n"))
"""finding roots of nth laguerre polynomial and storing it in t"""
Q = []
for i in range(n):
    Q.append(0)
Q.append(1)
t = lag.lagroots(Q)
print("x found by quadrature rule", t)

#finding the solution
sol = 0
for i in range(n):
    #weight
    w = -1 / (n * dl_dx(n, t[i]) * L(n - 1, t[i]))
    #print(w)
    sol1 = w * func(t[i])
    sol = sol + sol1

main_sol = sol
print("the value of integral of the function with n=", n, "is\n", main_sol)
man_sol = integral.quad(f, 0, math.inf)
man_sol = round(man_sol[0], 10)
def laguerreroots(order):
    return lagroots(laguerreseries(order))
Ejemplo n.º 8
0
from numpy.polynomial.laguerre import lagroots, Laguerre,poly2lag,lag2poly
import numpy as np

p = np.poly1d([1, 1, 3, 2, -1, -3, -11, -8, -12, -4, 4])
roots = p.roots()

p(-1.33826102e+01)

print(p)
for i in range(roots.size):
    print(p(roots[i]))
print()


p = n.poly1d([1, complex(0, 1), -1, complex(0, -1), 1])
roots = lagroots(p)

print(p)
for i in range(roots.size):
    print(roots[i])



pp=lag2poly(p)
lagroots(pp)
print (p)