New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(LinearAlgebra/Matrix/SVD): Singular Value Decomposition of R or C Matrices #6042
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these results should go in the existing matrix rank file; and they might already exist there!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I presume you mean this one. according to my looking they are not there (I did this search earlier when the proof was in lean 3 and again when I moved my proof to lean 4)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I mean is that you should add your results below the existing results in that file!
Any matrix A (M × N) with rank r = A.rank and with elements in the field ℝ or ℂ can be decomposed | ||
into the product of three matrices `A = UξVᴴ` where: | ||
|
||
* U: an `M × M` matrix containing the left eigenvectors of the matrix | ||
* ξ: an `M × N` matrix with an `r × r` block in the upper left corner with nonzero singular values | ||
* V: an `N × N` matrix containing the right eigenvectors of the matrix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am completely confused by this docstring. To me, singular values of a matrix A
have to do with the successive maximum expansion rates of the matrix, so I don't see how left or right eigenvectors of the matrix A
would appear in a singular value decomposition.
Also, you should probably recall the definition of the singular values, as there are various definitions (notably do you have r
singular values, or min M N
of them, or infinitely many of them but all zero after step r
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
The singular values for me are square roots of eigenvalues of AHA and AAH. They are also what remains of the matrix transformation if we rotate the source and target vector spaces until the transformation is diagonal.
-
The Singular Value Decomposition is a generalization of the Eigendecompositon. We sometimes abuse terminology and call the left singular vectors left eigenvectors and similarly for the right singular vectors.
If we write left singular vector in place of left eigenvectors and right singular vector in place of right eigenvectors, does that work for you?
* U: an `M × M` matrix containing the left singular vector of the matrix
* ξ: an `M × N` matrix with an `r × r` block in the upper left corner with nonzero singular values
* V: an `N × N` matrix containing the right singular vectorof the matrix
- I did not define a function for singular values. I only defined the matrix of singular values which has the same dimensions as the original matrix A. It is also partitioned such that it is upper left block of size
$r \times r$ contains the$r$ non-zero singular values.
I have no Idea what the maximum expansion rate of a matrix is.
Singular Value decomposition, SVD | ||
-/ | ||
|
||
variable {𝕂 : Type*} [IsROrC 𝕂] [DecidableEq 𝕂] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you put a [DecidableEq 𝕂]
assumption?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably IsROrC
should extend DecidableEq
, since we already have DecidableEq Real
and DecidableEq Complex
instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throughout the PR you will find comparisons against zero for example:
lemma rank_eq_card_pos_eigs_conjTranspose_mul_self (A: Matrix (Fin M) (Fin N) 𝕂) :
A.rank = Fintype.card {i // (isHermitian_transpose_mul_self A).eigenvalues i ≠ 0} := by
rw [← rank_conjTranspose_mul_self, IsHermitian.rank_eq_card_non_zero_eigs]
My understanding is this requires decidable equality to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#7315 looks to be a good solution here, as it seems we had this pattern elsewhere too.
namespace Matrix | ||
|
||
/-- The right eigenvectors of a matrix A corresponding to its non-zero eigenvalues -/ | ||
noncomputable def svdV₁ (A : Matrix (Fin M) (Fin N) 𝕂) : Matrix (Fin N) (Fin (A.rank)) 𝕂 := |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the names in mathlib have to convey a meaning. In general, they are longer and more expressive than the names we would use in papers (which are just notations), because they have to be compatible with the whole library.
…7266) Given two matrices $A$ and $B$ whose product is 1 $AB = 1$, then left multiplication/right multiplication with these matrices from the appropriate side is an injection. Suggested by @Vierkantor in PR #6042
The PR provides two lemmas showing that the eigenvalues of $A\^HA$ and $AA^H$ are non-negative: - `eigenvalues_conjTranspose_mul_self_nonneg`: $$\text{eig}(A\^H A) \geq 0 $$ - `eigenvalues_self_mul_conjTranspose_nonneg`: $$\text{eig}(A A\^H) \geq 0 $$ This was suggested by @Vierkantor in PR #6042
Sorry for the weeks of barely any contact. I finally have some real time to invest in reviewing. I hope I addressed all the open questions you still had. We've been discussing this PR with the mathlib reviewer team, and I'm afraid that we concluded that this result still needs quite some work. Namely, we find it important to get the decomposition also for homomorphisms (specifically, elements of |
@Vierkantor |
@MohanadAhmed if you're interested in reviving this PR, let's discuss the plan on the Zulip thread. In the meantime I'm marking this as WIP since we'll plan to refactor a bit. |
This file provides proves the SVD theorem which decomposes a real/complex matrix into left eigenvectors, singular values block diagonal matrix and right eigenvectors.
Any matrix A (M × N) with rank r = A.rank and with elements in ℝ or ℂ fields can be decompsed into the product of three matrices:
$$A = USV$$
Note that
Since in mathlib the eigenvalues of hermitian matrices are defined in an "arbitrary" undetermined order, we begin by partition the singular values into zero and non-zero values. We partition the corresponding eigenvectors from AᴴA and AAᴴ using similar rearrangements. These are included in
SVD.svdReindex
. The basic API for Column and Row partitioned matrices is fromColumnRowPartitioned
.We then proceed to transfer some of the lemmas we need about eigenvector matrices (for example that they are unitary: i.e. inverse is conjugate transpose.). Note that since invertibility in mathlib is defined for square matrices while our matrices are partitioned i.e. N × (N₁ ⊕ N₂) and N ≃ (N ⊕ N₂), Lean cannot apply the Invertible definition. We workaround this were necessary.
Lemma
reduced_spectral_theorem
(reduced_spectral_theorem'
) shows that AᴴA and AAᴴ, can be reduced to products containing only the non-zero singular eigenvectors. This is later used in proving the main SVD theroem. A few lemmas are provided about the invertibility of the non-zero singular values matrix:svdσ_inv
,σ_inv_μ_σ_inv_eq_one
,IsUnit_det_svdσ
,IsUnit_det_svdσ_mapK
andsvdσ_inv_mapK
.To make relating left eigenvectors to right eigenvectors easier we define U₁ = AV₁σ⁻¹ while U₂ is obtained from the eigenvectors of (AAᴴ). This avoid a lengthy reindexing operation with many proofs. The vectors in U₂ have no such issue since they are multiplied by zero singular values anyway.
Co-authored-by: Mohanad Ahmed m.a.m.elhassan@gmail.com