Created
September 14, 2015 08:37
-
-
Save andreasnoack/9d9acbda27be5f601082 to your computer and use it in GitHub Desktop.
Print sparsity pattern in terminal with unicode charaters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type SpyMatrix{T} | |
data::Matrix{T} | |
sz::Tuple{Int,Int} | |
end | |
function spy2(A::SparseMatrixCSC, s1 = 40, s2 = 80) | |
m, n = size(A) | |
c = A.colptr | |
r = A.rowval | |
B = zeros(Int, s1, s2) | |
for j = 1:length(c) - 1 | |
for k = c[j]:c[j + 1] - 1 | |
i = r[k] | |
iB = ceil(Int, i/m*s1) | |
jB = ceil(Int, j/n*s2) | |
B[iB, jB] += 1 | |
end | |
end | |
SpyMatrix(B/size(A,1)/size(A,2)*s1*s2, (m,n)) | |
end | |
function asciishow(io::IO, A::SpyMatrix) | |
charArray = [' ', '·', '•', '●'] | |
AA = A.data | |
m, n = size(AA) | |
B = similar(AA, Char) | |
mm, nn = div(A.sz[1], size(AA, 1)), div(A.sz[2], size(AA, 2)) | |
md2 = div(size(AA, 1), 2) | |
print(io, '┌') | |
for j = 1:n | |
print(io, '─') | |
end | |
println(io, '┐') | |
for i = 1:m | |
print(io, '│') | |
for j = 1:n | |
v = AA[i,j] | |
print(io, charArray[ifelse(v == 0, 1, ifelse((mm + nn)/2*v < 1, 2, ifelse(sqrt((mm + nn)/2)*v < 1, 3, 4)))]) | |
end | |
print(io, '│') | |
if i == md2 - 3 | |
println(io, " ┌──────────────────┐") | |
elseif i == md2 - 2 | |
println(io, " │$(charArray[1]) density = 0 │") | |
elseif i == md2 - 1 | |
println(io, " │$(charArray[2]) density < 1/n │") | |
elseif i == md2 | |
println(io, " │$(charArray[3]) density < 1/√n │") | |
elseif i == md2 + 1 | |
println(io, " │$(charArray[4]) density ≥ 1/√n │") | |
elseif i == md2 + 2 | |
println(io, " └──────────────────┘") | |
else | |
println(io) | |
end | |
end | |
print(io, '└') | |
for j = 1:size(AA, 2) | |
print(io, '─') | |
end | |
println(io, '┘') | |
nothing | |
end | |
function asciishow(io::IO, A::SparseMatrixCSC, sz = 40) | |
println(io, summary(A)) | |
asciishow(io, spy2(A, div(sz, 2), sz)) | |
nzs = @sprintf("density: %.3e", nnz(A)/length(A)) | |
lpad = div(sz - length(nzs), 2) | |
for i = 1:lpad | |
print(io, ' ') | |
end | |
println(io, nzs) | |
end | |
asciishow(A::SparseMatrixCSC, sz = 40) = asciishow(STDOUT, A, sz) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment