Skip to content

Commit

Permalink
add ultraspherical weight
Browse files Browse the repository at this point in the history
  • Loading branch information
daanhb committed Oct 9, 2024
1 parent e9121ce commit 25503b0
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 51 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DomainIntegrals"
uuid = "cc6bae93-f070-4015-88fd-838f9505a86c"
authors = ["Daan Huybrechs <[email protected]>"]
version = "0.5"
version = "0.5.1"

[deps]
CompositeTypes = "b152e2b5-7a66-4b01-a709-34e65c35f657"
Expand All @@ -12,18 +12,20 @@ HCubature = "19dc6840-f33b-545b-b366-655c7e3ffd49"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
julia = "1.10"
CompositeTypes = "0.1.3"
DomainSets = "0.7.2"
FastGaussQuadrature = "0.5"
GaussQuadrature = "0.5.7"
HCubature = "1.4.0"
IntervalSets = "0.7"
QuadGK = "2.3.1"
SpecialFunctions = "2"
StaticArrays = "1.0"
julia = "1.10"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
1 change: 1 addition & 0 deletions src/DomainIntegrals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module DomainIntegrals

using QuadGK, LinearAlgebra
using GaussQuadrature, FastGaussQuadrature, HCubature
using SpecialFunctions

using StaticArrays
using IntervalSets, DomainSets
Expand Down
103 changes: 79 additions & 24 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export LebesgueMeasure,
dx,
LegendreWeight,
JacobiWeight,
UltrasphericalWeight,
ChebyshevWeight,
ChebyshevTWeight,
ChebyshevUWeight,
Expand Down Expand Up @@ -103,6 +104,9 @@ struct LegendreWeight{T} <: LebesgueMeasure{T}
end
LegendreWeight() = LegendreWeight{Float64}()

jacobi_α::LegendreWeight{T}) where {T} = zero(T)
jacobi_β::LegendreWeight{T}) where {T} = zero(T)

similar::LegendreWeight, ::Type{T}) where {T <: Real} = LegendreWeight{T}()
support::LegendreWeight{T}) where {T} = ChebyshevInterval{T}()

Expand All @@ -116,12 +120,23 @@ abstract type AbstractJacobiWeight{T} <: Weight{T} end
==(μ1::AbstractJacobiWeight, μ2::AbstractJacobiWeight) =
jacobi_α(μ1) == jacobi_α(μ2) && jacobi_β(μ1) == jacobi_β(μ2)

support::AbstractJacobiWeight{T}) where {T} = ChebyshevInterval{T}()

jacobi_moment(α, β) = 2^+β+1) * gamma+1)*gamma+1) / gamma+β+2)

moment::AbstractJacobiWeight) = jacobi_moment(jacobi_α(μ), jacobi_β(μ))

isnormalized::AbstractJacobiWeight) = moment(μ) 1

"The Jacobi weight on the interval `[-1,1]`."
struct JacobiWeight{T} <: AbstractJacobiWeight{T}
α :: T
β :: T

JacobiWeight{T}= zero(T), β = zero(T)) where {T} = new(α, β)
function JacobiWeight{T}= zero(T), β = zero(T)) where {T}
@assert> -1) &&> -1)
new(α, β)
end
end
JacobiWeight() = JacobiWeight{Float64}()
JacobiWeight(α, β) = JacobiWeight(promote(α, β)...)
Expand All @@ -131,7 +146,6 @@ JacobiWeight(α::N, β::N) where {N<:Number} = JacobiWeight(float(α), float(β)
jacobi_weightfun(x, α, β) = (1-x)^α * (1+x)^β

similar::JacobiWeight, ::Type{T}) where {T <: Real} = JacobiWeight{T}.α, μ.β)
support::JacobiWeight{T}) where {T} = ChebyshevInterval{T}()
unsafe_weightfun::JacobiWeight, x) = jacobi_weightfun(x, μ.α, μ.β)

jacobi_α::JacobiWeight) = μ.α
Expand Down Expand Up @@ -189,35 +203,74 @@ Base.show(io::IO, μ::ChebyshevUWeight) = print(io, "√(1+x)^2 dx (ChebyshevU)
Display.object_parentheses::ChebyshevUWeight) = true


convert(::Type{JacobiWeight}, μ::LegendreWeight{T}) where {T} =
JacobiWeight{T}(0, 0)
convert(::Type{JacobiWeight}, μ::ChebyshevTWeight{T}) where {T} =
JacobiWeight{T}(-one(T)/2, -one(T)/2)
convert(::Type{JacobiWeight}, μ::ChebyshevUWeight{T}) where {T} =
JacobiWeight{T}(one(T)/2, one(T)/2)
"""
The `Ultraspherical` (or `Gegenbauer`) weight is the measure on `[-1,1]` with
the weight function `w(x) = (1-x^2)^(λ - 1/2)`.
"""
struct UltrasphericalWeight{T} <: DomainIntegrals.AbstractJacobiWeight{T}
λ :: T

