Lean / Reader
Lean reader

RookDeterminant.lean

56 lines8 declarationsno sorryimports Std
  1. import Std
  2. /- Integer coefficient verification of det(I - x(A+uD)).
  3. The two powers in a monomial are the exponents of x and u. -/
  4. namespace RookCertificate
  5. structure Term where
  6. xDegree : Nat
  7. uDegree : Nat
  8. weight : Int
  9. deriving Repr
  10. def occupancyEntry (row col : Nat) : Nat :=
  11. ((List.range 4).filter fun j =>
  12. !(row.testBit j) && ((row + 2^j)/2 == col)).length
  13. def entry (row col : Nat) : List Term :=
  14. let hold : List Term := if row == col then [⟨0, 0, 1⟩] else []
  15. let count := occupancyEntry row col
  16. let fill : List Term := if count == 0 then [] else [⟨1, 0, -(Int.ofNat count)⟩]
  17. let empty : List Term := if row/2 == col then [⟨1, 1, -1⟩] else []
  18. hold ++ fill ++ empty
  19. def times (a b : Term) (sgn : Int) : Term :=
  20. ⟨a.xDegree + b.xDegree, a.uDegree + b.uDegree, sgn * a.weight * b.weight⟩
  21. /-- Laplace expansion through progressively smaller column lists.
  22. The first available remaining column has positive cofactor sign. -/
  23. def expansion : Nat → Nat → List Nat → List Term
  24. | 0, _, _ => [⟨0, 0, 1⟩]
  25. | fuel+1, row, cols =>
  26. (cols.zipIdx).flatMap fun (col, idx) =>
  27. let factors := entry row col
  28. if factors.isEmpty then [] else
  29. let rest := expansion fuel (row+1) (cols.erase col)
  30. let sgn : Int := if idx % 2 == 0 then 1 else -1
  31. factors.flatMap fun a => rest.map fun b => times a b sgn
  32. def coefficients (ts : List Term) : List (List Int) :=
  33. (List.range 9).map fun i => (List.range 9).map fun j =>
  34. ts.foldl (fun acc t => if t.xDegree == i && t.uDegree == j then acc+t.weight else acc) 0
  35. def expected : List (List Int) :=
  36. [[1,0,0,0,0,0,0,0,0], [-4,-1,0,0,0,0,0,0,0], [4,0,0,0,0,0,0,0,0],
  37. [0,1,0,0,0,0,0,0,0], [2,4,1,0,0,0,0,0,0], [-4,-3,0,0,0,0,0,0,0],
  38. [0,0,0,0,0,0,0,0,0], [0,-1,0,0,0,0,0,0,0], [1,0,0,0,0,0,0,0,0]]
  39. set_option maxRecDepth 20000
  40. set_option maxHeartbeats 0
  41. theorem signed_expansion : coefficients (expansion 8 0 (List.range 8)) = expected := by
  42. decide +kernel
  43. #print axioms signed_expansion
  44. end RookCertificate