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

Use Leja ordering for fromroots #586

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
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
37 changes: 36 additions & 1 deletion src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,41 @@ export fromroots,
isintegral,
ismonic

function lejaorder!(roots) # see https://doi.org/10.1023/A:1025555803588
if length(roots) <= 2
return roots
end
#ii = argmax(j -> abs(roots[j]), eachindex(roots))
ii = firstindex(roots)
rmax = abs(roots[ii])
for j in eachindex(roots)
rabs = abs(roots[j])
if rabs > rmax
ii = j
rmax = rabs
end
end
Comment on lines +24 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively, Compat.jl provides that argmax method. Would depending on Compat be an option or should we keep this loop? (Same below.)

Copy link
Member

Choose a reason for hiding this comment

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

I don't mind having some small copy of package code in this package for easier compatability. I'd keep the loop unless you have a strong preference.

roots[1], roots[ii] = roots[ii], roots[1]
products = abs.(roots .- roots[1])
for k in eachindex(roots)[begin+1:end-1]
ii = findnext(iszero, products, k) # product[ii] == 0 means that roots[ii] == roots[k-1]
if isnothing(ii)
#ii = argmax(i -> products[i], k:lastindex(roots))
ii = k
for j in k+1:lastindex(products)
if products[j] > products[ii]
ii = j
end
end
end
roots[k], roots[ii] = roots[ii], roots[k]
products[k], products[ii] = products[ii], products[k]
products .*= abs.(roots .- roots[k])
end
return roots
end
lejaorder(roots) = lejaorder!(copy(roots))

"""
fromroots(::AbstractVector{<:Number}; var=:x)
fromroots(::Type{<:AbstractPolynomial}, ::AbstractVector{<:Number}; var=:x)
Expand All @@ -33,7 +68,7 @@ Polynomial(6 - 5*x + x^2)
"""
function fromroots(P::Type{<:AbstractPolynomial}, rs; var::SymbolLike = :x)
x = variable(P, var)
p = prod(x-r for r ∈ rs; init=one(x))
p = prod(x-r for r ∈ lejaorder(rs); init=one(x))
p = truncate!!(p)
p
end
Expand Down
1 change: 1 addition & 0 deletions src/polynomials/standard-basis/standard-basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ end
## polynomial-roots
function fromroots(P::Type{<:AbstractDenseUnivariatePolynomial{StandardBasis}}, r::AbstractVector{T}; var::SymbolLike = Var(:x)) where {T <: Number}
n = length(r)
r = lejaorder(r)
c = zeros(T, n + 1)
c[1] = one(T)
for j in 1:n, i in j:-1:1
Expand Down
4 changes: 4 additions & 0 deletions test/StandardBasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,10 @@ end
b = fromroots(r)
(b ≈ a) & isreal(coeffs(b)) # the coeff should be real
end
p = Polynomial([1; zeros(99); -1])
if P !== FactoredPolynomial
@test fromroots(P, roots(p)) * p[end] ≈ p
Comment on lines +988 to +989
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For FactoredPolynomial, fromroots is trivially correct; the actual conversion happens when promoting to a common type with p. That goes through evalpoly which uses an arbitrary ordering of the roots. I wonder whether it would generally be preferable to use the Leja ordering in evalpoly here, in which case it would probably be worth it to replace the Dict in FactoredPolynomial with an OrderedDict and do the Leja ordering upon construction. No clue whether that's a good idea.

Copy link
Member

Choose a reason for hiding this comment

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

Well that's a question for another day, but it seems like a reasonable thing to do. The extra package has negligible load time.

end
end
end

Expand Down
Loading