function UltrasphericalWeight{T}(λ) where T
@assert λ > -one(T)/2
new(λ)
end
end

const GegenbauerWeight = UltrasphericalWeight

UltrasphericalWeight::T) where {T<:AbstractFloat} = UltrasphericalWeight{T}(α)
UltrasphericalWeight::N) where {N<:Number} = UltrasphericalWeight(float(α), float(β))

ultraspherical_weightfun(x, λ::T) where T = (1-x^2)^- one(T)/2)

jacobi_α::UltrasphericalWeight{T}) where {T} = μ.λ-one(T)/2
jacobi_β::UltrasphericalWeight{T}) where {T} = μ.λ-one(T)/2

similar::UltrasphericalWeight, ::Type{T}) where {T <: Real} =
UltrasphericalWeight{T}.λ)
unsafe_weightfun::UltrasphericalWeight, x) = ultraspherical_weightfun(x, μ.λ)

Base.show(io::IO, μ::UltrasphericalWeight) = print(io, "(1-x^2)^(λ - 1/2) dx (Ultraspherical)")
Display.object_parentheses::UltrasphericalWeight) = true


convert(::Type{JacobiWeight}, μ::AbstractJacobiWeight{T}) where T =
JacobiWeight{T}(jacobi_α(μ), jacobi_β(μ))
convert(::Type{JacobiWeight{T}}, μ::AbstractJacobiWeight) where T =
JacobiWeight{T}(jacobi_α(μ), jacobi_β(μ))

function convert(::Type{ChebyshevTWeight}, μ::JacobiWeight{T}) where {T}
function convert(::Type{ChebyshevTWeight}, μ::AbstractJacobiWeight{T}) where T
(jacobi_α(μ) -one(T)/2 && jacobi_β(μ) -one(T)/2) || throw(InexactError(:convert, ChebyshevTWeight, μ))
ChebyshevTWeight{T}()
end

function convert(::Type{ChebyshevUWeight}, μ::JacobiWeight{T}) where {T}
function convert(::Type{ChebyshevTWeight{T}}, μ::AbstractJacobiWeight) where T
(jacobi_α(μ) -one(T)/2 && jacobi_β(μ) -one(T)/2) || throw(InexactError(:convert, ChebyshevTWeight, μ))
ChebyshevTWeight{T}()
end
function convert(::Type{ChebyshevUWeight}, μ::AbstractJacobiWeight{T}) where T
(jacobi_α(μ) one(T)/2 && jacobi_β(μ) one(T)/2) || throw(InexactError(:convert, ChebyshevUWeight, μ))
ChebyshevUWeight{T}()
end

function convert(::Type{LegendreWeight}, μ::JacobiWeight{T}) where {T}
.α 0 && μ.β 0) || throw(InexactError(:convert, LegendreWeight, μ))
function convert(::Type{ChebyshevUWeight{T}}, μ::AbstractJacobiWeight) where T
(jacobi_α(μ) one(T)/2 && jacobi_β(μ) one(T)/2) || throw(InexactError(:convert, ChebyshevUWeight, μ))
ChebyshevUWeight{T}()
end
function convert(::Type{LegendreWeight}, μ::AbstractJacobiWeight{T}) where {T}
(jacobi_α(μ) == 0 && jacobi_β(μ) == 0) || throw(InexactError(:convert, LegendreWeight, μ))
LegendreWeight{T}()
end

jacobi_α::LegendreWeight{T}) where {T} = zero(T)
jacobi_β::LegendreWeight{T}) where {T} = zero(T)

==(μ1::AbstractJacobiWeight, μ2::LegendreWeight) =
jacobi_α(μ1) == jacobi_α(μ2) && jacobi_β(μ1) == jacobi_β(μ2)
==(μ1::LegendreWeight, μ2::AbstractJacobiWeight) =
jacobi_α(μ1) == jacobi_α(μ2) && jacobi_β(μ1) == jacobi_β(μ2)
function convert(::Type{LegendreWeight{T}}, μ::AbstractJacobiWeight) where T
(jacobi_α(μ) == 0 && jacobi_β(μ) == 0) || throw(InexactError(:convert, LegendreWeight, μ))
LegendreWeight{T}()
end
function convert(::Type{UltrasphericalWeight}, μ::AbstractJacobiWeight{T}) where T
(jacobi_α(μ) == jacobi_β(μ)) || throw(InexactError(:convert, UltrasphericalWeight, μ))
UltrasphericalWeight{T}(jacobi_α(μ)+one(T)/2)
end
function convert(::Type{UltrasphericalWeight{T}}, μ::AbstractJacobiWeight) where T
(jacobi_α(μ) == jacobi_β(μ)) || throw(InexactError(:convert, UltrasphericalWeight, μ))
UltrasphericalWeight{T}(jacobi_α(μ)+one(T)/2)
end


