Skip to content

Commit

Permalink
Merge pull request #20 from lkapelevich/fixtypes
Browse files Browse the repository at this point in the history
fix type errors
  • Loading branch information
giordano authored May 31, 2020
2 parents 6291fdc + 15529bb commit 1e12379
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/PolynomialRoots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<faq<1)
end
root = newroot
Expand All @@ -334,7 +334,7 @@ function laguerre2newton(poly::AbstractVector{Complex{T}}, degree::Integer,
while true
#------------------------------------------------------------- mode 2
if mode >= 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)
Expand Down Expand Up @@ -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
Expand All @@ -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<faq<1)
end
root = newroot
Expand Down Expand Up @@ -442,7 +442,7 @@ function laguerre2newton(poly::AbstractVector{Complex{T}}, degree::Integer,
good_to_go = false # reset if we are outside the zone of the root
end
if dp == 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
fac_netwon = p / dp
fac_extra = d2p_half / dp
Expand All @@ -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<faq<1)
end
root = newroot
Expand Down Expand Up @@ -505,7 +505,7 @@ function laguerre2newton(poly::AbstractVector{Complex{T}}, degree::Integer,
good_to_go = false # reset if we are outside the zone of the root
end
if dp == 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 = p / dp
end
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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/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))
end
end

@testset "Errors" begin
Expand Down

0 comments on commit 1e12379

Please sign in to comment.