Skip to content

Commit 416cd46

Browse files
authored
Add one-based assertions in ultraspherical functions (#278)
1 parent c7c2038 commit 416cd46

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

src/ultraspherical.jl

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ export ultraconversion!,ultraint!
44

55
# diff from T -> U
66
function ultradiff(v::AbstractVector{T}) where T<:Number
7+
Base.require_one_based_indexing(v)
78
#polynomial is p(x) = sum ( v[i] * x^(i-1) )
89
if length(v)1
910
w = zeros(T,1)
1011
else
1112
w = Array{T}(undef, length(v)-1)
12-
for k=1:length(v)-1
13+
for k in eachindex(w)
1314
@inbounds w[k] = k*v[k+1]
1415
end
1516
end
@@ -21,13 +22,13 @@ end
2122

2223
#TODO: what about missing truncation?
2324
function ultraint!(v::AbstractMatrix{T}) where T<:Number
24-
for k=size(v,1):-1:2
25-
for j=1:size(v,2)
25+
for j in axes(v,2)
26+
for k in reverse(axes(v,1)[firstindex(v,1)+1:end])
2627
@inbounds v[k,j] = v[k-1,j]/(k-1)
2728
end
2829
end
2930

30-
@simd for j=1:size(v)[2]
31+
@simd for j in axes(v,2)
3132
@inbounds v[1,j] = zero(T)
3233
end
3334

@@ -36,17 +37,18 @@ end
3637

3738
function ultraint!(v::AbstractVector{T}) where T<:Number
3839
resize!(v,length(v)+1)
39-
@simd for k=length(v):-1:2
40+
@simd for k in reverse(eachindex(v)[firstindex(v)+1:end])
4041
@inbounds v[k] = v[k-1]/(k-1)
4142
end
4243

43-
@inbounds v[1] = zero(T)
44+
@inbounds v[firstindex(v)] = zero(T)
4445

4546
v
4647
end
4748

4849
# Convert from U -> T
4950
function ultraiconversion(v::AbstractVector{T}) where T<:Number
51+
Base.require_one_based_indexing(v)
5052
n = length(v)
5153
w = Array{T}(undef, n)
5254

@@ -71,37 +73,17 @@ end
7173

7274

7375
# Convert T -> U
74-
function ultraconversion(v::AbstractVector{T}) where T<:Number
75-
n = length(v)
76-
w = Array{T}(undef, n)
77-
78-
if n == 1
79-
w[1] = v[1]
80-
elseif n == 2
81-
w[1] = v[1]
82-
w[2] = .5v[2]
83-
elseif n 3
84-
w[1] = v[1] - .5v[3]
85-
86-
@simd for j=2:n-2
87-
@inbounds w[j] = .5*(v[j] - v[j+2])
88-
end
89-
90-
w[n-1] = .5v[n-1]
91-
w[n] = .5v[n]
92-
end
93-
94-
w
76+
function ultraconversion(v::AbstractVector{<:Number})
77+
ultraconversion!(float.(v))
9578
end
9679

97-
function ultraconversion!(v::AbstractVector{T}) where T<:Number
80+
function ultraconversion!(v::AbstractVector{<:Number})
81+
Base.require_one_based_indexing(v)
9882
n = length(v) #number of coefficients
9983

100-
if n 1
101-
#do nothing
102-
elseif n == 2
84+
if n == 2
10385
@inbounds v[2] /= 2
104-
else
86+
elseif n > 2
10587
@inbounds v[1] -= v[3]/2
10688

10789
for j=2:n-2
@@ -115,6 +97,7 @@ function ultraconversion!(v::AbstractVector{T}) where T<:Number
11597
end
11698

11799
function ultraconversion!(v::AbstractMatrix{T}) where T<:Number
100+
Base.require_one_based_indexing(v)
118101
n = size(v)[1] #number of coefficients
119102
m = size(v)[2] #number of funs
120103

0 commit comments

Comments
 (0)