Skip to content

Commit 1698661

Browse files
committed
Recurrence coefficients
1 parent b739598 commit 1698661

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

src/ClassicalOrthogonalPolynomials.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ function gaussradau(P::Monic{T}, n::Integer, endpt) where {T}
325325
J = jacobimatrix(P.P, n + 1)
326326
α, β = diagonaldata(J), supdiagonaldata(J)
327327
endpt = T(endpt)
328-
p0 = P[endpt, n] # πₙ₋₁
329-
p1 = P[endpt, n+1] # πₙ. Could be faster by computing p1 from p0 and πₙ₋₂, but the cost is tiny relative to eigen()
328+
p0, p1 = P[endpt,n:n+1]
330329
a = (endpt - β[end]^2 * p0 / p1)::T
331330
α′ = vcat(@view(α[begin:end-1]), a)
332331
J′ = SymTridiagonal(α′, β)
@@ -347,8 +346,8 @@ function gausslobatto(P::Monic{T}, n::Integer) where {T}
347346
a, b = axes(P, 1)[begin], axes(P, 1)[end]
348347
J = jacobimatrix(P.P, n + 2)
349348
α, β = diagonaldata(J), supdiagonaldata(J)
350-
p0a, p0b = P[a, n+1], P[b, n+1]
351-
p1a, p1b = P[a, n+2], P[b, n+2]
349+
p0a, p0b = P[a, (n+1):(n+2)]
350+
p1a, p1b = P[b, (n+1):(n+2)]
352351
# Solve Eq. 3.1.2.8
353352
Δ = p1a * p0b - p1b * p0a # This could underflow/overflow for large n
354353
aext = (a * p1a * p0b - b * p1b * p0a) / Δ

src/monic.jl

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
struct Monic{T,OPs<:AbstractQuasiMatrix{T},NL} <: OrthogonalPolynomial{T}
22
P::Normalized{T,OPs,NL}
3-
# X::AbstractMatrix{T} # Should X be stored? Probably not.
4-
# X would be the Jacobi matrix of the normalised polynomials, not for the monic polynomials.
3+
α::AbstractVector{T} # diagonal of Jacobi matrix of P
4+
β::AbstractVector{T} # squared supdiagonal of Jacobi matrix of P
55
end
6-
# Will need to figure out what this should be exactly.
7-
# Consider this internal for now until it stabilises.
86

97
Monic(P::AbstractQuasiMatrix) = Monic(Normalized(P))
10-
Monic(P::Monic) = P
8+
function Monic(P::Normalized)
9+
X = jacobimatrix(P)
10+
α = diagonaldata(X)
11+
β = supdiagonaldata(X)
12+
return Monic(P, α, β.^2)
13+
end
14+
Monic(P::Monic) = Monic(P.P, P.α, P.β)
1115

1216
Normalized(P::Monic) = P.P
1317

@@ -20,23 +24,15 @@ _p0(::Monic{T}) where {T} = one(T)
2024
show(io::IO, P::Monic) = print(io, "Monic($(P.P.P))")
2125
show(io::IO, ::MIME"text/plain", P::Monic) = show(io, P)
2226

23-
function getindex(P::Monic{T}, x::Number, n::Int)::T where {T}
24-
# TODO: Rewrite this to be more efficient using forwardrecurrence!
25-
p0 = _p0(P)
26-
n == 1 && return p0
27-
t = convert(T, x)
28-
J = jacobimatrix(P.P, n)
29-
α = diagonaldata(J)
30-
β = supdiagonaldata(J)
31-
p1 = ((t - α[1]) * p0)::T
32-
n == 2 && return p1
33-
for i in 2:(n-1)
34-
_p1 = p0::T
35-
p0 = p1::T
36-
p1 = ((t - α[i]) * p1 - β[i-1]^2 * _p1)::T
37-
end
38-
return p1
27+
function recurrencecoefficients(P::Monic{T}) where {T}
28+
α = P.α
29+
β = P.β
30+
return _monicrecurrencecoefficients(α, β) # function barrier
31+
end
32+
function _monicrecurrencecoefficients::AbstractVector{T}, β) where {T}
33+
A = Ones{T}(∞)
34+
B = -α
35+
C = Vcat(zero(T), β)
36+
return A, B, C
3937
end
40-
# Should a method be written that makes this more efficient when requesting multiple n?
41-
4238

test/test_monic.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using ClassicalOrthogonalPolynomials
22
using Test
3-
using ClassicalOrthogonalPolynomials: Monic, _p0, orthogonalityweight
3+
using ClassicalOrthogonalPolynomials: Monic, _p0, orthogonalityweight, recurrencecoefficients
44

55
@testset "Basic definition" begin
66
P1 = Legendre()
@@ -15,7 +15,7 @@ using ClassicalOrthogonalPolynomials: Monic, _p0, orthogonalityweight
1515
@test sprint(show, MIME"text/plain"(), P3) == "Monic(Legendre())"
1616
end
1717

18-
@testset "getindex" begin
18+
@testset "evaluation" begin
1919
function _pochhammer(x, n)
2020
y = one(x)
2121
for i in 0:(n-1)
@@ -53,6 +53,6 @@ end
5353
@test Q[0.17, 3] _P(0.17, 2)
5454
@test Q[0.4, 17] _P(0.4, 16)
5555
@test Q[0.9, 21] _P(0.9, 20)
56-
@inferred Q[0.4, 5]
56+
# @inferred Q[0.2, 5] # no longer inferred
5757
end
5858
end

0 commit comments

Comments
 (0)