|
| 1 | +using ClassicalOrthogonalPolynomials, FastGaussQuadrature |
| 2 | +const COP = ClassicalOrthogonalPolynomials |
| 3 | +const FGQ = FastGaussQuadrature |
| 4 | +using Test |
| 5 | +using ClassicalOrthogonalPolynomials: symtridiagonalize |
| 6 | +using LinearAlgebra |
| 7 | + |
| 8 | +@testset "gaussradau" begin |
| 9 | + @testset "Compare with FastGaussQuadrature" begin |
| 10 | + x1, w1 = COP.gaussradau(Legendre(), 5, -1.0) |
| 11 | + x2, w2 = FGQ.gaussradau(6) |
| 12 | + @test x1 ≈ x2 && w1 ≈ w2 |
| 13 | + @test x1[1] == -1 |
| 14 | + |
| 15 | + x1, w1 = COP.gaussradau(Jacobi(1.0, 3.5), 25, -1.0) |
| 16 | + x2, w2 = FGQ.gaussradau(26, 1.0, 3.5) |
| 17 | + @test x1 ≈ x2 && w1 ≈ w2 |
| 18 | + @test x1[1] == -1 |
| 19 | + |
| 20 | + I0, I1 = COP.ChebyshevInterval(), COP.UnitInterval() |
| 21 | + P = Jacobi(2.0, 0.0)[COP.affine(I1, I0), :] |
| 22 | + x1, w1 = COP.gaussradau(P, 18, 0.0) |
| 23 | + x2, w2 = FGQ.gaussradau(19, 2.0, 0.0) |
| 24 | + @test 2x1 .- 1 ≈ x2 && 2w1 ≈ w2 |
| 25 | + |
| 26 | + x1, w1 = COP.gaussradau(Jacobi(1 / 2, 1 / 2), 4, 1.0) |
| 27 | + x2, w2 = FGQ.gaussradau(5, 1 / 2, 1 / 2) |
| 28 | + @test sort(-x1) ≈ x2 |
| 29 | + @test_broken w1 ≈ w2 # What happens to the weights when inverting the interval? |
| 30 | + end |
| 31 | + |
| 32 | + @testset "Example 3.5 in Gautschi (2004)'s book" begin |
| 33 | + P = Laguerre(3.0) |
| 34 | + n = 5 |
| 35 | + J = symtridiagonalize(jacobimatrix(P))[1:(n-1), 1:(n-1)] |
| 36 | + _J = zeros(n, n) |
| 37 | + _J[1:n-1, 1:n-1] .= J |
| 38 | + _J[n-1, n] = sqrt((n - 1) * (n - 1 + P.α)) |
| 39 | + _J[n, n-1] = _J[n-1, n] |
| 40 | + _J[n, n] = n - 1 |
| 41 | + x, V = eigen(_J) |
| 42 | + w = 6V[1, :] .^ 2 |
| 43 | + xx, ww = COP.gaussradau(P, n - 1, 0.0) |
| 44 | + @test xx ≈ x && ww ≈ w |
| 45 | + end |
| 46 | + |
| 47 | + @testset "Some numerical integration" begin |
| 48 | + f = x -> 2x + 7x^2 + 10x^3 + exp(-x) |
| 49 | + x, w = COP.gaussradau(Chebyshev(), 10, -1.0) |
| 50 | + @test dot(f.(x), w) ≈ 14.97303754807069897 # integral of (2x + 7x^2 + 10x^3 + exp(-x))/sqrt(1-x^2) |
| 51 | + @test x[1] == -1 |
| 52 | + |
| 53 | + f = x -> -1.0 + 5x^6 |
| 54 | + x, w = COP.gaussradau(Jacobi(-1/2, -1/2), 2, 1.0) |
| 55 | + @test dot(f.(x), w) ≈ 9π/16 |
| 56 | + @test x[end] == 1 |
| 57 | + @test length(x) == 3 |
| 58 | + end |
| 59 | +end |
| 60 | + |
| 61 | +@testset "gausslobatto" begin |
| 62 | + @testset "Compare with FastGaussQuadrature" begin |
| 63 | + x1, w1 = COP.gausslobatto(Legendre(), 5) |
| 64 | + x2, w2 = FGQ.gausslobatto(7) |
| 65 | + @test x1 ≈ x2 && w1 ≈ w2 |
| 66 | + @test x1[1] == -1 |
| 67 | + @test x1[end] == 1 |
| 68 | + |
| 69 | + I0, I1 = COP.ChebyshevInterval(), COP.UnitInterval() |
| 70 | + P = Legendre()[COP.affine(I1, I0), :] |
| 71 | + x1, w1 = COP.gausslobatto(P, 18) |
| 72 | + x2, w2 = FGQ.gausslobatto(20) |
| 73 | + @test 2x1 .- 1 ≈ x2 && 2w1 ≈ w2 |
| 74 | + end |
| 75 | + |
| 76 | + @testset "Some numerical integration" begin |
| 77 | + f = x -> 2x + 7x^2 + 10x^3 + exp(-x) |
| 78 | + x, w = COP.gausslobatto(Chebyshev(), 10) |
| 79 | + @test dot(f.(x), w) ≈ 14.97303754807069897 |
| 80 | + @test x[1] == -1 |
| 81 | + @test x[end] == 1 |
| 82 | + @test length(x) == 12 |
| 83 | + |
| 84 | + f = x -> -1.0 + 5x^6 |
| 85 | + x, w = COP.gausslobatto(Jacobi(-1/2, -1/2), 4) |
| 86 | + @test dot(f.(x), w) ≈ 9π/16 |
| 87 | + @test x[1]==-1 |
| 88 | + @test x[end] == 1 |
| 89 | + @test length(x) == 6 |
| 90 | + end |
| 91 | +end |
0 commit comments