-
Notifications
You must be signed in to change notification settings - Fork 27
Generalise Clenshaw #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Generalise Clenshaw #112
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6dd6c87
Generalise clenshaw for other array types
dlfivefifty a553fc7
Support general Clenshaw
dlfivefifty b7c327d
Add forwardrecurrence!
dlfivefifty 8d29c39
Turn on codecov
dlfivefifty f78df78
fix tests
dlfivefifty 1c95d94
Match libfasttransforms in Clenshaw
dlfivefifty a8b52f1
Add ChebyshevU special case
dlfivefifty 5aa0f57
use propogate_inbounds
dlfivefifty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ deps/build.log | |
deps/libfasttransforms.* | ||
.DS_Store | ||
deps/FastTransforms/ | ||
Manifest.toml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
|
||
""" | ||
clenshaw!(c, A, B, C, x) | ||
|
||
evaluates the orthogonal polynomial expansion with coefficients `c` at points `x`, | ||
where `A`, `B`, and `C` are `AbstractVector`s containing the recurrence coefficients | ||
as defined in DLMF, | ||
overwriting `x` with the results. | ||
""" | ||
clenshaw!(c::AbstractVector, A::AbstractVector, B::AbstractVector, C::AbstractVector, x::AbstractVector) = | ||
clenshaw!(c, A, B, C, x, Ones{eltype(x)}(length(x)), x) | ||
|
||
|
||
""" | ||
clenshaw!(c, A, B, C, x, ϕ₀, f) | ||
|
||
evaluates the orthogonal polynomial expansion with coefficients `c` at points `x`, | ||
where `A`, `B`, and `C` are `AbstractVector`s containing the recurrence coefficients | ||
as defined in DLMF and ϕ₀ is the zeroth coefficient, | ||
overwriting `f` with the results. | ||
""" | ||
function clenshaw!(c::AbstractVector, A::AbstractVector, B::AbstractVector, C::AbstractVector, x::AbstractVector, ϕ₀::AbstractVector, f::AbstractVector) | ||
f .= ϕ₀ .* clenshaw.(Ref(c), Ref(A), Ref(B), Ref(C), x) | ||
end | ||
|
||
""" | ||
clenshaw(c, A, B, C, x) | ||
|
||
evaluates the orthogonal polynomial expansion with coefficients `c` at points `x`, | ||
where `A`, `B`, and `C` are `AbstractVector`s containing the recurrence coefficients | ||
as defined in DLMF. | ||
`x` may also be a single `Number`. | ||
""" | ||
|
||
function clenshaw(c::AbstractVector, A::AbstractVector, B::AbstractVector, C::AbstractVector, x::Number) | ||
N = length(c) | ||
T = promote_type(eltype(c),eltype(A),eltype(B),eltype(C),typeof(x)) | ||
if length(A) < N || length(B) < N || length(C) < N | ||
throw(ArgumentError("A, B, C must contain at least $N entries")) | ||
end | ||
N == 0 && return zero(T) | ||
@inbounds begin | ||
bk2 = zero(T) | ||
bk1 = convert(T,c[N]) | ||
for k = N-1:-1:1 | ||
bk1,bk2 = muladd(muladd(A[k],x,B[k]),bk1,muladd(-C[k],bk2,c[k])),bk1 | ||
end | ||
end | ||
bk1 | ||
end | ||
|
||
|
||
clenshaw(c::AbstractVector, A::AbstractVector, B::AbstractVector, C::AbstractVector, x::AbstractVector) = | ||
clenshaw!(c, A, B, C, copy(x)) | ||
|
||
### | ||
# Chebyshev T special cases | ||
### | ||
|
||
""" | ||
clenshaw!(c, x) | ||
|
||
evaluates the first-kind Chebyshev (T) expansion with coefficients `c` at points `x`, | ||
overwriting `x` with the results. | ||
""" | ||
clenshaw!(c::AbstractVector, x::AbstractVector) = clenshaw!(c, x, x) | ||
|
||
|
||
""" | ||
clenshaw!(c, x, f) | ||
|
||
evaluates the first-kind Chebyshev (T) expansion with coefficients `c` at points `x`, | ||
overwriting `f` with the results. | ||
""" | ||
function clenshaw!(c::AbstractVector, x::AbstractVector, f::AbstractVector) | ||
f .= clenshaw.(Ref(c), x) | ||
end | ||
|
||
""" | ||
clenshaw(c, x) | ||
|
||
evaluates the first-kind Chebyshev (T) expansion with coefficients `c` at the points `x`. | ||
`x` may also be a single `Number`. | ||
""" | ||
function clenshaw(c::AbstractVector, x::Number) | ||
N,T = length(c),promote_type(eltype(c),typeof(x)) | ||
if N == 0 | ||
return zero(T) | ||
elseif N == 1 # avoid issues with NaN x | ||
return first(c)*one(x) | ||
end | ||
|
||
y = 2x | ||
bk1,bk2 = zero(T),zero(T) | ||
@inbounds begin | ||
for k = N:-1:2 | ||
bk1,bk2 = muladd(y,bk1,c[k]-bk2),bk1 | ||
end | ||
muladd(x,bk1,c[1]-bk2) | ||
end | ||
end | ||
|
||
clenshaw(c::AbstractVector, x::AbstractVector) = clenshaw!(c, copy(x)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using FastTransforms, Test | ||
import FastTransforms: clenshaw, clenshaw! | ||
|
||
@testset "clenshaw" begin | ||
@testset "Chebyshev" begin | ||
c = [1,2,3] | ||
cf = float(c) | ||
@test @inferred(clenshaw(c,1)) ≡ 1 + 2 + 3 | ||
@test @inferred(clenshaw(c,0)) ≡ 1 + 0 - 3 | ||
@test @inferred(clenshaw(c,0.1)) == 1 + 2*0.1 + 3*cos(2acos(0.1)) | ||
@test @inferred(clenshaw(c,[-1,0,1])) == clenshaw!(c,[-1,0,1]) == [2,-2,6] | ||
@test clenshaw(c,[-1,0,1]) isa Vector{Int} | ||
@test @inferred(clenshaw(Float64[],1)) ≡ 0.0 | ||
|
||
x = [1,0,0.1] | ||
@test @inferred(clenshaw(c,x)) ≈ @inferred(clenshaw!(c,copy(x))) ≈ | ||
@inferred(clenshaw!(c,x,similar(x))) ≈ | ||
@inferred(clenshaw(cf,x)) ≈ @inferred(clenshaw!(cf,copy(x))) ≈ | ||
@inferred(clenshaw!(cf,x,similar(x))) ≈ [6,-2,-1.74] | ||
|
||
end | ||
|
||
@testset "general" begin | ||
@testset "Chebyshev-as-general" begin | ||
c, A, B, C = [1,2,3], [1,2,2], fill(0,3), fill(1,3) | ||
cf, Af, Bf, Cf = float(c), float(A), float(B), float(C) | ||
@test @inferred(clenshaw(c, A, B, C, 1)) ≡ 6 | ||
@test @inferred(clenshaw(c, A, B, C, 0.1)) ≡ -1.74 | ||
@test @inferred(clenshaw([1,2,3], A, B, C, [-1,0,1])) == clenshaw!([1,2,3],A, B, C, [-1,0,1]) == [2,-2,6] | ||
@test clenshaw(c, A, B, C, [-1,0,1]) isa Vector{Int} | ||
@test @inferred(clenshaw(Float64[], A, B, C, 1)) ≡ 0.0 | ||
|
||
x = [1,0,0.1] | ||
@test @inferred(clenshaw(c,A,B,C,x)) ≈ @inferred(clenshaw!(c,A,B,C,copy(x))) ≈ | ||
@inferred(clenshaw!(c,A,B,C,x,one.(x),similar(x))) ≈ | ||
@inferred(clenshaw!(cf,Af,Bf,Cf,x,one.(x),similar(x))) ≈ | ||
@inferred(clenshaw([1.,2,3],A,B,C,x)) ≈ | ||
@inferred(clenshaw!([1.,2,3],A,B,C,copy(x))) ≈ [6,-2,-1.74] | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,13 @@ | ||
using FastTransforms, LinearAlgebra, Test | ||
|
||
include("specialfunctionstests.jl") | ||
|
||
include("chebyshevtests.jl") | ||
|
||
include("quadraturetests.jl") | ||
|
||
include("libfasttransformstests.jl") | ||
|
||
include("nuffttests.jl") | ||
|
||
include("fftBigFloattests.jl") | ||
|
||
include("paduatests.jl") | ||
|
||
include("gaunttests.jl") | ||
|
||
include("hermitetests.jl") | ||
|
||
include("toeplitztests.jl") | ||
include("clenshawtests.jl") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MikaelSlevinsky Do you agree that
length(C) < N
is fine? Before it seemed like it required an extra coefficient for no reason.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because of the DLMF notation for C. You don't need C[0] to get p1 because p_{-1} == 0, but Clenshaw needs C[n] but only A[n-1] and B[n-1] (using C array indexing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could get away with pointing to the right place, but I thought an extra entry to match notation would be reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that makes sense to require allocating an extra entry that is never used. I guess we can do
pointer(c)-sizeof(T)
to work around this?