From 7be9e7eedde79d83db3684cb9d855d99409da138 Mon Sep 17 00:00:00 2001 From: john verzani Date: Wed, 26 Apr 2023 10:29:35 -0400 Subject: [PATCH] redefine scalar_div to avoid undesirable type conversion (#496) * redefine scalar_div to avoid undesirable type conversion * guard for 0 poly * both are method errors * use _convert --- Project.toml | 2 +- src/common.jl | 7 +++++-- test/StandardBasis.jl | 8 +++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 211ac814..8a08dfeb 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "Polynomials" uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" license = "MIT" author = "JuliaMath" -version = "3.2.9" +version = "3.2.10" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" diff --git a/src/common.jl b/src/common.jl index ad75fda7..5c5ba08e 100644 --- a/src/common.jl +++ b/src/common.jl @@ -1048,8 +1048,11 @@ end scalar_mult(p1::AbstractPolynomial, p2::AbstractPolynomial) = error("scalar_mult(::$(typeof(p1)), ::$(typeof(p2))) is not defined.") # avoid ambiguity, issue #435 # scalar div -Base.:/(p::P, c::S) where {P <: AbstractPolynomial,S} = scalar_div(p, c) -scalar_div(p::AbstractPolynomial, c) = scalar_mult(p, inv(c)) +Base.:/(p::AbstractPolynomial, c) = scalar_div(p, c) +function scalar_div(p::P, c::S) where {S, T, X, P<:AbstractPolynomial{T, X}} + iszero(p) && return zero(⟒(P){Base.promote_op(/,T,S), X}) + _convert(p, coeffs(p) ./ Ref(c)) +end ## Polynomial p*q ## Polynomial multiplication formula depend on the particular basis used. The subtype must implement diff --git a/test/StandardBasis.jl b/test/StandardBasis.jl index 1240eb04..fc69c863 100644 --- a/test/StandardBasis.jl +++ b/test/StandardBasis.jl @@ -284,7 +284,8 @@ end @test_throws MethodError q * p == P(conv([a,b], [a,b, c])) # Ok, no * for T # poly powers - @test_throws MethodError p^2 == p * p # Ok, no * for T + @test_throws MethodError p^2 # Ok, no * for T + @test_throws MethodError p * p # evaluation @test p(s) == a + b * s + c * s * s @@ -525,6 +526,11 @@ end @test p - p == 0*p end end + + # issue #495, (scalar div fix) + 𝐐 = Rational{Int} + v = Polynomial{𝐐}([0//1]) + @test eltype(integrate(v)) == 𝐐 end @testset "Divrem" begin