diff --git a/src/monomial.jl b/src/monomial.jl index 1872ef9..38d2465 100644 --- a/src/monomial.jl +++ b/src/monomial.jl @@ -164,8 +164,14 @@ end MP.variables(c::SA.AbstractCoefficients) = MP.variables(SA.keys(c)) +_lazy_collect(v::AbstractVector) = collect(v) +_lazy_collect(v::Vector) = v + function sparse_coefficients(p::MP.AbstractPolynomial) - return SA.SparseCoefficients(MP.monomials(p), MP.coefficients(p)) + return SA.SparseCoefficients( + _lazy_collect(MP.monomials(p)), + _lazy_collect(MP.coefficients(p)), + ) end function sparse_coefficients(t::MP.AbstractTermLike) @@ -178,7 +184,7 @@ function MA.promote_operation( ) where {P<:MP.AbstractPolynomialLike} M = MP.monomial_type(P) T = MP.coefficient_type(P) - return SA.SparseCoefficients{M,T,MP.monomial_vector_type(M),Vector{T}} + return SA.SparseCoefficients{M,T,Vector{M},Vector{T}} end function algebra_element(p::MP.AbstractPolynomialLike) diff --git a/test/runtests.jl b/test/runtests.jl index 73e04f2..041149f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -78,6 +78,10 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree) @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) + @test typeof(MB.sparse_coefficients(sum(x))) == + MA.promote_operation(MB.sparse_coefficients, typeof(sum(x))) + @test typeof(MB.algebra_element(sum(x))) == + MA.promote_operation(MB.algebra_element, typeof(sum(x))) end function univ_orthogonal_test(