Skip to content

Commit 6e60f2d

Browse files
authored
Merge pull request #125 from martinholters/printpoly
Extend and export printpoly
2 parents 8a71269 + e85de28 commit 6e60f2d

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

REQUIRE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.4
2-
Compat 0.9.4
2+
Compat 0.9.5

docs/src/index.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ The package can then be loaded into the current session using
1414
```julia
1515
using Polynomials
1616
```
17+
```@meta
18+
DocTestSetup = :( using Polynomials )
19+
```
1720

1821
## Functions
1922

@@ -27,6 +30,7 @@ poly
2730
degree
2831
coeffs
2932
variable
33+
printpoly
3034
polyval
3135
polyint
3236
polyder

src/Polynomials.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Polynomials
88
using Compat
99

1010
export Poly, poly
11-
export degree, coeffs, variable
11+
export degree, coeffs, variable, printpoly
1212
export polyval, polyint, polyder, roots, polyfit
1313
export Pade, padeval
1414

src/show.jl

+19-2
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,32 @@ end
6868

6969
###
7070

71-
function printpoly{T}(io::IO, p::Poly{T}, mimetype)
71+
"""
72+
printpoly(io::IO, p::Poly, mimetype = MIME"text/plain"(); descending_powers=false)
73+
74+
Print a human-readable representation of the polynomial `p` to `io`. The MIME
75+
types "text/plain" (default), "text/latex", and "text/html" are supported. By
76+
default, the terms are in order of ascending powers, matching the order in
77+
`coeffs(p)`; specifying `descending_powers=true` reverses the order.
78+
79+
# Examples
80+
```jldoctest
81+
julia> printpoly(STDOUT, Poly([1,2,3], :y))
82+
1 + 2*y + 3*y^2
83+
julia> printpoly(STDOUT, Poly([1,2,3], :y), descending_powers=true)
84+
3*y^2 + 2*y + 1
85+
```
86+
"""
87+
function printpoly{T}(io::IO, p::Poly{T}, mimetype = MIME"text/plain"(); descending_powers=false)
7288
first = true
7389
printed_anything = false
74-
for i in eachindex(p)
90+
for i in (descending_powers ? reverse(eachindex(p)) : eachindex(p))
7591
printed = showterm(io,p,i,first, mimetype)
7692
first &= !printed
7793
printed_anything |= printed
7894
end
7995
printed_anything || print(io, zero(T))
96+
return nothing
8097
end
8198

8299
function showterm{T}(io::IO,p::Poly{T},j,first, mimetype)

test/runtests.jl

+8
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ p = Poly([1,2,3])
248248
p = Poly([1//2, 2//3, 1])
249249
@test reprmime("text/latex", p) == "\$\\frac{1}{2} + \\frac{2}{3}\\cdot x + x^{2}\$"
250250

251+
# customized printing with printpoly
252+
function printpoly_to_string(args...; kwargs...)
253+
buf = IOBuffer()
254+
printpoly(buf, args...; kwargs...)
255+
return Compat.String(take!(buf))
256+
end
257+
@test printpoly_to_string(Poly([1,2,3], "y")) == "1 + 2*y + 3*y^2"
258+
@test printpoly_to_string(Poly([1,2,3], "y"), descending_powers=true) == "3*y^2 + 2*y + 1"
251259

252260
## want to be able to copy and paste
253261
if VERSION < v"0.6.0-dev"

0 commit comments

Comments
 (0)