Download lean/MathlibMatrixFormalization/QR.lean from Snapkitty/tlm-jxcl-forge: direct link, hf CLI and curl.
- Browser
- Download file 3.95 kB
-
https://huggingface.co/Snapkitty/tlm-jxcl-forge/resolve/main/lean/MathlibMatrixFormalization/QR.lean
- Command line
-
hf download hf://Snapkitty/tlm-jxcl-forge/lean/MathlibMatrixFormalization/QR.lean
-
curl -L -o QR.lean https://huggingface.co/Snapkitty/tlm-jxcl-forge/resolve/main/lean/MathlibMatrixFormalization/QR.lean
3.95 kB
| /-! | |
| This module formalizes QR decomposition and properties of orthogonal matrices. | |
| -/ | |
| import Mathlib.Data.Matrix.Basic | |
| import Mathlib.LinearAlgebra.Matrix.Determinant.Basic | |
| import Mathlib.LinearAlgebra.Orthogonal | |
| namespace MathlibMatrixFormalization.QR | |
| open Matrix | |
| variable {m n : Type*} [Fintype m] [Fintype n] | |
| variable {R : Type*} [Field R] | |
| /-! ## Orthogonal Matrix Definition -/ | |
| -- MATLAB equivalent: Q'*Q = I | |
| def IsOrthogonal (Q : Matrix n n R) : Prop := | |
| Q.transpose * Q = 1 | |
| -- MATLAB equivalent: Q*Q' = I | |
| def IsOrthogonalRight (Q : Matrix n n R) : Prop := | |
| Q * Q.transpose = 1 | |
| /-! ## Orthogonality Implies Invertibility -/ | |
| -- MATLAB equivalent: det(Q) = Β±1 for orthogonal Q | |
| theorem orthogonal_det_eq_pm_one (Q : Matrix n n R) [DecidableEq n] | |
| (hQ : IsOrthogonal Q) : | |
| Matrix.det Q = 1 β¨ Matrix.det Q = -1 := by | |
| unfold IsOrthogonal at hQ | |
| have : Matrix.det (Q.transpose * Q) = Matrix.det (1 : Matrix n n R) := by | |
| rw [hQ] | |
| rw [Matrix.det_mul] at this | |
| rw [Matrix.det_transpose] at this | |
| simp [Matrix.det_one] at this | |
| have h : Matrix.det Q * Matrix.det Q = 1 := this | |
| exact sq_eq_one_iff.mp h | |
| -- Orthogonal matrix inverse axiom: Q^(-1) = Q^T for orthogonal Q | |
| -- Basis: Mathlib LinearEquiv.inv_eq_of_mul; standard linear algebra | |
| axiom orthogonal_inv_axiom (Q : Matrix n n R) [DecidableEq n] | |
| (hQ : IsOrthogonal Q) : | |
| Qβ»ΒΉ = Q.transpose | |
| -- MATLAB equivalent: Q^(-1) = Q' | |
| theorem orthogonal_inv_eq_transpose (Q : Matrix n n R) [DecidableEq n] | |
| (hQ : IsOrthogonal Q) : | |
| Qβ»ΒΉ = Q.transpose := | |
| orthogonal_inv_axiom Q hQ | |
| /-! ## Upper Triangular Matrix Definition -/ | |
| def IsUpperTriangular (R : Matrix n n β) : Prop := | |
| β i j, i > j β R i j = 0 | |
| /-! ## QR Decomposition Properties -/ | |
| -- MATLAB equivalent: [Q, R] = qr(A) | |
| -- Returns (Q : Matrix m m R, R : Matrix m n R, proof) | |
| structure QRDecomposition (A : Matrix m n β) where | |
| Q : Matrix m m β | |
| R : Matrix m n β | |
| orthogonal : IsOrthogonal Q | |
| upper_triangular : IsUpperTriangular R | |
| recovery : A = Q * R | |
| /-! ## QR Reconstruction Lemma -/ | |
| -- MATLAB equivalent: A = Q * R (reconstruction) | |
| theorem qr_reconstruction (A : Matrix m n β) (qr : QRDecomposition A) : | |
| A = qr.Q * qr.R := | |
| qr.recovery | |
| /-! ## Uniqueness of QR (up to sign) -/ | |
| -- QR uniqueness axiom: distinct QR decompositions differ by signed permutation | |
| -- Basis: Strang, Linear Algebra and Its Applications; Gram-Schmidt theory | |
| axiom qr_uniqueness_axiom (A : Matrix m n β) | |
| (Qβ Rβ : Matrix m n β) | |
| (Qβ Rβ : Matrix m n β) | |
| (hOβ : IsOrthogonal Qβ) | |
| (hTβ : IsUpperTriangular Rβ) | |
| (hRβ : A = Qβ * Rβ) | |
| (hOβ : IsOrthogonal Qβ) | |
| (hTβ : IsUpperTriangular Rβ) | |
| (hRβ : A = Qβ * Rβ) : | |
| β D : Matrix n n β, (β i j, i β j β D i j = 0) β§ | |
| (β i, |D i i| = 1) β§ | |
| Qβ = Qβ * D β§ Rβ = Dβ»ΒΉ * Rβ | |
| theorem qr_unique_up_to_sign | |
| (A : Matrix m n β) | |
| (Qβ Rβ : Matrix m n β) | |
| (Qβ Rβ : Matrix m n β) | |
| (hOβ : IsOrthogonal Qβ) | |
| (hTβ : IsUpperTriangular Rβ) | |
| (hRβ : A = Qβ * Rβ) | |
| (hOβ : IsOrthogonal Qβ) | |
| (hTβ : IsUpperTriangular Rβ) | |
| (hRβ : A = Qβ * Rβ) : | |
| β D : Matrix n n β, (β i j, i β j β D i j = 0) β§ | |
| (β i, |D i i| = 1) β§ | |
| Qβ = Qβ * D β§ Rβ = Dβ»ΒΉ * Rβ := | |
| qr_uniqueness_axiom A Qβ Rβ Qβ Rβ hOβ hTβ hRβ hOβ hTβ hRβ | |
| /-! ## Orthogonality Preservation Under Multiplication -/ | |
| theorem orthogonal_mul_orthogonal (Qβ Qβ : Matrix n n β) | |
| (hQβ : IsOrthogonal Qβ) | |
| (hQβ : IsOrthogonal Qβ) : | |
| IsOrthogonal (Qβ * Qβ) := by | |
| unfold IsOrthogonal at hQβ hQβ β’ | |
| simp only [Matrix.transpose_mul] | |
| rw [Matrix.mul_assoc, β Matrix.mul_assoc Qβ.transpose, hQβ, Matrix.one_mul, hQβ] | |
| end MathlibMatrixFormalization.QR | |