Skip to content

Commit b4d31f6

Browse files
authored
Ngcd bug (#333)
* multroot issue with zero as a root
1 parent f0ed210 commit b4d31f6

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ name = "Polynomials"
22
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
33
license = "MIT"
44
author = "JuliaMath"
5-
version = "2.0.6"
5+
version = "2.0.7"
66

77
[deps]
88
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
99
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1111

12-
1312
[compat]
1413
Intervals = "0.5, 1.0, 1.3"
1514
RecipesBase = "0.7, 0.8, 1"

src/polynomials/multroot.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@ is misidentified.
7979
function multroot(p::Polynomials.StandardBasisPolynomial{T}; verbose=false,
8080
kwargs...) where {T}
8181

82+
# degenerate case, all zeros
83+
if findfirst(!iszero, coeffs(p)) == length(coeffs(p))
84+
return (values=zeros(T,1), multiplicities=nz, κ=NaN, ϵ=NaN)
85+
end
86+
8287
z, l = pejorative_manifold(p; kwargs...)
8388
= pejorative_root(p, z, l)
8489
κ, ϵ = stats(p, z̃, l)
8590

8691
verbose && show_stats(κ, ϵ)
87-
8892
(values = z̃, multiplicities = l, κ = κ, ϵ = ϵ)
8993

9094
end

src/polynomials/ngcd.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ function ngcd(p::P, q::Q,
1111
args...; kwargs...) where {T,X,P<:StandardBasisPolynomial{T,X},
1212
S,Y,Q<:StandardBasisPolynomial{S,Y}}
1313

14-
degree(q) > degree(p) && return ngcd(q,p,args...;kwargs...)
14+
if (degree(q) > degree(p))
15+
u,w,v,Θ,κ = ngcd(q,p,args...;kwargs...)
16+
return (u=u,v=v,w=w, Θ=Θ, κ=κ)
17+
end
1518
if degree(p) > 5*(1+degree(q))
1619
a,b = divrem(p,q)
1720
return ngcd(q,b, args...; λ=100, kwargs...)
@@ -134,7 +137,7 @@ function ngcd(p::PnPolynomial{T,X},
134137
verbose=false,
135138
minⱼ = -1,
136139
λ = 1
137-
) where {T <: AbstractFloat, X}
140+
) where {T, X}
138141

139142
m,n = length(p)-1, length(q)-1
140143
@assert m >= n
@@ -552,12 +555,12 @@ end
552555

553556

554557
function residual_error(p::P,q,uv,uw) where {T,X,P<:AbstractPolynomial{T,X}}
555-
tot = zero(T)
558+
tot = zero(real(T))
556559
for (pᵢ, uvᵢ) in zip(p,uv)
557-
tot += (pᵢ-uvᵢ)^2
560+
tot += norm(pᵢ-uvᵢ)^2
558561
end
559562
for (qᵢ, uwᵢ) in zip(q, uw)
560-
tot += (qᵢ-uwᵢ)^2
563+
tot += norm(qᵢ-uwᵢ)^2
561564
end
562565
sqrt(tot)
563566
end

test/StandardBasis.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,13 @@ end
10311031
out3 = Polynomials.ngcd(p,q,3)
10321032
@test out.Θ <= out1.Θ
10331033
@test out.Θ <= out3.Θ
1034+
1035+
# check for correct output if degree p < degree q
1036+
x = variable(P{Float64})
1037+
p = -18.0 - 37.0*x - 54.0*x^2 - 36.0*x^3 - 16.0*x^4
1038+
q = 2.0 + 5.0*x + 8.0*x^2 + 7.0*x^3 + 4.0*x^4 + 1.0*x^5
1039+
out = Polynomials.ngcd(p,q)
1040+
@test out.u * out.v p
10341041
end
10351042
end
10361043

0 commit comments

Comments
 (0)