"The generalised Laguerre measure on the halfline `[0,∞)`."
Expand All @@ -240,6 +293,8 @@ unsafe_weightfun(μ::LaguerreWeight, x) = laguerre_weightfun(x, μ.α)

laguerre_α::LaguerreWeight) = μ.α

==(μ1::LaguerreWeight, μ2::LaguerreWeight) = laguerre_α(μ1) == laguerre_α(μ2)

Base.show(io::IO, μ::LaguerreWeight) =
laguerre_α(μ) == 0 ? print(io, "exp(-x)dx (Laguerre)") : print(io, "x^$(laguerre_α(μ))exp(-x)dx (Laguerre)")
Display.object_parentheses::LaguerreWeight) = true
Expand All @@ -262,13 +317,13 @@ Base.show(io::IO, μ::HermiteWeight) = print(io, "exp(-x^2)dx (Hermite)")
Display.object_parentheses::HermiteWeight) = true


"The Gaussian measure with weight exp(-|x|^2/2)."
"The Gaussian measure with weight 1/sqrt(2π)^(n/2)*exp(-|x|^2/2)."
struct GaussianWeight{T} <: Weight{T}
end
GaussianWeight() = GaussianWeight{Float64}()

gaussian_weightfun(x, ::Type{T} = prectype(x)) where {T} =
1/(2*convert(T, pi))^(length(x)/2) * exp(-norm(x)^2)
1/(2*T(pi))^(length(x)/2) * exp(-norm(x)^2)

similar::GaussianWeight, ::Type{T}) where {T} = GaussianWeight{T}()
isnormalized::GaussianWeight) = true
Expand Down
23 changes: 22 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
include("test_suite.jl")

using Test
using DomainSets, HCubature, LinearAlgebra, StaticArrays

using DomainIntegrals

include("test_measures.jl")
include("test_integrals.jl")
include("test_gauss.jl")

@testset "Measures" begin
test_measures()
test_discrete_measures()
end

@testset "Quadrature rules" begin
test_gauss()
end

@testset "Selected integrals" begin
test_integrals()
end
5 changes: 3 additions & 2 deletions test/test_integrals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ function test_some_integrals()
@test abs(integral(strategy, x->1, UnitDisk()) - pi) < 1e-5
end

function test_discrete_measures()
function test_integrals_with_discrete_measures()
m1 = DomainIntegrals.GenericDiscreteWeight([0.2], [0.5])
@test integral(exp, m1) == 0.5*exp(0.2)
@test integral(exp, m1) 0.5*exp(0.2)
end

function test_integrals()
test_some_integrals()
test_readme_examples()
test_integrals_with_discrete_measures()
end
22 changes: 22 additions & 0 deletions test/test_measures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ function test_measures()
@test jacobi_α(m8) == 1/2
@test jacobi_β(m8) == 1/2
@test m8 == JacobiWeight(1/2, 1/2)

m9 = JacobiWeight(0.4, 0.7)
@test support(m9) == (-1..1)
@test !isdiscrete(m9)
@test iscontinuous(m9)
@test !isnormalized(m9)
@test domaintype(m8) == Float64
@test jacobi_α(m9) == 0.4
@test jacobi_β(m9) == 0.7
@test weightfun(m9, 0.22) (1-0.22)^0.4 * (1+0.22)^0.7
@test_throws AssertionError JacobiWeight(-2, 0)
@test_throws AssertionError JacobiWeight(0, -1)

m10 = UltrasphericalWeight(0.3)
@test support(m10) == (-1..1)
@test !isdiscrete(m10)
@test iscontinuous(m10)
@test !isnormalized(m10)
@test domaintype(m10) == Float64
@test jacobi_α(m10) == 0.3-0.5
@test jacobi_β(m10) == 0.3-0.5
@test weightfun(m10, 0.22) (1-0.22^2)^(0.3-0.5)
end

function test_discrete_measures()
Expand Down
22 changes: 0 additions & 22 deletions test/test_suite.jl

This file was deleted.

2 comments on commit 25503b0

@daanhb
Copy link
Member Author

@daanhb daanhb commented on 25503b0 Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/116951

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" 25503b0c82c9e8541b2ea0782e1f1628e0c1b082
git push origin v0.5.1

Please sign in to comment.