-
Notifications
You must be signed in to change notification settings - Fork 113
/
MatrixDisplay.jl
292 lines (249 loc) · 9.42 KB
/
MatrixDisplay.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#############################################################################
##
## functionality for displaying labelled matrices,
## that is, 2-dimensional arrays of strings
## together with row labels, columns labels, header, and footer
##
## Typical examples are character tables of groups.
##
## There is support for the output of \LaTeX format strings
## as well as for column aligned text in a Julia session.
##
## The code was inspired by the ideas underlying
## [GAP's Browse package](http://www.math.rwth-aachen.de/~Browse/),
## and by Jean Michel's implementation of character table display in his
## [Julia package Gapjm.jl](https://github.com/jmichel7/Gapjm.jl).
##
#T TODO:
#T - Support left/centered/right alignment of columns and of the whole table.
#T - Concerning irrational values in character tables:
#T in `matrix_of_strings`,
#T identify also unique Galois conjugates (mark with superscript *);
#T show both values in the footer.
#T (For elements in quadratic fields, show also an expression in terms of
#T square roots.)
# `lpad` is wrong for example for strings containing overlined characters,
# such as "A̅".
# (This had been observed already by Jean Michel.)
mylpad(s::String,n::Int) = " "^(n-textwidth(s))*s
# for conversions from \LaTeX format to unicode format,
# deals with subscripts and superscripts in braces `{}`,
# character values, character names
const sup = Dict(zip("-0123456789", "⁻⁰¹²³⁴⁵⁶⁷⁸⁹"))
const sub = Dict(zip("-0123456789", "₋₀₁₂₃₄₅₆₇₈₉"))
function replace_TeX(str::String)
str = replace(str, "\\chi" => "χ")
str = replace(str, "\\phi" => "φ")
str = replace(str, "\\varphi" => "φ")
str = replace(str, "\\zeta" => "ζ")
str = replace(str, r"\^\{[-0123456789]*\}" => (s -> map(x->sup[x], s[3:end-1])))
str = replace(str, r"_\{[-0123456789]*\}" => (s -> map(x->sub[x], s[3:end-1])))
str = replace(str, r"\\overline\{(?<nam>[A-Z]*)\}" => s"\g<nam>\u0305")
return str
end
"""
labelled_matrix_formatted(io::IO, mat::Matrix{String})
Output a formatted version of `mat` to `io`.
The following attributes of `io` are supported.
- `:TeX`:
Produce a LaTeX format `array` if the value is `true`,
otherwise produce column aligned text format with unicode characters and
sub-/superscripts.
- `:subset_row`:
array of positions of those rows of `mat` that shall be shown,
- `:subset_col`:
array of positions of those columns of `mat` that shall be shown,
- `:header`:
array of strings to be shown above `mat`,
- `:corner`:
matrix (or vector) of strings to be shown on the left of (and aligned to)
the the column labels and above (and aligned to) the row labels
- `:labels_col`:
matrix of strings to be shown above and aligned to the columns of `mat`,
- `:labels_row`:
matrix (or vector) of strings to be shown on the left of (and aligned to)
the rows of `mat`,
- `:footer`:
array of strings to be shown below `mat`,
- `:column_separators`:
array of column numbers after which a vertical line shall be drawn
instead of a blank (enter `0` for a line in front of the first column),
- `:row_separators`:
array of row numbers after which a horizontal line shall be drawn
(enter `0` for a line above the first row),
- `:column_portions`:
array of column numbers after which a new labelled table shall be started;
the default is to have just one portion in the `:TeX` case,
and to create portions according to the screen width otherwise,
- `:row_portions`:
array of row numbers after which a new labelled table shall be started;
the default is to have just one portion.
## Examples:
...
"""
function labelled_matrix_formatted(io::IO, mat::Matrix{String})
TeX = get(io, :TeX, false)
subset_row = get(io, :subset_row, 1:size(mat, 1))
subset_col = get(io, :subset_col, 1:size(mat, 2))
m2 = size(subset_row, 1)
n2 = size(subset_col, 1)
# Get the three surrounding matrices.
labels_col = get(io, :labels_col, fill("", 0, n2))
labels_row = get(io, :labels_row, fill("", m2, 0))
m1 = size(labels_col, 1)
n1 = size(labels_row, 2)
corner = get(io, :corner, fill("", m1, n1))
#FIXME:
# If `subset_row`, `subset_col` are given, are `labels_row`, `labels_col`
# to be read relative to the subsets or to `mat`?
@assert size(corner, 1) == m1 "row numbers in corner and column labels differ"
@assert size(corner, 2) == n1 "column numbers in corner and row labels differ"
@assert size(labels_row, 1) == m2 "row numbers in row labels and matrix differ"
@assert size(labels_col, 2) == n2 "column numbers in column labels and matrix differ"
m = m1 + m2
header = get(io, :header, [])
footer = get(io, :footer, [])
# Concatenate corner part and row labels.
leftpart = vcat(corner, labels_row)
# Concatenate column labels and `mat`.
rightpart = vcat(labels_col, mat[subset_row, subset_col])
if ! TeX
# If no `TeX` output is required then
# replace markup in the matrices, ...
leftpart = map(replace_TeX, leftpart)
rightpart = map(replace_TeX, rightpart)
header = map(replace_TeX, header)
footer = map(replace_TeX, footer)
# ... compute columns widths of the corner entries and row labels ...
leftwidth = map(textwidth, leftpart)
leftrows = [leftwidth[i,:] for i in 1:m]
widths_leftpart = map(max, leftrows...)
# ... and of the column labels and matrix entries.
rightwidth = map(textwidth, rightpart)
rightrows = [rightwidth[i,:] for i in 1:m]
widths_rightpart = map(max, rightrows...)
# Compute the width of the row labels part.
leftwidthsum = sum(widths_leftpart) + length(widths_leftpart) - 1
end
# Print the header (a vector of strings).
for line in header
println(io, line)
end
column_separators = get(io, :column_separators, [])
row_separators = map(x -> x+m1, get(io, :row_separators, []))
# Determine the column portions.
column_portions = get(io, :column_portions, [])
if column_portions == []
# Set a default.
if TeX
# Do not split into column portions.
column_portions = [n2]
else
# Distribute the matrix into column portions,
# according to the screen width.
screen_width = displaysize(io)[2]
if leftwidthsum >= screen_width
println(io, "(row label part is too wide for the screen)")
return
end
num = 0
width = leftwidthsum
for w in widths_rightpart
width = width + w + 1
if width >= screen_width
if num == 0
# This column will be too wide
# but we have to take at least one.
push!(column_portions, 1)
num = 0
width = leftwidthsum
else
push!(column_portions, num)
num = 1
width = leftwidthsum + w + 1
end
else
num = num+1
end
end
push!(column_portions, num)
end
end
# We do not distribute the table into row portions
# if this is not wanted.
row_portions = get(io, :row_portions, [m2])
# Run over the column portions.
cpos = 0
lastportion = (column_portions[end], row_portions[end])
for cnum in column_portions
crange = (cpos + 1):(cpos + cnum)
cpos = cpos + cnum
if TeX
cpattern = 0 in column_separators ? "|" : ""
for j in crange
cpattern = cpattern * (j in column_separators ? "r|" : "r")
end
else
cprefix = 0 in column_separators ? "\u2502" : " "
cpattern = String[]
for j in crange
push!(cpattern, j in column_separators ? "\u2502" : " ")
end
end
# For each column portion, run over the row portions.
rpos = 0
for rnum in row_portions
# Print first the column labels and then the row portion.
rrange = vcat(1:m1, (rpos + m1 + 1):(rpos + m1 + rnum))
rpos = rpos + rnum
if TeX
println(io, "\\begin{array}{" * repeat("r", n1) * cpattern * "}")
end
# Compute the horizontal separator line.
if TeX
hline = "\\hline"
else
hline = repeat("\u2500", leftwidthsum)
if 0 in column_separators
hline = hline * "\u253C"
else
hline = hline * "\u2500"
end
for j in crange
hline = hline * repeat("\u2500", widths_rightpart[j])
if j in column_separators
hline = hline * "\u253C"
else
hline = hline * "\u2500"
end
end
end
# Print the matrices.
for i in rrange
if TeX
println(io, join(leftpart[i,:], " & "), " & ", join(rightpart[i,:][crange], " & "), " \\\\")
if i in row_separators
println(io, "\\hline")
end
else
println(io, join(map(mylpad, leftpart[i,:], widths_leftpart), " "),
cprefix,
join(join.(collect(zip(map(mylpad, rightpart[i,:][crange], widths_rightpart[crange]), cpattern)))))
if i in row_separators
println(io, hline)
end
end
end
if TeX
println(io, "\\end{array}")
end
if (cnum, rnum) != lastportion
println(io, "")
end
end
end
# footer (vector of strings)
for line in footer
println(io, line)
end
end