Skip to content

Commit

Permalink
Complex{Measurement{Num}} printing support
Browse files Browse the repository at this point in the history
  • Loading branch information
LebedevRI committed May 16, 2023
1 parent a547f60 commit 1e21b5c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ end
# Representation of complex measurements. Print something that is easy to
# understand and that can be meaningfully copy-pasted into the REPL, at least
# for standard numeric types.
# FIXME: does this handle complex symbolic Measurements correctly?
for mime in (MIME"text/plain", MIME"text/x-tex", MIME"text/x-latex")
function Base.show(io::IO, mtype::mime, measure::Complex{<:Measurement})
r, i = reim(measure)
compact = get(io, :compact, false)
print(io, "(")
show(io, mtype, r)
print(io, ")")
if signbit(i) && !isnan(i)
if !_is_symbolic(i.val) && signbit(i) && !isnan(i)
i = -i
print(io, compact ? "-" : " - ")
else
Expand All @@ -83,6 +82,16 @@ for mime in (MIME"text/plain", MIME"text/x-tex", MIME"text/x-latex")
end
end

function Base.show(io::IO, ::MIME"text/latex", measure::Complex{<:Measurement})
print(io, "\$")
Base.show(io, "text/x-latex", measure)
print(io, "\$")
end

function Base.show(io::IO, measure::Complex{<:Measurement})
Base.show(io, "text/plain", measure)
end

# This is adapted from base/show.jl for Complex type.
function Base.alignment(io::IO, measure::Measurement)
m = match(r"^(.*[])(.*)$", sprint(show, measure, context=io, sizehint=0))
Expand Down
35 changes: 35 additions & 0 deletions test/symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ end
@test repr(mime, x, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "x_val\\pmx_err"
end
end

@variables y_val, y_err
y = measurement(y_val, y_err)
c = complex(x, y)

@test_throws ErrorException repr(c, context=:error_digits=>-4)
@test repr(c) == "(x_val ± x_err) + (y_val ± y_err)im"
@test repr(c, context=:compact=>true) == "(x_val±x_err)+(y_val±y_err)im"
for error_digits in (0, 7)
@test repr(c, context=:error_digits=>error_digits) == "(x_val ± x_err) + (y_val ± y_err)im"
@test repr(c, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "(x_val±x_err)+(y_val±y_err)im"
end

@test repr("text/plain", c) == "(x_val ± x_err) + (y_val ± y_err)im"
@test repr("text/plain", c, context=:compact=>true) == "(x_val±x_err)+(y_val±y_err)im"
for error_digits in (0, 7)
@test repr("text/plain", c, context=:error_digits=>error_digits) == "(x_val ± x_err) + (y_val ± y_err)im"
@test repr("text/plain", c, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "(x_val±x_err)+(y_val±y_err)im"
end

@test repr("text/latex", c) == "\$(x_val \\pm x_err) + (y_val \\pm y_err)im\$"
@test repr("text/latex", c, context=:compact=>true) == "\$(x_val\\pmx_err)+(y_val\\pmy_err)im\$"
for error_digits in (0, 7)
@test repr("text/latex", c, context=:error_digits=>error_digits) == "\$(x_val \\pm x_err) + (y_val \\pm y_err)im\$"
@test repr("text/latex", c, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "\$(x_val\\pmx_err)+(y_val\\pmy_err)im\$"
end

for mime in ("text/x-tex", "text/x-latex")
@test repr(mime, c) == "(x_val \\pm x_err) + (y_val \\pm y_err)im"
@test repr(mime, c, context=:compact=>true) == "(x_val\\pmx_err)+(y_val\\pmy_err)im"
for error_digits in (0, 7)
@test repr(mime, c, context=:error_digits=>error_digits) == "(x_val \\pm x_err) + (y_val \\pm y_err)im"
@test repr(mime, c, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "(x_val\\pmx_err)+(y_val\\pmy_err)im"
end
end
end

##### Mathematical Operations
Expand Down

0 comments on commit 1e21b5c

Please sign in to comment.