From 088298bb5951ac3e4c2040017f825bbd55a0a76d Mon Sep 17 00:00:00 2001 From: lkapelevich Date: Sun, 31 May 2020 19:06:52 -0400 Subject: [PATCH 1/3] fix casting errors --- src/PolynomialRoots.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/PolynomialRoots.jl b/src/PolynomialRoots.jl index 23d9bb2..1722992 100644 --- a/src/PolynomialRoots.jl +++ b/src/PolynomialRoots.jl @@ -229,7 +229,7 @@ function newton_spec(poly::AbstractVector{Complex{T}}, degree::Integer, end # if abs2p == 0 if dp == 0 # problem with zero. Make some random jump - dx::Complex{T} = (abs(root) + 1) * cis(FRAC_JUMPS[trunc(Integer, mod(i, FRAC_JUMP_LEN)) + 1]*2*pi) + dx::Complex{T} = (abs(root) + 1) * cis(T(FRAC_JUMPS[trunc(Integer, mod(i, FRAC_JUMP_LEN)) + 1]) * 2 * pi) else dx = p / dp # Newton method, see http://en.wikipedia.org/wiki/Newton's_method end @@ -295,7 +295,7 @@ function laguerre(poly::AbstractVector{Complex{T}}, degree::Integer, end end if denom == 0 # test if demoninators are > 0.0 not to divide by zero - dx::Complex{T} = (abs(root) + 1) * cis(FRAC_JUMPS[trunc(Integer, mod(i,FRAC_JUMP_LEN)) + 1] * 2 * pi) # make some random jump + dx::Complex{T} = (abs(root) + 1) * cis(T(FRAC_JUMPS[trunc(Integer, mod(i,FRAC_JUMP_LEN)) + 1]) * 2 * pi) # make some random jump else dx = fac_netwon / denom end @@ -308,7 +308,7 @@ function laguerre(poly::AbstractVector{Complex{T}}, degree::Integer, return root, iter, success end if mod(i, FRAC_JUMP_EVERY) == 0 # decide whether to do a jump of modified length (to break cycles) - faq = FRAC_JUMPS[trunc(Integer, mod(i / FRAC_JUMP_EVERY - 1, FRAC_JUMP_LEN)) + 1] + faq = T(FRAC_JUMPS[trunc(Integer, mod(i / FRAC_JUMP_EVERY - 1, FRAC_JUMP_LEN)) + 1]) newroot = root - faq*dx # do jump of some semi-random length (0= 2 # LAGUERRE'S METHOD - one_nth = 1 / degree + one_nth = 1 / T(degree) n_1_nth = (degree - 1)*one_nth two_n_div_n_1 = 2 / n_1_nth c_one_nth = complex(one_nth) @@ -382,7 +382,7 @@ function laguerre2newton(poly::AbstractVector{Complex{T}}, degree::Integer, end end if denom == 0 #test if demoninators are > 0.0 not to divide by zero - dx = (abs(root) + 1) * cis(FRAC_JUMPS[trunc(Integer, mod(i,FRAC_JUMP_LEN)) + 1] * 2 * pi) # make some random jump + dx = (abs(root) + 1) * cis(T(FRAC_JUMPS[trunc(Integer, mod(i,FRAC_JUMP_LEN)) + 1]) * 2 * pi) # make some random jump else dx = fac_netwon / denom end @@ -400,7 +400,7 @@ function laguerre2newton(poly::AbstractVector{Complex{T}}, degree::Integer, break # go to Newton's or SG end if mod(i, FRAC_JUMP_EVERY) == 0 # decide whether to do a jump of modified length (to break cycles) - faq = FRAC_JUMPS[trunc(Integer, mod(i / FRAC_JUMP_EVERY - 1, FRAC_JUMP_LEN)) + 1] + faq = T(FRAC_JUMPS[trunc(Integer, mod(i / FRAC_JUMP_EVERY - 1, FRAC_JUMP_LEN)) + 1]) newroot = root - faq*dx # do jump of some semi-random length (0 0.0 not to divide by zero - dx = (abs(root) + 1) * cis(FRAC_JUMPS[trunc(Integer, mod(i,FRAC_JUMP_LEN)) + 1]*2*pi) # make some random jump + dx = (abs(root) + 1) * cis(T(FRAC_JUMPS[trunc(Integer, mod(i,FRAC_JUMP_LEN)) + 1]) * 2 * pi) # make some random jump else fac_netwon = p / dp fac_extra = d2p_half / dp @@ -467,7 +467,7 @@ function laguerre2newton(poly::AbstractVector{Complex{T}}, degree::Integer, break # go to Newton's end if mod(i, FRAC_JUMP_EVERY) == 0 # decide whether to do a jump of modified length (to break cycles) - faq = FRAC_JUMPS[trunc(Integer, mod(i / FRAC_JUMP_EVERY - 1, FRAC_JUMP_LEN)) + 1] + faq = T(FRAC_JUMPS[trunc(Integer, mod(i / FRAC_JUMP_EVERY - 1, FRAC_JUMP_LEN)) + 1]) newroot = root - faq*dx # do jump of some semi-random length (0 0.0 not to divide by zero - dx = (abs(root) + 1) * cis(FRAC_JUMPS[trunc(Integer, mod(i,FRAC_JUMP_LEN)) + 1] * 2 * pi) # make some random jump + dx = (abs(root) + 1) * cis(T(FRAC_JUMPS[trunc(Integer, mod(i,FRAC_JUMP_LEN)) + 1]) * 2 * pi) # make some random jump else dx = p / dp end From 3e435b8d7ae816a13bec324fd57a6539a99768e5 Mon Sep 17 00:00:00 2001 From: lkapelevich Date: Sun, 31 May 2020 19:25:27 -0400 Subject: [PATCH 2/3] add test --- test/runtests.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index f524964..25292ad 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -231,6 +231,11 @@ end # https://github.com/giordano/PolynomialRoots.jl/issues/11 poly = [1.0, -2.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] @test isapprox(@inferred(roots(poly)), [1, 1]) + # https://github.com/giordano/PolynomialRoots.jl/pull/19 + for T in [Float32, Float64] + poly = T[0.7513126327861701, 0.6448833539420931, 0.07782644396003469, 0.8481854810000327] + @test isapprox(zeros(length(poly)-1), evalpoly(roots(poly), poly), atol = 1000eps(T)) + end end @testset "Errors" begin From 15529bb0eb3b2cc8040617dd99feee45004f6ab7 Mon Sep 17 00:00:00 2001 From: lkapelevich Date: Sun, 31 May 2020 19:27:45 -0400 Subject: [PATCH 3/3] fix comment --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 25292ad..c151078 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -231,7 +231,7 @@ end # https://github.com/giordano/PolynomialRoots.jl/issues/11 poly = [1.0, -2.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] @test isapprox(@inferred(roots(poly)), [1, 1]) - # https://github.com/giordano/PolynomialRoots.jl/pull/19 + # https://github.com/giordano/PolynomialRoots.jl/pull/20 for T in [Float32, Float64] poly = T[0.7513126327861701, 0.6448833539420931, 0.07782644396003469, 0.8481854810000327] @test isapprox(zeros(length(poly)-1), evalpoly(roots(poly), poly), atol = 1000eps(T))