Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short form printing #144

Merged
merged 10 commits into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,31 @@ Base.print_matrix_row(io::IO,

# Display concise description of a Fill.

function Base.show(io::IO, ::MIME"text/plain", x::Union{Eye, AbstractFill})
if get(IOContext(io), :compact, false) # for example [Fill(i==j,2,2) for i in 1:3, j in 1:4]
return show(io, x)
end
summary(io, x)
if x isa Union{Zeros, Ones, Eye}
# then no need to print entries
elseif length(x) > 1
print(io, ", with entries equal to ", getindex_value(x))
else
print(io, ", with entry equal to ", getindex_value(x))
end
end

Base.show(io::IO, x::AbstractFill) = print(io, "$(summary(x)): entries equal to $(getindex_value(x))")
Base.show(io::IO, x::Union{Zeros,Ones,Eye}) = print(io, "$(summary(x))")
function Base.show(io::IO, x::AbstractFill) # for example (Fill(π,3),)
print(io, nameof(typeof(x)), "(")
if x isa Union{Zeros, Ones}
else
show(io, getindex_value(x)) # show not print to handle (Fill(1f0,2),)
print(io, ", ")
end
join(io, size(x), ", ")
print(io, ")")
end
Base.show(io::IO, x::Eye) = print(io, "Eye(", size(x,1), ")")

if VERSION ≥ v"1.5"
Base.array_summary(io::IO, ::Zeros{T}, inds::Tuple{Vararg{Base.OneTo}}) where T =
Expand All @@ -633,13 +655,12 @@ if VERSION ≥ v"1.5"
print(io, Base.dims2string(length.(inds)), " Eye{$T}")
end

Base.show(io::IO, ::MIME"text/plain", x::Union{Eye,AbstractFill}) = show(io, x)

##
# interface
##

getindex_value(a::LinearAlgebra.AdjOrTrans) = getindex_value(parent(a))
getindex_value(a::LinearAlgebra.AdjOrTrans) = getindex_value(parent(a)) # FillArrays.getindex_value(Adjoint(Fill(1+im,2,2)))
getindex_value(a::SubArray) = getindex_value(parent(a))


Expand Down
14 changes: 10 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1132,16 +1132,22 @@ end
@test_throws DimensionMismatch dot(u, Z, v[1:end-1])
end

if VERSION ≥ v"1.5"
@testset "print" begin
@testset "print" begin
if VERSION ≥ v"1.5"
@test stringmime("text/plain", Zeros(3)) == "3-element Zeros{Float64}"
@test stringmime("text/plain", Ones(3)) == "3-element Ones{Float64}"
@test stringmime("text/plain", Fill(7,2)) == "2-element Fill{$Int}: entries equal to 7"
@test stringmime("text/plain", Fill(7,2)) == "2-element Fill{$Int}, with entries equal to 7"
@test stringmime("text/plain", Zeros(3,2)) == "3×2 Zeros{Float64}"
@test stringmime("text/plain", Ones(3,2)) == "3×2 Ones{Float64}"
@test stringmime("text/plain", Fill(7,2,3)) == "2×3 Fill{$Int}: entries equal to 7"
@test stringmime("text/plain", Fill(7,2,3)) == "2×3 Fill{$Int}, with entries equal to 7"
@test stringmime("text/plain", Fill(8.0,1)) == "1-element Fill{Float64}, with entry equal to 8.0"
@test stringmime("text/plain", Eye(5)) == "5×5 Eye{Float64}"
end
@test repr(Zeros(3)) == "Zeros(3)"
@test repr(Ones(3,2)) == "Ones(3, 2)"
@test repr(Fill(7,3,2)) == "Fill(7, 3, 2)"
@test repr(Fill(1f0,10)) == "Fill(1.0f0, 10)" # Float32!
@test repr(Eye(9)) == "Eye(9)"
end

@testset "reshape" begin
Expand Down