-
Notifications
You must be signed in to change notification settings - Fork 239
/
matrices.py
699 lines (586 loc) · 19.8 KB
/
matrices.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
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
"""Module exposing the `Matrices` and `MatricesMetric` class."""
from functools import reduce
import geomstats.backend as gs
import geomstats.errors
from geomstats.algebra_utils import from_vector_to_diagonal_matrix
from geomstats.geometry.base import VectorSpace
from geomstats.geometry.euclidean import EuclideanMetric
class Matrices(VectorSpace):
"""Class for the space of matrices (m, n).
Parameters
----------
m, n : int
Integers representing the shapes of the matrices: m x n.
"""
def __init__(self, m, n, **kwargs):
if "default_point_type" not in kwargs.keys():
kwargs["default_point_type"] = "matrix"
super(Matrices, self).__init__(
shape=(m, n), metric=MatricesMetric(m, n), **kwargs
)
geomstats.errors.check_integer(n, "n")
geomstats.errors.check_integer(m, "m")
self.m = m
self.n = n
def belongs(self, point, atol=gs.atol):
"""Check if point belongs to the Matrices space.
Parameters
----------
point : array-like, shape=[..., m, n]
Point to be checked.
atol : float
Unused here.
Returns
-------
belongs : array-like, shape=[...,]
Boolean evaluating if point belongs to the Matrices space.
"""
ndim = point.ndim
if ndim == 1:
return False
mat_dim_1, mat_dim_2 = point.shape[-2:]
belongs = (mat_dim_1 == self.m) and (mat_dim_2 == self.n)
return belongs if ndim == 2 else gs.tile(gs.array([belongs]), [point.shape[0]])
@staticmethod
def equal(mat_a, mat_b, atol=gs.atol):
"""Test if matrices a and b are close.
Parameters
----------
mat_a : array-like, shape=[..., dim1, dim2]
Matrix.
mat_b : array-like, shape=[..., dim2, dim3]
Matrix.
atol : float
Tolerance.
Optional, default: backend atol.
Returns
-------
eq : array-like, shape=[...,]
Boolean evaluating if the matrices are close.
"""
return gs.all(gs.isclose(mat_a, mat_b, atol=atol), (-2, -1))
@staticmethod
def mul(*args):
"""Compute the product of matrices a1, ..., an.
Parameters
----------
a1 : array-like, shape=[..., dim_1, dim_2]
Matrix.
a2 : array-like, shape=[..., dim_2, dim_3]
Matrix.
...
an : array-like, shape=[..., dim_n-1, dim_n]
Matrix.
Returns
-------
mul : array-like, shape=[..., dim_1, dim_n]
Result of the product of matrices.
"""
return reduce(gs.matmul, args)
@classmethod
def bracket(cls, mat_a, mat_b):
"""Compute the commutator of a and b, i.e. `[a, b] = ab - ba`.
Parameters
----------
mat_a : array-like, shape=[..., n, n]
Matrix.
mat_b : array-like, shape=[..., n, n]
Matrix.
Returns
-------
mat_c : array-like, shape=[..., n, n]
Commutator.
"""
return cls.mul(mat_a, mat_b) - cls.mul(mat_b, mat_a)
@staticmethod
def transpose(mat):
"""Return the transpose of matrices.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
transpose : array-like, shape=[..., n, n]
Transposed matrix.
"""
is_vectorized = gs.ndim(gs.array(mat)) == 3
axes = (0, 2, 1) if is_vectorized else (1, 0)
return gs.transpose(mat, axes)
@staticmethod
def diagonal(mat):
"""Return the diagonal of a matrix as a vector.
Parameters
----------
mat : array-like, shape=[..., m, n]
Matrix.
Returns
-------
diagonal : array-like, shape=[..., min(m, n)]
Vector of diagonal coefficients.
"""
return gs.diagonal(mat, axis1=-2, axis2=-1)
@staticmethod
def is_square(mat):
"""Check if a matrix is square.
Parameters
----------
mat : array-like, shape=[..., m, n]
Matrix.
Returns
-------
is_square : array-like, shape=[...,]
Boolean evaluating if the matrix is square.
"""
n = mat.shape[-1]
m = mat.shape[-2]
return m == n
@classmethod
def is_diagonal(cls, mat, atol=gs.atol):
"""Check if a matrix is square and diagonal.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default: backend atol.
Returns
-------
is_diagonal : array-like, shape=[...,]
Boolean evaluating if the matrix is square and diagonal.
"""
is_square = cls.is_square(mat)
if not gs.all(is_square):
return False
diagonal_mat = from_vector_to_diagonal_matrix(cls.diagonal(mat))
is_diagonal = gs.all(gs.isclose(mat, diagonal_mat, atol=atol), axis=(-2, -1))
return is_diagonal
@classmethod
def is_lower_triangular(cls, mat, atol=gs.atol):
"""Check if a matrix is lower triangular.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default : backend atol.
Returns
-------
is_tril : array-like, shape=[...,]
Boolean evaluating if the matrix is lower triangular
"""
is_square = cls.is_square(mat)
if not is_square:
is_vectorized = gs.ndim(gs.array(mat)) == 3
return gs.array([False] * len(mat)) if is_vectorized else False
return cls.equal(mat, gs.tril(mat), atol)
@classmethod
def is_upper_triangular(cls, mat, atol=gs.atol):
"""Check if a matrix is upper triangular.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default : backend atol.
Returns
-------
is_triu : array-like, shape=[...,]
Boolean evaluating if the matrix is upper triangular.
"""
is_square = cls.is_square(mat)
if not is_square:
is_vectorized = gs.ndim(gs.array(mat)) == 3
return gs.array([False] * len(mat)) if is_vectorized else False
return cls.equal(mat, gs.triu(mat), atol)
@classmethod
def is_strictly_lower_triangular(cls, mat, atol=gs.atol):
"""Check if a matrix is strictly lower triangular.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default : backend atol.
Returns
-------
is_strictly_tril : array-like, shape=[...,]
Boolean evaluating if the matrix is strictly lower triangular
"""
is_square = cls.is_square(mat)
if not is_square:
is_vectorized = gs.ndim(gs.array(mat)) == 3
return gs.array([False] * len(mat)) if is_vectorized else False
return cls.equal(mat, gs.tril(mat, k=-1), atol)
@classmethod
def is_strictly_upper_triangular(cls, mat, atol=gs.atol):
"""Check if a matrix is strictly upper triangular.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default : backend atol.
Returns
------
is_strictly_triu : array-like, shape=[...,]
Boolean evaluating if the matrix is strictly upper triangular
"""
is_square = cls.is_square(mat)
if not is_square:
is_vectorized = gs.ndim(gs.array(mat)) == 3
return gs.array([False] * len(mat)) if is_vectorized else False
return cls.equal(mat, gs.triu(mat, k=1))
@classmethod
def is_symmetric(cls, mat, atol=gs.atol):
"""Check if a matrix is symmetric.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default: backend atol.
Returns
-------
is_sym : array-like, shape=[...,]
Boolean evaluating if the matrix is symmetric.
"""
is_square = cls.is_square(mat)
if not is_square:
is_vectorized = gs.ndim(gs.array(mat)) == 3
return gs.array([False] * len(mat)) if is_vectorized else False
return cls.equal(mat, cls.transpose(mat), atol)
@classmethod
def is_pd(cls, mat):
"""Check if a matrix is positive definite.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default: backend atol.
Returns
-------
is_pd : array-like, shape=[...,]
Boolean evaluating if the matrix is positive definite.
"""
if mat.ndim == 2:
return gs.array(gs.linalg.is_single_matrix_pd(mat))
return gs.array([gs.linalg.is_single_matrix_pd(m) for m in mat])
@classmethod
def is_spd(cls, mat, atol=gs.atol):
"""Check if a matrix is symmetric positive definite.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default: backend atol.
Returns
-------
is_spd : array-like, shape=[...,]
Boolean evaluating if the matrix is symmetric positive definite.
"""
is_spd = gs.logical_and(cls.is_symmetric(mat, atol), cls.is_pd(mat))
return is_spd
@classmethod
def is_skew_symmetric(cls, mat, atol=gs.atol):
"""Check if a matrix is skew symmetric.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
atol : float
Absolute tolerance.
Optional, default: backend atol.
Returns
-------
is_skew_sym : array-like, shape=[...,]
Boolean evaluating if the matrix is skew-symmetric.
"""
is_square = cls.is_square(mat)
if not is_square:
is_vectorized = gs.ndim(gs.array(mat)) == 3
return gs.array([False] * len(mat)) if is_vectorized else False
return cls.equal(mat, -cls.transpose(mat), atol)
@classmethod
def to_diagonal(cls, mat):
"""Make a matrix diagonal, by zeroing out non diagonal elements.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
diag : array-like, shape=[..., n, n]
"""
return cls.to_upper_triangular(cls.to_lower_triangular(mat))
@classmethod
def to_lower_triangular(cls, mat):
"""Make a matrix lower triangular, by zeroing out upper elements.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
tril : array-like, shape=[..., n, n]
Lower triangular matrix.
"""
return gs.tril(mat)
@classmethod
def to_upper_triangular(cls, mat):
"""Make a matrix upper triangular, by zeroing out lower elements.
Parameters
---------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
triu : array-like, shape=[..., n, n]
"""
return gs.triu(mat)
@classmethod
def to_strictly_lower_triangular(cls, mat):
"""Make a matrix strictly lower triangular, by zeroing out
upper+diag elements.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
tril : array-like, shape=[..., n, n]
Lower triangular matrix.
"""
return gs.tril(mat, k=-1)
@classmethod
def to_strictly_upper_triangular(cls, mat):
"""Make a matrix stritcly upper triangular, by zeroing out
lower+diag elements.
Parameters
---------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
triu : array-like, shape=[..., n, n]
"""
return gs.triu(mat, k=1)
@classmethod
def to_symmetric(cls, mat):
"""Make a matrix symmetric, by averaging with its transpose.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
sym : array-like, shape=[..., n, n]
Symmetric matrix.
"""
return 1 / 2 * (mat + cls.transpose(mat))
@classmethod
def to_skew_symmetric(cls, mat):
"""
Make a matrix skew-symmetric, by averaging with minus its transpose.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
skew_sym : array-like, shape=[..., n, n]
Skew-symmetric matrix.
"""
return 1 / 2 * (mat - cls.transpose(mat))
@classmethod
def to_lower_triangular_diagonal_scaled(cls, mat, K=2.0):
"""Make a matrix lower triangular, by zeroing out upper elements
and also diagonal is divided by factor K.
Parameters
----------
mat : array-like, shape=[..., n, n]
Matrix.
Returns
-------
tril : array-like, shape=[..., n, n]
Lower triangular matrix.
"""
slt = cls.to_strictly_lower_triangular(mat)
diag = cls.to_diagonal(mat) / K
return slt + diag
def random_point(self, n_samples=1, bound=1.0):
"""Sample from a uniform distribution in a cube.
Parameters
----------
n_samples : int
Number of samples.
Optional, default: 1.
bound : float
Bound of the interval in which to sample each entry.
Optional, default: 1.
Returns
-------
point : array-like, shape=[..., m, n]
Sample.
"""
m, n = self.m, self.n
size = (n_samples, m, n) if n_samples != 1 else (m, n)
point = bound * (gs.random.rand(*size) - 0.5)
return point
@classmethod
def congruent(cls, mat_1, mat_2):
"""Compute the congruent action of mat_2 on mat_1.
This is :math: `mat_2 mat_1 mat_2^T`.
Parameters
----------
mat_1 : array-like, shape=[..., n, n]
Matrix.
mat_2 : array-like, shape=[..., n, n]
Matrix.
Returns
-------
cong : array-like, shape=[..., n, n]
Result of the congruent action.
"""
return cls.mul(mat_2, mat_1, cls.transpose(mat_2))
@staticmethod
def frobenius_product(mat_1, mat_2):
"""Compute Frobenius inner-product of two matrices.
The `einsum` function is used to avoid computing a matrix product. It
is also faster than using a sum an element-wise product.
Parameters
----------
mat_1 : array-like, shape=[..., m, n]
Matrix.
mat_2 : array-like, shape=[..., m, n]
Matrix.
Returns
-------
product : array-like, shape=[...,]
Frobenius inner-product of mat_1 and mat_2
"""
return gs.einsum("...ij,...ij->...", mat_1, mat_2)
@staticmethod
def trace_product(mat_1, mat_2):
"""Compute trace of the product of two matrices.
The `einsum` function is used to avoid computing a matrix product. It
is also faster than using a sum an element-wise product.
Parameters
----------
mat_1 : array-like, shape=[..., m, n]
Matrix.
mat_2 : array-like, shape=[..., m, n]
Matrix.
Returns
-------
product : array-like, shape=[...,]
Frobenius inner-product of mat_1 and mat_2
"""
return gs.einsum("...ij,...ji->...", mat_1, mat_2)
def flatten(self, mat):
"""Return a flattened form of the matrix.
Flatten a matrix (compatible with vectorization on data axis 0).
The reverse operation is reshape. These operations are often called
matrix vectorization / matricization in mathematics
(https://en.wikipedia.org/wiki/Tensor_reshaping).
The names flatten / reshape were chosen to avoid confusion with
vectorization on data axis 0.
Parameters
----------
mat : array-like, shape=[..., m, n]
Matrix.
Returns
-------
vec : array-like, shape=[..., m * n]
Flatten copy of mat.
"""
is_data_vectorized = gs.ndim(gs.array(mat)) == 3
shape = (
(mat.shape[0], self.m * self.n)
if is_data_vectorized
else (self.m * self.n,)
)
return gs.reshape(mat, shape)
def reshape(self, vec):
"""Return a matricized form of the vector.
Matricize a vector (compatible with vectorization on data axis 0).
The reverse operation is matrices.flatten. These operations are often
called matrix vectorization / matricization in mathematics
(https://en.wikipedia.org/wiki/Tensor_reshaping).
The names flatten / reshape were chosen to avoid confusion with
vectorization on data axis 0.
Parameters
----------
vec : array-like, shape=[..., m * n]
Vector.
Returns
-------
mat : array-like, shape=[..., m, n]
Matricized copy of vec.
"""
is_data_vectorized_on_axis_0 = gs.ndim(gs.array(vec)) == 2
if is_data_vectorized_on_axis_0:
vector_size = vec.shape[1]
shape = (vec.shape[0], self.m, self.n)
else:
vector_size = vec.shape[0]
shape = (
self.m,
self.n,
)
if vector_size != self.m * self.n:
raise ValueError("Incompatible vector and matrix sizes")
return gs.reshape(vec, shape)
class MatricesMetric(EuclideanMetric):
"""Euclidean metric on matrices given by Frobenius inner-product.
Parameters
----------
m, n : int
Integers representing the shapes of the matrices: m x n.
"""
def __init__(self, m, n, **kwargs):
dimension = m * n
super(MatricesMetric, self).__init__(dim=dimension, default_point_type="matrix")
def inner_product(self, tangent_vec_a, tangent_vec_b, base_point=None):
"""Compute Frobenius inner-product of two tangent vectors.
Parameters
----------
tangent_vec_a : array-like, shape=[..., m, n]
Tangent vector.
tangent_vec_b : array-like, shape=[..., m, n]
Tangent vector.
base_point : array-like, shape=[..., m, n]
Base point.
Optional, default: None.
Returns
-------
inner_prod : array-like, shape=[...,]
Frobenius inner-product of tangent_vec_a and tangent_vec_b.
"""
return Matrices.frobenius_product(tangent_vec_a, tangent_vec_b)
def norm(self, vector, base_point=None):
"""Compute norm of a matrix.
Norm of a matrix associated to the Frobenius inner product.
Parameters
----------
vector : array-like, shape=[..., dim]
Vector.
base_point : array-like, shape=[..., dim]
Base point.
Optional, default: None.
Returns
-------
norm : array-like, shape=[...,]
Norm.
"""
return gs.linalg.norm(vector, axis=(-2, -1))