|
70 | 70 | ###
|
71 | 71 |
|
72 | 72 | """
|
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) |
74 | 74 |
|
75 | 75 | Print a human-readable representation of the polynomial `p` to `io`. The MIME
|
76 | 76 | types "text/plain" (default), "text/latex", and "text/html" are supported. By
|
77 | 77 | default, the terms are in order of ascending powers, matching the order in
|
78 | 78 | `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. |
79 | 80 |
|
80 | 81 | # Examples
|
81 | 82 | ```jldoctest
|
82 | 83 | julia> printpoly(stdout, Poly([1,2,3], :y))
|
83 | 84 | 1 + 2*y + 3*y^2
|
84 | 85 | julia> printpoly(stdout, Poly([1,2,3], :y), descending_powers=true)
|
85 | 86 | 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 |
86 | 91 | ```
|
87 | 92 | """
|
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} |
89 | 94 | first = true
|
90 | 95 | printed_anything = false
|
91 | 96 | 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) |
93 | 98 | first &= !printed
|
94 | 99 | printed_anything |= printed
|
95 | 100 | end
|
96 | 101 | printed_anything || print(io, zero(T))
|
97 | 102 | return nothing
|
98 | 103 | end
|
99 | 104 |
|
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) |
102 | 107 |
|
| 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} |
103 | 112 | pj == zero(T) && return false
|
104 | 113 |
|
105 |
| - pj = printsign(io, pj, j, first, mimetype) |
| 114 | + pj = printsign(io, pj, first, mimetype) |
106 | 115 | !(pj == one(T) && !(showone(T) || j == 0)) && printcoefficient(io, pj, j, mimetype)
|
107 | 116 | printproductsign(io, pj, j, mimetype)
|
108 |
| - printexponent(io,p.var,j, mimetype) |
| 117 | + printexponent(io, var, j, mimetype) |
109 | 118 | true
|
110 | 119 | end
|
111 | 120 |
|
112 | 121 |
|
113 | 122 |
|
114 | 123 | ## print the sign
|
115 | 124 | ## 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} |
117 | 126 | neg = isneg(pj)
|
118 | 127 | if first
|
119 | 128 | neg && print(io, showop(mimetype, "l-")) #Prepend - if first and negative
|
|
0 commit comments