Skip to content

Commit 1774cbf

Browse files
committed
Extend code documentation
1 parent 1c9d4e8 commit 1774cbf

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,24 @@ Base.show(io::IO, S::AbstractSparseMatrixCSC) = Base.show(convert(IOContext, io)
196196
function Base.show(io::IOContext, S::AbstractSparseMatrixCSC)
197197
m, n = size(S)
198198
(m == 0 || n == 0) && return show(io, MIME("text/plain"), S)
199+
200+
# The maximal number of characters we allow to display the matrix
199201
maxHeight = displaysize(io)[1] - 4 # -4 from [Prompt, header, newline after elements, new prompt]
200202
maxWidth = displaysize(io)[2] ÷ 2
201203

202-
# Determine if scaling is needed and set the scaling factors accordingly
204+
# In the process of generating the braille pattern to display the nonzero
205+
# structure of `S`, we need to be able to scale the matrix `S` to a
206+
# smaller matrix with the same aspect ratio as `S`, but fits on the
207+
# available screen space. The size of that smaller matrix is stored
208+
# in the variables `scaleHeight` and `scaleWidth`. If no scaling is needed,
209+
# we can use the size `m × n` of `S` directly.
203210
scaleHeight = m
204211
scaleWidth = n
212+
213+
# Determine if scaling is needed and set the scaling factors
214+
# `scaleHeight` and `scaleWidth` accordingly. Note that each available
215+
# character can contain up to 4 braille dots in its height (⡇) and up to
216+
# 2 braille dots in its width (⠉).
205217
if get(io, :limit, true) && (m > 4maxHeight || n > 2maxWidth)
206218
s = (m * 2maxWidth ÷ n, n * 4maxHeight ÷ m)
207219
if s[1] <= 4maxHeight
@@ -214,21 +226,27 @@ function Base.show(io::IOContext, S::AbstractSparseMatrixCSC)
214226
end
215227

216228
brailleBlocks = split("⠁⠂⠄⡀⠈⠐⠠⢀", "") .|> x -> Int(x[1])
229+
230+
# `brailleGrid` is used to store the needed braille characters for
231+
# the matrix `S`. Each row of the braille pattern to print is stored
232+
# in a column of `brailleGrid`.
217233
brailleGrid = fill(10240, (scaleWidth - 1) ÷ 2 + 2, (scaleHeight - 1) ÷ 4 + 1)
218-
brailleGrid[end, :] .= 10
234+
brailleGrid[end, :] .= Int('\n')
219235

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

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

0 commit comments

Comments
 (0)