SNAPKITTYWEST's picture
push from SNAPKITTYWEST/hyperkitty-constraint-dsl
224e773 verified
Raw History Blame Contribute Delete
3.12 kB
14
structure MyComplex where
body · 3 lines
  re : Int
  im : Int
deriving DecidableEq, Repr
20
def I : MyComplex := ⟨0, 1⟩
23
abbrev Vec3 = Fin 3 → Int
26
def dot (v w : Vec3) : Int :=
body · 1 line
  (v 0 * w 0 + v 1 * w 1 + v 2 * w 2)
30
abbrev Matrix3 = Fin 3 → Fin 3 → Int
33
def matVec (A : Matrix3) (x : Vec3) : Vec3 :=
body · 1 line
  fun i => (A i 0 * x 0 + A i 1 * x 1 + A i 2 * x 2)
37
def quadForm (Q : Matrix3) (x : Vec3) : Int :=
body · 1 line
  dot x (matVec Q x)
41
def I3 : Matrix3 :=
body · 1 line
  fun i j => if i = j then 1 else 0
45
def transpose (M : Matrix3) : Matrix3 :=
body · 1 line
  fun i j => M j i
49
def psd (M : Matrix3) : Prop :=
body · 1 line
  ∀ v : Vec3, 0 ≤ dot v (matVec M v)
53
def nsqd (M : Matrix3) : Prop :=
body · 1 line
  ∀ v : Vec3, 0 ≤ dot v (matVec (transpose M) v)
60
structure QLG where
body · 8 lines
  Q : Matrix3
  b : Vec3
  c : Int
  K : Int
  Qplus Qminus : Matrix3
  Qp_s : psd Qplus
  Qm_s : nsqd Qminus
deriving DecidableEq
71
def isBalanced (L : QLG) (x : Vec3) : Prop :=
body · 3 lines
  (quadForm L.Q x + dot L.b x + L.c = 0) ∧
  (quadForm L.Qplus x = quadForm L.Qminus x) ∧
  (quadForm L.Qplus x = L.K)
80
def exampleQLG : QLG :=
body · 15 lines
  { Q := fun i j => if i = j then 1 else 0 -- Q = I₃
    b := fun _ => 0
    c := -1
    K := 1
    Qplus := fun i j => if i = j then 1 else 0 -- Q⁺ = I₃
    Qminus := fun i j => 0 -- Q⁻ = 0
    Qp_s := by
      intro v
      simp [psd, quadForm, dot, matVec, I3]
      nlinarith [sq_nonneg (v 0), sq_nonneg (v 1), sq_nonneg (v 2)]
    Qm_s := by
      intro v
      simp [nsqd, quadForm, dot, matVec, I3, transpose]
      nlinarith
  }
101
theorem exampleQLG_has_solution :
    ∃ (x : Vec3), isBalanced exampleQLG x ∧ x ≠ (fun _ => 0) := by
proof · 9 lines
  use ![1, 0, 0]
  constructor
  · -- Prove isBalanced
    simp [exampleQLG, isBalanced, quadForm, dot, matVec, I3]
    norm_num
  · -- Prove x ≠ 0 vector
    intro h
    have h₁ := congr_fun h 0
    norm_num at h₁