简体   繁体   中英

Question about a Python Project about Legendre Polynomials and Gram matrices

My question is about a small Python project (numerics,studying math).

I am relatelively sure the Gram Matrices and the bases should be correctly algorithmized. It works just fine with the monome basis, yet gives out an error I don't understand for the other two...

The error is module object not callable : in the prelambdalegendre function at the for loop

import math
from scipy import integrate as int
import numpy as np

def gaussklammer(n):
    if n%2==0:
        return n
    elif n%2==1:
        return n-1

monom= lambda x,n: x**n


def prelambdalegendre(x,k):
    pol=0
    for i in range (0,int(gaussklammer(k)/2)+1):
                    pol+= (-1)**i *math.factorial(2*k-2*i)/(math.factorial(k-i)*math.factorial(k-2*i)*math.factorial(i)*2**k)*(x**(k-2*i))

    return pol

legendre = lambda x,n:prelambdalegendre(x,n)


normlegendre =lambda x,k: math.factorial(2*k)/(2**k *math.factorial(k)**2) *legendre(x,k)

def grammatrix(baseofchoice,size):
    if baseofchoice=='monom':
        base =lambda x,k:monom(x,k)
    elif baseofchoice=='legendre':
        base =lambda x,k:legendre(x,k)
    elif baseofchoice=='normlegendre':
        base =lambda x,k:normlegendre(x,k)    
    #More elegant implementations didn't work , unfortunately.
    #To add another base, just add another elif statement

    A=np.zeros((size,size))


    for i in range (0,size):
        for j in range (0,size):
            f= lambda x : base(x,i)*base(x,j)
            A[i][j]=(int.quad(f,-1,1)[0])


    return A

print(grammatrix('monom',5))
print(grammatrix('legendre',5))


You problem is you declare scipy.integrate as int ,

and later call the int eger function from python in your for loop.

When you import integrate , try importing it with another name.

Found another way to do it , thanks for your input anyways ;)

import math
from scipy import integrate
import numpy as np


monom= lambda x,n: x**n


def legendre(x,k):
    if k==0:
        return 1
    elif k==1: 
        return x
    else :

        pol=x*legendre(x,k-1)-((k-1)**2)/(4*(k-1)**2 -1)*legendre(x,k-2)

    return pol


normlegendre =lambda x,k: math.factorial(2*k)/(2**k *math.factorial(k)**2) *legendre(x,k)

def grammatrix(baseofchoice,size):
    if baseofchoice=='monom':
        base =lambda x,k:monom(x,k)
    elif baseofchoice=='legendre':
        base=lambda x,k:legendre(x,k)
    elif baseofchoice=='normlegendre':
        base=lambda x,k:normlegendre(x,k)    
    #Dieser Liste können nach Bedarf für neue Basen neue gleichförmige Clauses
    #hinzugefügt werden, elegantere Implementierungen scheiterten leider.

    A=np.zeros((size,size))


    for i in range (0,size):
        for j in range (0,size):
            f= lambda x : base(x,i)*base(x,j)
            A[i][j]=(integrate.quad(f,-1,1)[0])


    return A
A=grammatrix('monom',4)
print(A)
B=grammatrix('legendre',4)
print(B)
C=grammatrix('normlegendre',4)
print(C)
condA=np.linalg.cond(A)
condB=np.linalg.cond(B)
condC=np.linalg.cond(C)
print(condA)
print(condB)
print(condC)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM