Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed May 14, 2024
1 parent f491908 commit ed03cca
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 260 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ jobs:
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- name: dev
shell: julia --project=@. {0}
run: |
using Pkg
Pkg.add([
PackageSpec(name="StarAlgebras", rev="mk/non_monomial_basis"),
])
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
with:
Expand Down
5 changes: 3 additions & 2 deletions src/MultivariateBases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export maxdegree_basis, basis_covering_monomials, empty_basis
include("interface.jl")

export AbstractMonomialBasis, MonomialBasis, ScaledMonomialBasis
include("polynomial.jl")
include("monomial.jl")
include("scaled.jl")

Expand All @@ -29,10 +30,10 @@ export generators,
reccurence_second_coef,
reccurence_third_coef,
reccurence_deno_coef
include("fixed.jl")
#include("fixed.jl")

import LinearAlgebra
include("orthonormal.jl")
#include("orthonormal.jl")
include("orthogonal.jl")
include("hermite.jl")
include("laguerre.jl")
Expand Down
134 changes: 19 additions & 115 deletions src/chebyshev.jl
Original file line number Diff line number Diff line change
@@ -1,95 +1,26 @@
# TODO Add to MultivariatePolynomials
Base.keytype(p::MP.AbstractPolynomial) = MP.monomial_type(p)
Base.valtype(p::MP.AbstractPolynomial) = MP.coefficient_type(p)
#Base.keys(p::MP.AbstractPolynomial) = MP.monomials(p)
Base.pairs(p::MP.AbstractPolynomial) = MP.terms(p)
function Base.similar(p::PT, ::Type{T}) where {PT<:MP.AbstractPolynomial,T}
return convert(MP.similar_type(PT, T), copy(p)) # Missing the `copy` in MP
end
Base.iterate(t::MP.Term) = iterate(t, 1)
function Base.iterate(t::MP.Term, state)
if state == 1
return MP.monomial(t), 2
elseif state == 2
return MP.coefficient(t), 3
else
return nothing
end
end
function MA.operate!(
::SA.UnsafeAddMul{typeof(*)},
mc::MP.AbstractPolynomial,
val,
c::MP.AbstractPolynomial,
)
return MA.operate!(MA.add_mul, mc, val, c)
end

function SA.__canonicalize!(::MP.AbstractPolynomial) end

struct Polynomial{B,M<:MP.AbstractMonomial}
monomial::M
function Polynomial{B}(mono::MP.AbstractMonomial) where {B}
return new{B,typeof(mono)}(mono)
end
end
export ChebyshevFirstKind, Chebyshev

function Polynomial{B}(v::MP.AbstractVariable) where {B}
return Polynomial{B}(MP.monomial(v))
end

function _algebra_element(p, ::Type{B}) where {B}
basis = MonomialIndexedBasis{B,MP.monomial_type(p)}()
return SA.AlgebraElement(
p,
SA.StarAlgebra(
Polynomial{B}(MP.constant_monomial(p)),
basis,
),
)
end

function Base.:*(a::Polynomial{B}, b::Polynomial{B}) where {B}
_algebra_element(Mul{B}()(a.monomial, b.monomial), B)
end
abstract type AbstractChebyshev <: AbstractGegenbauer end

function _show(io::IO, mime::MIME, p::Polynomial{B}) where {B}
print(io, B)
print(io, "(")
show(io, mime, p.monomial)
print(io, ")")
end
function Base.show(io::IO, mime::MIME"text/plain", p::Polynomial)
_show(io, mime, p)
end
function Base.show(io::IO, p::Polynomial)
show(io, MIME"text/plain"(), p)
end

struct MonomialIndexedBasis{B,M<:MP.AbstractMonomial} <: SA.ImplicitBasis{Polynomial{B,M},M} end
function Base.getindex(::MonomialIndexedBasis{B,M}, mono::M) where {B,M}
return Polynomial{B}(mono)
end
function Base.getindex(::MonomialIndexedBasis{B,M}, p::Polynomial{B,M}) where {B,M}
return mono
end
SA.mstructure(::MonomialIndexedBasis{B}) where {B} = Mul{B}()
_promote_div(::Type{I}) where {I<:Integer} = Rational{I}
_promote_div(::Type{T}) where {T} = MA.promote_operation(/, T, Int)

