-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblock_partitioned_matrices.py
More file actions
1626 lines (1279 loc) · 53.2 KB
/
block_partitioned_matrices.py
File metadata and controls
1626 lines (1279 loc) · 53.2 KB
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""A library of operations on partitioned matrices.
The blocks of these matrices can in turn be block matrices themselves, or they
canbe torch tensors. This way, it becomes easy to express linear algebra
operations like matrix multiplication, inversion, and solving linear systems
with matrices that have hierarchical structure. For example, if a matrix A has
a 2x2 block structre and each of these blocks are in turn block diagonal
matrices,
one can define
A = Symmetri2x2(
# A11 is a block diagona matrix with two blocks.
block11=Diagonal([torch.randn(3, 3), torch.randn(2, 2)]),
# A12 is a blockdiagonal matrix with two blocks.
block12=Diagonal([torch.randn(3, 2), torch.randn(2, 2)]),
A A22 is just a dense matrix
block22=torch.randn(2, 2)
)
One can then multiply A by a vector v with A @ v, or solve the linear system A @
x = b with x = A.solve(b), or recover the inverse of A with A.invert().
The package provides placeholder matrices for Zero and Identity that don't take up
any space in memory or compute other than for their metadata.
Here is the class hierarchy:
Matrix
├── Tensor (also torch.Tensor)
├── Identity
│ └── ScaledIdentity
├── Zero
└── Ragged
├── Generic
│ ├── Generic3x3
│ ├── Vertical
│ └── Horizontal
├── Symmetric2x2
└── Tridiagonal
├── SymmetricTriDiagonal
├── LowerBiDiagonal
│ ├── IdentityWithLowerDiagonal
│ └── LowerDiagonal
├── UpperBiDiagonal
│ ├── IdentityWithUpperDiagonal
│ └── UpperDiagonal
└── Diagonal
"""
from typing import Any, Callable, Iterator, Sequence
from functools import singledispatchmethod, cached_property
import numpy as np
import torch
class Matrix:
"Base class for partitioned matrices."
def to_tensor(self) -> torch.Tensor:
raise NotImplementedError
def invert(self) -> "Matrix":
raise NotImplementedError
def solve(self, rhs: "Matrix") -> "Matrix":
raise NotImplementedError
def __matmul__(self, other: "Matrix") -> "Matrix":
raise NotImplementedError
def __add__(self, other: "Matrix") -> "Matrix":
raise NotImplementedError
def __sub__(self, other: "Matrix") -> "Matrix":
raise NotImplementedError
@property
def width(self) -> int:
raise NotImplementedError
@property
def height(self) -> int:
raise NotImplementedError
@property
def T(self) -> "Matrix":
raise NotImplementedError
class Tensor(torch.Tensor, Matrix):
"Endows torch.Tensor with extra matrix operations."
def __init__(self, *args):
torch.Tensor.__init__(*args)
if self.ndim != 2:
raise ValueError("Tensor must be a 2D tensor")
def __repr__(self) -> str:
return "bpm." + super().__repr__()
def invert(self) -> "Tensor":
return Tensor(torch.linalg.inv(self))
@singledispatchmethod
def solve(self, rhs: Matrix) -> Matrix:
return Tensor(torch.linalg.solve(self, rhs.to_tensor()))
def to_tensor(self) -> torch.Tensor:
return torch.Tensor(self)
@singledispatchmethod
def __matmul__(self, other: Matrix) -> Matrix:
return Tensor(torch.Tensor.__matmul__(self, other))
@singledispatchmethod
def __add__(self, other: Matrix) -> Matrix:
return Tensor(torch.Tensor.__add__(self, other))
@singledispatchmethod
def __sub__(self, other: Matrix) -> Matrix:
return Tensor(torch.Tensor.__sub__(self, other))
@property
def T(self) -> "Tensor":
return Tensor(super(torch.Tensor, self).T)
def __neg__(self) -> "Tensor":
return Tensor(torch.Tensor.__neg__(self))
@property
def width(self) -> int:
return self.shape[1]
@property
def height(self) -> int:
return self.shape[0]
@staticmethod
def wrap(tensor: torch.Tensor | Matrix) -> "Tensor":
if isinstance(tensor, Matrix):
return tensor
elif isinstance(tensor, torch.Tensor):
if tensor.ndim != 2:
raise ValueError("Tensor must be a 2D tensor")
return Tensor(tensor)
raise ValueError("Tensor must be a torch.Tensor or Matrix")
# TODO: is this needed?
@Tensor.solve.register
def _(self, rhs: Tensor) -> Tensor:
return Tensor(torch.linalg.solve(self, rhs))
class Identity(Matrix):
def __init__(self, dimension: int = 0):
self.dimension = dimension
def __matmul__(self, other: Matrix) -> Matrix:
if self.dimension != 0 and other.height != self.dimension:
raise ValueError(
f"Dimension mismatch: Identity({self.dimension}) @ Matrix({other.height}x{other.width})"
)
return other
def solve(self, rhs: Matrix) -> Matrix:
return rhs
def invert(self) -> "Identity":
return self
def to_tensor(self) -> torch.Tensor:
return torch.eye(self.dimension)
def __eq__(self, other: Matrix) -> bool:
return isinstance(other, Identity) and self.dimension == other.dimension
def __add__(self, other: Matrix) -> Matrix:
return other + self
@singledispatchmethod
def __sub__(self, other: Matrix) -> Matrix:
return Tensor(self.to_tensor() - other.to_tensor())
@property
def T(self) -> "Identity":
return self
@property
def width(self) -> int:
return self.dimension
@property
def height(self) -> int:
return self.dimension
def __mul__(self, scalar: float) -> "ScaledIdentity":
return ScaledIdentity(scalar, self.dimension)
def __rmul__(self, scalar: float) -> "ScaledIdentity":
return ScaledIdentity(scalar, self.dimension)
def __neg__(self) -> "ScaledIdentity":
return ScaledIdentity(-1.0, self.dimension)
@Identity.__sub__.register
def _(self, other: Tensor) -> Matrix:
return Tensor(self.to_tensor() - other)
@Tensor.__matmul__.register
def _(self, other: Identity) -> Tensor:
if other.dimension != 0 and self.width != other.dimension:
raise ValueError(
f"Dimension mismatch: Tensor({self.height}x{self.width}) @ Identity({other.dimension})"
)
return self
class ScaledIdentity(Identity):
def __init__(self, scale: float, dimension: int = 0):
super().__init__(dimension)
self.scale = scale
def to_tensor(self) -> torch.Tensor:
return self.scale * torch.eye(self.dimension)
def invert(self) -> "ScaledIdentity":
return ScaledIdentity(1.0 / self.scale, self.dimension)
def solve(self, rhs: Matrix) -> Matrix:
return ScaledIdentity(1.0 / self.scale, self.dimension) @ rhs
@singledispatchmethod
def __matmul__(self, other: Matrix) -> Matrix:
return self.scale * other
def __add__(self, other: Matrix) -> Matrix:
# Delegate to the other matrix.
return other + self
def __mul__(self, scalar: float) -> "ScaledIdentity":
return ScaledIdentity(self.scale * scalar, self.dimension)
def __rmul__(self, scalar: float) -> "ScaledIdentity":
return self.__mul__(scalar)
def __neg__(self) -> "ScaledIdentity":
return ScaledIdentity(-self.scale, self.dimension)
def __repr__(self):
return f"ScaledIdentity(scale={self.scale}, dimension={self.dimension})"
@ScaledIdentity.__matmul__.register
def _(self, other: "ScaledIdentity") -> "ScaledIdentity":
if (
self.dimension != 0
and other.dimension != 0
and self.dimension != other.dimension
):
raise ValueError("Dimension mismatch")
return ScaledIdentity(self.scale * other.scale, self.dimension or other.dimension)
@ScaledIdentity.__matmul__.register
def _(self, other: Tensor) -> Tensor:
return Tensor(self.scale * other.to_tensor())
@Tensor.__matmul__.register
def _(self, other: ScaledIdentity) -> Tensor:
return Tensor(self.to_tensor() * other.scale)
class Zero(Matrix):
def __init__(self, shape: tuple[int, int] = ()):
self.shape = shape
def __matmul__(self, other: Matrix) -> "Zero":
if not self.shape:
return Zero(other.shape)
if other.height != self.shape[1]:
raise ValueError(f"Shape mismatch {self} vs {other.height} x {other.width}")
return Zero((self.height, other.width))
def to_tensor(self) -> torch.Tensor:
return torch.zeros(*self.shape)
def __neg__(self) -> "Zero":
return self
def invert(self) -> "Zero":
raise ValueError("Zero matrix is not invertible")
def solve(self, rhs: "Matrix") -> "Matrix":
raise ValueError("Zero matrix is not invertible")
def __eq__(self, other: Matrix) -> bool:
return isinstance(other, Zero) and self.shape == other.shape
def __add__(self, other: Matrix) -> Matrix:
return other
def __sub__(self, other: Matrix) -> Matrix:
return -other
@property
def width(self) -> int:
return self.shape[1]
@property
def height(self) -> int:
return self.shape[0]
@property
def T(self) -> "Zero":
if not self.shape:
return Zero()
return Zero((self.shape[1], self.shape[0]))
@Tensor.__matmul__.register
def _(self, other: Zero) -> Tensor:
if self.width != other.height:
raise ValueError(f"Shape mismatch {self} vs {other.height} x {other.width}")
return Zero((self.shape[0], other.width))
@Tensor.__add__.register
def _(self, other: Zero) -> Tensor:
if self.width != other.width or self.height != other.height:
raise ValueError(f"Shape mismatch {self} vs {other.height} x {other.width}")
return self
@Tensor.__sub__.register
def _(self, other: Zero) -> Tensor:
return self.__add__(other)
@Tensor.solve.register
def _(self, rhs: Zero) -> Zero:
return Zero((self.width, rhs.width))
def reshape_to_2d_list(lst: Sequence[Any], shape: tuple[int, int]) -> list[list[Any]]:
if len(lst) != shape[0] * shape[1]:
raise ValueError(f"Length of list {len(lst)} does not match shape {shape}")
return [lst[row * shape[1] : (row + 1) * shape[1]] for row in range(shape[0])]
class Ragged(Matrix):
"""An unstructured block-partitioned matrix.
Each block in this matrix can in turn be a matrix. You can index into the
blocks, traverse them, and reshape the block structure.
"""
def __init__(self, blocks: Sequence[Sequence[Matrix]]):
self.blocks = [list(map(Tensor.wrap, row)) for row in blocks]
def __neg__(self) -> "Ragged":
return self.apply_unary_operation(lambda m: -m)
@singledispatchmethod
def __add__(self, other: Matrix) -> Matrix:
raise NotImplementedError # Special cases implemented below.
@singledispatchmethod
def __sub__(self, other: Matrix) -> Matrix:
raise NotImplementedError # Special cases implemented below.
def flatten(self) -> Iterator[Matrix]:
"Iterate over all the blocks in the matrix in row-major order."
for row in self.blocks:
yield from row
@cached_property
def flat(self) -> list[Matrix]:
return list(self.flatten())
def num_blocks(self) -> int:
return len(self.flat)
def apply_unary_operation(self, op: Callable[[Matrix], Matrix]) -> list[Matrix]:
return self.__class__(Ragged([[op(b) for b in row] for row in self.blocks]))
def apply_binary_operation(
self, other: "Ragged", op: Callable[[Matrix, Matrix], Matrix]
) -> "Ragged":
if len(self.blocks) != len(other.blocks):
raise ValueError("Number of rows in the operands must match")
result_blocks = []
for row, other_row in zip(self.blocks, other.blocks):
if len(row) != len(other_row):
raise ValueError("Number of columns in each row must match")
result_blocks.append([op(b, b_other) for b, b_other in zip(row, other_row)])
return self.__class__(Ragged(result_blocks))
@singledispatchmethod
def __mul__(self, scalar: Any) -> "Ragged":
return NotImplemented
@__mul__.register
def _(self, scalar: float) -> "Ragged":
return self.apply_unary_operation(lambda m: m * scalar)
@__mul__.register
def _(self, scalar: int) -> "Ragged":
return self.apply_unary_operation(lambda m: m * scalar)
def __rmul__(self, scalar: Any) -> "Ragged":
return self.__mul__(scalar)
@Ragged.__add__.register
def _(self, other: Ragged) -> Ragged:
return self.apply_binary_operation(other, lambda m1, m2: m1 + m2)
@Ragged.__add__.register
def _(self, other: Zero) -> Ragged:
return self
@Ragged.__sub__.register
def _(self, other: Ragged) -> Ragged:
return self.apply_binary_operation(other, lambda m1, m2: m1 - m2)
@Ragged.__sub__.register
def _(self, other: Zero) -> Ragged:
return self
class Generic(Ragged):
"""A ragged array whose rows have the same number of columns."""
@singledispatchmethod
def __init__(self, blocks: Sequence[Sequence[Matrix]], validate=True):
super().__init__(blocks)
self.shape = (len(blocks), len(blocks[0]))
if validate:
self.validate()
@__init__.register
def _(self, blocks: Ragged, validate=True):
self.__init__(blocks.flat, validate)
@singledispatchmethod
def __matmul__(self, other: Matrix) -> Matrix:
raise NotImplementedError
def validate(self):
# Ensure the matrix isn't ragged.
for row in self.blocks:
if len(row) != len(self.blocks[0]):
raise ValueError("All rows must have the same length")
# Ensure the blocks have compatible shapes. The blocks in a row must
# have the same height, and the blocks in a column must have the same width.
for r in range(self.shape[0]):
heights = np.array([b.height for b in self[r, :].flatten()])
if not np.all(heights == heights[0]):
raise ValueError("All blocks in row must have the same height")
for c in range(self.shape[1]):
widths = np.array([b.width for b in self[:, c].flatten()])
if not np.all(widths == widths[0]):
raise ValueError("All blocks in column must have the same width")
return self
def __getitem__(self, index: Any) -> Matrix | "Horizontal" | "Vertical" | "Generic":
if not isinstance(index, tuple):
raise ValueError("Index must be a tuple of two elements (row, column)")
row, col = index
if isinstance(row, int) and isinstance(col, int):
# Return an element
return self.blocks[row][col]
if isinstance(row, int):
# col is a slice. Return a horizontally stacked matrix.
return Horizontal(self.blocks[row][col], validate=False)
if isinstance(col, int):
# row is a slice. Return a vertically stacked matrix.
return Vertical([row[col] for row in self.blocks[row]], validate=False)
# Both row and col are slices. Return a block matrix.
return Generic([row[col] for row in self.blocks[row]], validate=False)
def reshape(self, shape: tuple[int, int]) -> "Generic":
return Generic(reshape_to_2d_list(list(self.flatten()), shape))
def to_tensor(self) -> torch.Tensor:
return torch.vstack(
[torch.hstack([b.to_tensor() for b in row]) for row in self.blocks]
)
@property
def width(self) -> int:
# Sum up the width of the blocks of the first row.
return sum(b.width for b in self.blocks[0])
@property
def height(self) -> int:
# Sum up the height of the blocks of the first column.
return sum(row[0].height for row in self.blocks)
@property
def T(self) -> "Generic":
return Generic(
[
[self.blocks[r][c].T for r in range(self.shape[0])]
for c in range(self.shape[1])
]
)
@Generic.__matmul__.register
def _(self, other: Generic) -> Generic:
if self.shape[1] != other.shape[0]:
raise ValueError(f"Block dimension mismatch: {self.shape} vs {other.shape}")
return Generic(
[
[
sum(
(
self.blocks[i][j] @ other.blocks[j][k]
for j in range(self.shape[1])
),
start=Zero(),
)
for k in range(other.shape[1])
]
for i in range(self.shape[0])
]
)
class Generic3x3(Generic):
"""A 3x3 block matrix."""
@singledispatchmethod
def __init__(self, blocks: Sequence[Sequence[Matrix]]):
super().__init__(blocks)
if self.shape != (3, 3):
raise ValueError("Generic3x3 must be 3x3")
@__init__.register
def _(self, ragged: Ragged):
self.__init__(ragged.blocks)
@singledispatchmethod
def solve(self, other: Matrix) -> Matrix:
raise NotImplementedError
@property
def T(self) -> "Generic3x3":
return Generic3x3(super().T.blocks)
@singledispatchmethod
def __matmul__(self, other: Matrix) -> Matrix:
return super().__matmul__(other)
@Generic3x3.__matmul__.register
def _(self, other: Generic3x3) -> Generic3x3:
return Generic3x3(Generic.__matmul__(self, other).blocks)
def check_torch_escape(x: Any):
assert isinstance(x, Matrix)
if isinstance(x, torch.Tensor):
assert isinstance(x, Tensor)
def _generic3x3_solve(self, other: Generic) -> Generic:
if other.shape[0] != 3:
raise ValueError("Other must be a 3xN block matrix")
A = self.blocks
B = other.blocks
check_torch_escape(A[0][0])
# The Schur complement of A in its (0,0) block.
# We calculate U01 = A00^{-1} A01 and U02 = A00^{-1} A02 using solve()
# instead of explicit inversion.
U01 = A[0][0].solve(A[0][1])
U02 = A[0][0].solve(A[0][2])
S11 = A[1][1] - A[1][0] @ U01
S21 = A[2][1] - A[2][0] @ U01
S22_level1 = A[2][2] - A[2][0] @ U02
S12 = A[1][2] - A[1][0] @ U02
# Calculate U12_schur = S11^{-1} S12 using solve()
U12_schur = S11.solve(S12)
S22 = S22_level1 - S21 @ U12_schur
# The LDU decomposition of A is
#
# L = [ I 0 0 ]
# [ A10 invA00 I 0 ]
# [ A20 invA00 S21 invS11 I ]
#
# D = [ A00 0 0 ]
# [ 0 S11 0 ]
# [ 0 0 S22 ]
#
# U = [ I invA00 A01 invA00 A02 ]
# [ 0 I invS11 S12 ]
# [ 0 0 I ]
#
# So that A = L D U.
#
# Define Z = U X so that A X = B becomes L D Z = B. We'll first solve for Z.
# To do that, we solve L Y = B followed by D Z = Y.
# --- Step 1: Forward substitution (solve for Z)
# Row 0 of Z:
# A00 * Z0 = B0 => Z0 = A00^{-1} B0
# In this first step, Y0 = B0 because L00 is Identity and L is lower triangular.
Z0 = [A[0][0].solve(b) for b in B[0]]
# Row 1 of Z
# Eliminate A10 using Z0.
# Effective equation: A11' * Z1 = B1 - A10 * Z0
Y1 = [b - A[1][0] @ z for b, z in zip(B[1], Z0)]
Z1 = [S11.solve(y) for y in Y1]
# Row 2:
# Eliminate A20 and A21.
# Effective equation: S22 * Z2 = B2 - A20 * Z0 - S21 * Z1
Y2 = [b - A[2][0] @ z0 - S21 @ z1 for b, z0, z1 in zip(B[2], Z0, Z1)]
Z2 = [S22.solve(y) for y in Y2]
# --- Step 3: Backward substitution (solve for X given Z)
# With Z computed, we solve U X = Z for X.
# U X = Z
# X2 = Z2
# X1 = Z1 - U12 * X2
# X0 = Z0 - U01 * X1 - U02 * X2
# We need U blocks:
# U01 = invA00 @ A01 (Already computed as U01)
# U02 = invA00 @ A02 (Already computed as U02)
# U12 = invS11 @ S12 (Already computed as U12_schur)
X2 = Z2
# Compute U12 term: U12_schur @ X2
U12_X2 = [U12_schur @ x for x in X2]
X1 = [z - u for z, u in zip(Z1, U12_X2)]
# Compute U01 and U02 terms
U01_X1 = [U01 @ x for x in X1]
U02_X2 = [U02 @ x for x in X2]
X0 = [z - u1 - u2 for z, u1, u2 in zip(Z0, U01_X1, U02_X2)]
return Generic([X0, X1, X2])
@Generic3x3.solve.register
def _(self, other: Generic3x3) -> Generic:
return Generic3x3(_generic3x3_solve(self, other).blocks)
class Symmetric2x2(Ragged):
"""Represents a symmetric 2x2 block matrix whose blocks are in turn Matrices."""
def __init__(self, block11: Matrix, block12: Matrix, block22: Matrix):
super().__init__([[block11, block12], [block22]])
@property
def block11(self) -> Matrix:
return self.blocks[0][0]
@property
def block12(self) -> Matrix:
return self.blocks[0][1]
@property
def block21(self) -> Matrix:
return self.block12.T
@property
def block22(self) -> Matrix:
return self.blocks[1][0]
@singledispatchmethod
def __matmul__(self, v: Matrix) -> Matrix:
raise NotImplementedError # Special cases implemented below.
def invert(self) -> "Symmetric2x2":
# S = UDU^T
U, D = self.UDU_decomposition()
# S^{-1} = U^-T D^-1 U^-1
# = [I 0] [D0^{-1} 0 ] [I -U0] = [D0^{-1} -D0^{-1} U0 ]
# [-U0^T I] [ 0 D1^{-1}] [0 I] [-U0^T D0^{-1} U0^T D0^{-1} U0 + D1^{-1}]
Dinv = D.invert()
block12 = -Dinv.flat[0] @ U.upper_blocks[0]
return Symmetric2x2(
block11=Dinv.flat[0],
block12=block12,
block22=Dinv.flat[1] - U.upper_blocks[0].T @ block12,
)
def invert_via_LDL(self) -> "Symmetric2x2":
# S = LDL^T
L, D = self.LDL_decomposition()
# S^{-1} = L^-T D^-1 L^-1
# = [I -L0'] [D0^{-1} 0 ] [I 0] = [D0^{-1} + L0' D1^{-1} L0 -L0' D1^{-1} ]
# [0 I ] [ 0 D1^{-1}] [-L0 I] [-D1^{-1} L0 D1^{-1} ]
Dinv = D.invert()
block12 = -L.lower_blocks[0].T @ Dinv.flat[1]
return Symmetric2x2(
block11=Dinv.flat[0]
+ L.lower_blocks[0].T @ Dinv.flat[1] @ L.lower_blocks[0],
block12=block12,
block22=Dinv.flat[1],
)
def to_tensor(self) -> torch.Tensor:
return torch.vstack(
[
torch.hstack([self.block11.to_tensor(), self.block12.to_tensor()]),
torch.hstack([self.block12.T.to_tensor(), self.block22.to_tensor()]),
]
)
def UDU_decomposition(
self,
) -> tuple["IdentityWithUpperDiagonal", "Diagonal"]:
b22_inv = self.block22.invert()
U = IdentityWithUpperDiagonal([self.block12 @ b22_inv])
D = Diagonal(
[self.block11 - self.block12 @ b22_inv @ self.block12.T, self.block22]
)
return U, D
def LDL_decomposition(self) -> tuple["IdentityWithLowerDiagonal", "Diagonal"]:
a11_inv = self.block11.invert()
L = IdentityWithLowerDiagonal([self.block12.T @ a11_inv])
D = Diagonal(
[self.block11, self.block22 - self.block12.T @ a11_inv @ self.block12]
)
return L, D
class Vertical(Generic):
"""Blocks stacked vertically."""
@singledispatchmethod
def __init__(self, blocks: Sequence[Matrix], validate=True):
super().__init__([[b] for b in blocks], validate)
if self.shape[1] != 1:
raise ValueError("Vertical matrix must have exactly one column")
@__init__.register
def _(self, blocks: Ragged, validate=True):
self.__init__(blocks.flat, validate=validate)
def blockwise_transpose(self) -> "Vertical":
"""Assuming this object has blocks v_ij, return a new Vertical object with blocks v_ji."""
# For the operation to make sense, every block v_i must have the
# the same number of sub-blocks. Ensure this is the case.
num_subblocks = self.flat[0].num_blocks()
for b in self.flat:
if b.num_blocks() != num_subblocks:
raise ValueError("All blocks must have the same number of sub-blocks")
return Vertical(
[
Vertical([self.flat[i].flat[j] for i in range(self.num_blocks())])
for j in range(num_subblocks)
]
)
@property
def T(self) -> "Horizontal":
return Horizontal([b.T for b in self.flat])
@Symmetric2x2.__matmul__.register
def __matmul__(self, v: Vertical) -> Vertical:
if v.num_blocks() != 2:
raise ValueError("Number of blocks in vector and matrix must match")
return Vertical(
[
self.block11 @ v.flat[0] + self.block12 @ v.flat[1],
self.block21 @ v.flat[0] + self.block22 @ v.flat[1],
]
)
@Generic.__matmul__.register
def _(self, other: Vertical) -> Vertical:
return Vertical(self @ Generic(other.blocks))
@Generic3x3.solve.register
def _(self, other: Vertical) -> Vertical:
return Vertical(_generic3x3_solve(self, other).flat)
class Horizontal(Generic):
"""Blocks stacked horizontally."""
@singledispatchmethod
def __init__(self, blocks: Sequence[Matrix], validate=True):
super().__init__([blocks], validate)
if self.shape[0] != 1:
raise ValueError("Horizontal matrix must have exactly one row")
@__init__.register
def _(self, blocks: Ragged, validate=True):
self.__init__(blocks.flat, validate=validate)
@property
def T(self) -> "Vertical":
return Vertical([b.T for b in self.flat])
def zero_upper_blocks_from_diagonal_blocks(
diagonal_blocks: Sequence[Matrix],
) -> list[Matrix]:
return [
Zero((D.height, Dnext.width))
for D, Dnext in zip(diagonal_blocks[:-1], diagonal_blocks[1:])
]
def zero_lower_blocks_from_diagonal_blocks(
diagonal_blocks: Sequence[Matrix],
) -> list[Matrix]:
return [
Zero((Dnext.height, D.width))
for D, Dnext in zip(diagonal_blocks[:-1], diagonal_blocks[1:])
]
class Tridiagonal(Ragged):
@singledispatchmethod
def __init__(
self,
diagonal_blocks: Sequence[Matrix],
lower_blocks: Sequence[Matrix],
upper_blocks: Sequence[Matrix],
):
if lower_blocks != [] and (len(diagonal_blocks) != len(lower_blocks) + 1):
raise ValueError(
"Number of diagonal blocks must be one more than the number of lower blocks"
)
if upper_blocks != [] and (len(diagonal_blocks) != len(upper_blocks) + 1):
raise ValueError(
"Number of diagonal blocks must be one more than the number of upper blocks"
)
super().__init__([diagonal_blocks, lower_blocks, upper_blocks])
@__init__.register
def _(self, diagonal_blocks: Ragged):
if len(diagonal_blocks.blocks) != 3:
raise ValueError("Ragged input must have exactly three rows")
self.__init__(*diagonal_blocks.blocks)
@property
def diagonal_blocks(self) -> Sequence[Matrix]:
return self.blocks[0]
@property
def lower_blocks(self) -> Sequence[Matrix]:
if self.blocks[1] == []:
return zero_lower_blocks_from_diagonal_blocks(self.diagonal_blocks)
return self.blocks[1]
@property
def upper_blocks(self) -> Sequence[Matrix]:
if self.blocks[2] == []:
return zero_upper_blocks_from_diagonal_blocks(self.diagonal_blocks)
return self.blocks[2]
@singledispatchmethod
def __matmul__(self, other: Matrix):
raise NotImplementedError
@__matmul__.register
def _(self, v: Vertical) -> Vertical:
L = LowerDiagonal(
self.diagonal_blocks[0].height,
self.lower_blocks,
self.diagonal_blocks[-1].width,
)
D = Diagonal(self.diagonal_blocks)
U = UpperDiagonal(
self.diagonal_blocks[0].width,
self.upper_blocks,
self.diagonal_blocks[-1].height,
)
return L @ v + D @ v + U @ v
@property
def height(self) -> int:
return sum(b.height for b in self.diagonal_blocks)
@property
def width(self) -> int:
return sum(b.width for b in self.diagonal_blocks)
@staticmethod
def blockwise_transpose(M: Generic) -> "Tridiagonal":
"""Compute the blockwise transpose of matrix whose blocks are tridiagonal.
The blockwise transpose of a block matrix generalizes the transpose of a
2D matrix. For a block matrix M, denote the (u,v)'th element of its
(i,j)'th block by M_{ij, uv}. The blockwise transpsoe of M is a block
matrix whose (u,v)'th element of its (i,j)'th block is M_{uv, ij}.
For this particular method, the blocks of M are presumbed to be
tridiagonal. That means M_{ij, uv} = 0 whenever |u-v| > 1. This implies
the blockwise tranpose of M satisfies M_{ij} = 0 whenever |i-j| > 1. In
other words, the blockwise transpose of M is block-tridiagonal.
"""
# Validation: All blocks must have the same number of diagonal blocks.
num_diags = len(next(M.flatten()).diagonal_blocks)
if not all(len(block.diagonal_blocks) == num_diags for block in M.flatten()):
raise ValueError("All blocks must have the same number of diagonal blocks")
return Tridiagonal(
[
Generic([[b.diagonal_blocks[i] for b in M.flatten()]], validate=False)
.reshape(M.shape)
.validate()
for i in range(num_diags)
],
lower_blocks=[
Generic([[b.lower_blocks[i] for b in M.flatten()]], validate=False)
.reshape(M.shape)
.validate()
for i in range(num_diags - 1)
],
upper_blocks=[
Generic([[b.upper_blocks[i] for b in M.flatten()]], validate=False)
.reshape(M.shape)
.validate()
for i in range(num_diags - 1)
],
)
def solve(self, v: Vertical) -> Vertical:
L, D, U = self.LDU_decomposition()
return U.solve(D.solve(L.solve(v)))
def LDU_decomposition(
self,
) -> tuple["IdentityWithLowerDiagonal", "Diagonal", "IdentityWithUpperDiagonal"]:
"Factorize into L D U, where L is lower block-diagonal, U is upper block-diagonal, and D is diagonal."
# An illustration of block LDU decomposition:
#
# T = L D U
#
# [ D₀ U₀ 0 ... ] = [ I 0 0 ... ] [ B₀ 0 0 ... ] [ I V₀ 0 ... ]
# [ L₀ D₁ U₁ ... ] [ K₀ I 0 ... ] [ 0 B₁ 0 ... ] [ 0 I V₁ ... ]
# [ 0 L₁ D₂ ... ] [ 0 K₁ I ... ] [ 0 0 B₂ ... ] [ 0 0 I ... ]
# ... ...
# [ 0 0 0 ... D_{n-2} U_{n-2} ] [ 0 0 0 ... I 0 ] [ 0 0 0 ... B_{n-2} 0 ] [ 0 ... 0 I V_{n-2} ]
# [ 0 0 0 ... L_{n-2} D_{n-1} ] [ 0 0 0 ... K_{n-2} I ] [ 0 0 0 ... 0 B_{n-1} ] [ 0 ... 0 I ]
#
#
# We have the base case
# B[0] = D[0]
#
# For i < n-1, we also have
# U[i] = B[i] @ V[i]
# L[i] = K[i] @ B[i]
# D[i+1] = B[i+1] + K[i] @ B[i] @ V[i]
#
# These imply, respectively,
#
# V[i] = B[i] \ U[i]
# K[i] = (B[i].T \ L[i].T).T
# B[i+1] = D[i+1] - K[i] @ B[i] @ V[i]
Bs = [self.diagonal_blocks[0]]
Vs = []
Ks = []
for U, L, Dnext in zip(
self.upper_blocks, self.lower_blocks, self.diagonal_blocks[1:]