Skip to content
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

Display sparse matrices by showing their structure with braille patterns #33821

Merged
merged 31 commits into from
May 22, 2020
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3918e34
Display sparse matrices by showing their structure with braille patterns
goggle Nov 12, 2019
e45c6ab
Adopt test which involves showing of sparse matrix
goggle Nov 12, 2019
e871927
Adopt docstrings
goggle Nov 12, 2019
4223e4a
Call `rowvals(S)` only once
goggle Nov 13, 2019
ff9bc5a
Extend code documentation
goggle Nov 13, 2019
64b111e
Simplify calculation of `scaleHeight` and `scaleWidth`
goggle Nov 17, 2019
4c0f3d2
Make `brailleBlocks` a `const`
goggle Nov 17, 2019
78e1ade
Initialize `brailleGrid` with `UInt16(10240)`
goggle Nov 17, 2019
0bcb47a
Let the compiler do the conversion of `\n`
goggle Nov 17, 2019
7a7b32a
Improve printing to `io`
goggle Nov 17, 2019
c324db5
Unwrap the helper functions directly into the loop
goggle Nov 17, 2019
b96ee2a
Show small sparse matrix in traditional manner
goggle Nov 18, 2019
d919d85
Add tests for `isstored`
goggle Nov 18, 2019
6c79e6b
Adapt tests for `show`
goggle Nov 18, 2019
49e8fc2
Adapt doctests
goggle Nov 18, 2019
246d9d1
Use `m` and `n` instead of `size` calls
goggle Nov 18, 2019
6ceae94
Initialize `scaleHeight` and `scaleWidth` in `else` clause
goggle Nov 18, 2019
e93dafc
Reenable commented test
goggle Nov 18, 2019
0ea6ee9
Declare type of `maxHeight` and `maxWidth`
goggle Nov 19, 2019
748c108
Do not print leading newline in `_show_with_braille_patterns`
goggle Nov 19, 2019
c8aa9de
Reenable test for issue 30589
goggle Nov 20, 2019
1e1af8e
Shorten type declarations of `maxHeight` and `maxWidth`
goggle Nov 26, 2019
c50bb66
Add `isstored` to Base
goggle Nov 26, 2019
7c772d9
Add `isstored` for sparse vectors
goggle Nov 26, 2019
0de07c2
Make use of Base function `isstored`
goggle Nov 26, 2019
bd7431c
Add `boundscheck` macro
goggle Nov 26, 2019
c998d75
Clarify news
goggle Nov 26, 2019
25f211d
Call `issorted` by `Base.issored`
goggle Nov 26, 2019
2fdfc20
Use `Base.isstored` in tests
goggle Nov 26, 2019
3f095bb
Set cutoff value to print sparse matrices with braille to 16
goggle Nov 28, 2019
f8495b1
Merge branch 'master' into 30587-spy-printing-sparse-matrices
stevengj May 21, 2020
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
Prev Previous commit
Next Next commit
Extend code documentation
  • Loading branch information
goggle committed Nov 23, 2019
commit ff9bc5aa7a5b15f323dc22a1c8f935144b9fce07
30 changes: 24 additions & 6 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,24 @@ Base.show(io::IO, S::AbstractSparseMatrixCSC) = Base.show(convert(IOContext, io)
function Base.show(io::IOContext, S::AbstractSparseMatrixCSC)
m, n = size(S)
(m == 0 || n == 0) && return show(io, MIME("text/plain"), S)

# The maximal number of characters we allow to display the matrix
maxHeight = displaysize(io)[1] - 4 # -4 from [Prompt, header, newline after elements, new prompt]
maxWidth = displaysize(io)[2] ÷ 2
stevengj marked this conversation as resolved.
Show resolved Hide resolved
goggle marked this conversation as resolved.
Show resolved Hide resolved

# Determine if scaling is needed and set the scaling factors accordingly
# In the process of generating the braille pattern to display the nonzero
# structure of `S`, we need to be able to scale the matrix `S` to a
# smaller matrix with the same aspect ratio as `S`, but fits on the
# available screen space. The size of that smaller matrix is stored
# in the variables `scaleHeight` and `scaleWidth`. If no scaling is needed,
# we can use the size `m × n` of `S` directly.
scaleHeight = m
scaleWidth = n
goggle marked this conversation as resolved.
Show resolved Hide resolved

# Determine if scaling is needed and set the scaling factors
# `scaleHeight` and `scaleWidth` accordingly. Note that each available
# character can contain up to 4 braille dots in its height (⡇) and up to
# 2 braille dots in its width (⠉).
if get(io, :limit, true) && (m > 4maxHeight || n > 2maxWidth)
s = (m * 2maxWidth ÷ n, n * 4maxHeight ÷ m)
if s[1] <= 4maxHeight
Expand All @@ -214,21 +226,27 @@ function Base.show(io::IOContext, S::AbstractSparseMatrixCSC)
end

brailleBlocks = split("⠁⠂⠄⡀⠈⠐⠠⢀", "") .|> x -> Int(x[1])
goggle marked this conversation as resolved.
Show resolved Hide resolved

# `brailleGrid` is used to store the needed braille characters for
# the matrix `S`. Each row of the braille pattern to print is stored
# in a column of `brailleGrid`.
brailleGrid = fill(10240, (scaleWidth - 1) ÷ 2 + 2, (scaleHeight - 1) ÷ 4 + 1)
goggle marked this conversation as resolved.
Show resolved Hide resolved
brailleGrid[end, :] .= 10
brailleGrid[end, :] .= Int('\n')
goggle marked this conversation as resolved.
Show resolved Hide resolved

# Given an index `(i, j)` of a matrix `S`, find the corresponding
# index `(a, b)` of a matrix of size `height × width`
# Let `(i, j)` be an index pair of a matrix `S`. Consider a matrix `B`
# of size `height × width`. This method calculates an index pair `(a, b)`,
# such that the position of the element `B[a, b]` in `B` corresponds
# the best to the position of the element `S[i, j]` in `S`.
@inline function _scale_index(i::Integer, j::Integer, S::AbstractSparseMatrixCSC, height::Integer, width::Integer)
a = size(S, 1) <= 1 || height <= 1 ? 1.0 : (i - 1) / (size(S, 1) - 1) * (height - 1) + 1
b = size(S, 2) <= 1 || width <= 1 ? 1.0 : (j - 1) / (size(S, 2) - 1) * (width - 1) + 1
return round.(Int, (a, b))
end

# Given an index `(i, j)` of a matrix, find the corresponding triple
# Given an index pair `(i, j)` of a matrix, find the corresponding triple
# `(k, l, p)` such that the element at `(i, j)` can be found at
# position `(k, l)` in the braille grid `brailleGrid` and corresponds
# to the 1-dot braille pattern `brailleBlocks[p]`
# to the 1-dot braille character `brailleBlocks[p]`
@inline function _to_braille_grid_space(i::Integer, j::Integer)
k = (j - 1) ÷ 2 + 1
l = (i - 1) ÷ 4 + 1
Expand Down