Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/PolynomialRoots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,20 @@ function roots(poly::AbstractVector{<:Number}, roots::AbstractVector{<:Number};
epsilon, degree, polish)
end

# Returns a view into the polynomial that excludes any trailing zero
# coefficients.
#
# Errors if the coefficients are all zero.
function normalized_poly_coefs(poly::AbstractVector{<:Number})
l = findlast(!iszero, poly)
isnothing(l) && error("zero polynomial")
@view poly[begin:l]
end

function roots(poly::AbstractVector{N}; epsilon::AbstractFloat=NaN,
polish::Bool=false) where {N<:Number}
# Before starting, truncate the polynomial if it has zeros in the trailing elements
last_nz = findlast(!iszero, poly)
if lastindex(poly) == last_nz
_poly = poly
else
_poly = poly[1:last_nz]
end
_poly = normalized_poly_coefs(poly)
degree = length(_poly) - 1
roots!(zeros(Complex{real(float(N))}, degree), float.(complex(_poly)),
epsilon, degree, polish)
Expand Down