SNAPKITTYWEST's picture
October 2026 main drop: mirror from GitHub (part 2)
ae44e8c verified
Raw History Blame Contribute Delete
3.95 kB
/-!
# QR Decomposition and Orthogonal Matrix Properties
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