-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spectral_Operators_Cheb.jl
326 lines (202 loc) · 5.35 KB
/
Spectral_Operators_Cheb.jl
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#Generates the function ∫ₐˣ ⋅ dx
export ChebIntegratorGen, 𝘾, 𝘾⁻¹, IntegrateCoeff, δ₁, δ₋₁,δ̂₁,δ̂₋₁,DerivativeCheb
using FFTW
# using BenchmarkTools
# using Plots
function IntegrateCoeff(f̂)
N = size(f̂)[1]
∫f̂ = zeros(N)
# ∫f̂[1] = 0.0
# ∫f̂[2] = (f̂[1] - f̂[3]/2)
# ∫f̂[3] = (f̂[2]/4 - f̂[4]/4)
∫f̂[1] = 0.0
∫f̂[2] = (f̂[1] - f̂[3]/2)
∫f̂[3] = (f̂[2]/4 - f̂[4]/4)
for k=4:(N -1)
# ∫f̂[k+1] = (f̂[k] - f̂[k+2])/(2*k)
∫f̂[k] = (f̂[k-1] - f̂[k+1])/(2*(k-1))
end
#∫f̂[N] = f̂[N-1]/(2*(N-1))
∫f̂[N] = f̂[N-1]/(2*(N-1))
return ∫f̂
end
function der_matrix(deg::Int, a::Real=-1, b::Real=1)
N = deg
D = zeros(Float64, N, N+1)
for i=1:N, j=1:N+1
if i == 1
if iseven(i + j)
continue
end
D[i, j] = 2*(j-1)/(b-a)
else
if j < i || iseven(i+j)
continue
end
D[i, j] = 4*(j-1)/(b-a)
end
end
D
end
function DerivativeCheb(f, L, DCT, DCT⁻¹, type)
f̂ = 𝘾(f, DCT, type)
println("testing sizes")
println(length(f̂))
println(size(der_matrix(length(f) - 1, 0, L)))
df̂ = der_matrix(length(f) - 1, 0, L) * f̂
append!(df̂ , 0)
return 𝘾⁻¹(df̂, DCT⁻¹, type)
end
function 𝘾(f, DCT, type)
N = size(f)[1]
f̂ = DCT*f
#Now we need to renormalize depending on the type
if type == 1
f̂[1] = f̂[1]/2
f̂[size(f)[1]] = f̂[size(f)[1]]/2
f̂ = f̂/(N-1)
elseif type == 2
f̂[1] = f̂[1]/2
f̂ = f̂/N
end
return f̂
end
function 𝘾⁻¹(f̂, DCT⁻¹, type)
N = size(f̂)[1]
f̂_ = copy(f̂)
#Now we need to renormalize depending on the type
if type == 1
f̂_[1] = f̂[1]*2 #*(N-1)
f̂_[end] = f̂[end]*2#*(N-1)
elseif type == 2
f̂_[1] = f̂_[1]*2#*(N)
end
return (DCT⁻¹*f̂_)/2
end
function ∫indefinite(f, L, type, DCT, DCT⁻¹)
#type corresponds to the type of cosine transform
#options are 1 or 2
#We still no normalize depending on the interval length, and starting point
f̂ = 𝘾(f, DCT, type)
∫f̂ = IntegrateCoeff(f̂)
∫f = 𝘾⁻¹(f̂, DCT⁻¹, type)
return L/2*∫f
end
function δ₁(f, DCT ,type)
if type ==1
return f[1]#f[end]
else
return δ̂₁(𝘾(f, DCT, type))
end
end
function δ₋₁(f, DCT ,type)
if type ==1
return f[end]#f[1]
else
return δ̂₋₁(𝘾(f, DCT, type))
end
end
function δ̂₁(f̂)
return sum(f̂)
end
function δ̂₋₁(f̂)
f₋₁ = 0
N = size(f̂)[1]
for i=1:N
f₋₁+= f̂[i]*(-1)^(i-1)
end
return f₋₁
end
function Cheb_∫_Gen(N, a, b, type)
if type == 1
DCT = FFTW.plan_r2r(ones(N) ,FFTW.REDFT00, 1, flags = FFTW.MEASURE)
DCT⁻¹ = DCT
else
#type ==2
DCT = FFTW.plan_r2r(ones(N) ,FFTW.REDFT10, 1, flags = FFTW.MEASURE)
DCT⁻¹ = FFTW.plan_r2r(ones(N) ,FFTW.REDFT01, 1, flags = FFTW.MEASURE)
end
end
function def_int_left(f, DCT, L)
N = size(f)[1]
f̂ = DCT * f #un normalized
f̂[2:N] = f̂[2:N]/(N-1)
f̂[1] = f̂[1]/(2*N-2)
#println(f̂)
∫f̂ = zeros(N)
∫f̂[1] = 0.0
∫f̂[2] = (f̂[1] - f̂[3]/2)
∫f̂[3] = (f̂[2]/4 - f̂[4]/4)
for k=4:(N -1)
# ∫f̂[k+1] = (f̂[k] - f̂[k+2])/(2*k)
∫f̂[k] = (f̂[k-1] - f̂[k+1])/(2*(k-1))
end
#∫f̂[N] = f̂[N-1]/(2*(N-1))
∫f̂[N] = f̂[N-1]/((N-1))
#we do stuff with the coefficients
∫f̂[1] = ∫f̂[1]*(2*N-2)
∫f̂[2:N] = ∫f̂[2:N]*(N-1)
∫f = DCT * ∫f̂/ (2*N-2) #This is the primitive
∫f = L/2*(∫f .- ∫f[end] )#Scale to the proper length of the interval
return ∫f
end
function def_int_right(f, DCT, L)
N = size(f)[1]
f̂ = DCT * f #un normalized
f̂[2:N] = f̂[2:N]/(N-1)
f̂[1] = f̂[1]/(2*N-2)
#println(f̂)
∫f̂ = zeros(N)
∫f̂[1] = 0.0
∫f̂[2] = (f̂[1] - f̂[3]/2)
∫f̂[3] = (f̂[2]/4 - f̂[4]/4)
for k=4:(N -1)
# ∫f̂[k+1] = (f̂[k] - f̂[k+2])/(2*k)
∫f̂[k] = (f̂[k-1] - f̂[k+1])/(2*(k-1))
end
#∫f̂[N] = f̂[N-1]/(2*(N-1)) #the last coef has a special normalization as well
∫f̂[N] = f̂[N-1]/((N-1))
#we do stuff with the coefficients
∫f̂[1] = ∫f̂[1]*(2*N-2)
∫f̂[2:N] = ∫f̂[2:N]*(N-1)
∫f = DCT * ∫f̂/ (2*N-2) #This is the primitive
∫f = L/2*(∫f[1] .- ∫f )#Scale to the proper length of the interval
return ∫f
end
function ChebIntegratorGen2(N, a, b, cumulative = true)
precomp_dct = FFTW.plan_r2r(ones(N) ,FFTW.REDFT00, 1, flags = FFTW.MEASURE)
if cumulative
∫ₐˣ(f) = def_int_left(f, precomp_dct, b-a)
return ∫ₐˣ
else
∫ₓᵇ(f) = def_int_right(f, precomp_dct, b-a)
return ∫ₓᵇ
end
end
#
# N = 14
# θ = 1/N*collect(0:N)
# x = cos.(π*θ)
#
#
# n = 13
# y = x .^ n
# ∫y = 1/(n+1)*x .^ (n+1) .- 1/(n+1)*x[end]^(n+1)
#
# p = FFTW.plan_r2r(y ,FFTW.REDFT00, 1, flags = FFTW.MEASURE)
#
#
# # Fk_2 = FFTW.r2r(y, FFTW.REDFT00)
# #
# # @btime FFTW.r2r($y, FFTW.REDFT00)
# # @btime $p * $y
# ∫ = ChebIntegratorGen(N +1, -1, 1, false)
#
# a = ∫(y)
#
# plot(x, a)
# plot!(x, ∫y)
#
# error_sup = maximum(abs.(∫y - a))
#
# println(error_sup)