Skip to content

Commit

Permalink
Fix dispatch for coeff (#39)
Browse files Browse the repository at this point in the history
* Fix dispatch for coeff

* Fixes
  • Loading branch information
blegat authored Jul 3, 2024
1 parent 1027a0b commit 0cc7d89
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,11 @@ function SA.coeffs(
# The defaults initialize to zero and then sums which promotes
# `JuMP.VariableRef` to `JuMP.AffExpr`
return SA.SparseCoefficients(_vec(source.monomials), _vec(cfs))
elseif B2 === Monomial
res = SA.zero_coeffs(_promote_coef(valtype(cfs), B1), target)
return SA.coeffs!(res, cfs, source, target)
else
error("Convertion from `$source` to `$target` not implemented yet")
res = SA.zero_coeffs(
_promote_coef(_promote_coef(valtype(cfs), B1), B2),
target,
)
return SA.coeffs!(res, cfs, source, target)
end
end
4 changes: 0 additions & 4 deletions test/hermite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,4 @@ end
M = typeof(x^2)
mono = MB.FullBasis{MB.Monomial,M}()
basis = MB.FullBasis{MB.PhysicistsHermite,M}()
err = ErrorException(
"Convertion from `$mono` to `$basis` not implemented yet",
)
@test_throws err SA.coeffs(MB.algebra_element(x + 1), basis)
end
8 changes: 7 additions & 1 deletion test/scaled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ end
basis = MB.SubBasis{MB.ScaledMonomial}([x^2, x * y, y^2])
@test polynomial_type(basis, Int) == polynomial_type(x, Float64)
@test polynomial(i -> i^2, basis) == 9x^2 + 4 * 2 * x * y + y^2
@test coefficients(x^2 + 4x * y + 9y^2, basis) == [9, 4 / 2, 1]
p = x^2 + 4x * y + 9y^2
@test coefficients(p, basis) == [9, 4 / 2, 1]
a = MB.algebra_element(p)
@test SA.coeffs(a, basis) == [9, 4 / 2, 1]
full = MB.FullBasis{MB.ScaledMonomial,monomial_type(p)}()
@test SA.coeffs(a, full) ==
SA.SparseCoefficients([y^2, x * y, x^2], [9, 4 / 2, 1])
@test polynomial(basis[1]) == y^2
@test polynomial(basis[2]) == 2 * x * y
@test polynomial(basis[3]) == x^2
Expand Down

0 comments on commit 0cc7d89

Please sign in to comment.