From 6652b19c4ac8360575a36bde1c8bf3c527486640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 18 Jun 2024 11:49:24 +0200 Subject: [PATCH] Fix variables with FullBasis (#32) * Fix variables with FullBasis * Fix * Fixes --- src/MultivariateBases.jl | 9 +++++++++ src/monomial.jl | 2 ++ src/polynomial.jl | 2 +- test/runtests.jl | 6 ++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/MultivariateBases.jl b/src/MultivariateBases.jl index 79f692d..943268e 100644 --- a/src/MultivariateBases.jl +++ b/src/MultivariateBases.jl @@ -72,4 +72,13 @@ 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) + end # module diff --git a/src/monomial.jl b/src/monomial.jl index 42b92a6..1872ef9 100644 --- a/src/monomial.jl +++ b/src/monomial.jl @@ -112,6 +112,8 @@ function SubBasis{B}( ) end +SubBasis{B}(monos::Tuple) where {B} = SubBasis{B}([monos...]) + function Base.copy(basis::SubBasis) return typeof(basis)(copy(basis.monomials)) end diff --git a/src/polynomial.jl b/src/polynomial.jl index d1f718b..ecadb9e 100644 --- a/src/polynomial.jl +++ b/src/polynomial.jl @@ -1,5 +1,5 @@ # TODO Add to MultivariatePolynomials -MP.variables(p::SA.AlgebraElement) = MP.variables(SA.basis(p)) +MP.variables(p::SA.AlgebraElement) = MP.variables(explicit_basis(p)) Base.keytype(p::MP.AbstractPolynomialLike) = MP.monomial_type(p) Base.valtype(p::MP.AbstractPolynomialLike) = MP.coefficient_type(p) #Base.keys(p::MP.AbstractPolynomial) = MP.monomials(p) diff --git a/test/runtests.jl b/test/runtests.jl index ec8fc2f..73e04f2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -54,6 +54,7 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree) @test full_basis[mono] == p @test polynomial_type(mono, String) == polynomial_type(typeof(p), String) a = MB.algebra_element(p) + @test variables(a) == x @test typeof(polynomial(a)) == polynomial_type(typeof(a)) @test typeof(polynomial(a)) == polynomial_type(typeof(p), Int) @test a ≈ a @@ -72,6 +73,11 @@ 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) end function univ_orthogonal_test(