Grace/Cumulus/NumberTheory/FourSquareIdentity.g17

Open in Workbench Download source

Imports

Grace.Cumulus.Int · Grace.Cumulus.Nat

Declarations

Source

1-- Sums of four squares in ℕ and ℤ, Euler's four-square identity, and2-- closure of sums of four squares under products.3-- Part of Cumulus, the Grace17 mathematics library.4-- Copyright (c) 2026 Grant Passmore.5-- Released under the Apache License 2.0. See LICENSE in the repository root.6-- Authors: Grant Passmore78module Grace.Cumulus.NumberTheory.FourSquareIdentity9import Grace.Cumulus.Int10import Grace.Cumulus.Nat1112-- Sums of four squares, in ℕ and in ℤ1314predicate SumOfFourSquares (n : Nat) :=15  ∃ a, Nat(a) ∧ ∃ b, Nat(b) ∧ ∃ c, Nat(c) ∧ ∃ d, Nat(d) ∧16    n = a * a + b * b + c * c + d * d1718predicate IntegerSumOfFourSquares (z : Set) :=19  ∃ a ∈ ℤ, ∃ b ∈ ℤ, ∃ c ∈ ℤ, ∃ d ∈ ℤ,20    z = a * a + b * b + c * c + d * d2122theorem SumOfFourSquares.intro (a b c d : Nat)23  shows SumOfFourSquares(a * a + b * b + c * c + d * d)24proof25  have aNat : Nat(a) by typecheck26  have bNat : Nat(b) by typecheck27  have cNat : Nat(c) by typecheck28  have dNat : Nat(d) by typecheck29  exact ⟨a, aNat, b, bNat, c, cNat, d, dNat, rfl⟩3031theorem SumOfFourSquares.zero32  shows SumOfFourSquares(0)33proof34  have same : 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 = 0 by ring35  have sum := SumOfFourSquares.intro at 0, 0, 0, 036  rwa same at sum3738theorem SumOfFourSquares.one39  shows SumOfFourSquares(1)40proof41  have same : 1 * 1 + 0 * 0 + 0 * 0 + 0 * 0 = 1 by ring42  have sum := SumOfFourSquares.intro at 1, 0, 0, 043  rwa same at sum4445theorem SumOfFourSquares.two46  shows SumOfFourSquares(2)47proof48  have same : 1 * 1 + 1 * 1 + 0 * 0 + 0 * 0 = 2 by ring49  have sum := SumOfFourSquares.intro at 1, 1, 0, 050  rwa same at sum5152theorem IntegerSumOfFourSquares.intro (a b c d : Integer)53  shows IntegerSumOfFourSquares(a * a + b * b + c * c + d * d)54proof55  have aIn : a ∈ ℤ by settle with Integer56  have bIn : b ∈ ℤ by settle with Integer57  have cIn : c ∈ ℤ by settle with Integer58  have dIn : d ∈ ℤ by settle with Integer59  exact ⟨a, aIn, b, bIn, c, cIn, d, dIn, rfl⟩6061-- Passing between ℕ and ℤ6263theorem IntegerSumOfFourSquares.of_natural (n : Nat)64  requires sum : SumOfFourSquares(n)65  shows IntegerSumOfFourSquares(integerOfNat(n))66proof67  obtain ⟨a, aNat, b, bNat, c, cNat, d, dNat, shape⟩ := sum68  have lifted : integerOfNat(n) =69      integerOfNat(a) * integerOfNat(a) + integerOfNat(b) * integerOfNat(b) +70      integerOfNat(c) * integerOfNat(c) + integerOfNat(d) * integerOfNat(d)71    rw shape72    simp [integerOfNat.add, integerOfNat.mul]73  rw lifted74  exact IntegerSumOfFourSquares.intro at integerOfNat(a), integerOfNat(b),75    integerOfNat(c), integerOfNat(d)7677-- A square is the square of the absolute value, whichever the sign.78-- An integer sum of four squares that is a natural is a sum of the four79-- squares of the absolute values.80theorem SumOfFourSquares.of_integer (n : Nat)81  requires sum : IntegerSumOfFourSquares(integerOfNat(n))82  shows SumOfFourSquares(n)83proof84  obtain ⟨a, aIn, b, bIn, c, cIn, d, dIn, shape⟩ := sum85  have aSquare := Integer.square_natAbs at a86  have bSquare := Integer.square_natAbs at b87  have cSquare := Integer.square_natAbs at c88  have dSquare := Integer.square_natAbs at d89  rw [aSquare, bSquare, cSquare, dSquare] at shape90  rw [← integerOfNat.add, ← integerOfNat.add, ← integerOfNat.add] at shape91  have same := integerOfNat.injective_iff.forward with shape92  rw same93  exact SumOfFourSquares.intro at integerNatAbs(a), integerNatAbs(b),94    integerNatAbs(c), integerNatAbs(d)9596-- Euler's identity and closure under products9798theorem euler.four_square_identity (a b c d e f g k : Integer)99  shows (a * a + b * b + c * c + d * d) * (e * e + f * f + g * g + k * k) =100    (a * e + b * f + c * g + d * k) * (a * e + b * f + c * g + d * k) +101    (a * f - b * e + c * k - d * g) * (a * f - b * e + c * k - d * g) +102    (a * g - b * k - c * e + d * f) * (a * g - b * k - c * e + d * f) +103    (a * k + b * g - c * f - d * e) * (a * k + b * g - c * f - d * e)104proof105  ring106107theorem IntegerSumOfFourSquares.mul (x y : Integer)108  requires first : IntegerSumOfFourSquares(x)109  requires second : IntegerSumOfFourSquares(y)110  shows IntegerSumOfFourSquares(x * y)111proof112  obtain ⟨a, aIn, b, bIn, c, cIn, d, dIn, xShape⟩ := first113  obtain ⟨e, eIn, f, fIn, g, gIn, k, kIn, yShape⟩ := second114  rw [xShape, yShape, euler.four_square_identity]115  exact IntegerSumOfFourSquares.intro at a * e + b * f + c * g + d * k,116    a * f - b * e + c * k - d * g, a * g - b * k - c * e + d * f,117    a * k + b * g - c * f - d * e118119-- The product of two sums of four squares, through ℤ.120theorem SumOfFourSquares.mul (m n : Nat)121  requires first : SumOfFourSquares(m)122  requires second : SumOfFourSquares(n)123  shows SumOfFourSquares(m * n)124proof125  have liftM := IntegerSumOfFourSquares.of_natural with first126  have liftN := IntegerSumOfFourSquares.of_natural with second127  have product := IntegerSumOfFourSquares.mul with liftM, liftN128  rw integerMulValue.ofNat at product129  exact SumOfFourSquares.of_integer with product130