-
Notifications
You must be signed in to change notification settings - Fork 9
Implement gausslobatto / gaussradau and Monic
#199
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e12006c
radau/lobatto
DanielVandH d6f9375
Take the first iteration out of the loop to avoid if
DanielVandH 27fc2e2
Implement monic
DanielVandH b739598
typo
DanielVandH 1698661
Recurrence coefficients
DanielVandH 251e691
Doesn't really do much
DanielVandH dcb37b7
Typo
DanielVandH 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "ClassicalOrthogonalPolynomials" | ||
uuid = "b30e2e7b-c4ee-47da-9d5f-2c5c27239acd" | ||
authors = ["Sheehan Olver <[email protected]>"] | ||
version = "0.13.4" | ||
version = "0.13.5" | ||
|
||
[deps] | ||
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" | ||
|
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,42 @@ | ||
struct Monic{T,OPs<:AbstractQuasiMatrix{T},NL} <: OrthogonalPolynomial{T} | ||
P::Normalized{T,OPs,NL} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this actually benefit from requiring normalized OPs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes the code for getting the recurrence coefficients simpler I think |
||
# X::AbstractMatrix{T} # Should X be stored? Probably not. | ||
# X would be the Jacobi matrix of the normalised polynomials, not for the monic polynomials. | ||
end | ||
# Will need to figure out what this should be exactly. | ||
# Consider this internal for now until it stabilises. | ||
|
||
Monic(P::AbstractQuasiMatrix) = Monic(Normalized(P)) | ||
Monic(P::Monic) = P | ||
|
||
Normalized(P::Monic) = P.P | ||
|
||
axes(P::Monic) = axes(P.P) | ||
|
||
orthogonalityweight(P::Monic) = orthogonalityweight(P.P) | ||
|
||
_p0(::Monic{T}) where {T} = one(T) | ||
|
||
show(io::IO, P::Monic) = print(io, "Monic($(P.P.P))") | ||
show(io::IO, ::MIME"text/plain", P::Monic) = show(io, P) | ||
|
||
function getindex(P::Monic{T}, x::Number, n::Int)::T where {T} | ||
DanielVandH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# TODO: Rewrite this to be more efficient using forwardrecurrence! | ||
p0 = _p0(P) | ||
n == 1 && return p0 | ||
t = convert(T, x) | ||
J = jacobimatrix(P.P, n) | ||
α = diagonaldata(J) | ||
β = supdiagonaldata(J) | ||
p1 = ((t - α[1]) * p0)::T | ||
n == 2 && return p1 | ||
for i in 2:(n-1) | ||
_p1 = p0::T | ||
p0 = p1::T | ||
p1 = ((t - α[i]) * p1 - β[i-1]^2 * _p1)::T | ||
end | ||
return p1 | ||
end | ||
# Should a method be written that makes this more efficient when requesting multiple n? | ||
|
||
|
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,91 @@ | ||
using ClassicalOrthogonalPolynomials, FastGaussQuadrature | ||
const COP = ClassicalOrthogonalPolynomials | ||
const FGQ = FastGaussQuadrature | ||
using Test | ||
using ClassicalOrthogonalPolynomials: symtridiagonalize | ||
using LinearAlgebra | ||
|
||
@testset "gaussradau" begin | ||
@testset "Compare with FastGaussQuadrature" begin | ||
x1, w1 = COP.gaussradau(Legendre(), 5, -1.0) | ||
x2, w2 = FGQ.gaussradau(6) | ||
@test x1 ≈ x2 && w1 ≈ w2 | ||
@test x1[1] == -1 | ||
|
||
x1, w1 = COP.gaussradau(Jacobi(1.0, 3.5), 25, -1.0) | ||
x2, w2 = FGQ.gaussradau(26, 1.0, 3.5) | ||
@test x1 ≈ x2 && w1 ≈ w2 | ||
@test x1[1] == -1 | ||
|
||
I0, I1 = COP.ChebyshevInterval(), COP.UnitInterval() | ||
P = Jacobi(2.0, 0.0)[COP.affine(I1, I0), :] | ||
x1, w1 = COP.gaussradau(P, 18, 0.0) | ||
x2, w2 = FGQ.gaussradau(19, 2.0, 0.0) | ||
@test 2x1 .- 1 ≈ x2 && 2w1 ≈ w2 | ||
|
||
x1, w1 = COP.gaussradau(Jacobi(1 / 2, 1 / 2), 4, 1.0) | ||
x2, w2 = FGQ.gaussradau(5, 1 / 2, 1 / 2) | ||
@test sort(-x1) ≈ x2 | ||
@test_broken w1 ≈ w2 # What happens to the weights when inverting the interval? | ||
end | ||
|
||
@testset "Example 3.5 in Gautschi (2004)'s book" begin | ||
P = Laguerre(3.0) | ||
n = 5 | ||
J = symtridiagonalize(jacobimatrix(P))[1:(n-1), 1:(n-1)] | ||
_J = zeros(n, n) | ||
_J[1:n-1, 1:n-1] .= J | ||
_J[n-1, n] = sqrt((n - 1) * (n - 1 + P.α)) | ||
_J[n, n-1] = _J[n-1, n] | ||
_J[n, n] = n - 1 | ||
x, V = eigen(_J) | ||
w = 6V[1, :] .^ 2 | ||
xx, ww = COP.gaussradau(P, n - 1, 0.0) | ||
@test xx ≈ x && ww ≈ w | ||
end | ||
|
||
@testset "Some numerical integration" begin | ||
f = x -> 2x + 7x^2 + 10x^3 + exp(-x) | ||
x, w = COP.gaussradau(Chebyshev(), 10, -1.0) | ||
@test dot(f.(x), w) ≈ 14.97303754807069897 # integral of (2x + 7x^2 + 10x^3 + exp(-x))/sqrt(1-x^2) | ||
@test x[1] == -1 | ||
|
||
f = x -> -1.0 + 5x^6 | ||
x, w = COP.gaussradau(Jacobi(-1/2, -1/2), 2, 1.0) | ||
@test dot(f.(x), w) ≈ 9π/16 | ||
@test x[end] == 1 | ||
@test length(x) == 3 | ||
end | ||
end | ||
|
||
@testset "gausslobatto" begin | ||
@testset "Compare with FastGaussQuadrature" begin | ||
x1, w1 = COP.gausslobatto(Legendre(), 5) | ||
x2, w2 = FGQ.gausslobatto(7) | ||
@test x1 ≈ x2 && w1 ≈ w2 | ||
@test x1[1] == -1 | ||
@test x1[end] == 1 | ||
|
||
I0, I1 = COP.ChebyshevInterval(), COP.UnitInterval() | ||
P = Legendre()[COP.affine(I1, I0), :] | ||
x1, w1 = COP.gausslobatto(P, 18) | ||
x2, w2 = FGQ.gausslobatto(20) | ||
@test 2x1 .- 1 ≈ x2 && 2w1 ≈ w2 | ||
end | ||
|
||
@testset "Some numerical integration" begin | ||
f = x -> 2x + 7x^2 + 10x^3 + exp(-x) | ||
x, w = COP.gausslobatto(Chebyshev(), 10) | ||
@test dot(f.(x), w) ≈ 14.97303754807069897 | ||
@test x[1] == -1 | ||
@test x[end] == 1 | ||
@test length(x) == 12 | ||
|
||
f = x -> -1.0 + 5x^6 | ||
x, w = COP.gausslobatto(Jacobi(-1/2, -1/2), 4) | ||
@test dot(f.(x), w) ≈ 9π/16 | ||
@test x[1]==-1 | ||
@test x[end] == 1 | ||
@test length(x) == 6 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using ClassicalOrthogonalPolynomials | ||
using Test | ||
using ClassicalOrthogonalPolynomials: Monic, _p0, orthogonalityweight | ||
|
||
@testset "Basic definition" begin | ||
P1 = Legendre() | ||
P2 = Normalized(P1) | ||
P3 = Monic(P1) | ||
@test P3.P == P2 | ||
@test Monic(P3) === P3 | ||
@test axes(P3) == axes(Legendre()) | ||
@test Normalized(P3) === P3.P | ||
@test _p0(P3) == 1 | ||
@test orthogonalityweight(P3) == orthogonalityweight(P1) | ||
@test sprint(show, MIME"text/plain"(), P3) == "Monic(Legendre())" | ||
end | ||
|
||
@testset "getindex" begin | ||
function _pochhammer(x, n) | ||
y = one(x) | ||
for i in 0:(n-1) | ||
y *= (x + i) | ||
end | ||
return y | ||
end | ||
jacobi_kn = (α, β, n) -> _pochhammer(n + α + β + 1, n) / (2.0^n * factorial(n)) | ||
ultra_kn = (λ, n) -> 2^n * _pochhammer(λ, n) / factorial(n) | ||
chebt_kn = n -> n == 0 ? 1.0 : 2.0 .^ (n - 1) | ||
chebu_kn = n -> 2.0^n | ||
leg_kn = n -> 2.0^n * _pochhammer(1 / 2, n) / factorial(n) | ||
lag_kn = n -> (-1)^n / factorial(n) | ||
herm_kn = n -> 2.0^n | ||
_Jacobi(α, β, x, n) = Jacobi(α, β)[x, n+1] / jacobi_kn(α, β, n) | ||
_Ultraspherical(λ, x, n) = Ultraspherical(λ)[x, n+1] / ultra_kn(λ, n) | ||
_ChebyshevT(x, n) = ChebyshevT()[x, n+1] / chebt_kn(n) | ||
_ChebyshevU(x, n) = ChebyshevU()[x, n+1] / chebu_kn(n) | ||
_Legendre(x, n) = Legendre()[x, n+1] / leg_kn(n) | ||
_Laguerre(α, x, n) = Laguerre(α)[x, n+1] / lag_kn(n) | ||
_Hermite(x, n) = Hermite()[x, n+1] / herm_kn(n) | ||
Ps = [ | ||
Jacobi(2.0, 5.0) (x, n)->_Jacobi(2.0, 5.0, x, n) | ||
Ultraspherical(1.7) (x, n)->_Ultraspherical(1.7, x, n) | ||
ChebyshevT() _ChebyshevT | ||
ChebyshevU() _ChebyshevU | ||
Legendre() _Legendre | ||
Laguerre(1.5) (x, n)->_Laguerre(1.5, x, n) | ||
Hermite() _Hermite | ||
] | ||
for (P, _P) in eachrow(Ps) | ||
Q = Monic(P) | ||
@test Q[0.2, 1] == 1.0 | ||
@test Q[0.25, 2] ≈ _P(0.25, 1) | ||
@test Q[0.17, 3] ≈ _P(0.17, 2) | ||
@test Q[0.4, 17] ≈ _P(0.4, 16) | ||
@test Q[0.9, 21] ≈ _P(0.9, 20) | ||
@inferred Q[0.4, 5] | ||
end | ||
end |
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.
Uh oh!
There was an error while loading. Please reload this page.