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

Allow multiplication with Polynomial #36

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 5 additions & 9 deletions src/MultivariateBases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ end
SA.basis(a::Algebra) = a.basis

#Base.:(==)(::Algebra{BT1,B1,M}, ::Algebra{BT2,B2,M}) where {BT1,B1,BT2,B2,M} = true
#Base.:(==)(::Algebra, ::Algebra) = false
function Base.:(==)(a::Algebra, b::Algebra)
# `===` is a shortcut for speedup
return a.basis === b.basis || a.basis == b.basis
end

function Base.show(io::IO, ::Algebra{BT,B}) where {BT,B}
ioc = IOContext(io, :limit => true, :compact => true)
Expand Down Expand Up @@ -72,13 +75,6 @@ function MA.promote_operation(
return Algebra{BT,B,M}
end

const _APL = MP.AbstractPolynomialLike
# We don't define it for all `AlgebraElement` as this would be type piracy
const _AE = SA.AlgebraElement{<:Algebra}

Base.:(+)(p::_APL, q::_AE) = +(p, MP.polynomial(q))
Base.:(+)(p::_AE, q::_APL) = +(MP.polynomial(p), q)
Base.:(-)(p::_APL, q::_AE) = -(p, MP.polynomial(q))
Base.:(-)(p::_AE, q::_APL) = -(MP.polynomial(p), q)
include("arithmetic.jl")

end # module
32 changes: 32 additions & 0 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const _APL = MP.AbstractPolynomialLike
# We don't define it for all `AlgebraElement` as this would be type piracy
const _AE = SA.AlgebraElement{<:Algebra}

Base.:(+)(p::_APL, q::_AE) = +(p, MP.polynomial(q))
Base.:(+)(p::_AE, q::_APL) = +(MP.polynomial(p), q)
Base.:(-)(p::_APL, q::_AE) = -(p, MP.polynomial(q))
Base.:(-)(p::_AE, q::_APL) = -(MP.polynomial(p), q)

Base.:(+)(p, q::_AE) = +(constant_algebra_element(typeof(SA.basis(q)), p), q)
function Base.:(+)(p::_AE, q)
return +(MP.polynomial(p), constant_algebra_element(typeof(SA.basis(p)), q))
end
function Base.:(-)(p, q::_AE)
return -(constant_algebra_element(typeof(SA.basis(q)), p), MP.polynomial(q))
end
function Base.:(-)(p::_AE, q)
return -(MP.polynomial(p), constant_algebra_element(typeof(SA.basis(p)), q))
end

function Base.:(+)(p::_AE, q::_AE)
return MA.operate_to!(SA._preallocate_output(+, p, q), +, p, q)
end

function Base.:(-)(p::_AE, q::_AE)
return MA.operate_to!(SA._preallocate_output(-, p, q), -, p, q)
end

Base.:(*)(p::Union{_APL,_AE}, q::Polynomial{B}) where {B} = *(p, algebra_element(q))
function Base.:(*)(α, p::Polynomial{B}) where {B}
return _algebra_element(MP.term(α, p.monomial), B)
end
20 changes: 10 additions & 10 deletions src/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,23 @@ function algebra_element(f::Function, basis::SubBasis)
return algebra_element(map(f, eachindex(basis)), basis)
end

function constant_algebra_element(
::Type{FullBasis{B,M}},
::Type{T},
) where {B,M,T}
_one_if_type(α) = α
_one_if_type(::Type{T}) where {T} = one(T)

function constant_algebra_element(::Type{FullBasis{B,M}}, α) where {B,M}
return algebra_element(
sparse_coefficients(
MP.polynomial(MP.term(one(T), MP.constant_monomial(M))),
MP.polynomial(MP.term(_one_if_type(α), MP.constant_monomial(M))),
),
FullBasis{B,M}(),
)
end

function constant_algebra_element(
::Type{<:SubBasis{B,M}},
::Type{T},
) where {B,M,T}
return algebra_element([one(T)], SubBasis{B}([MP.constant_monomial(M)]))
function constant_algebra_element(::Type{<:SubBasis{B,M}}, α) where {B,M}
return algebra_element(
[_one_if_type(α)],
SubBasis{B}([MP.constant_monomial(M)]),
)
end

function _show(io::IO, mime::MIME, basis::SubBasis{B}) where {B}
Expand Down
12 changes: 7 additions & 5 deletions src/orthogonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ function SA.coeffs(
end

function SA.coeffs(
p::Polynomial{B},
p::Polynomial{B,M},
::FullBasis{Monomial},
) where {B<:AbstractMultipleOrthogonal}
) where {B<:AbstractMultipleOrthogonal,M}
return sparse_coefficients(
prod(
univariate_orthogonal_basis(B, var, deg)[deg+1] for
(var, deg) in MP.powers(p.monomial)
),
MP.powers(p.monomial);
init = MP.constant_monomial(M),
) do (var, deg)
return univariate_orthogonal_basis(B, var, deg)[deg+1]
end,
)
end
4 changes: 3 additions & 1 deletion src/scaled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ function Base.promote_rule(
end

function scaling(m::MP.AbstractMonomial)
return √(factorial(MP.degree(m)) / prod(factorial, MP.exponents(m)))
return √(
factorial(MP.degree(m)) / prod(factorial, MP.exponents(m); init = 1),
)
end
unscale_coef(t::MP.AbstractTerm) = MP.coefficient(t) / scaling(MP.monomial(t))
function SA.coeffs(
Expand Down
12 changes: 8 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree)
_wrap(MB.SA.trim_LaTeX(mime, sprint(show, mime, p.monomial))) *
" \$\$"
const_mono = constant_monomial(prod(x))
@test const_mono + MB.algebra_element(MB.Polynomial{B}(const_mono)) == 2
@test MB.algebra_element(MB.Polynomial{B}(const_mono)) + const_mono == 2
@test iszero(const_mono - MB.algebra_element(MB.Polynomial{B}(const_mono)))
@test iszero(MB.algebra_element(MB.Polynomial{B}(const_mono)) - const_mono)
const_poly = MB.Polynomial{B}(const_mono)
const_alg_el = MB.algebra_element(const_poly)
for other in (const_mono, 1, const_alg_el)
@test other + const_alg_el ≈ 2 * other
@test const_alg_el + other ≈ 2 * other
@test iszero(other - const_alg_el)
@test iszero(const_alg_el - other)
end
@test typeof(MB.sparse_coefficients(sum(x))) ==
MA.promote_operation(MB.sparse_coefficients, typeof(sum(x)))
@test typeof(MB.algebra_element(sum(x))) ==
Expand Down
Loading