Skip to content

Commit

Permalink
redefine scalar_div to avoid undesirable type conversion (#496)
Browse files Browse the repository at this point in the history
* redefine scalar_div to avoid undesirable type conversion

* guard for 0 poly

* both are method errors

* use _convert
  • Loading branch information
jverzani authored Apr 26, 2023
1 parent 461c508 commit 7be9e7e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion test/StandardBasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

2 comments on commit 7be9e7e

@jverzani
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/82369

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.2.10 -m "<description of version>" 7be9e7eedde79d83db3684cb9d855d99409da138
git push origin v3.2.10

Please sign in to comment.