function Base.zero(::Type{Polynomial{B,M}}) where {B,M}
return _algebra_element(zero(MP.polynomial_type(M, Rational{Int})), B)
function MP.polynomial_type(::Type{<:AbstractChebyshev}, V::Type)
return MP.polynomial_type(V, _promote_div(MP.coefficient_type(V)))
end

Base.zero(p::Polynomial) = zero(typeof(p))

struct Mul{B} <: SA.MultiplicativeStructure end
reccurence_first_coef(::Type{<:AbstractChebyshev}, degree) = 2
reccurence_third_coef(::Type{<:AbstractChebyshev}, degree) = -1
reccurence_deno_coef(::Type{<:AbstractChebyshev}, degree) = 1

export ChebyshevFirstKind, ChebyshevFirstKindBasis
"""
struct ChebyshevFirstKind <: AbstractChebyshev end
Orthogonal polynomial with respect to the univariate weight function ``w(x) = \\frac{1}{\\sqrt{1 - x^2}}`` over the interval ``[-1, 1]``.
"""
struct ChebyshevFirstKind end
const Chebyshev = ChebyshevFirstKind

export Chebyshev

# https://en.wikipedia.org/wiki/Chebyshev_polynomials#Properties
# T_n * T_m = T_{n + m} / 2 + T_{|n - m|} / 2
function (::Mul{Chebyshev})(a::MP.AbstractMonomial, b::MP.AbstractMonomial)
Expand Down Expand Up @@ -126,37 +57,14 @@ function (::Mul{Chebyshev})(a::MP.AbstractMonomial, b::MP.AbstractMonomial)
return MP.polynomial!(terms)
end

abstract type AbstractChebyshevBasis{P} <: AbstractGegenbauerBasis{P} end

function MP.polynomial_type(::Type{<:AbstractChebyshevBasis}, V::Type)
return MP.polynomial_type(V, Float64)
end

reccurence_first_coef(::Type{<:AbstractChebyshevBasis}, degree) = 2
reccurence_third_coef(::Type{<:AbstractChebyshevBasis}, degree) = -1
reccurence_deno_coef(::Type{<:AbstractChebyshevBasis}, degree) = 1

"""
struct ChebyshevBasisFirstKind{P} <: AbstractChebyshevBasis{P}
polynomials::Vector{P}
end
Orthogonal polynomial with respect to the univariate weight function ``w(x) = \\frac{1}{\\sqrt{1 - x^2}}`` over the interval ``[-1, 1]``.
"""
struct ChebyshevBasisFirstKind{P} <: AbstractChebyshevBasis{P}
polynomials::Vector{P}
end

const ChebyshevBasis{P} = ChebyshevBasisFirstKind{P}

function degree_one_univariate_polynomial(
::Type{<:ChebyshevBasisFirstKind},
::Type{Chebyshev},
variable::MP.AbstractVariable,
)
MA.@rewrite(variable + 0)
end

function _scalar_product_function(::Type{<:ChebyshevBasisFirstKind}, i::Int)
function _scalar_product_function(::Type{Chebyshev}, i::Int)
if i == 0
return π
elseif isodd(i)
Expand All @@ -168,24 +76,20 @@ function _scalar_product_function(::Type{<:ChebyshevBasisFirstKind}, i::Int)
end

"""
struct ChebyshevBasisSecondKind{P} <: AbstractChebyshevBasis{P}
polynomials::Vector{P}
end
struct ChebyshevSecondKind <: AbstractChebyshevBasis end
Orthogonal polynomial with respect to the univariate weight function ``w(x) = \\sqrt{1 - x^2}`` over the interval ``[-1, 1]``.
"""
struct ChebyshevBasisSecondKind{P} <: AbstractChebyshevBasis{P}
polynomials::Vector{P}
end
struct ChebyshevSecondKind <: AbstractChebyshev end

function degree_one_univariate_polynomial(
::Type{<:ChebyshevBasisSecondKind},
::Type{ChebyshevSecondKind},
variable::MP.AbstractVariable,
)
MA.@rewrite(2variable + 0)
end

function _scalar_product_function(::Type{<:ChebyshevBasisSecondKind}, i::Int)
function _scalar_product_function(::Type{<:ChebyshevSecondKind}, i::Int)
if i == 0
return π / 2
elseif isodd(i)
Expand Down
1 change: 1 addition & 0 deletions src/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function empty_basis(
) where {PT}
return B(PT[])
end

