Skip to content

Commit 86ee9a2

Browse files
authored
Merge pull request #185 from mfalt/kwargs
Adding kwargs to roots and printpoly
2 parents bdfa5e3 + 7b8265f commit 86ee9a2

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/Polynomials.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,11 @@ polyder(a::AbstractArray{Poly{T}}, order::Int = 1) where {T} = map(p->polyder(p,
580580

581581
# compute the roots of a polynomial
582582
"""
583-
roots(p::Poly)
583+
roots(p::Poly; kwargs...)
584584
585585
Return the roots (zeros) of `p`, with multiplicity. The number of roots
586586
returned is equal to the order of `p`. The returned roots may be real or
587-
complex.
587+
complex. Keyword arguments `kwargs` are sent to the `eigvals` call.
588588
589589
# Examples
590590
@@ -610,9 +610,20 @@ julia> roots(poly([1,2,3,4]))
610610
3.0
611611
2.0
612612
1.0
613+
614+
julia> roots(Poly([1, 0, -1]), scale=false, permute=false)
615+
2-element Array{Float64,1}:
616+
-1.0
617+
1.0
618+
619+
# In Julia > 1.2
620+
julia> roots(Poly([1, 0, -1]), sortby = t -> -real(t))
621+
2-element Array{Float64,1}:
622+
1.0
623+
-1.0
613624
```
614625
"""
615-
function roots(p::Poly{T}) where {T}
626+
function roots(p::Poly{T}; kwargs...) where {T}
616627
R = promote_type(T, Float64)
617628
length(p) == 0 && return zeros(R, 0)
618629

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

637-
D = eigvals(companion)
648+
D = eigvals(companion; kwargs...)
638649
r = zeros(eltype(D),length(p)-num_trailing_zeros-1)
639650
r[1:n] = D
640651
return r
641652
end
642-
roots(p::Poly{Rational{T}}) where {T} = roots(convert(Poly{promote_type(T, Float64)}, p))
653+
roots(p::Poly{Rational{T}}; kwargs...) where {T} = roots(convert(Poly{promote_type(T, Float64)}, p); kwargs...)
643654

644655
## compute gcd of two polynomials
645656
"""

src/show.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ end
7070
###
7171

7272
"""
73-
printpoly(io::IO, p::Poly, mimetype = MIME"text/plain"(); descending_powers=false, offset::Int=0)
73+
printpoly(io::IO, p::Poly, mimetype = MIME"text/plain"(); descending_powers=false, offset::Int=0, var=p.var)
7474
7575
Print a human-readable representation of the polynomial `p` to `io`. The MIME
7676
types "text/plain" (default), "text/latex", and "text/html" are supported. By
7777
default, the terms are in order of ascending powers, matching the order in
7878
`coeffs(p)`; specifying `descending_powers=true` reverses the order.
7979
`offset` allows for an integer number to be added to the exponent, just for printing.
80+
`var` allows for overriding the variable used for printing.
8081
8182
# Examples
8283
```jldoctest
@@ -88,13 +89,15 @@ julia> printpoly(stdout, Poly([2, 3, 1], :z), descending_powers=true, offset=-2)
8889
1 + 3*z^-1 + 2*z^-2
8990
julia> printpoly(stdout, Poly([-1, 0, 1], :z), offset=-1, descending_powers=true)
9091
z - z^-1
92+
julia> printpoly(stdout, Poly([-1, 0, 1], :z), offset=-1, descending_powers=true, var=:x)
93+
x - x^-1
9194
```
9295
"""
93-
function printpoly(io::IO, p::Poly{T}, mimetype=MIME"text/plain"(); descending_powers=false, offset::Int=0) where {T}
96+
function printpoly(io::IO, p::Poly{T}, mimetype=MIME"text/plain"(); descending_powers=false, offset::Int=0, var=p.var) where {T}
9497
first = true
9598
printed_anything = false
9699
for i in (descending_powers ? reverse(eachindex(p)) : eachindex(p))
97-
printed = showterm(io, p[i], p.var, i+offset, first, mimetype)
100+
printed = showterm(io, p[i], var, i+offset, first, mimetype)
98101
first &= !printed
99102
printed_anything |= printed
100103
end

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ end
283283
@test printpoly_to_string(Poly([2, 3, 1], :z), descending_powers=true, offset=-2) == "1 + 3*z^-1 + 2*z^-2"
284284
@test printpoly_to_string(Poly([-1, 0, 1], :z), offset=-1, descending_powers=true) == "z - z^-1"
285285

286+
# Override symbol in print Issue: #184
287+
@test printpoly_to_string(Poly([1,2,3], "y"), var=:z) == "1 + 2*z + 3*z^2"
288+
@test printpoly_to_string(Poly([1,2,3], "y"), descending_powers=true, var="z") == "3*z^2 + 2*z + 1"
289+
@test printpoly_to_string(Poly([2, 3, 1], :z), descending_powers=true, offset=-2, var=:y) == "1 + 3*y^-1 + 2*y^-2"
290+
@test printpoly_to_string(Poly([-1, 0, 1], :z), offset=-1, descending_powers=true, var='y') == "y - y^-1"
291+
286292
## want to be able to copy and paste
287293
## check hashing
288294
p = poly([1,2,3])

0 commit comments

Comments
 (0)