Skip to content

Commit

Permalink
Fix printing
Browse files Browse the repository at this point in the history
  • Loading branch information
david-pl committed Jul 30, 2018
1 parent a57d480 commit eabd7eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ function show(stream::IO, x::DenseOperator)
summary(stream, x)
print(stream, "\n")
if !_std_order
if !haskey(stream, :compact)
stream = IOContext(stream, :compact => true)
end
Base.print_array(stream, round.(x.data; digits=machineprecorder))
else
showarray_stdord(stream, round.(x.data; digits=machineprecorder), x.basis_l.shape, x.basis_r.shape, false, header=false)
Expand All @@ -135,6 +138,9 @@ function show(stream::IO, x::SparseOperator)
print(stream, "\n []")
else
if !_std_order
if !haskey(stream, :compact)
stream = IOContext(stream, :compact => true)
end
show(stream, round.(x.data; digits=machineprecorder))
else
showsparsearray_stdord(stream, round.(x.data; digits=machineprecorder), x.basis_l.shape, x.basis_r.shape)
Expand Down Expand Up @@ -437,7 +443,7 @@ function print_matrix_std(io::IO, X::AbstractVecOrMat, ldims::Vector, rdims::Vec
end

function showarray_stdord(io::IO, X::AbstractVecOrMat, ldims::Vector, rdims::Vector, repr::Bool = true; header = true)
if !haskey(io, :compact)
if !haskey(io, :compact) && length(axes(X, 2)) > 1
io = IOContext(io, :compact => true)
end
if !isempty(X)
Expand All @@ -457,7 +463,7 @@ function showsparsearray_stdord(io::IO, S::SparseMatrixCSC, ldims::Vector, rdims
end
pad = ndigits(max(S.m,S.n))
sep = "\n "
if !haskey(io, :compact)
if !haskey(io, :compact) && length(axes(S, 2)) > 1
io = IOContext(io, :compact => true)
end

Expand Down
20 changes: 10 additions & 10 deletions test/test_printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ b_mb = ManyBodyBasis(b_fock, fermionstates(b_fock, 2))
@test sprint(show, b_mb) == "ManyBody(onebodybasis=Fock(cutoff=4), states:10)"

state = fockstate(FockBasis(2), 2)
@test sprint(show, state) == "Ket(dim=3)\n basis: Fock(cutoff=2)\n 0.0+0.0im\n 0.0+0.0im\n 1.0+0.0im"
@test sprint(show, state) == "Ket(dim=3)\n basis: Fock(cutoff=2)\n 0.0 + 0.0im\n 0.0 + 0.0im\n 1.0 + 0.0im"
state = dagger(state)
@test sprint(show, state) == "Bra(dim=3)\n basis: Fock(cutoff=2)\n 0.0-0.0im\n 0.0-0.0im\n 1.0-0.0im"
@test sprint(show, state) == "Bra(dim=3)\n basis: Fock(cutoff=2)\n 0.0 - 0.0im\n 0.0 - 0.0im\n 1.0 - 0.0im"

op = DenseOperator(FockBasis(1))
@test sprint(show, op) == "DenseOperator(dim=2x2)
Expand Down Expand Up @@ -82,23 +82,23 @@ Tpx = transform(bp, bx)
QuantumOptics.set_printing(standard_order=true)

n = fockstate(b_fock, 1)
@test sprint(show, n) == "Ket(dim=3)\n basis: Fock(cutoff=2)\n 0.0+0.0im\n 1.0+0.0im\n 0.0+0.0im"
@test sprint(show, n) == "Ket(dim=3)\n basis: Fock(cutoff=2)\n 0.0 + 0.0im\n 1.0 + 0.0im\n 0.0 + 0.0im"

spin1 = spindown(b_spin)
spin2 = spinup(b_spin)
state = n spin1 spin2
state_data = kron(n.data, spin1.data, spin2.data)
type_len = length("Complex{Float64}")
state_data_str = join(split(sprint(show, state_data)[type_len+2:end-1], ','), "\n")
state_data_str = sprint(Base.print_array, state_data)
@test sprint(show, state) == "Ket(dim=12)
basis: [Fock(cutoff=2) ⊗ Spin(1/2) ⊗ Spin(1/2)]\n "*state_data_str
basis: [Fock(cutoff=2) ⊗ Spin(1/2) ⊗ Spin(1/2)]\n"*state_data_str

state_data_str = join(split(sprint(show, state_data')[type_len+2:end-1]), "\n ")
state_data_str = sprint(Base.print_array, conj.(state_data))
@test sprint(show, dagger(state)) == "Bra(dim=12)
basis: [Fock(cutoff=2) ⊗ Spin(1/2) ⊗ Spin(1/2)]\n "*state_data_str
basis: [Fock(cutoff=2) ⊗ Spin(1/2) ⊗ Spin(1/2)]\n"*state_data_str

op = dm(state)
op_data = state_data * state_data'
type_len = length("Complex{Float64}")
op_data_str1 = split(sprint(show, op_data)[type_len+2:end-1], ";")
for i=1:length(op_data_str1)
op_data_str1[i] = join(split(op_data_str1[i]), " ")
Expand Down Expand Up @@ -141,9 +141,9 @@ nlevel3 = basisstate(NLevelBasis(3), 3)
# Test switching back
QuantumOptics.set_printing(standard_order=false)
state_data = kron(spin2.data, spin1.data, n.data)
state_data_str = join(split(sprint(show, state_data)[type_len+2:end-1], ','), "\n")
state_data_str = sprint(Base.print_array, state_data)
@test sprint(show, state) == "Ket(dim=12)
basis: [Fock(cutoff=2) ⊗ Spin(1/2) ⊗ Spin(1/2)]\n "*state_data_str
basis: [Fock(cutoff=2) ⊗ Spin(1/2) ⊗ Spin(1/2)]\n"*state_data_str


end # testset

0 comments on commit eabd7eb

Please sign in to comment.