From ec32e2292bf43cd59f127e882c188d07520936c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 3 Jul 2024 12:10:43 +0200 Subject: [PATCH 1/2] Fix dispatch for coeff --- src/monomial.jl | 18 ++++++++++++++---- test/scaled.jl | 8 +++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/monomial.jl b/src/monomial.jl index 8c99ad0..70af78b 100644 --- a/src/monomial.jl +++ b/src/monomial.jl @@ -423,10 +423,20 @@ 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 + +function SA.coeffs!( + _, + _, + source::MonomialIndexedBasis, + target::MonomialIndexedBasis, +) + return error("Convertion from `$source` to `$target` not implemented yet") +end diff --git a/test/scaled.jl b/test/scaled.jl index 77d4ae5..4975b21 100644 --- a/test/scaled.jl +++ b/test/scaled.jl @@ -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 From 0cfa0e176fea3ea51751f33c8bd59af6da9d3820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 3 Jul 2024 12:11:51 +0200 Subject: [PATCH 2/2] Fixes --- src/monomial.jl | 9 --------- test/hermite.jl | 4 ---- 2 files changed, 13 deletions(-) diff --git a/src/monomial.jl b/src/monomial.jl index 70af78b..13ae659 100644 --- a/src/monomial.jl +++ b/src/monomial.jl @@ -431,12 +431,3 @@ function SA.coeffs( return SA.coeffs!(res, cfs, source, target) end end - -function SA.coeffs!( - _, - _, - source::MonomialIndexedBasis, - target::MonomialIndexedBasis, -) - return error("Convertion from `$source` to `$target` not implemented yet") -end diff --git a/test/hermite.jl b/test/hermite.jl index c72c97e..cd28b62 100644 --- a/test/hermite.jl +++ b/test/hermite.jl @@ -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