Skip to content

Commit

Permalink
Test algebra_element and sparse_coefficients (#33)
Browse files Browse the repository at this point in the history
* Test algebra_element and sparse_coefficients

* Fix format
  • Loading branch information
blegat authored Jun 20, 2024
1 parent 6652b19 commit 4ba5138
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4ba5138

Please sign in to comment.