Skip to content

Commit 5808c56

Browse files
committed
Implement singular_values
1 parent 92fdb6b commit 5808c56

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/sage/matrix/matrix2.pyx

+31
Original file line numberDiff line numberDiff line change
@@ -6992,6 +6992,37 @@ cdef class Matrix(Matrix1):
69926992
extend=extend, algorithm=algorithm,
69936993
suppress_future_warning=False))
69946994

6995+
def singular_values(self) -> Sequence:
6996+
"""
6997+
Return a sequence of singular values of a matrix.
6998+
6999+
EXAMPLES::
7000+
7001+
sage: A = matrix([[1, 2], [3, 4]])
7002+
sage: A.singular_values()
7003+
[0.3659661906262578?, 5.464985704219043?]
7004+
7005+
TESTS::
7006+
7007+
sage: type(A.singular_values())
7008+
<class 'sage.structure.sequence.Sequence_generic'>
7009+
sage: set_random_seed(100)
7010+
sage: A = matrix.random(CC, 5)
7011+
sage: Sequence((A*A.H).eigenvalues(), universe=RR)
7012+
Traceback (most recent call last):
7013+
...
7014+
TypeError: unable to convert ... to an element of Real Field with 53 bits of precision
7015+
sage: A.singular_values() # abs tol 1e-13
7016+
[0.317896596475411, 1.25232496300299, 1.48403213017074, 2.08062167993720, 2.59091978815526]
7017+
"""
7018+
from sage.rings.abc import ComplexField
7019+
e: Sequence = (self*self.H).eigenvalues() # guaranteed to be real
7020+
R = self.base_ring()
7021+
if isinstance(R, ComplexField):
7022+
# because of floating point error e may not be all real
7023+
e = Sequence([x.real() for x in e], universe=R._real_field())
7024+
return Sequence([x.sqrt() for x in e])
7025+
69957026
def _eigenvectors_result_to_eigenvalues(self, eigenvectors: list) -> Sequence:
69967027
"""
69977028
Convert the result of :meth:`eigenvectors_left` to a sequence of eigenvalues

0 commit comments

Comments
 (0)