Skip to content

Commit cc3d03c

Browse files
authored
pretty printing for sparse arrays (#188)
1 parent 761d057 commit cc3d03c

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

src/blockarray.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,23 @@ Base.dataids(arr::BlockArray) = (dataids(arr.blocks)..., dataids(arr.axes)...)
414414
# This is not entirely valid. In principle, we have to concatenate
415415
# all dataids of all blocks. However, it makes `dataids` non-inferable.
416416

417+
# Pretty-printing for sparse arrays
418+
function _replace_in_print_matrix_inds(block_arr, i...)
419+
J = findblockindex.(axes(block_arr), i)
420+
blind = map(block, J)
421+
bl = block_arr[blind...]
422+
inds = map(blockindex, J)
423+
bl, inds
424+
end
425+
function Base.replace_in_print_matrix(block_arr::BlockArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString)
426+
bl, inds = _replace_in_print_matrix_inds(block_arr, i, j)
427+
Base.replace_in_print_matrix(bl, inds..., s)
428+
end
429+
function Base.replace_in_print_matrix(block_arr::BlockArray{<:Any,1}, i::Integer, j::Integer, s::AbstractString)
430+
bl, inds = _replace_in_print_matrix_inds(block_arr, i)
431+
Base.replace_in_print_matrix(bl, inds..., j, s)
432+
end
433+
417434
########
418435
# Misc #
419436
########

test/test_blockarrays.jl

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ end
7171
@test Matrix(ret) == zeros(6,6)
7272

7373
A = [1,2,3,4,5,6]
74-
@test A == BlockArray(A, 1:3) == BlockArray{Int}(A, 1:3) ==
74+
@test A == BlockArray(A, 1:3) == BlockArray{Int}(A, 1:3) ==
7575
BlockArray(A, (blockedrange(1:3),)) == BlockArray{Int}(A, (blockedrange(1:3),)) ==
7676
BlockArray{Float64}(A, 1:3)
7777
end
@@ -91,7 +91,7 @@ end
9191

9292
A = [1,2,3,4,5,6]
9393
@test_throws BoundsError PseudoBlockArray(A,10:20)
94-
@test A == PseudoBlockArray(A, 1:3) == PseudoBlockArray{Int}(A, 1:3) ==
94+
@test A == PseudoBlockArray(A, 1:3) == PseudoBlockArray{Int}(A, 1:3) ==
9595
PseudoBlockArray(A, (blockedrange(1:3),)) == PseudoBlockArray{Int}(A, (blockedrange(1:3),)) ==
9696
PseudoBlockArray{Float64}(A, 1:3)
9797
end
@@ -151,7 +151,7 @@ end
151151
)
152152
end
153153
end
154-
154+
155155
@testset "BlockVector" begin
156156
a_data = [1,2,3]
157157
a = BlockVector(a_data,[1,2])
@@ -196,29 +196,29 @@ end
196196
B = BlockArray(I, fill(2,4), fill(2,5))
197197
@test B isa BlockMatrix{Bool}
198198
@test B == BlockMatrix(I, fill(2,4), fill(2,5)) ==
199-
BlockArray(I, blockedrange.((fill(2,4), fill(2,5)))) ==
200-
BlockMatrix(I, blockedrange.((fill(2,4), fill(2,5)))) ==
199+
BlockArray(I, blockedrange.((fill(2,4), fill(2,5)))) ==
200+
BlockMatrix(I, blockedrange.((fill(2,4), fill(2,5)))) ==
201201
Matrix(I, 8, 10)
202-
202+
203203
B = BlockArray{Float64}(I, fill(2,4), fill(2,5))
204204
@test B isa BlockMatrix{Float64}
205205
@test B == BlockMatrix{Float64}(I, fill(2,4), fill(2,5)) ==
206-
BlockArray{Float64}(I, blockedrange.((fill(2,4), fill(2,5)))) ==
207-
BlockMatrix{Float64}(I, blockedrange.((fill(2,4), fill(2,5)))) ==
206+
BlockArray{Float64}(I, blockedrange.((fill(2,4), fill(2,5)))) ==
207+
BlockMatrix{Float64}(I, blockedrange.((fill(2,4), fill(2,5)))) ==
208208
Matrix(I, 8, 10)
209209

