/
Chebyshev.jl
276 lines (213 loc) · 7.22 KB
/
Chebyshev.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
export Chebyshev, NormalizedChebyshev
"""
`Chebyshev()` is the space spanned by the Chebyshev polynomials
```
T_0(x),T_1(x),T_2(x),…
```
where `T_k(x) = cos(k*acos(x))`. This is the default space
as there exists a fast transform and general smooth functions on `[-1,1]`
can be easily resolved.
"""
struct Chebyshev{D<:Domain,R} <: PolynomialSpace{D,R}
domain::D
function Chebyshev{D,R}(d) where {D,R}
isempty(d) && throw(ArgumentError("Domain cannot be empty"))
new(d)
end
Chebyshev{D,R}() where {D,R} = new(strictconvert(D, ChebyshevInterval()))
end
Chebyshev(d::Domain) = Chebyshev{typeof(d),real(prectype(d))}(d)
Chebyshev() = Chebyshev(ChebyshevInterval())
Chebyshev(d) = Chebyshev(Domain(d))
const NormalizedChebyshev{D<:Domain,R} = NormalizedPolynomialSpace{Chebyshev{D, R}, D, R}
NormalizedChebyshev() = NormalizedPolynomialSpace(Chebyshev())
NormalizedChebyshev(d) = NormalizedPolynomialSpace(Chebyshev(d))
function getproperty(S::Chebyshev, v::Symbol)
if v==:b || v==:a
-0.5
else
getfield(S,v)
end
end
normalization(::Type{T}, sp::Chebyshev, k::Int) where T = T(π)/(2-FastTransforms.δ(k,0))
Space(d::SegmentDomain) = Chebyshev(d)
_norm(x) = norm(x)
_norm(x::Real) = abs(x) # this preserves integers, whereas norm returns a float
function Space(d::AbstractInterval)
a,b = endpoints(d)
d2 = if isinf(_norm(a)) && isinf(_norm(b))
Line(d)
elseif isinf(_norm(a)) || isinf(_norm(b))
Ray(d)
else
d
end
Chebyshev(d2)
end
setdomain(S::Chebyshev, d::Domain) = Chebyshev(d)
ones(::Type{T}, S::Chebyshev) where {T<:Number} = Fun(S,fill(one(T),1))
ones(S::Chebyshev) = Fun(S,fill(1.0,1))
function first(f::Fun{<:Chebyshev})
n = ncoefficients(f)
T = rangetype(space(f))
oneel = oneunit(T)
n == 0 && return zero(cfstype(f)) * oneel
n == 1 && return f.coefficients[1] * oneel
foldr(-,coefficients(f)) * oneel
end
function last(f::Fun{<:Chebyshev})
T = rangetype(space(f))
oneel = oneunit(T)
reduce(+,coefficients(f)) * oneel
end
spacescompatible(a::Chebyshev,b::Chebyshev) = domainscompatible(a,b)
hasfasttransform(::Chebyshev) = true
supportsinplacetransform(::Chebyshev{<:Domain{T},T}) where {T<:AbstractFloat} = true
## Transform
transform(::Chebyshev,vals::AbstractVector,plan) = plan*vals
itransform(::Chebyshev,cfs::AbstractVector,plan) = plan*cfs
plan_transform(::Chebyshev,vals::AbstractVector) = plan_chebyshevtransform(vals)
plan_itransform(::Chebyshev,cfs::AbstractVector) = plan_ichebyshevtransform(cfs)
plan_transform!(::Chebyshev, vals::AbstractVector) = plan_chebyshevtransform!(vals)
plan_itransform!(::Chebyshev, cfs::AbstractVector) = plan_ichebyshevtransform!(cfs)
## Evaluation
clenshaw(sp::Chebyshev, c::AbstractVector, x::AbstractArray) =
clenshaw(c,x,ClenshawPlan(promote_type(eltype(c),eltype(x)),sp,length(c),length(x)))
clenshaw(::Chebyshev,c::AbstractVector,x) = chebyshev_clenshaw(c,x)
clenshaw(c::AbstractVector,x::AbstractVector,plan::ClenshawPlan{S,V}) where {S<:Chebyshev,V}=
clenshaw(c,collect(x),plan)
#TODO: This modifies x, which is not threadsafe
clenshaw(c::AbstractVector,x::Vector,plan::ClenshawPlan{<:Chebyshev}) = chebyshev_clenshaw(c,x,plan)
function clenshaw(c::AbstractMatrix{T},x::T,plan::ClenshawPlan{S,T}) where {S<:Chebyshev,T<:Number}
Base.require_one_based_indexing(x, c)
bk=plan.bk
bk1=plan.bk1
bk2=plan.bk2
m,n=size(c) # m is # of coefficients, n is # of funs
@inbounds for i = 1:n
bk1[i] = zero(T)
bk2[i] = zero(T)
end
x = 2x
@inbounds for k=m:-1:2
for j=1:n
ck = c[k,j]
bk[j] = muladd(x,bk1[j],ck - bk2[j])
end
bk2, bk1, bk = bk1, bk, bk2
end
x = x/2
@inbounds for i = 1:n
ce = c[1,i]
bk[i] = muladd(x,bk1[i],ce - bk2[i])
end
bk
end
function clenshaw(c::AbstractMatrix{T},x::AbstractVector{T},plan::ClenshawPlan{S,T}) where {S<:Chebyshev,T<:Number}
Base.require_one_based_indexing(x, c)
bk=plan.bk
bk1=plan.bk1
bk2=plan.bk2
m,n=size(c) # m is # of coefficients, n is # of funs
@inbounds for i = 1:n
x[i] = 2x[i]
bk1[i] = zero(T)
bk2[i] = zero(T)
end
@inbounds for k=m:-1:2
for j=1:n
ck = c[k,j]
bk[j] = muladd(x[j],bk1[j],ck - bk2[j])
end
bk2, bk1, bk = bk1, bk, bk2
end
@inbounds for i = 1:n
x[i] = x[i]/2
ce = c[1,i]
bk[i] = muladd(x[i],bk1[i],ce - bk2[i])
end
bk
end
# overwrite x
function clenshaw!(c::AbstractVector,x::AbstractVector,plan::ClenshawPlan{S,V}) where {S<:Chebyshev,V}
Base.require_one_based_indexing(x, c)
N,n = length(c),length(x)
if isempty(c)
fill!(x,0)
return x
end
bk=plan.bk
bk1=plan.bk1
bk2=plan.bk2
@inbounds for i = 1:n
x[i] = 2x[i]
bk1[i] = zero(V)
bk2[i] = zero(V)
end
@inbounds for k = N:-1:2
ck = c[k]
for i = 1:n
bk[i] = muladd(x[i],bk1[i],ck - bk2[i])
end
bk2, bk1, bk = bk1, bk, bk2
end
ce = c[1]
@inbounds for i = 1:n
x[i] = x[i]/2
x[i] = muladd(x[i],bk1[i],ce-bk2[i])
end
x
end
## Calculus
# diff T -> U, then convert U -> T
function integrate(f::Fun{<:Chebyshev{<:IntervalOrSegment}})
cfs = coefficients(f)
z = fromcanonicalD(f,0)
v = z .* float.(cfs)
ultraint!(ultraconversion!(v))
Fun(f.space, v)
end
function differentiate(f::Fun{<:Chebyshev{<:IntervalOrSegment}})
cfs = coefficients(f)
z = fromcanonicalD(f,0)
w = float.(cfs) ./ z
v = ultraiconversion!(ultradiff!(w))
Fun(f.space, v)
end
## Multivariate
# determine correct parameter to have at least
# N point
_padua_length(N) = Int(cld(-3+sqrt(1+8N),2))
function squarepoints(::Type{T}, N) where T
pts = paduapoints(T, _padua_length(N))
n = size(pts,1)
ret = Array{SVector{2,T}}(undef, n)
@inbounds for k in eachindex(ret)
ret[k] = SVector{2,T}(pts[k,1],pts[k,2])
end
ret
end
points(S::TensorSpace{<:Tuple{<:Chebyshev{<:ChebyshevInterval},<:Chebyshev{<:ChebyshevInterval}}}, N) =
squarepoints(real(prectype(S)), N)
function points(S::TensorSpace{<:Tuple{<:Chebyshev,<:Chebyshev}},N)
T = real(prectype(S))
pts = squarepoints(T, N)
pts .= fromcanonical.(Ref(domain(S)), pts)
pts
end
plan_transform(S::TensorSpace{<:Tuple{<:Chebyshev,<:Chebyshev}},v::AbstractVector) =
plan_paduatransform!(v,Val{false})
transform(S::TensorSpace{<:Tuple{<:Chebyshev,<:Chebyshev}},v::AbstractVector,
plan=plan_transform(S,v)) = plan*copy(v)
plan_itransform(S::TensorSpace{<:Tuple{<:Chebyshev,<:Chebyshev}},v::AbstractVector) =
plan_ipaduatransform!(eltype(v),sum(1:Int(block(S,length(v)))),Val{false})
itransform(S::TensorSpace{<:Tuple{<:Chebyshev,<:Chebyshev}},v::AbstractVector,
plan=plan_itransform(S,v)) = plan*pad(v,sum(1:Int(block(S,length(v)))))
#TODO: adaptive
for op in (:(Base.sin),:(Base.cos))
@eval ($op)(f::ProductFun{<:Chebyshev,<:Chebyshev}) =
ProductFun(chebyshevtransform($op.(values(f))),space(f))
end
reverseorientation(f::Fun{<:Chebyshev}) =
Fun(Chebyshev(reverseorientation(domain(f))),alternatesign!(copy(f.coefficients)))
include("ChebyshevOperators.jl")