Skip to content

Commit a5e032e

Browse files
gogglestevengj
andauthored
Display sparse matrices by showing their structure with braille patterns (#33821)
* Display sparse matrices by showing their structure with braille patterns * Adopt test which involves showing of sparse matrix * Adopt docstrings * Call `rowvals(S)` only once * Extend code documentation * Simplify calculation of `scaleHeight` and `scaleWidth` * Make `brailleBlocks` a `const` * Initialize `brailleGrid` with `UInt16(10240)` * Let the compiler do the conversion of `\n` * Improve printing to `io` * Unwrap the helper functions directly into the loop * Show small sparse matrix in traditional manner * Add tests for `isstored` * Adapt tests for `show` * Adapt doctests * Use `m` and `n` instead of `size` calls * Initialize `scaleHeight` and `scaleWidth` in `else` clause * Reenable commented test * Declare type of `maxHeight` and `maxWidth` * Do not print leading newline in `_show_with_braille_patterns` * Reenable test for issue 30589 * Shorten type declarations of `maxHeight` and `maxWidth` * Add `isstored` to Base * Add `isstored` for sparse vectors * Make use of Base function `isstored` * Add `boundscheck` macro * Clarify news * Call `issorted` by `Base.issored` * Use `Base.isstored` in tests * Set cutoff value to print sparse matrices with braille to 16 Co-authored-by: Steven G. Johnson <stevenj@alum.mit.edu>
1 parent 97a6258 commit a5e032e

File tree

12 files changed

+295
-201
lines changed

12 files changed

+295
-201
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Standard library changes
6060

6161
#### SparseArrays
6262

63+
* Display large sparse matrices with a Unicode "spy" plot of their nonzero patterns, and display small sparse matrices by an `Matrix`-like 2d layout of their contents.
6364

6465
#### Dates
6566

base/abstractarray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,11 @@ function isassigned(a::AbstractArray, i::Integer...)
412412
end
413413
end
414414

415+
function isstored(A::AbstractArray{<:Any,N}, I::Vararg{Integer,N}) where {N}
416+
@boundscheck checkbounds(A, I...)
417+
return true
418+
end
419+
415420
# used to compute "end" for last index
416421
function trailingsize(A, n)
417422
s = 1

stdlib/LinearAlgebra/src/svd.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ Q factor:
303303
0.0 1.0
304304
D1 factor:
305305
2×2 SparseArrays.SparseMatrixCSC{Float64,Int64} with 2 stored entries:
306-
[1, 1] = 0.707107
307-
[2, 2] = 0.707107
306+
0.707107
307+
0.707107
308308
D2 factor:
309309
2×2 SparseArrays.SparseMatrixCSC{Float64,Int64} with 2 stored entries:
310-
[1, 1] = 0.707107
311-
[2, 2] = 0.707107
310+
0.707107
311+
0.707107
312312
R0 factor:
313313
2×2 Array{Float64,2}:
314314
1.41421 0.0

stdlib/SparseArrays/docs/src/index.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ remove stored zeros from the sparse matrix.
5252
```jldoctest
5353
julia> A = sparse([1, 1, 2, 3], [1, 3, 2, 3], [0, 1, 2, 0])
5454
3×3 SparseMatrixCSC{Int64,Int64} with 4 stored entries:
55-
[1, 1] = 0
56-
[2, 2] = 2
57-
[1, 3] = 1
58-
[3, 3] = 0
55+
0 ⋅ 1
56+
⋅ 2 ⋅
57+
⋅ ⋅ 0
5958
6059
julia> dropzeros(A)
6160
3×3 SparseMatrixCSC{Int64,Int64} with 2 stored entries:
62-
[2, 2] = 2
63-
[1, 3] = 1
61+
⋅ ⋅ 1
62+
⋅ 2 ⋅
63+
⋅ ⋅ ⋅
6464
```
6565

6666
## Sparse Vector Storage
@@ -106,10 +106,8 @@ julia> I = [1, 4, 3, 5]; J = [4, 7, 18, 9]; V = [1, 2, -5, 3];
106106
107107
julia> S = sparse(I,J,V)
108108
5×18 SparseMatrixCSC{Int64,Int64} with 4 stored entries:
109-
[1, 4] = 1
110-
[4, 7] = 2
111-
[5, 9] = 3
112-
[3, 18] = -5
109+
⠀⠈⠀⡀⠀⠀⠀⠀⠠
110+
⠀⠀⠀⠀⠁⠀⠀⠀⠀
113111
114112
julia> R = sparsevec(I,V)
115113
5-element SparseVector{Int64,Int64} with 4 stored entries:
@@ -152,11 +150,11 @@ the [`sparse`](@ref) function:
152150
```jldoctest
153151
julia> sparse(Matrix(1.0I, 5, 5))
154152
5×5 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
155-
[1, 1] = 1.0
156-
[2, 2] = 1.0
157-
[3, 3] = 1.0
158-
[4, 4] = 1.0
159-
[5, 5] = 1.0
153+
1.0 ⋅ ⋅ ⋅ ⋅
154+
1.0 ⋅ ⋅ ⋅
155+
⋅ ⋅ 1.0 ⋅ ⋅
156+
⋅ ⋅ ⋅ 1.0
157+
⋅ ⋅ ⋅ ⋅ 1.0
160158
161159
julia> sparse([1.0, 0.0, 1.0])
162160
3-element SparseVector{Float64,Int64} with 2 stored entries:

stdlib/SparseArrays/src/abstractsparse.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ Return a tuple `(I, J, V)` where `I` and `J` are the row and column indices of t
112112
```jldoctest
113113
julia> A = sparse([1 2 0; 0 0 3; 0 4 0])
114114
3×3 SparseMatrixCSC{Int64,Int64} with 4 stored entries:
115-
[1, 1] = 1
116-
[1, 2] = 2
117-
[3, 2] = 4
118-
[2, 3] = 3
115+
1 2 ⋅
116+
⋅ ⋅ 3
117+
⋅ 4 ⋅
119118
120119
julia> findnz(A)
121120
([1, 1, 3, 2], [1, 2, 2, 3], [1, 2, 4, 3])

0 commit comments

Comments
 (0)