Skip to content

Commit

Permalink
More arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 20, 2024
1 parent 4ba5138 commit 89c5cf5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
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
26 changes: 26 additions & 0 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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)
Base.:(+)(p::_AE, q) = +(MP.polynomial(p), constant_algebra_element(typeof(SA.basis(p)), q))
Base.:(-)(p, q::_AE) = -(constant_algebra_element(typeof(SA.basis(q)), p), MP.polynomial(q))
Base.:(-)(p::_AE, q) = -(MP.polynomial(p), constant_algebra_element(typeof(SA.basis(p)), q))

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) = *(p, algebra_element(q))
function Base.:(*)(α, p::Polynomial{B}) where {B}
return _algebra_element(MP.term(α, p.monomial), B)
end
15 changes: 9 additions & 6 deletions src/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,26 @@ function algebra_element(f::Function, basis::SubBasis)
return algebra_element(map(f, eachindex(basis)), basis)
end

_one_if_type(α) = α
_one_if_type(::Type{T}) where {T} = one(T)

function constant_algebra_element(
::Type{FullBasis{B,M}},
::Type{T},
) where {B,M,T}
α,
) 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)]))
α,
) 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

0 comments on commit 89c5cf5

Please sign in to comment.