Skip to content
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

Add QuotientBasis #29

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/MultivariateBases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ include("hermite.jl")
include("laguerre.jl")
include("legendre.jl")
include("chebyshev.jl")
include("quotient.jl")

end # module
9 changes: 6 additions & 3 deletions src/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,15 @@ function MP.polynomial(Q::AbstractMatrix, mb::SubBasis{Monomial}, T::Type)
return MP.polynomial(Q, mb.monomials, T)
end

function MP.coefficients(p, basis::SubBasis{Monomial})
function MP.coefficients(
p::MP.AbstractPolynomialLike,
basis::SubBasis{Monomial},
)
return MP.coefficients(p, basis.monomials)
end

function MP.coefficients(p, ::FullBasis{Monomial})
return MP.coefficients(p)
function MP.coefficients(p::MP.AbstractPolynomialLike, ::FullBasis{Monomial})
return p
end

# Overload some of the `MP` interface for convenience
Expand Down
10 changes: 10 additions & 0 deletions src/quotient.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct QuotientBasis{T,I,B<:SA.AbstractBasis{T,I},D} <: SA.ExplicitBasis{T,I}
basis::B
divisor::D
end

Base.length(basis::QuotientBasis) = length(basis.basis)

function MP.coefficients(p, basis::QuotientBasis)
return MP.coefficients(rem(p, basis.divisor), basis.basis)
end
19 changes: 19 additions & 0 deletions test/quotient.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
struct PlusMinusOne # Reduce modulo `x^2 = 1`
end
function Base.rem(m::AbstractMonomial, ::PlusMinusOne)
return prod(v^mod(e, 2) for (v, e) in powers(m))
end
function Base.rem(t::AbstractTerm, d::PlusMinusOne)
return coefficient(t) * rem(monomial(t), d)
end
function Base.rem(p::AbstractPolynomial, d::PlusMinusOne)
return sum(rem(t, d) for t in terms(p))
end

@testset "PlusMinusOne" begin
@polyvar x y
basis =
MB.QuotientBasis(MB.SubBasis{MB.Monomial}([1, y, x]), PlusMinusOne())
@test length(basis) == 3
@test coefficients(x^3 - 2x^2 * y + 3x^2, basis) == [3, -2, 1]
end
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,6 @@ end
@testset "Chebyshev" begin
include("chebyshev.jl")
end
@testset "Quotient" begin
include("quotient.jl")
end
Loading