Skip to content

Commit 7be9e7e

Browse files
authored
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
1 parent 461c508 commit 7be9e7e

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "Polynomials"
22
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
33
license = "MIT"
44
author = "JuliaMath"
5-
version = "3.2.9"
5+
version = "3.2.10"
66

77
[deps]
88
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/common.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,11 @@ end
10481048
scalar_mult(p1::AbstractPolynomial, p2::AbstractPolynomial) = error("scalar_mult(::$(typeof(p1)), ::$(typeof(p2))) is not defined.") # avoid ambiguity, issue #435
10491049

10501050
# scalar div
1051-
Base.:/(p::P, c::S) where {P <: AbstractPolynomial,S} = scalar_div(p, c)
1052-
scalar_div(p::AbstractPolynomial, c) = scalar_mult(p, inv(c))
1051+
Base.:/(p::AbstractPolynomial, c) = scalar_div(p, c)
1052+
function scalar_div(p::P, c::S) where {S, T, X, P<:AbstractPolynomial{T, X}}
1053+
iszero(p) && return zero((P){Base.promote_op(/,T,S), X})
1054+
_convert(p, coeffs(p) ./ Ref(c))
1055+
end
10531056

10541057
## Polynomial p*q
10551058
## Polynomial multiplication formula depend on the particular basis used. The subtype must implement

test/StandardBasis.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ end
284284
@test_throws MethodError q * p == P(conv([a,b], [a,b, c])) # Ok, no * for T
285285

286286
# poly powers
287-
@test_throws MethodError p^2 == p * p # Ok, no * for T
287+
@test_throws MethodError p^2 # Ok, no * for T
288+
@test_throws MethodError p * p
288289

289290
# evaluation
290291
@test p(s) == a + b * s + c * s * s
@@ -525,6 +526,11 @@ end
525526
@test p - p == 0*p
526527
end
527528
end
529+
530+
# issue #495, (scalar div fix)
531+
𝐐 = Rational{Int}
532+
v = Polynomial{𝐐}([0//1])
533+
@test eltype(integrate(v)) == 𝐐
528534
end
529535

530536
@testset "Divrem" begin

0 commit comments

Comments
 (0)