Skip to content

Commit 5de63a2

Browse files
committed
fix related issues, add new test
1 parent 2a637f6 commit 5de63a2

3 files changed

Lines changed: 58 additions & 20 deletions

File tree

src/sparsematrix.jl

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ function Base.show(io::IO, ::MIME"text/plain", S::AbstractSparseMatrixCSCInclAdj
355355

356356
screen = displaysize(io)
357357
get(io, :limit, false) && screen[1] <= 4 && return print(io, ": …")
358-
println(io, ":")
359358

360359
show_circular(io, S) && return
361360
io = IOContext(io, :compact=>true, :typeinfo=>eltype(S), :SHOWN_SET=>S)
@@ -435,7 +434,7 @@ function _show_with_braille_patterns(io::IO, S::AbstractSparseMatrixCSCInclAdjoi
435434
# `scaleHeight` and `scaleWidth` accordingly. Note that each available
436435
# character can contain up to 4 braille dots in its height (⡇) and up to
437436
# 2 braille dots in its width (⠉).
438-
if get(io, :limit, true) && (m > 4maxHeight || n > 2maxWidth)
437+
if get(io, :limit, false) && (m > 4maxHeight || n > 2maxWidth)
439438
s = min(2maxWidth / n, 4maxHeight / m)
440439
scaleHeight = floor(Int, s * m)
441440
scaleWidth = floor(Int, s * n)
@@ -468,6 +467,12 @@ function _show_with_braille_patterns(io::IO, S::AbstractSparseMatrixCSCInclAdjoi
468467
rvals = rowvals(parent(S))
469468
rowscale = max(1, scaleHeight - 1) / max(1, m - 1)
470469
colscale = max(1, scaleWidth - 1) / max(1, n - 1)
470+
471+
if rowscale != 1 || colscale != 1
472+
print(io, " (downscaled to fit on screen)")
473+
end
474+
println(io, ":")
475+
471476
if isa(S, AbstractSparseMatrixCSC)
472477
@inbounds for j in axes(S,2)
473478
# Scale the column index `j` to the best matching column index
@@ -516,6 +521,8 @@ function _show_with_dotted_zeros(io::IO, S::AbstractSparseMatrixCSCInclAdjointAn
516521
colwidths = [max.((0, 0), align[findall(==(col), cols)]...) for col in axes(S,2)]
517522
displaysize(io)[2] < sum(sum.(colwidths) .+ 2) && return _show_with_braille_patterns(io, S)
518523

524+
println(io, ":")
525+
519526
try
520527
zero(eltype(S))
521528
catch
@@ -531,10 +538,26 @@ function _show_with_dotted_zeros(io::IO, S::AbstractSparseMatrixCSCInclAdjointAn
531538
l, r = cld(l+r-1, 2) + 1, div(l+r-1, 2) + 1
532539
print(io, " "^l * "" * (col==axes(S,2)[end] ? "" : " "^r))
533540
else
534-
l, r = (l+1, r+1) .- align[index][]
535-
print(io, " "^l)
536-
isassigned(vals, index[]) ? show(io, vals[index][]) : print(io, "#undef")
537-
col == axes(S,2)[end] || print(io, " "^r)
541+
try # print the element with 1 space of buffer on each side
542+
l, r = (l+1, r+1) .- align[index][]
543+
print(io, " "^l)
544+
isassigned(vals, index[]) ? show(io, vals[index][]) : print(io, "#undef")
545+
col == axes(S,2)[end] || print(io, " "^r)
546+
catch # if there's overlapping entries, [] will error. default to summing
547+
# entries, but print in red to warn user that something's wrong
548+
elm = any(!isassigned(vals, id) for id in index) ? "#undef" :
549+
try repr(sum(vals[index]); context=:compact=>true) catch e "#NaN" end
550+
551+
if length(elm) <= l+r # give up on alignment, center item in column
552+
l, r = cld(l+r-length(elm), 2) + 1, div(l+r-length(elm), 2) + 1
553+
else # len of new item does not fit in column
554+
elm = ' ' * ""^(l+r) * ' '
555+
l, r = 0, 0
556+
end
557+
558+
printstyled(io, " "^l, elm, color=:red)
559+
col == axes(S,2)[end] || print(io, " "^r)
560+
end
538561
end
539562
end
540563
row == axes(S,1)[end] || println(io)
@@ -683,7 +706,7 @@ function _sparse_copyto!(dest::AbstractMatrix, src::AbstractSparseMatrixCSC)
683706
@inbounds for col in axes(src, 2), ptr in nzrange(src, col)
684707
row = rowvals(src)[ptr]
685708
val = nonzeros(src)[ptr]
686-
dest[isrc[row, col]] = val
709+
dest[isrc[row, col]] += val
687710
end
688711
return dest
689712
end

test/issues.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,21 @@ end
821821
@test eltype(rowvals(zero(v))) <: Int16
822822
end
823823

824+
@testset "Issue #618" begin
825+
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], [1.0, 1.0, 1.0, 1.0])
826+
@test contains(repr(MIME"text/plain"(), x), "2.0")
827+
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], [7.0, 7.0, 1.0, 1.0])
828+
@test contains(repr(MIME"text/plain"(), x), "▒▒▒")
829+
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], [7.0, 'o', 1.0, 1.0])
830+
@test contains(repr(MIME"text/plain"(), x), "#NaN")
831+
v = similar(Any[1, 2, 3, 4]); v[2:4] = [1.0, 1.0, 1.0]
832+
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], v)
833+
@test contains(repr(MIME"text/plain"(), x), "#undef")
834+
835+
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], [1, 1, 1, 1])
836+
@test 2 == Matrix(x)[1,1]
837+
end
838+
824839
end # SparseTestsBase
825840