function MP.polynomial_type(
::AbstractPolynomialVectorBasis{PT},
T::Type,
Expand Down
36 changes: 16 additions & 20 deletions src/hermite.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
abstract type AbstractHermiteBasis{P} <: AbstractMultipleOrthogonalBasis{P} end
abstract type AbstractHermite{P} <: AbstractMultipleOrthogonal{P} end

function MP.polynomial_type(::Type{<:AbstractHermiteBasis}, V::Type)
function MP.polynomial_type(::Type{<:AbstractHermite}, V::Type)
return MP.polynomial_type(V, Int)
end

even_odd_separated(::Type{<:AbstractHermiteBasis}) = true
even_odd_separated(::Type{<:AbstractHermite}) = true

reccurence_second_coef(::Type{<:AbstractHermiteBasis}, degree) = 0
reccurence_deno_coef(::Type{<:AbstractHermiteBasis}, degree) = 1
reccurence_second_coef(::Type{<:AbstractHermite}, degree) = 0
reccurence_deno_coef(::Type{<:AbstractHermite}, degree) = 1

"""
struct ProbabilistsHermiteBasis{P} <: AbstractHermiteBasis{P}
Expand All @@ -16,21 +16,19 @@ reccurence_deno_coef(::Type{<:AbstractHermiteBasis}, degree) = 1
Orthogonal polynomial with respect to the univariate weight function ``w(x) = \\exp(-x^2/2)`` over the interval ``[-\\infty, \\infty]``.
"""
struct ProbabilistsHermiteBasis{P} <: AbstractHermiteBasis{P}
polynomials::Vector{P}
end
reccurence_first_coef(::Type{<:ProbabilistsHermiteBasis}, degree) = 1
function reccurence_third_coef(::Type{<:ProbabilistsHermiteBasis}, degree)
struct ProbabilistsHermite <: AbstractHermite end
reccurence_first_coef(::Type{ProbabilistsHermite}, degree) = 1
function reccurence_third_coef(::Type{ProbabilistsHermite}, degree)
return -(degree - 1)
end
function degree_one_univariate_polynomial(
::Type{<:ProbabilistsHermiteBasis},
::Type{ProbabilistsHermite},
variable::MP.AbstractVariable,
)
MA.@rewrite(1variable)
end

function _scalar_product_function(::Type{<:ProbabilistsHermiteBasis}, i::Int)
function _scalar_product_function(::Type{ProbabilistsHermite}, i::Int)
if i == 0
return (2 * π)
elseif isodd(i)
Expand All @@ -42,25 +40,23 @@ function _scalar_product_function(::Type{<:ProbabilistsHermiteBasis}, i::Int)
end

"""
struct PhysicistsHermiteBasis{P} <: AbstractHermiteBasis{P}
struct PhysicistsHermite{P} <: AbstractHermite{P}
polynomials::Vector{P}
end
Orthogonal polynomial with respect to the univariate weight function ``w(x) = \\exp(-x^2)`` over the interval ``[-\\infty, \\infty]``.
"""
struct PhysicistsHermiteBasis{P} <: AbstractHermiteBasis{P}
polynomials::Vector{P}
end
reccurence_first_coef(::Type{<:PhysicistsHermiteBasis}, degree) = 2
reccurence_third_coef(::Type{<:PhysicistsHermiteBasis}, degree) = -2(degree - 1)
struct PhysicistsHermite <: AbstractHermite end
reccurence_first_coef(::Type{PhysicistsHermite}, degree) = 2
reccurence_third_coef(::Type{PhysicistsHermite}, degree) = -2(degree - 1)
function degree_one_univariate_polynomial(
::Type{<:PhysicistsHermiteBasis},
::Type{PhysicistsHermite},
variable::MP.AbstractVariable,
)
MA.@rewrite(2variable)
end

function _scalar_product_function(::Type{<:PhysicistsHermiteBasis}, i::Int)
function _scalar_product_function(::Type{PhysicistsHermite}, i::Int)
if i == 0
return (π)
elseif isodd(i)
Expand Down
26 changes: 12 additions & 14 deletions src/laguerre.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
"""
struct LaguerreBasis{P} <: AbstractMultipleOrthogonalBasis{P}
polynomials::Vector{P}
end
struct LaguerreBasis <: AbstractMultipleOrthogonal end
Orthogonal polynomial with respect to the univariate weight function ``w(x) = \\exp(-x)`` over the interval ``[0, \\infty]``.
"""
struct LaguerreBasis{P} <: AbstractMultipleOrthogonalBasis{P}
polynomials::Vector{P}
end
struct Laguerre <: AbstractMultipleOrthogonal end

# TODO implement multiplication with https://www.jstor.org/stable/2002985

function MP.polynomial_type(::Type{<:LaguerreBasis}, V::Type)
function MP.polynomial_type(::Type{Laguerre}, V::Type)
return MP.polynomial_type(V, Float64)
end

even_odd_separated(::Type{<:LaguerreBasis}) = false
even_odd_separated(::Type{Laguerre}) = false

reccurence_first_coef(::Type{<:LaguerreBasis}, degree) = -1
reccurence_second_coef(::Type{<:LaguerreBasis}, degree) = (2degree - 1)
reccurence_third_coef(::Type{<:LaguerreBasis}, degree) = -(degree - 1)
reccurence_deno_coef(::Type{<:LaguerreBasis}, degree) = degree
reccurence_first_coef(::Type{Laguerre}, degree) = -1
reccurence_second_coef(::Type{Laguerre}, degree) = (2degree - 1)
reccurence_third_coef(::Type{Laguerre}, degree) = -(degree - 1)
reccurence_deno_coef(::Type{Laguerre}, degree) = degree

function degree_one_univariate_polynomial(
::Type{<:LaguerreBasis},
::Type{Laguerre},
variable::MP.AbstractVariable,
)
MA.@rewrite(1 - variable)
end

function _scalar_product_function(::Type{<:LaguerreBasis}, i::Int)
function _scalar_product_function(::Type{Laguerre}, i::Int)
return factorial(i)
end
22 changes: 10 additions & 12 deletions src/legendre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
Orthogonal polynomial with respect to the univariate weight function ``w(x) = (1 - x^2)^{\\alpha - 1/2}`` over the interval ``[-1, 1]``.
"""
abstract type AbstractGegenbauerBasis{P} <: AbstractMultipleOrthogonalBasis{P} end
abstract type AbstractGegenbauer <: AbstractMultipleOrthogonal end

even_odd_separated(::Type{<:AbstractGegenbauerBasis}) = true
reccurence_second_coef(::Type{<:AbstractGegenbauerBasis}, degree) = 0
even_odd_separated(::Type{<:AbstractGegenbauer}) = true
reccurence_second_coef(::Type{<:AbstractGegenbauer}, degree) = 0

"""
struct LegendreBasis{P} <: AbstractGegenbauerBasis{P}
Expand All @@ -17,26 +17,24 @@ reccurence_second_coef(::Type{<:AbstractGegenbauerBasis}, degree) = 0
Orthogonal polynomial with respect to the univariate weight function ``w(x) = 1`` over the interval ``[-1, 1]``.
"""
struct LegendreBasis{P} <: AbstractGegenbauerBasis{P}
polynomials::Vector{P}
end
struct Legendre <: AbstractGegenbauer end

function MP.polynomial_type(::Type{<:LegendreBasis}, V::Type)
function MP.polynomial_type(::Type{<:Legendre}, V::Type)
return MP.polynomial_type(V, Float64)
end

reccurence_first_coef(::Type{<:LegendreBasis}, degree) = (2degree - 1)
reccurence_third_coef(::Type{<:LegendreBasis}, degree) = -(degree - 1)
reccurence_deno_coef(::Type{<:LegendreBasis}, degree) = degree
reccurence_first_coef(::Type{Legendre}, degree) = (2degree - 1)
reccurence_third_coef(::Type{Legendre}, degree) = -(degree - 1)
reccurence_deno_coef(::Type{Legendre}, degree) = degree

function degree_one_univariate_polynomial(
::Type{<:LegendreBasis},
::Type{Legendre},
variable::MP.AbstractVariable,
)
MA.@rewrite(variable + 0)
end

function _scalar_product_function(::Type{<:LegendreBasis}, i::Int)
function _scalar_product_function(::Type{Legendre}, i::Int)
if isodd(i)
return 0
else
Expand Down
Loading

0 comments on commit ed03cca

Please sign in to comment.