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

Adding kwargs to roots and printpoly #185

Merged
merged 2 commits into from
Nov 7, 2019
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
21 changes: 16 additions & 5 deletions src/Polynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,11 @@ polyder(a::AbstractArray{Poly{T}}, order::Int = 1) where {T} = map(p->polyder(p,

# compute the roots of a polynomial
"""
roots(p::Poly)
roots(p::Poly; kwargs...)

Return the roots (zeros) of `p`, with multiplicity. The number of roots
returned is equal to the order of `p`. The returned roots may be real or
complex.
complex. Keyword arguments `kwargs` are sent to the `eigvals` call.

# Examples

Expand All @@ -610,9 +610,20 @@ julia> roots(poly([1,2,3,4]))
3.0
2.0
1.0

julia> roots(Poly([1, 0, -1]), scale=false, permute=false)
2-element Array{Float64,1}:
-1.0
1.0

# In Julia > 1.2
julia> roots(Poly([1, 0, -1]), sortby = t -> -real(t))
2-element Array{Float64,1}:
1.0
-1.0
```
"""
function roots(p::Poly{T}) where {T}
function roots(p::Poly{T}; kwargs...) where {T}
R = promote_type(T, Float64)
length(p) == 0 && return zeros(R, 0)

Expand All @@ -634,12 +645,12 @@ function roots(p::Poly{T}) where {T}
an = p[end-num_trailing_zeros]
companion[1,:] = -p[(end-num_trailing_zeros-1):-1:num_leading_zeros] / an

D = eigvals(companion)
D = eigvals(companion; kwargs...)
r = zeros(eltype(D),length(p)-num_trailing_zeros-1)
r[1:n] = D
return r
end
roots(p::Poly{Rational{T}}) where {T} = roots(convert(Poly{promote_type(T, Float64)}, p))
roots(p::Poly{Rational{T}}; kwargs...) where {T} = roots(convert(Poly{promote_type(T, Float64)}, p); kwargs...)

## compute gcd of two polynomials
"""
Expand Down
9 changes: 6 additions & 3 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ end
###

"""
printpoly(io::IO, p::Poly, mimetype = MIME"text/plain"(); descending_powers=false, offset::Int=0)
printpoly(io::IO, p::Poly, mimetype = MIME"text/plain"(); descending_powers=false, offset::Int=0, var=p.var)

Print a human-readable representation of the polynomial `p` to `io`. The MIME
types "text/plain" (default), "text/latex", and "text/html" are supported. By
default, the terms are in order of ascending powers, matching the order in
`coeffs(p)`; specifying `descending_powers=true` reverses the order.
`offset` allows for an integer number to be added to the exponent, just for printing.
`var` allows for overriding the variable used for printing.

# Examples
```jldoctest
Expand All @@ -88,13 +89,15 @@ julia> printpoly(stdout, Poly([2, 3, 1], :z), descending_powers=true, offset=-2)
1 + 3*z^-1 + 2*z^-2
julia> printpoly(stdout, Poly([-1, 0, 1], :z), offset=-1, descending_powers=true)
z - z^-1
julia> printpoly(stdout, Poly([-1, 0, 1], :z), offset=-1, descending_powers=true, var=:x)
x - x^-1
```
"""
function printpoly(io::IO, p::Poly{T}, mimetype=MIME"text/plain"(); descending_powers=false, offset::Int=0) where {T}
function printpoly(io::IO, p::Poly{T}, mimetype=MIME"text/plain"(); descending_powers=false, offset::Int=0, var=p.var) where {T}
first = true
printed_anything = false
for i in (descending_powers ? reverse(eachindex(p)) : eachindex(p))
printed = showterm(io, p[i], p.var, i+offset, first, mimetype)
printed = showterm(io, p[i], var, i+offset, first, mimetype)
first &= !printed
printed_anything |= printed
end
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ end
@test printpoly_to_string(Poly([2, 3, 1], :z), descending_powers=true, offset=-2) == "1 + 3*z^-1 + 2*z^-2"
@test printpoly_to_string(Poly([-1, 0, 1], :z), offset=-1, descending_powers=true) == "z - z^-1"

# Override symbol in print Issue: #184
@test printpoly_to_string(Poly([1,2,3], "y"), var=:z) == "1 + 2*z + 3*z^2"
@test printpoly_to_string(Poly([1,2,3], "y"), descending_powers=true, var="z") == "3*z^2 + 2*z + 1"
@test printpoly_to_string(Poly([2, 3, 1], :z), descending_powers=true, offset=-2, var=:y) == "1 + 3*y^-1 + 2*y^-2"
@test printpoly_to_string(Poly([-1, 0, 1], :z), offset=-1, descending_powers=true, var='y') == "y - y^-1"

## want to be able to copy and paste
## check hashing
p = poly([1,2,3])
Expand Down