Skip to content

Short form printing #144

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

Merged
merged 10 commits into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FillArrays"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.11.9"
version = "0.12.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
30 changes: 26 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),)
ndims(x) > 0 && 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,13 @@ 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.Adjoint) = adjoint(getindex_value(parent(a)))
getindex_value(a::LinearAlgebra.Transpose) = transpose(getindex_value(parent(a)))
getindex_value(a::SubArray) = getindex_value(parent(a))


Expand Down
29 changes: 20 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1132,16 +1132,27 @@ 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"
# 3-arg show, full printing
@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
# 2-arg show, compact printing
@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(Fill(0)) == "Fill(0)"
@test repr(Eye(9)) == "Eye(9)"
# also used for arrays of arrays:
@test occursin("Eye(2) ", stringmime("text/plain", [Eye(2) for i in 1:2, j in 1:2]))
end

@testset "reshape" begin
Expand Down Expand Up @@ -1236,10 +1247,10 @@ end
end

@testset "adjtrans" begin
a = Fill(2.0,5)
@test FillArrays.getindex_value(a') == FillArrays.unique_value(a') == 2.0
@test convert(Fill, a') ≡ Fill(2.0,1,5)
@test FillArrays.getindex_value(transpose(a)) == FillArrays.unique_value(transpose(a)) == 2.0
@test convert(Fill, transpose(a)) ≡ Fill(2.0,1,5)
a = Fill(2.0+im, 5)
@test FillArrays.getindex_value(a') == FillArrays.unique_value(a') == 2.0 - im
@test convert(Fill, a') ≡ Fill(2.0-im,1,5)
@test FillArrays.getindex_value(transpose(a)) == FillArrays.unique_value(transpose(a)) == 2.0 + im
@test convert(Fill, transpose(a)) ≡ Fill(2.0+im,1,5)
end
end