-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathchebfftPy.py
More file actions
28 lines (25 loc) · 772 Bytes
/
Copy pathchebfftPy.py
File metadata and controls
28 lines (25 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from numpy import pi,cos,arange,array, flipud,\
real,zeros, sqrt
from numpy.fft import fft,ifft
def chebfft(v):
'''Chebyshev differentiation via fft.
Ref.: Trefethen's 'Spectral Methods in MATLAB' book.
'''
N = len(v)-1
if N == 0:
w = 0.0 # only when N is even!
return w
x = cos(pi*arange(0,N+1)/N)
ii = arange(0,N)
V = flipud(v[1:N]); V = list(v) + list(V);
U = real(fft(V))
b = list(ii); b.append(0); b = b + list(arange(1-N,0));
w_hat = 1j*array(b)
w_hat = w_hat * U
W = real(ifft(w_hat))
w = zeros(N+1)
w[1:N] = -W[1:N]/sqrt(1-x[1:N]**2)
w[0] = sum(ii**2*U[ii])/N + 0.5*N*U[N]
w[N] = sum((-1)**(ii+1)*ii**2*U[ii])/N + \
0.5*(-1)**(N+1)*N*U[N]
return w