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..52ccdb5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -20,6 +20,11 @@ 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 +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, 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))