Skip to content

Commit 0aab8d7

Browse files
committed
complex numbers
1 parent 61988de commit 0aab8d7

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

src/InTheRed.jl

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,61 @@ end
210210
# That's called by b & c but not d:
211211
# (a = 3f0, b = randn(3) .> 0, c = [true, false], d = randn(1000) .> 0)
212212

213+
#####
214+
##### complex
215+
#####
216+
217+
# function show(io::IO, z::Complex)
218+
# r, i = reim(z)
219+
# compact = get(io, :compact, false)::Bool
220+
# show(io, r)
221+
# if signbit(i) && !isnan(i)
222+
# print(io, compact ? "-" : " - ")
223+
# if isa(i,Signed) && !isa(i,BigInt) && i == typemin(typeof(i))
224+
# show(io, -widen(i))
225+
# else
226+
# show(io, -i)
227+
# end
228+
# else
229+
# print(io, compact ? "+" : " + ")
230+
# show(io, i)
231+
# end
232+
# if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i))
233+
# print(io, "*")
234+
# end
235+
# print(io, "im")
236+
# end
237+
238+
function Base.show(io::IO, z::Complex{<:Union{Integer, AbstractFloat}})
239+
r, i = reim(z)
240+
compact = get(io, :compact, false)::Bool
241+
show(io, r)
242+
if signbit(i) && !isnan(i)
243+
print(io, compact ? "-" : " - ")
244+
# if isa(i,Signed) && !isa(i,BigInt) && i == typemin(typeof(i))
245+
# show(io, -widen(i))
246+
# else
247+
# show(io, -i)
248+
# end
249+
str = sprint(show, i, context=IOContext(io))
250+
j = findfirst('-', str)
251+
str = string(str[1:j-1], str[j+1:end])
252+
print(io, str)
253+
else
254+
print(io, compact ? "+" : " + ")
255+
show(io, i)
256+
end
257+
if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i))
258+
print(io, "*")
259+
end
260+
print(io, "im")
261+
262+
# iscolor = get(io, :color, false)::Bool
263+
# q = iscolor && _preprint(io, i)
264+
# print(io, "im") # this looks weird for randn(ComplexF32, 30)
265+
# q && _postprint(io)
266+
end
267+
213268

214269
#####
215270
##### types
@@ -286,7 +341,7 @@ function Base.print_matrix_row(io::IO,
286341
2::Int) # this 2 is a trick to make 2nd column line up!
287342

288343
skip && return
289-
printstyled(io, " # ", hidden=true)
344+
printstyled(io, " # ", hidden=true, color=:light_black) # some terminals can't do hidden=true
290345

291346
WIDTH = 33.0 # NB not an integer, else `2*WIDTH * xi` overflows Float16
292347

@@ -320,12 +375,14 @@ function Base.print_matrix_row(io::IO,
320375
printstyled(io, repeat(" ", spaces), minus_str, mid_str, plus_str; color)
321376
end
322377

378+
ARROWS = collect("➡️↗️⬆️↖️⬅️↙️⬇️↘️0️⃣⏹")[1:2:end]
379+
323380
function _arrow(x::Complex)
324-
symb = collect("➡️↗️⬆️↖️⬅️↙️⬇️↘️0️⃣⏹")[1:2:end]
325381
isfinite(x) || return " "
326-
iszero(x) && return symb[end-1]
327-
i = trunc(Int, mod(angle(x) - pi/8, 2pi) * (4/pi)) + 1
328-
symb[mod1(i+1, 8)]
382+
iszero(x) && return ARROWS[end-1]
383+
i = trunc(Int, mod(angle(x) - pi / 8, 2pi) * (4 / pi)) + 1
384+
ARROWS[mod1(i + 1, 8)]
385+
end
329386
end
330387

331388
end # module InTheRed

0 commit comments

Comments
 (0)