Skip to content

Commit

Permalink
Compact printing for Adjoint vectors, and Diagonal (#40722)
Browse files Browse the repository at this point in the history
This changes the compact printing to preserve more information -- an
adjoint vector is not quite a matrix, and Diagonal wastes a lot of
space:
```julia
julia> (Diagonal(1:4), [5,6,7]', transpose(8:10))  # before
([1 0 0 0; 0 2 0 0; 0 0 3 0; 0 0 0 4], [5 6 7], [8 9 10])

julia> (Diagonal(1:4), [5,6,7]', transpose(8:10))  # after
(Diagonal(1:4), adjoint([5, 6, 7]), transpose(8:10))
```
Would have been better to do at the same time as 1.6's other printing
changes, I guess.

---------

Co-authored-by: Jishnu Bhattacharya <jishnub.github@gmail.com>
  • Loading branch information
mcabbott and jishnub authored Aug 6, 2024
1 parent d1b1a5d commit 1b32fa6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,7 @@ julia> vcat(range(1, 2, length=3)) # collects lazy ranges
2.0
julia> two = ([10, 20, 30]', Float64[4 5 6; 7 8 9]) # row vector and a matrix
([10 20 30], [4.0 5.0 6.0; 7.0 8.0 9.0])
(adjoint([10, 20, 30]), [4.0 5.0 6.0; 7.0 8.0 9.0])
julia> vcat(two...)
3×3 Matrix{Float64}:
Expand Down
10 changes: 10 additions & 0 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ function Base.showarg(io::IO, v::Transpose, toplevel)
toplevel && print(io, " with eltype ", eltype(v))
return nothing
end
function Base.show(io::IO, v::Adjoint{<:Real, <:AbstractVector})
print(io, "adjoint(")
show(io, parent(v))
print(io, ")")
end
function Base.show(io::IO, v::Transpose{<:Number, <:AbstractVector})
print(io, "transpose(")
show(io, parent(v))
print(io, ")")
end

# some aliases for internal convenience use
const AdjOrTrans{T,S} = Union{Adjoint{T,S},Transpose{T,S}} where {T,S}
Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ end
function Base.replace_in_print_matrix(A::Diagonal,i::Integer,j::Integer,s::AbstractString)
i==j ? s : Base.replace_with_centered_mark(s)
end
function Base.show(io::IO, A::Diagonal)
print(io, "Diagonal(")
show(io, A.diag)
print(io, ")")
end

parent(D::Diagonal) = D.diag

Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ end
@test String(take!(io)) == "transpose(::Matrix{Float64})"
end

@testset "show" begin
@test repr(adjoint([1,2,3])) == "adjoint([1, 2, 3])"
@test repr(transpose([1f0,2f0])) == "transpose(Float32[1.0, 2.0])"
end

@testset "strided transposes" begin
for t in (Adjoint, Transpose)
@test strides(t(rand(3))) == (3, 1)
Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,11 @@ Base.size(::SMatrix1) = (1, 1)
@test C isa Matrix{SMatrix1{String}}
end

@testset "show" begin
@test repr(Diagonal([1,2])) == "Diagonal([1, 2])" # 2-arg show
@test contains(repr(MIME"text/plain"(), Diagonal([1,2])), "⋅ 2") # 3-arg show
end

@testset "copyto! with UniformScaling" begin
@testset "Fill" begin
for len in (4, InfiniteArrays.Infinity())
Expand Down

0 comments on commit 1b32fa6

Please sign in to comment.