210210
B = PseudoBlockArray(I, fill(2,4), fill(2,5))
211211
@test B isa PseudoBlockMatrix{Bool}
212212
@test B == PseudoBlockMatrix(I, fill(2,4), fill(2,5)) ==
213-
PseudoBlockArray(I, blockedrange.((fill(2,4), fill(2,5)))) ==
214-
PseudoBlockMatrix(I, blockedrange.((fill(2,4), fill(2,5)))) ==
213+
PseudoBlockArray(I, blockedrange.((fill(2,4), fill(2,5)))) ==
214+
PseudoBlockMatrix(I, blockedrange.((fill(2,4), fill(2,5)))) ==
215215
Matrix(I, 8, 10)
216-
216+
217217
B = PseudoBlockArray{Float64}(I, fill(2,4), fill(2,5))
218218
@test B isa PseudoBlockMatrix{Float64}
219219
@test B == PseudoBlockMatrix{Float64}(I, fill(2,4), fill(2,5)) ==
220-
PseudoBlockArray{Float64}(I, blockedrange.((fill(2,4), fill(2,5)))) ==
221-
PseudoBlockMatrix{Float64}(I, blockedrange.((fill(2,4), fill(2,5)))) ==
220+
PseudoBlockArray{Float64}(I, blockedrange.((fill(2,4), fill(2,5)))) ==
221+
PseudoBlockMatrix{Float64}(I, blockedrange.((fill(2,4), fill(2,5)))) ==
222222
Matrix(I, 8, 10)
223223
end
224224
end
@@ -352,9 +352,9 @@ end
352352

353353
= PseudoBlockArray(rand(2,3), Fill(1,2), [2,1])
354354
@test convert(typeof(A), Ã) ==
355-
355+
356356
@test PseudoBlockArray(A, axes(Ã)) isa typeof(Ã)
357-
@test PseudoBlockArray(A, axes(Ã)) == A
357+
@test PseudoBlockArray(A, axes(Ã)) == A
358358

359359

360360
A = BlockArray(rand(2,3), [1,1], [2,1])
@@ -381,7 +381,7 @@ end
381381
buf = IOBuffer()
382382
Base.showerror(buf, BlockBoundsError(A, (3,2)))
383383
@test String(take!(buf)) == "BlockBoundsError: attempt to access 2×2-blocked 4×5 BlockMatrix{Float64, Matrix{Matrix{Float64}}, Tuple{BlockedUnitRange{Vector{$Int}}, BlockedUnitRange{Vector{$Int}}}} at block index [3,2]"
384-
384+
385385

386386
A = PseudoBlockArray(rand(4, 5), [1,3], [2,3]);
387387
Base.showerror(buf, BlockBoundsError(A, (3,2)))
@@ -522,7 +522,7 @@ end
522522
b = randn(6)
523523
@test A*b isa PseudoBlockVector{Float64}
524524
@test*b isa PseudoBlockVector{Float64}
525-
@test A*b *b Matrix(A)*b
525+
@test A*b *b Matrix(A)*b
526526
end
527527

528528
@testset "Blockindex" begin
@@ -532,8 +532,8 @@ end
532532
A = PseudoBlockArray(randn(3,3), [1,2], [1,2])
533533
@test A[Block(1)[1], Block(1)[1]] == A[Block(1,1)[1,1]] == A[1,1]
534534
@test A[Block(1)[1:1], Block(1)[1:1]] == A[Block(1,1)[1:1,1:1]] == A[1:1,1:1]
535-
@test A[Block(1)[1:1], Block(1)[1]] == BlockArray(A)[Block(1)[1:1], Block(1)[1]] == A[1:1,1]
536-
@test A[Block(1)[1], Block(1)[1:1]] == BlockArray(A)[Block(1)[1], Block(1)[1:1]] == A[1,1:1]
535+
@test A[Block(1)[1:1], Block(1)[1]] == BlockArray(A)[Block(1)[1:1], Block(1)[1]] == A[1:1,1]
536+
@test A[Block(1)[1], Block(1)[1:1]] == BlockArray(A)[Block(1)[1], Block(1)[1:1]] == A[1,1:1]
537537
end
538538

539539
@testset "permutedims" begin
@@ -560,4 +560,23 @@ end
560560
A = BlockVector{Float64}(undef,Int[])
561561
@test @inferred(isempty(blocklengths(axes(A,1))))
562562
end
563-
end
563+
564+
@testset "pretty-printing" begin
565+
v = Zeros(3)
566+
m = mortar([v])
567+
io = IOBuffer()
568+
Base.print_array(io, m)
569+
s1 = String(take!(io))
570+
Base.print_array(io, v)
571+
s2 = String(take!(io))
572+
@test s1 == s2
573+
574+
d = Diagonal(Ones(2,2))
575+
m = mortar(reshape([d], 1, 1))
576+
Base.print_array(io, m)
577+
s1 = String(take!(io))
578+
Base.print_array(io, d)
579+
s2 = String(take!(io))
580+
@test s1 == s2
581+
end
582+
end

0 commit comments

Comments
 (0)