1-- Blocks of coordinates, their sums, the product of simplices cut out of the2-- cube by asking every block sum to be one, and the two continuous3-- corrections, max(0, 1 − s) and max(1, s), applied to a block sum s4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.FixedPoint.BlockSimplex10import BlockSums1112-- The coordinates that the block map blk sends to p.13def blockSet (n blk p : Set) : Set:= { i ∈ n | blk(i) = p }1415theorem blockSet.own (n blk j : Set)16requires hj : j ∈ n17shows j ∈ blockSet(n, blk, blk(j))18proof19apply blockSet.membership.backward20exact⟨hj, rfl⟩2122-- The sum of the coordinates in block p, as a cube function.23def blockSum (n blk p : Set) : Set:= sumCube(n, blockSet(n, blk, p))2425theorem blockSum.definition (n blk p : Set)26shows sumCube(n, blockSet(n, blk, p)) = blockSum(n, blk, p)27proof28unfold blockSum29rfl3031theorem blockSum.continuous (n : Nat) (blk p : Set)32shows CubeFunctionContinuous(blockSum(n, blk, p), n)33proof34have same := blockSum.definition at n, blk, p35have continuous := sumCube.continuous at n, blockSet(n, blk, p)36 rwa same at continuous3738theorem blockSum.function (n : Nat) (blk p : Set)39shows CubeFunction(blockSum(n, blk, p), n)40proof41exact CubeFunctionContinuous.function with (blockSum.continuous at n, blk, p)4243theorem blockSum.real (n : Nat) (blk p x : Set)44requires hx : x ∈ unitCube(n)45shows blockSum(n, blk, p)(x) ∈ ℝ46proof47exact CubeFunction.value with (blockSum.function at n, blk, p), hx4849theorem blockSum.nonnegative (n : Nat) (blk p x : Set)50requires hx : x ∈ unitCube(n)51shows (0:R) ≤ (blockSum(n, blk, p)(x):R)52proof53have same := blockSum.definition at n, blk, p54have bound := sumCube.nonnegative at n, blockSet(n, blk, p), x with hx55 rwa same at bound5657theorem blockSum.ge_coordinate (n : Nat) (blk x j : Set)58requires hx : x ∈ unitCube(n)59requires hj : j ∈ n60shows (x(j):R) ≤ (blockSum(n, blk, blk(j))(x):R)61proof62have same := blockSum.definition at n, blk, blk(j)63have inBlock := blockSet.own at n, blk, j with hj64have bound := sumCube.ge_coordinate with hx, hj, inBlock65 rwa same at bound6667-- The product of simplices: the points of the cube whose block sums are one.68def simplexProduct (n m blk : Set) : Set:=69 { x ∈ unitCube(n) |∀ p ∈ m, blockSum(n, blk, p)(x) = (1:R) }7071theorem simplexProduct.cube (n m blk x : Set)72requires hx : x ∈ simplexProduct(n, m, blk)73shows x ∈ unitCube(n)74proof75have parts := simplexProduct.membership.forward with hx76exact parts.left7778theorem simplexProduct.block_sum (n m blk x p : Set)79requires hx : x ∈ simplexProduct(n, m, blk)80requires hp : p ∈ m81shows blockSum(n, blk, p)(x) = (1:R)82proof83have parts := simplexProduct.membership.forward with hx84exact parts.right with hp8586-- The shortfall of a block sum below one, and the block sum capped below by one.87def excessCube (n blk p : Set) : Set:=88 maxCube(n, constantFunction(unitCube(n), ℝ, (0:R)), addCube(n, constantFunction(unitCube(n), ℝ, (1:R)), negCube(n, blockSum(n, blk, p))))8990def scaleCube (n blk p : Set) : Set:=91 maxCube(n, constantFunction(unitCube(n), ℝ, (1:R)), blockSum(n, blk, p))9293theorem excessCube.definition (n blk p : Set)94shows excessCube(n, blk, p) = maxCube(n, constantFunction(unitCube(n), ℝ, (0:R)), addCube(n, constantFunction(unitCube(n), ℝ, (1:R)), negCube(n, blockSum(n, blk, p))))95proof96unfold excessCube97rfl9899theorem scaleCube.definition (n blk p : Set)100shows scaleCube(n, blk, p) = maxCube(n, constantFunction(unitCube(n), ℝ, (1:R)), blockSum(n, blk, p))101proof102unfold scaleCube103rfl104105theorem excessCube.continuous (n : Nat) (blk p : Set)106shows CubeFunctionContinuous(excessCube(n, blk, p), n)107proof108have zeroIn : (0:R) ∈ ℝ bytypecheck109have oneIn : (1:R) ∈ ℝ bytypecheck110have zero := constantCube.continuous with zeroIn111have one := constantCube.continuous with oneIn112have sum := blockSum.continuous at n, blk, p113have negative := negCube.continuous with sum114have shifted := addCube.continuous with one, negative115have same := excessCube.definition at n, blk, p116rw same117exact maxCube.continuous with zero, shifted118119theorem scaleCube.continuous (n : Nat) (blk p : Set)120shows CubeFunctionContinuous(scaleCube(n, blk, p), n)121proof122have oneIn : (1:R) ∈ ℝ bytypecheck123have one := constantCube.continuous with oneIn124have sum := blockSum.continuous at n, blk, p125have same := scaleCube.definition at n, blk, p126rw same127exact maxCube.continuous with one, sum128129theorem excessCube.value (n : Nat) (blk p x : Set)130requires hx : x ∈ unitCube(n)131shows excessCube(n, blk, p)(x) = realMax((0:R), (1:R) + -(blockSum(n, blk, p)(x):R))132proof133have zeroIn : (0:R) ∈ ℝ bytypecheck134have oneIn : (1:R) ∈ ℝ bytypecheck135have zero := constantCube.continuous with zeroIn136have one := constantCube.continuous with oneIn137have sum := blockSum.continuous at n, blk, p138have negative := negCube.continuous with sum139have shifted := addCube.continuous with one, negative140have zeroFunction := CubeFunctionContinuous.function with zero141have oneFunction := CubeFunctionContinuous.function with one142have sumFunction := CubeFunctionContinuous.function with sum143have negativeFunction := CubeFunctionContinuous.function with negative144have shiftedFunction := CubeFunctionContinuous.function with shifted145have same := excessCube.definition at n, blk, p146have maxValue := maxCube.value with zeroFunction, shiftedFunction, hx147have zeroValue := constantFunction.apply at unitCube(n), ℝ, x, (0:R) with hx148have oneValue := constantFunction.apply at unitCube(n), ℝ, x, (1:R) with hx149have shiftedValue := addCube.value with oneFunction, negativeFunction, hx150have negativeValue := negCube.value with sumFunction, hx151rw [same, maxValue, zeroValue, shiftedValue, oneValue, negativeValue]152rfl153154theorem scaleCube.value (n : Nat) (blk p x : Set)155requires hx : x ∈ unitCube(n)156shows scaleCube(n, blk, p)(x) = realMax((1:R), (blockSum(n, blk, p)(x):R))157proof158have oneIn : (1:R) ∈ ℝ bytypecheck159have one := constantCube.continuous with oneIn160have oneFunction := CubeFunctionContinuous.function with one161have sumFunction := blockSum.function at n, blk, p162have same := scaleCube.definition at n, blk, p163have maxValue := maxCube.value with oneFunction, sumFunction, hx164have oneValue := constantFunction.apply at unitCube(n), ℝ, x, (1:R) with hx165rw [same, maxValue, oneValue]166rfl167168-- The two corrections are nonnegative, the scale is at least one, and a block169-- sum plus its shortfall is exactly the scale.170theorem realMax.excess (S : Real)171shows S + realMax((0:R), (1:R) + -S) = realMax((1:R), S)172proof173have alternatives : (1:R) ≤ S ∨¬(1:R) ≤ S bytauto174cases alternatives with atLeastOne | below175 . have shortfall : (1:R) + -S ≤ (0:R) bylinarith176have excessZero := realMax.of_le with shortfall177have scaleS := realMax.of_ge with atLeastOne178rw [excessZero, scaleS]179linarith180 . have notLess : ¬((1:R) < S)181intro less182apply below183exact realLess.implies_le with less184have atMostOne := realLe.of_not_less with notLess185have shortfall : (0:R) ≤ (1:R) + -S bylinarith186have excessShortfall := realMax.of_ge with shortfall187have scaleOne := realMax.of_le with atMostOne188rw [excessShortfall, scaleOne]189linarith190191theorem excessCube.nonnegative (n : Nat) (blk p x : Set)192requires hx : x ∈ unitCube(n)193shows (0:R) ≤ (excessCube(n, blk, p)(x):R)194proof195have value := excessCube.value at n, blk, p, x with hx196rw value197exact realMax.ge_left198199theorem scaleCube.at_least_one (n : Nat) (blk p x : Set)200requires hx : x ∈ unitCube(n)201shows (1:R) ≤ (scaleCube(n, blk, p)(x):R)202proof203have value := scaleCube.value at n, blk, p, x with hx204rw value205exact realMax.ge_left206207theorem scaleCube.ge_sum (n : Nat) (blk p x : Set)208requires hx : x ∈ unitCube(n)209shows (blockSum(n, blk, p)(x):R) ≤ (scaleCube(n, blk, p)(x):R)210proof211have value := scaleCube.value at n, blk, p, x with hx212rw value213exact realMax.ge_right214215theorem scaleCube.sum_excess (n : Nat) (blk p x : Set)216requires hx : x ∈ unitCube(n)217shows (blockSum(n, blk, p)(x):R) + (excessCube(n, blk, p)(x):R) = scaleCube(n, blk, p)(x)218proof219have excessValue := excessCube.value at n, blk, p, x with hx220have scaleValue := scaleCube.value at n, blk, p, x with hx221rw [excessValue, scaleValue]222exact realMax.excess223224theorem scaleCube.real (n : Nat) (blk p x : Set)225requires hx : x ∈ unitCube(n)226shows scaleCube(n, blk, p)(x) ∈ ℝ227proof228exact CubeFunction.value with (CubeFunctionContinuous.function with (scaleCube.continuous at n, blk, p)), hx229230theorem excessCube.real (n : Nat) (blk p x : Set)231requires hx : x ∈ unitCube(n)232shows excessCube(n, blk, p)(x) ∈ ℝ233proof234exact CubeFunction.value with (CubeFunctionContinuous.function with (excessCube.continuous at n, blk, p)), hx235