Skip to content

Commit ad67b84

Browse files
authored
Merge pull request #243 from gustaphe/master
Print imaginay unit as "i" with latex mimetype
2 parents 6605bc5 + 5c22aeb commit ad67b84

File tree

4 files changed

+127
-84
lines changed

4 files changed

+127
-84
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1212

1313
[compat]
1414
Intervals = "0.5, 1.0, 1.3"
15-
RecipesBase = "0.7, 0.8, 1"
1615
OffsetArrays = "1"
16+
RecipesBase = "0.7, 0.8, 1"
1717
julia = "1"
1818

1919
[extras]

src/show.jl

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ function printproductsign(io::IO, pj::T, j, mimetype) where {T}
159159
(showone(T) || pj != one(T)) && print(io, showop(mimetype, "*"))
160160
end
161161

162+
function printproductsign(io::IO, pj::T, j, mimetype) where {T<:Complex}
163+
j == 0 && return
164+
(a,b) = reim(pj)
165+
!iszero(a) && !iszero(b) && return # parentheses inserted, no * needed
166+
!iszero(a) && return printproductsign(io, a, j, mimetype)
167+
print(io, showop(mimetype, "*"))
168+
end
169+
170+
162171
# show a single term
163172
# Other types can overload Polynomials.printcofficient with a mimetype
164173
# or Base.show_unquoted(io, pj, indent, prec)
@@ -203,24 +212,51 @@ function printcoefficient(io::IO, a::Rational{T}, j, mimetype::MIME"text/latex")
203212
end
204213

205214
# print complex numbers with parentheses as needed
206-
function printcoefficient(io::IO, pj::Complex{T}, j, mimetype) where {T}
215+
function printcoefficient(io::IO, pj::S, j, mimetype) where {T,S <: Complex{T}}
207216

208-
hasreal = abs(real(pj)) > 0 || isnan(real(pj)) || isinf(real(pj))
209-
hasimag = abs(imag(pj)) > 0 || isnan(imag(pj)) || isinf(imag(pj))
217+
(a,b) = reim(pj)
218+
hasreal = !iszero(a) || isnan(a) || isinf(a)
219+
hasimag = !iszero(b) || isnan(b) || isinf(b)
210220

211221
if hasreal && hasimag
212-
Base.show_unquoted(io, pj, 0, Base.operator_precedence(:*))
222+
iszero(j) || print(io, "(")
223+
print(io, a)
224+
225+
# print b
226+
if isone(b) || isone(-b)
227+
if hasneg(S) && b < 0
228+
print(io, showop(mimetype, "-"))
229+
else
230+
print(io, showop(mimetype, "+"))
231+
end
232+
else
233+
if hasneg(S) && b < 0
234+
print(io, showop(mimetype, "-"))
235+
(showone(S) || !isone(-b)) && print(io, -b)
236+
else
237+
print(io, showop(mimetype,"+"))
238+
print(io, b)
239+
end
240+
(isnan(b) || isinf(b)) && print(io, showop(mimetype, "*"))
241+
end
242+
243+
print(io, imagsymbol(mimetype))
244+
iszero(j) || print(io, ")")
245+
213246
elseif hasreal
214-
a = real(pj)
215-
(j==0 || showone(T) || a != one(T)) && printcoefficient(io, a, j, mimetype)
247+
248+
(iszero(j) || showone(T) || isone(a)) && printcoefficient(io, a, j, mimetype)
249+
216250
elseif hasimag
217-
b = imag(pj)
218-
(showone(T) || b != one(T)) && printcoefficient(io, b, j, mimetype)
219-
(isnan(imag(pj)) || isinf(imag(pj))) && print(io, showop(mimetype, "*"))
220-
print(io, im)
221-
else
222-
return nothing
251+
252+
(showone(T) || !isone(b)) && printcoefficient(io, b, j, mimetype)
253+
(isnan(b) || isinf(b)) && print(io, showop(mimetype, "*"))
254+
print(io, imagsymbol(mimetype))
255+
223256
end
257+
258+
return nothing
259+
224260
end
225261

226262
## show exponent
@@ -252,3 +288,6 @@ function unicode_subscript(io, j)
252288
print(io, a[Int(i)-44])
253289
end
254290
end
291+
292+
imagsymbol(::Any) = "im"
293+
imagsymbol(::MIME"text/latex") = "i"

test/Poly.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,16 @@ p = Poly([1.0, 2.0, 3.0, 1.0])
286286
p = Poly([1, im])
287287
@test repr(p) == "Poly(1 + im*x)"
288288
p = Poly([1+im, 1-im, -1+im, -1 - im])# minus signs
289-
@test repr(p) == "Poly((1 + 1im) + (1 - 1im)*x - (1 - 1im)*x^2 - (1 + 1im)*x^3)"
289+
@test repr(p) == "Poly(1 + im + (1 - im)x - (1 - im)x^2 - (1 + im)x^3)"
290290
p = Poly([1.0, 0 + NaN*im, NaN, Inf, 0 - Inf*im]) # handle NaN or Inf appropriately
291291
@test repr(p) == "Poly(1.0 + NaN*im*x + NaN*x^2 + Inf*x^3 - Inf*im*x^4)"
292292

293293
p = Poly([1,2,3])
294-
295294
@test repr("text/latex", p) == "\$1 + 2\\cdot x + 3\\cdot x^{2}\$"
296295
p = Poly([1//2, 2//3, 1])
297296
@test repr("text/latex", p) == "\$\\frac{1}{2} + \\frac{2}{3}\\cdot x + x^{2}\$"
297+
p = Poly([complex(1,1),complex(0,1),complex(1,0),complex(1,1)])
298+
@test repr("text/latex", p) == "\$1 + i + i\\cdot x + x^{2} + (1 + i)x^{3}\$"
298299

299300
# customized printing with printpoly
300301
function printpoly_to_string(args...; kwargs...)

0 commit comments

Comments
 (0)