Skip to content

Commit 4ba5138

Browse files
authored
Test algebra_element and sparse_coefficients (#33)
* Test algebra_element and sparse_coefficients * Fix format
1 parent 6652b19 commit 4ba5138

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/monomial.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,14 @@ end
164164

165165
MP.variables(c::SA.AbstractCoefficients) = MP.variables(SA.keys(c))
166166

167+
_lazy_collect(v::AbstractVector) = collect(v)
168+
_lazy_collect(v::Vector) = v
169+
167170
function sparse_coefficients(p::MP.AbstractPolynomial)
168-
return SA.SparseCoefficients(MP.monomials(p), MP.coefficients(p))
171+
return SA.SparseCoefficients(
172+
_lazy_collect(MP.monomials(p)),
173+
_lazy_collect(MP.coefficients(p)),
174+
)
169175
end
170176

171177
function sparse_coefficients(t::MP.AbstractTermLike)
@@ -178,7 +184,7 @@ function MA.promote_operation(
178184
) where {P<:MP.AbstractPolynomialLike}
179185
M = MP.monomial_type(P)
180186
T = MP.coefficient_type(P)
181-
return SA.SparseCoefficients{M,T,MP.monomial_vector_type(M),Vector{T}}
187+
return SA.SparseCoefficients{M,T,Vector{M},Vector{T}}
182188
end
183189

184190
function algebra_element(p::MP.AbstractPolynomialLike)

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree)
7878
@test MB.algebra_element(MB.Polynomial{B}(const_mono)) + const_mono == 2
7979
@test iszero(const_mono - MB.algebra_element(MB.Polynomial{B}(const_mono)))
8080
@test iszero(MB.algebra_element(MB.Polynomial{B}(const_mono)) - const_mono)
81+
@test typeof(MB.sparse_coefficients(sum(x))) ==
82+
MA.promote_operation(MB.sparse_coefficients, typeof(sum(x)))
83+
@test typeof(MB.algebra_element(sum(x))) ==
84+
MA.promote_operation(MB.algebra_element, typeof(sum(x)))
8185
end
8286

8387
function univ_orthogonal_test(

0 commit comments

Comments
 (0)