-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.py
615 lines (544 loc) · 23.4 KB
/
write.py
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import numpy as np
from algo import *
from pylatex.base_classes import Environment
from pylatex.package import Package
from pylatex import Document, Math, Matrix, Alignat, Command, Section, Subsection
from pylatex.utils import NoEscape, bold
class bNiceMatrix(Environment):
_latex_name = 'bNiceMatrix'
packages = [Package('nicematrix')]
def __init__(self, matrix, *, color=None, rows=None, cols=None):
self.matrix = matrix
if color is not None:
self.color = color
if rows is not None:
self.rows = rows
if cols is not None:
self.cols = cols
super().__init__()
def dumps_content(self):
string = ''
matrix_string = ''
rows_string = ''
cols_string = ''
#
shape = self.matrix.shape
for (y, x), value in np.ndenumerate(self.matrix):
if x:
matrix_string += '&'
matrix_string += str(value)
if x == shape[1] - 1 and y != shape[0] - 1:
matrix_string += r'\\' + '%\n'
#
if self.color is not None and self.rows is not None:
rows_string = r'\rowcolor{' + self.color + r'}{' + self.rows + r'}'
#
if self.color is not None and self.cols is not None:
cols_string = r'\columncolor{' + self.color + r'}{' + self.cols + r'}'
#
string += r'\CodeBefore' + '%\n'
string += rows_string + '%\n'
string += cols_string + '%\n'
string += r'\Body' + '%\n'
string += matrix_string
super().dumps_content()
return string
def write_result_CR(doc: Document, decom: Decomposition, oneline: bool):
doc.append('The matrix can be decomposed into the following combination:')
# Write A = CR
with doc.create(Alignat(numbering=False, escape=False)) as agn:
agn.append(Matrix(decom.original_matrix, mtype='b'))
agn.append(r' &= ')
for i in range(len(decom.rows)):
agn.append(Matrix(decom.cols[i], mtype='b'))
agn.append('\cdot')
agn.append(Matrix(decom.rows[i], mtype='b'))
if i != len(decom.rows) - 1:
if oneline:
agn.append(r'\\ &+ ')
else:
agn.append(r' + ')
# Write short answer
short = 'A = '
for i in range(len(decom.rows)):
short += 'C_{'
short += str(decom.indices[i][1] + 1)
short += '}R_{'
short += str(decom.indices[i][0] + 1)
short += '}'
if i != len(decom.rows) - 1:
short += ' + '
doc.append('This means that:')
doc.append(Math(data=[short], inline=False, escape=False))
doc.append('Look at the detailed solution to see where ')
doc.append(Math(data=[r'R_{i}\ '], inline=True, escape=False))
doc.append('and ')
doc.append(Math(data=[r'C_{j}\ '], inline=True, escape=False))
doc.append('come from.')
return
def write_result_LU(doc: Document, decom: Decomposition):
doc.append('The matrix can also be decomposed into ')
#doc.append(Math(data=[r'A = LU'], inline=True, escape=False))
doc.append(Math(
data=[
Matrix(decom.original_matrix, mtype='b'),
r' = ',
Matrix(decom.L, mtype='b'),
r' \cdot ',
Matrix(decom.U, mtype='b'),
],
inline=False,
escape=False,
))
return
def write_detailed_solution(doc: Document, decom: Decomposition):
doc.append('We will now go into the details of ')
doc.append(Math(data=[r'A = \sum_{}^{}C_{i}R_{j}'], inline=True, escape=False))
def write_row(doc: Document, data: Data):
doc.append(Math(data=[r'R_{', data.m + 1, r'} \ '], inline=True, escape=False))
return
def write_col(doc: Document, data: Data):
doc.append(Math(data=[r'C_{', data.n + 1, r'} \ '], inline=True, escape=False))
return
def write_element(doc: Document, data: Data):
doc.append(Math(
data=[
r'e_{', data.m + 1, data.n + 1, r'} = ', data.element, r'\ '
],
inline=True,
escape=False,
))
return
def write_step_state(doc: Document, step: Step):
data = step.data
match step.state:
case State.START:
doc.append('We prepare ourselves mentally.')
return
case State.END:
doc.append('Finally, we have the matrix')
doc.append(Math(
data=[
r'A_{', data.mat_idx, r'} = ',
Matrix(data.mat, mtype='b'),
#bNiceMatrix(
#data.mat, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#)
],
inline=False,
escape=False,
))
doc.append('We can see that the row ')
write_row(doc, data)
doc.append('and the column ')
write_col(doc, data)
doc.append('are both ')
doc.append(bold('non-zero vectors'))
doc.append(', meaning their multiplication will be a non-zero matrix. ')
doc.append('Therefore, they are ')
doc.append(bold('linearly independent'))
doc.append(', and thus we can pick them out.')
doc.append('\nAlso, we have to divide either one of them by the element ')
write_element(doc, data)
doc.append('to ensure it is not duplicated in the matrix multiplication.')
doc.append('\nAnd then, we have')
with doc.create(Alignat(numbering=False, escape=False)) as agn:
agn.append(r'C_{')
agn.append(data.n + 1)
agn.append(r'} \cdot')
agn.append(r'R_{')
agn.append(data.m + 1)
agn.append(r'} &= ')
agn.append(Matrix(data.col, mtype='b'))
agn.append(r'\cdot')
agn.append(Matrix(data.row, mtype='b'))
agn.append(r'\\ &= ')
agn.append(Matrix(data.CnRm, mtype='b'))
doc.append(', which will always be exactly equal to ')
doc.append(Math(data=[r'A_{', data.mat_idx, r'} \ '], inline=True, escape=False))
doc.append('.\nThus, we are left with the last matrix ')
doc.append(Math(data=[r'A_{', data.remainder_idx, r'} \ '], inline=True, escape=False))
doc.append(r'with a ')
doc.append(bold('rank of zero'))
doc.append(r', or in other words, a ')
doc.append(bold('zero-matrix'))
doc.append(r'.')
with doc.create(Alignat(numbering=False, escape=False)) as agn:
agn.append(r'A_{')
agn.append(data.mat_idx)
agn.append(r'} - ')
agn.append(r'C_{')
agn.append(data.n + 1)
agn.append(r'} \cdot')
agn.append(r'R_{')
agn.append(data.m + 1)
agn.append(r'} &= ')
agn.append(Matrix(data.mat, mtype='b'))
agn.append(r' - ')
agn.append(Matrix(data.CnRm, mtype='b'))
agn.append(r'\\ = A_{')
agn.append(data.remainder_idx)
agn.append(r'} &= ')
agn.append(Matrix(data.remainder, mtype='b'))
#agn.append(bNiceMatrix(
#data.remainder, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#))
return
case State.NO_ZERO:
doc.append('We have the matrix')
doc.append(Math(
data=[
r'A_{', data.mat_idx, r'} = ',
Matrix(data.mat, mtype='b'),
#bNiceMatrix(
#data.mat, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#)
],
inline=False,
escape=False,
))
doc.append('We can see that the row ')
write_row(doc, data)
doc.append('and the column ')
write_col(doc, data)
doc.append('are both ')
doc.append(bold('non-zero vectors'))
doc.append(', meaning their multiplication will be a non-zero matrix. ')
doc.append('Therefore, they are ')
doc.append(bold('linearly independent'))
doc.append(', and thus we can pick them out.')
doc.append('\nAlso, we have to divide either one of them by the element ')
write_element(doc, data)
doc.append('to ensure it is not duplicated in the matrix multiplication.')
doc.append('\nAnd then, we have')
with doc.create(Alignat(numbering=False, escape=False)) as agn:
agn.append(r'C_{')
agn.append(data.n + 1)
agn.append(r'} \cdot')
agn.append(r'R_{')
agn.append(data.m + 1)
agn.append(r'} &= ')
agn.append(Matrix(data.col, mtype='b'))
agn.append(r'\cdot')
agn.append(Matrix(data.row, mtype='b'))
agn.append(r'\\ &= ')
agn.append(Matrix(data.CnRm, mtype='b'))
doc.append(', which when substracted from ')
doc.append(Math(data=[r'A_{', data.mat_idx, r'} \ '], inline=True, escape=False))
doc.append(r'will result in a new matrix ')
doc.append(Math(data=[r'A_{', data.remainder_idx, r'} \ '], inline=True, escape=False))
doc.append(r'with a ')
doc.append(bold('lower rank'))
with doc.create(Alignat(numbering=False, escape=False)) as agn:
agn.append(r'A_{')
agn.append(data.mat_idx)
agn.append(r'} - ')
agn.append(r'C_{')
agn.append(data.n + 1)
agn.append(r'} \cdot')
agn.append(r'R_{')
agn.append(data.m + 1)
agn.append(r'} &= ')
agn.append(Matrix(data.mat, mtype='b'))
agn.append(r' - ')
agn.append(Matrix(data.CnRm, mtype='b'))
agn.append(r'\\ = A_{')
agn.append(data.remainder_idx)
agn.append(r'} &= ')
agn.append(Matrix(data.remainder, mtype='b'))
#agn.append(bNiceMatrix(
#data.remainder, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#))
doc.append('because ')
write_row(doc, data)
doc.append('and ')
write_col(doc, data)
doc.append('have been reduced to 0.')
return
case State.ZERO_ROW_COL:
doc.append('We have the matrix')
doc.append(Math(
data=[
r'A_{', data.mat_idx, r'} = ',
Matrix(data.mat, mtype='b'),
#bNiceMatrix(
#data.mat, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#)
],
inline=False,
escape=False,
))
doc.append('We can see that the row ')
write_row(doc, data)
doc.append('and the column ')
write_col(doc, data)
doc.append('are both ')
doc.append(bold('zero vectors'))
doc.append(', meaning they are linearly dependent and choosing them will do nothing.')
return
case State.ZERO_ROW:
doc.append('We have the matrix')
doc.append(Math(
data=[
r'A_{', data.mat_idx, r'} = ',
Matrix(data.mat, mtype='b'),
#bNiceMatrix(
#data.mat, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#)
],
inline=False,
escape=False,
))
doc.append('We can see that the row ')
write_row(doc, data)
doc.append('is a ')
doc.append(bold('zero vector'))
doc.append(', meaning validly multiplying it with any vector/matrix ')
doc.append('will always give a zero-matrix.')
return
case State.ZERO_COL:
doc.append('We have the matrix')
doc.append(Math(
data=[
r'A_{', data.mat_idx, r'} = ',
Matrix(data.mat, mtype='b'),
#bNiceMatrix(
#data.mat, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#)
],
inline=False,
escape=False,
))
doc.append('We can see that the column ')
write_col(doc, data)
doc.append('is a ')
doc.append(bold('zero vector'))
doc.append(', meaning validly multiplying it with any vector/matrix ')
doc.append('will always give a zero-matrix.')
return
case State.ZERO_EL:
doc.append('We have the matrix')
doc.append(Math(
data=[
r'A_{', data.mat_idx, r'} = ',
Matrix(data.mat, mtype='b'),
#bNiceMatrix(
#data.mat, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#)
],
inline=False,
escape=False,
))
doc.append('Though neither the row ')
write_row(doc, data)
doc.append('nor the column ')
write_col(doc, data)
doc.append('is a ')
doc.append(bold('non-zero vector'))
doc.append(', we can see that the element ')
write_element(doc, data)
doc.append('\nThis means ... ')
doc.append('and they cannot be picked.')
return
case State.TEMPORARY_SKIPPING_COL:
doc.append('We have found a non-zero element, which is ')
write_element(doc, data)
doc.append(Math(
data=[
r'A_{', data.mat_idx, r'} = ',
Matrix(data.mat, mtype='b'),
#bNiceMatrix(
#data.mat, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#)
],
inline=False,
escape=False,
))
doc.append('Now the row ')
write_row(doc, data)
doc.append('and the column ')
write_col(doc, data)
doc.append('are both ')
doc.append(bold('linearly independent '))
doc.append(r' and thus we can pick them out.')
doc.append('\nAlso, we have to divide either one of them by the element ')
write_element(doc, data)
doc.append('to ensure it is not duplicated in the matrix multiplication.')
doc.append('\nAnd then, we have')
with doc.create(Alignat(numbering=False, escape=False)) as agn:
agn.append(r'C_{')
agn.append(data.n + 1)
agn.append(r'} \cdot')
agn.append(r'R_{')
agn.append(data.m + 1)
agn.append(r'} &= ')
agn.append(Matrix(data.col, mtype='b'))
agn.append(r'\cdot')
agn.append(Matrix(data.row, mtype='b'))
agn.append(r'\\ &= ')
agn.append(Matrix(data.CnRm, mtype='b'))
doc.append(', which when substracted from ')
doc.append(Math(data=[r'A_{', data.mat_idx, r'} \ '], inline=True, escape=False))
doc.append(r'will result in a new matrix ')
doc.append(Math(data=[r'A_{', data.remainder_idx, r'} \ '], inline=True, escape=False))
doc.append(r'with a ')
doc.append(bold('lower rank'))
with doc.create(Alignat(numbering=False, escape=False)) as agn:
agn.append(r'A_{')
agn.append(data.mat_idx)
agn.append(r'} - ')
agn.append(r'C_{')
agn.append(data.n + 1)
agn.append(r'} \cdot')
agn.append(r'R_{')
agn.append(data.m + 1)
agn.append(r'} &= ')
agn.append(Matrix(data.mat, mtype='b'))
agn.append(r' - ')
agn.append(Matrix(data.CnRm, mtype='b'))
agn.append(r'\\ = A_{')
agn.append(data.remainder_idx)
agn.append(r'} &= ')
agn.append(Matrix(data.remainder, mtype='b'))
#agn.append(bNiceMatrix(
#data.remainder, color='red!15',
#rows=str(data.m + 1),
#cols=str(data.n + 1),
#))
doc.append('because ')
write_row(doc, data)
doc.append('and ')
write_col(doc, data)
doc.append('have been reduced to 0.')
return
case _:
doc.append('\nSomething went wrong?')
return
def write_step_action(doc: Document, step: Step):
data = step.data
match step.action:
case Action.START:
return
case Action.END:
doc.append('\nBravo! We have successfully decomposed the matrix.')
return
case Action.INCREMENT_ROW_COL:
doc.append('\nWe then increment our row and column indices and continue searching.')
return
case Action.INCREMENT_ROW:
doc.append('\nTherefore, we increment our row index while keeping the same column.')
return
case Action.INCREMENT_COL:
doc.append('\nThus, we increment our column index but keep the same row.')
return
case Action.FIND_NONZERO_EL:
doc.append('\nIn this case, we increment our column index while keeping the same row ')
doc.append('until we find a non-zero element. (We will definitely find it because ')
doc.append('otherwise it will be a zero-vector row case.)')
return
case Action.INCREMENT_ROW_AND_RECALL_COL:
doc.append('\nHowever, now we have to go back to the old column index ')
doc.append('while incrementing our row, searching for the next combination.')
return
case _:
doc.append('\nSomething went wrong?')
return
for step in decom.steps:
with doc.create(Subsection('Step ' + str(step.number))):
write_step_state(doc, step)
write_step_action(doc, step)
return
# Helper functions
def format(x): # Replace all -0.0 with 0.0
if x == 0.0:
return 0.0
#float_precision = 2 # Set = -1 to ignore rounding
#if float_precision != -1:
#return np.round(x, float_precision)
return x
def vectorized_format(x):
return np.vectorize(format)(x)
# Main
def write_pdf(matrix: np.matrix):
decomposition = Decomposition()
# Maybe these clear()s fixed the bug? (repeating data of previous compositions)
decomposition.steps.clear()
decomposition.rows.clear()
decomposition.cols.clear()
decomposition.indices.clear()
decomposition.steps.append(Step(
0, State.START, Action.START,
Data(None, None, None, None, None, None, None, None, 0, 0)
))
decomposition = decom(matrix, 0, 0, 0, False, decomposition)
decomposition.original_matrix = matrix
# A = L
## Get matrix L
cols_as_arrays = [
col.A1 for col in decomposition.cols
]
decomposition.L = np.matrix(cols_as_arrays).T # Tranpose
## Get matrix U
rows_as_arrays = [
row.A1 for row in decomposition.rows
]
decomposition.U = np.matrix(rows_as_arrays)
# Format every matrix in the Decomposition
decomposition.L = vectorized_format(decomposition.L)
decomposition.U = vectorized_format(decomposition.U)
for i, row in enumerate(decomposition.rows):
decomposition.rows[i] = vectorized_format(row)
for i, col in enumerate(decomposition.cols):
decomposition.cols[i] = vectorized_format(col)
for step in decomposition.steps:
if step.data.mat is not None:
step.data.mat = vectorized_format(step.data.mat)
if step.data.row is not None:
step.data.row = vectorized_format(step.data.row)
if step.data.col is not None:
step.data.col = vectorized_format(step.data.col)
if step.data.CnRm is not None:
step.data.CnRm = vectorized_format(step.data.CnRm)
if step.data.remainder is not None:
step.data.remainder = vectorized_format(step.data.remainder)
#step.view()
#########################################
# Set up LaTex Document
doc = Document()
doc.documentclass = Command(
'documentclass',
options=['preview=true', NoEscape(r'border={10pt 10pt 50pt 10pt}')],
arguments=['standalone'],
)
# Incompatible with Streamlit
#doc.packages.append(Package('nicematrix'))
# Write answer
with doc.create(Section('Answer')):
with doc.create(Subsection('Solution')):
write_result_CR(doc, decomposition, False)
with doc.create(Subsection('Solution 2')):
write_result_LU(doc, decomposition)
# Write details
with doc.create(Section('Detailed Solution')):
write_detailed_solution(doc, decomposition)
# Generate PDF
doc.generate_pdf('decomposition', clean_tex=True, clean=True, compiler='latexmk')
return