Skip to content

Commit 1571b10

Browse files
committed
printpoly api changes and exponent offset.
Changed `printpoly` and relevant functions' API for to better control custom printing or to print own polynomials. More importantly, can now add an integer offset to exponents when printing, mostly for printing DSP-related polynomials.
1 parent 0f85564 commit 1571b10

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/show.jl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,50 +70,59 @@ end
7070
###
7171

7272
"""
73-
printpoly(io::IO, p::Poly, mimetype = MIME"text/plain"(); descending_powers=false)
73+
printpoly(io::IO, p::Poly, mimetype = MIME"text/plain"(); descending_powers=false, offset::Int=0)
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.
79+
`offset` allows for an integer number to be added to the exponent, just for printing.
7980
8081
# Examples
8182
```jldoctest
8283
julia> printpoly(stdout, Poly([1,2,3], :y))
8384
1 + 2*y + 3*y^2
8485
julia> printpoly(stdout, Poly([1,2,3], :y), descending_powers=true)
8586
3*y^2 + 2*y + 1
87+
julia> printpoly(stdout, Poly([2, 3, 1], :z), descending_powers=true, offset=-2)
88+
1 + 3*z^-1 + 2*z^-2
89+
julia> printpoly(stdout, Poly([-1, 0, 1], :z), offset=-1, descending_powers=true)
90+
z - z^-1
8691
```
8792
"""
88-
function printpoly(io::IO, p::Poly{T}, mimetype = MIME"text/plain"(); descending_powers=false) where {T}
93+
function printpoly(io::IO, p::Poly{T}, mimetype=MIME"text/plain"(); descending_powers=false, offset::Int=0) where {T}
8994
first = true
9095
printed_anything = false
9196
for i in (descending_powers ? reverse(eachindex(p)) : eachindex(p))
92-
printed = showterm(io,p,i,first, mimetype)
97+
printed = showterm(io, p[i], p.var, i+offset, first, mimetype)
9398
first &= !printed
9499
printed_anything |= printed
95100
end
96101
printed_anything || print(io, zero(T))
97102
return nothing
98103
end
99104

100-
function showterm(io::IO,p::Poly{T},j,first, mimetype) where {T}
101-
pj = p[j]
105+
"""
106+
showterm(io::IO, pj, var, j, first, mimetype)
102107
108+
Show the term `pj * var^j`.
109+
Returns `true` after successfully printing.
110+
"""
111+
function showterm(io::IO, pj::T, var, j, first::Bool, mimetype) where {T}
103112
pj == zero(T) && return false
104113

105-
pj = printsign(io, pj, j, first, mimetype)
114+
pj = printsign(io, pj, first, mimetype)
106115
!(pj == one(T) && !(showone(T) || j == 0)) && printcoefficient(io, pj, j, mimetype)
107116
printproductsign(io, pj, j, mimetype)
108-
printexponent(io,p.var,j, mimetype)
117+
printexponent(io, var, j, mimetype)
109118
true
110119
end
111120

112121

113122

114123
## print the sign
115124
## returns aspos(pj)
116-
function printsign(io::IO, pj::T, j, first, mimetype) where {T}
125+
function printsign(io::IO, pj::T, first, mimetype) where {T}
117126
neg = isneg(pj)
118127
if first
119128
neg && print(io, showop(mimetype, "l-")) #Prepend - if first and negative

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ function printpoly_to_string(args...; kwargs...)
280280
end
281281
@test printpoly_to_string(Poly([1,2,3], "y")) == "1 + 2*y + 3*y^2"
282282
@test printpoly_to_string(Poly([1,2,3], "y"), descending_powers=true) == "3*y^2 + 2*y + 1"
283+
@test printpoly_to_string(Poly([2, 3, 1], :z), descending_powers=true, offset=-2) == "1 + 3*z^-1 + 2*z^-2"
284+
@test printpoly_to_string(Poly([-1, 0, 1], :z), offset=-1, descending_powers=true) == "z - z^-1"
283285

284286
## want to be able to copy and paste
285287
## check hashing

0 commit comments

Comments
 (0)