Skip to content

Commit b4c84f0

Browse files
authored
Slightly reduce allocations in chebyshev Fun integrate (#279)
1 parent 416cd46 commit b4c84f0

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/Spaces/Chebyshev/Chebyshev.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ clenshaw(c::AbstractVector,x::AbstractVector,plan::ClenshawPlan{S,V}) where {S<:
100100
clenshaw(c::AbstractVector,x::Vector,plan::ClenshawPlan{<:Chebyshev}) = chebyshev_clenshaw(c,x,plan)
101101

102102
function clenshaw(c::AbstractMatrix{T},x::T,plan::ClenshawPlan{S,T}) where {S<:Chebyshev,T<:Number}
103+
Base.require_one_based_indexing(x, c)
103104
bk=plan.bk
104105
bk1=plan.bk1
105106
bk2=plan.bk2
@@ -130,6 +131,7 @@ function clenshaw(c::AbstractMatrix{T},x::T,plan::ClenshawPlan{S,T}) where {S<:C
130131
end
131132

132133
function clenshaw(c::AbstractMatrix{T},x::AbstractVector{T},plan::ClenshawPlan{S,T}) where {S<:Chebyshev,T<:Number}
134+
Base.require_one_based_indexing(x, c)
133135
bk=plan.bk
134136
bk1=plan.bk1
135137
bk2=plan.bk2
@@ -163,6 +165,7 @@ end
163165
# overwrite x
164166

165167
function clenshaw!(c::AbstractVector,x::AbstractVector,plan::ClenshawPlan{S,V}) where {S<:Chebyshev,V}
168+
Base.require_one_based_indexing(x, c)
166169
N,n = length(c),length(x)
167170

168171
if isempty(c)
@@ -201,11 +204,19 @@ end
201204

202205

203206
# diff T -> U, then convert U -> T
204-
integrate(f::Fun{Chebyshev{D,R}}) where {D<:IntervalOrSegment,R} =
205-
Fun(f.space,fromcanonicalD(f,0)*ultraint!(ultraconversion(f.coefficients)))
206-
differentiate(f::Fun{Chebyshev{D,R}}) where {D<:IntervalOrSegment,R} =
207-
Fun(f.space,1/fromcanonicalD(f,0)*ultraiconversion(ultradiff(f.coefficients)))
208-
207+
function integrate(f::Fun{Chebyshev{D,R}}) where {D<:IntervalOrSegment,R}
208+
cfs = coefficients(f)
209+
z = fromcanonicalD(f,0)
210+
v = z .* float.(cfs)
211+
ultraint!(ultraconversion!(v))
212+
Fun(f.space, v)
213+
end
214+
function differentiate(f::Fun{Chebyshev{D,R}}) where {D<:IntervalOrSegment,R}
215+
cfs = coefficients(f)
216+
z = fromcanonicalD(f,0)
217+
v = ultraiconversion(ultradiff(cfs)) ./ z
218+
Fun(f.space, v)
219+
end
209220

210221

211222

@@ -220,7 +231,7 @@ function squarepoints(::Type{T}, N) where T
220231
pts = paduapoints(T, _padua_length(N))
221232
n = size(pts,1)
222233
ret = Array{SVector{2,T}}(undef, n)
223-
@inbounds for k=1:n
234+
@inbounds for k in eachindex(ret)
224235
ret[k] = SVector{2,T}(pts[k,1],pts[k,2])
225236
end
226237
ret

0 commit comments

Comments
 (0)