import Mathlib.Data.Real.Basic import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.Tactic /-! A refinement of the determinant-circle infrastructure from [rc7qs6]. Main theorem: `circlePoly_eq_quadratic_linear` with **no sorry**. -/ namespace ApTangentDet2 noncomputable section open Matrix abbrev Pt : Type := ℝ × ℝ def sx (p : Pt) : ℝ := p.1^2 + p.2^2 /-- The $4\times 4$ matrix used to define the circle through three points. -/ def circleMat (p1 p2 p3 : Pt) (x y : ℝ) : Matrix (Fin 4) (Fin 4) ℝ := fun i j => match (i.1, j.1) with | (0,0) => x^2 + y^2 | (0,1) => x | (0,2) => y | (0,3) => 1 | (1,0) => sx p1 | (1,1) => p1.1 | (1,2) => p1.2 | (1,3) => 1 | (2,0) => sx p2 | (2,1) => p2.1 | (2,2) => p2.2 | (2,3) => 1 | (3,0) => sx p3 | (3,1) => p3.1 | (3,2) => p3.2 | (3,3) => 1 | _ => 0 /-- Determinant polynomial defining the (general) circle through three points. -/ def circlePoly (p1 p2 p3 : Pt) (x y : ℝ) : ℝ := (circleMat p1 p2 p3 x y).det /-- Standard basis row-vector. -/ def e (k : Fin 4) : (Fin 4 → ℝ) := Pi.single k 1 /-- The row vector appearing in the first row of `circleMat`. -/ def uvec (x y : ℝ) : (Fin 4 → ℝ) := fun j => match j.1 with | 0 => x^2 + y^2 | 1 => x | 2 => y | 3 => (1 : ℝ) | _ => 0 lemma uvec_eq (x y : ℝ) : uvec x y = (x^2 + y^2) • e 0 + x • e 1 + y • e 2 + (1:ℝ) • e 3 := by funext j fin_cases j <;> simp [uvec, e, Pi.single] /-- Rewrite `circleMat` as `updateRow` applied to a matrix not depending on `x,y`. -/ lemma circleMat_eq_updateRow (p1 p2 p3 : Pt) (x y : ℝ) : circleMat p1 p2 p3 x y = Matrix.updateRow (circleMat p1 p2 p3 0 0) 0 (uvec x y) := by funext i j fin_cases i <;> fin_cases j <;> simp [circleMat, uvec, Matrix.updateRow] /-- `circlePoly` is linear in the first-row vector, hence can be written with four coefficients corresponding to $x^2+y^2$, $x$, $y$, and the constant term. -/ theorem circlePoly_eq_quadratic_linear (p1 p2 p3 : Pt) : ∃ A B C D : ℝ, ∀ x y, circlePoly p1 p2 p3 x y = A*(x^2+y^2) + B*x + C*y + D := by classical let M : Matrix (Fin 4) (Fin 4) ℝ := circleMat p1 p2 p3 0 0 let A : ℝ := (Matrix.updateRow M 0 (e 0)).det let B : ℝ := (Matrix.updateRow M 0 (e 1)).det let C : ℝ := (Matrix.updateRow M 0 (e 2)).det let D : ℝ := (Matrix.updateRow M 0 (e 3)).det refine ⟨A, B, C, D, ?_⟩ intro x y -- rewrite circlePoly using updateRow have hdet : circlePoly p1 p2 p3 x y = (Matrix.updateRow M 0 (uvec x y)).det := by -- avoid `simp` loops: unfold and rewrite directly unfold circlePoly -- rewrite the matrix only (no simp lemma registration) rw [circleMat_eq_updateRow (p1:=p1) (p2:=p2) (p3:=p3) (x:=x) (y:=y)] -- decompose the updated row have hu : uvec x y = (x^2 + y^2) • e 0 + x • e 1 + y • e 2 + (1:ℝ) • e 3 := uvec_eq x y -- abbreviations let u0 : Fin 4 → ℝ := (x^2 + y^2) • e 0 let u1 : Fin 4 → ℝ := x • e 1 let u2 : Fin 4 → ℝ := y • e 2 let u3 : Fin 4 → ℝ := (1:ℝ) • e 3 have hu' : uvec x y = u0 + u1 + u2 + u3 := by simp [u0, u1, u2, u3, hu, add_assoc] -- expand determinant using linearity in the updated row rw [hdet, hu'] -- split off u3 have h3 : (Matrix.updateRow M 0 (u0 + u1 + u2 + u3)).det = (Matrix.updateRow M 0 (u0 + u1 + u2)).det + (Matrix.updateRow M 0 u3).det := by simpa [add_assoc] using (Matrix.det_updateRow_add M 0 (u0 + u1 + u2) u3) rw [h3] -- split u2 have h2 : (Matrix.updateRow M 0 (u0 + u1 + u2)).det = (Matrix.updateRow M 0 (u0 + u1)).det + (Matrix.updateRow M 0 u2).det := by simpa [add_assoc] using (Matrix.det_updateRow_add M 0 (u0 + u1) u2) rw [h2] -- split u1 have h1 : (Matrix.updateRow M 0 (u0 + u1)).det = (Matrix.updateRow M 0 u0).det + (Matrix.updateRow M 0 u1).det := by simpa using (Matrix.det_updateRow_add M 0 u0 u1) rw [h1] -- scale out coefficients have hs0 : (Matrix.updateRow M 0 u0).det = (x^2 + y^2) * A := by simp [u0, A, Matrix.det_updateRow_smul] have hs1 : (Matrix.updateRow M 0 u1).det = x * B := by simp [u1, B, Matrix.det_updateRow_smul] have hs2 : (Matrix.updateRow M 0 u2).det = y * C := by simp [u2, C, Matrix.det_updateRow_smul] have hs3 : (Matrix.updateRow M 0 u3).det = D := by -- coefficient is 1 simpa [u3, D, Matrix.det_updateRow_smul] -- finish -- combine all terms simp [hs0, hs1, hs2, hs3, mul_add, add_mul, add_assoc, add_comm, add_left_comm, A, B, C, D] ring end end ApTangentDet2