826841
end # module

test/sparsematrix_constructors_indexing.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ end
15381538
show(io, MIME"text/plain"(), transform(A))
15391539
@test String(take!(io)) == showstring
15401540
_show_with_braille_patterns(convert(IOContext, io), transform(A))
1541-
@test String(take!(io)) == braille
1541+
@test contains(String(take!(io)), braille)
15421542
end
15431543

15441544
# every 1-dot braille pattern
@@ -1559,7 +1559,7 @@ end
15591559
for transform in (identity, adjoint, transpose)
15601560
expected = "" * Char(10240)^2 * "\n" * Char(10240)^2 * ""
15611561
_show_with_braille_patterns(convert(IOContext, io), transform(A))
1562-
@test String(take!(io)) == expected
1562+
@test contains(String(take!(io)), expected)
15631563
end
15641564

15651565
A = sparse(Int64[1, 2, 4, 2, 3], Int64[1, 1, 1, 2, 2], Int64[1, 1, 1, 1, 1], 4, 2)
@@ -1578,7 +1578,7 @@ end
15781578
show(io, MIME"text/plain"(), transform(A))
15791579
@test String(take!(io)) == showstring
15801580
_show_with_braille_patterns(convert(IOContext, io), transform(A))
1581-
@test String(take!(io)) == braille
1581+
@test contains(String(take!(io)), braille)
15821582
end
15831583

15841584
A = sparse(Int64[1, 3, 2, 4], Int64[1, 1, 2, 2], Int64[1, 1, 1, 1], 7, 3)
@@ -1597,7 +1597,7 @@ end
15971597
show(io, MIME"text/plain"(), transform(A))
15981598
@test String(take!(io)) == showstring
15991599
_show_with_braille_patterns(convert(IOContext, io), transform(A))
1600-
@test String(take!(io)) == braille
1600+
@test contains(String(take!(io)), braille)
16011601
end
16021602

16031603
A = sparse(Int64[1:10;], Int64[1:10;], fill(Float64(1), 10))
@@ -1606,7 +1606,7 @@ end
16061606
"⎣⠀⠀⠀⠀⠑⎦"
16071607
for transform in (identity, adjoint, transpose)
16081608
_show_with_braille_patterns(convert(IOContext, io), transform(A))
1609-
@test String(take!(io)) == brailleString
1609+
@test contains(String(take!(io)), brailleString)
16101610
end
16111611

16121612
# Issue #30589
@@ -1622,22 +1622,22 @@ end
16221622
# vertical scaling
16231623
ioc = IOContext(io, :displaysize => (5, 80), :limit => true)
16241624
_show_with_braille_patterns(ioc, _filled_sparse(10, 10))
1625-
@test String(take!(io)) == "⎡⣿⣿⎤\n" *
1626-
"⎣⣿⣿⎦"
1625+
@test contains(String(take!(io)), "⎡⣿⣿⎤\n" *
1626+
"⎣⣿⣿⎦")
16271627

16281628
_show_with_braille_patterns(ioc, _filled_sparse(20, 10))
1629-
@test String(take!(io)) == "⎡⣿⣿⎤\n" *
1630-
"⎣⣿⣿⎦"
1629+
@test contains(String(take!(io)), "⎡⣿⣿⎤\n" *
1630+
"⎣⣿⣿⎦")
16311631

16321632
# horizontal scaling
16331633
ioc = IOContext(io, :displaysize => (80, 4), :limit => true)
16341634
_show_with_braille_patterns(ioc, _filled_sparse(8, 8))
1635-
@test String(take!(io)) == "⎡⣿⣿⎤\n" *
1636-
"⎣⣿⣿⎦"
1635+
@test contains(String(take!(io)), "⎡⣿⣿⎤\n" *
1636+
"⎣⣿⣿⎦")
16371637

16381638
_show_with_braille_patterns(ioc, _filled_sparse(8, 16))
1639-
@test String(take!(io)) == "⎡⣿⣿⎤\n" *
1640-
"⎣⣿⣿⎦"
1639+
@test contains(String(take!(io)), "⎡⣿⣿⎤\n" *
1640+
"⎣⣿⣿⎦")
16411641

16421642
# respect IOContext while displaying J
16431643
I, J, V = shuffle(1:50), shuffle(1:50), [1:50;]

0 commit comments

Comments
 (0)