From 8151bfef5d9143f7c254c795021b244cdc0ff0be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 4 Jul 2024 08:55:47 +0200 Subject: [PATCH 1/2] Add tests --- src/chebyshev.jl | 2 +- src/monomial.jl | 4 ++++ src/scaled.jl | 2 +- test/monomial.jl | 4 +++- test/runtests.jl | 8 +++++++- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/chebyshev.jl b/src/chebyshev.jl index 7a5dc77..a02f629 100644 --- a/src/chebyshev.jl +++ b/src/chebyshev.jl @@ -4,7 +4,7 @@ _promote_div(::Type{I}) where {I<:Integer} = Rational{I} _promote_div(::Type{T}) where {T<:Number} = MA.promote_operation(/, T, Int) # Could be for instance `MathOptInterface.ScalarAffineFunction{Float64}` # which does not support division with `Int` -_promote_div(::Type{F}) where {F} = F +_promote_div(::Type{F}) where {F} = _float(F) function _promote_coef(::Type{T}, ::Type{<:AbstractChebyshev}) where {T} return _promote_div(T) diff --git a/src/monomial.jl b/src/monomial.jl index 13ae659..0f1876b 100644 --- a/src/monomial.jl +++ b/src/monomial.jl @@ -155,6 +155,10 @@ end _explicit_basis(_, basis::SubBasis) = basis +function explicit_basis(p::MP.AbstractPolynomialLike) + return SubBasis{Monomial}(MP.monomials(p)) +end + function explicit_basis(a::SA.AlgebraElement) return _explicit_basis(SA.coeffs(a), SA.basis(a)) end diff --git a/src/scaled.jl b/src/scaled.jl index b5d6c7a..fde028e 100644 --- a/src/scaled.jl +++ b/src/scaled.jl @@ -51,7 +51,7 @@ end _float(::Type{T}) where {T<:Number} = float(T) # Could be for instance `MathOptInterface.ScalarAffineFunction{Float64}` # which does not implement `float` -_float(::Type{F}) where {F} = F +_float(::Type{F}) where {F} = MA.promote_operation(*, Float64, F) _promote_coef(::Type{T}, ::Type{ScaledMonomial}) where {T} = _float(T) diff --git a/test/monomial.jl b/test/monomial.jl index 89bb4fb..2f55d4f 100644 --- a/test/monomial.jl +++ b/test/monomial.jl @@ -38,8 +38,10 @@ end # It will be sorted and 1 will be moved at the end basis = SubBasis{MB.Monomial}([1, x, y]) @test polynomial_type(basis, Int) == polynomial_type(x, Int) - @test polynomial(i -> i^2, basis) == 9x + 4y + 1 + poly = 9x + 4y + 1 + @test polynomial(i -> i^2, basis) == poly @test coefficients(9 + x + 4y, basis) == [9, 4, 1] + @test MB.explicit_basis(poly) == basis end @testset "Quadratic" begin basis = SubBasis{MB.Monomial}([x^2, x * y, y^2]) diff --git a/test/runtests.jl b/test/runtests.jl index 067c35f..9cfc60b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -20,6 +20,12 @@ function _test_basis(basis) MB.constant_algebra_element_type(B, Int) end +struct TypeA end +struct TypeB end +Base.zero(::Type{TypeA}) = TypeA() +Base.:*(::Float64, ::TypeA) = TypeB() + + function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree) @polyvar x[1:2] M = typeof(prod(x)) @@ -65,7 +71,7 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree) p = MB.Polynomial{B}(mono) @test full_basis[p] == mono @test full_basis[mono] == p - @test polynomial_type(mono, String) == polynomial_type(typeof(p), String) + @test polynomial_type(mono, B == Monomial ? TypeA : TypeB) == polynomial_type(typeof(p), TypeA) a = MB.algebra_element(p) @test variables(a) == x @test typeof(polynomial(a)) == polynomial_type(typeof(a)) From c853d7c08e89f3fadba83dfd3f7137406a0f791d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 4 Jul 2024 08:59:25 +0200 Subject: [PATCH 2/2] Fix format --- test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 9cfc60b..52ccdb5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -25,7 +25,6 @@ struct TypeB end Base.zero(::Type{TypeA}) = TypeA() Base.:*(::Float64, ::TypeA) = TypeB() - function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree) @polyvar x[1:2] M = typeof(prod(x)) @@ -71,7 +70,8 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree) p = MB.Polynomial{B}(mono) @test full_basis[p] == mono @test full_basis[mono] == p - @test polynomial_type(mono, B == Monomial ? TypeA : TypeB) == polynomial_type(typeof(p), TypeA) + @test polynomial_type(mono, B == Monomial ? TypeA : TypeB) == + polynomial_type(typeof(p), TypeA) a = MB.algebra_element(p) @test variables(a) == x @test typeof(polynomial(a)) == polynomial_type(typeof(a))