Skip to content
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

Open
wants to merge 88 commits into
base: master
Choose a base branch
from

Conversation

MohanadAhmed
Copy link
Collaborator

@MohanadAhmed MohanadAhmed commented Jul 21, 2023

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$$

  • The three matrices are defined as
    • $U$: an M × M matrix containing the left eigenvectors of the matrix
    • $S$: 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
      Note that
  • $S$ is a block matrix $S = [S₁₁, S₁₂; S₂₁, S₂₂]$ with
    • $S₁₁$: a diagonal r × r matrix and
    • $S₁₂$ : r × (N - r) zero matrix, S₂₁ : (M - r) × r zero matrix and
    • $S₂₂$: (M - r) × (N - r) zero matrix
  • $U$ is a block column matrix U = [U₁ U₂] with
    • $U₁$ : a M × r containing left eigenvectors with nonzero singular values.
    • $U₂$ : a M × (M - r) containing left eigenvectors with zero singular values.
  • $V$ is a block column matrix V = [V₁ V₂] with
    • $V₁$ : a N × r containing right eigenvectors with nonzero singular values.
    • $V₂$ : a M × (M - r) containing right eigenvectors with zero singular values.

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 from ColumnRowPartitioned.

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 and svdσ_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


Open in Gitpod

Copy link
Member

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!

Copy link
Collaborator Author

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)

Copy link
Member

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!

@MohanadAhmed MohanadAhmed added awaiting-review The author would like community review of the PR awaiting-CI and removed awaiting-author A reviewer has asked the author a question or requested changes labels Sep 20, 2023
Comment on lines +16 to +21
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
Copy link
Contributor

@sgouezel sgouezel Sep 21, 2023

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).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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.

  2. 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
  1. 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 𝕂]
Copy link
Contributor

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?

Copy link
Member

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.

Copy link
Collaborator Author

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.

Copy link
Member

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)) 𝕂 :=
Copy link
Contributor

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.

kodyvajjha pushed a commit that referenced this pull request Sep 22, 2023
…ces (#7249)

Lemmas `toBlocks₁₁_diagonal`, `toBlocks₂₂_diagonal`, `toBlocks₁₂_diagonal`, `toBlocks₂₁_diagonal` to make dealing with sub blocks of diagonal matrices a bit easier

Useful for PR #6042
kodyvajjha pushed a commit that referenced this pull request Sep 22, 2023
…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
bors bot pushed a commit that referenced this pull request Sep 26, 2023
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
@Vierkantor
Copy link
Contributor

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 Module.End), and it seems better to first prove SVD for homomorphisms, and then derive the matrix result from that. Basically, the same approach as we already have for eigenvalues in mathlib.

@MohanadAhmed
Copy link
Collaborator Author

MohanadAhmed commented Sep 26, 2023

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 Module.End), and it seems better to first prove SVD for homomorphisms, and then derive the matrix result from that. Basically, the same approach as we already have for eigenvalues in mathlib.

@Vierkantor
Can you spell out what this means for this PR?

@Vierkantor Vierkantor removed their assignment Feb 13, 2024
@j-loreaux j-loreaux self-assigned this Feb 13, 2024
@j-loreaux
Copy link
Collaborator

j-loreaux commented Feb 13, 2024

@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.

@j-loreaux j-loreaux added WIP Work in progress and removed awaiting-review The author would like community review of the PR labels Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t-algebra Algebra (groups, rings, fields etc) WIP Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants