1-- Sums of selected coordinates as continuous cube functions, and maps of the2-- cube assembled coordinate by coordinate from continuous cube functions3-- 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.FixedPoint.CubeAssembly9import CubeMask10import Real.FiniteSum1112-- Sums of the first m selected coordinates, as cube functions.13defgraph prefixSumCube (n S m : Set) (x ∈ unitCube(n)) : ℝ := realPartialSum(maskedPoint(n, S, x), m)14requires nNat : Nat(n)15requires mNat : Nat(m)16proof17have xFunction := unitCube.point with member18have sequence := maskedPoint.sequence at n, S, x with xFunction19exact realPartialSum.real at maskedPoint(n, S, x), m with sequence2021theorem prefixSumCube.continuous (n : Nat) (S : Set) (m : Nat)22shows m ∈ succ(n) → CubeFunctionContinuous(prefixSumCube(n, S, m), n)23proof24induction m25| zero =>26intro hm27have zeroIn : (0:R) ∈ ℝ bytypecheck28have constant := constantCube.continuous at n, (0:R) with zeroIn29have function : CubeFunction(prefixSumCube(n, S, 0), n)30exact prefixSumCube.function at n, S, 0with*31apply CubeFunctionContinuous.congr with function, constant32intros x hx33have value := prefixSumCube.value at n, S, 0, x with*34have constantValue := constantFunction.apply at unitCube(n), ℝ, x, (0:R) with hx, zeroIn35rw [value, constantValue]36exact realPartialSum.zero at maskedPoint(n, S, x)37| succ(j) using ih =>38intro hm39have jNat : Nat(j) bytypecheck40have jIn : j ∈ succ(n)41have less := Less.of_membership with hm42have below : j < succ(n) byarith43exact membership.of_Less with below44have jInN : j ∈ n45have less := Less.of_membership with hm46have below : j < n byarith47exact membership.of_Less with below48have previous := ih with jIn49have weightNat := indicator.natural at S, j50have weightReal := realNat.real at indicator(S, j)51have weightIn : (realNat(indicator(S, j)):R) ∈ ℝ bytypecheck52have constant := constantCube.continuous at n, (realNat(indicator(S, j)):R) with weightIn53have coordinate := coordinateCube.continuous at n, j with jInN54have term := mulCube.continuous with constant, coordinate55have total := addCube.continuous with previous, term56have function : CubeFunction(prefixSumCube(n, S, succ(j)), n)57exact prefixSumCube.function at n, S, succ(j) with*58apply CubeFunctionContinuous.congr with function, total59intros x hx60have xFunction := unitCube.point with hx61have previousFunction := CubeFunctionContinuous.function with previous62have termFunction := CubeFunctionContinuous.function with term63have constantFunction2 := CubeFunctionContinuous.function with constant64have coordinateFunction := CubeFunctionContinuous.function with coordinate65have value := prefixSumCube.value at n, S, succ(j), x with*66have previousValue := prefixSumCube.value at n, S, j, x with*67have sumValue := addCube.value at n, prefixSumCube(n, S, j), mulCube(n, constantFunction(unitCube(n), ℝ, (realNat(indicator(S, j)):R)), coordinateCube(n, j)), x with previousFunction, termFunction, hx68have termValue := mulCube.value at n, constantFunction(unitCube(n), ℝ, (realNat(indicator(S, j)):R)), coordinateCube(n, j), x with constantFunction2, coordinateFunction, hx69have constantValue := constantFunction.apply at unitCube(n), ℝ, x, (realNat(indicator(S, j)):R) with hx, weightIn70have coordinateValue := coordinateCube.value at n, j, x with jInN, hx71have step := realPartialSum.succ at maskedPoint(n, S, x), j72have masked := maskedPoint.coordinate at n, S, x, j with xFunction, jInN73have sequence := maskedPoint.sequence at n, S, x with xFunction74have xReal := unitCube.coordinate_real with hx, jInN75rw [value, sumValue, previousValue, termValue, constantValue, coordinateValue, step, masked]76linarith7778def sumCube (n S : Set) : Set:= prefixSumCube(n, S, n)7980theorem sumCube.continuous (n : Nat) (S : Set)81shows CubeFunctionContinuous(sumCube(n, S), n)82proof83have all := prefixSumCube.continuous at n, S, n84have same : sumCube(n, S) = prefixSumCube(n, S, n)85unfold sumCube86rfl87rw same88exact all with (bootstrap.succ.self at n)8990theorem sumCube.value (n : Nat) (S x : Set)91requires hx : x ∈ unitCube(n)92shows sumCube(n, S)(x) = realPartialSum(maskedPoint(n, S, x), n)93proof94have same : sumCube(n, S) = prefixSumCube(n, S, n)95unfold sumCube96rfl97rw same98exact prefixSumCube.value at n, S, n, x with*99100-- A map of the cube assembled from one cube function per coordinate.101defgraph assembledPoint (n G x : Set) (i ∈ n) : ℝ := G(i)(x)102requires family : ∀ i ∈ n, CubeFunction(G(i), n)103requires hx : x ∈ unitCube(n)104proof105exact CubeFunction.value with (family with member), hx106107defgraph assembledMap (n G : Set) (x ∈ unitCube(n)) : unitCube(n) := assembledPoint(n, G, x)108requires family : ∀ i ∈ n, CubeFunction(G(i), n)109requires bounds : ∀ i ∈ n, ∀ x ∈ unitCube(n), (0:R) ≤ (G(i)(x):R) ∧ (G(i)(x):R) ≤ (1:R)110proof111have function := assembledPoint.function with family, member112apply unitCube.membership.backward113constructor114 . exact functionSpace.mem_iff.backward with function115 . intros i hi116have value := assembledPoint.value with family, member, hi117have forI := bounds with hi118obtain⟨lower, upper⟩:= forI with member119apply closedInterval.membership.backward120rw value121constructor122 . exact CubeFunction.value with (family with hi), member123 . exact⟨lower, upper⟩124125theorem assembledMap.coordinate (n : Nat) (G x i : Set)126requires family : ∀ i ∈ n, CubeFunction(G(i), n)127requires bounds : ∀ i ∈ n, ∀ x ∈ unitCube(n), (0:R) ≤ (G(i)(x):R) ∧ (G(i)(x):R) ≤ (1:R)128requires hx : x ∈ unitCube(n)129requires hi : i ∈ n130shows assembledMap(n, G)(x)(i) = G(i)(x)131proof132have value := assembledMap.value with family, bounds, hx133have point := assembledPoint.value with family, hx, hi134rw [value, point]135rfl136137-- Finitely many coordinate functions have a common tolerance at a point.138theorem assembledMap.common_tolerance (n : Nat) (G x : Set) (E : Real) (m : Nat)139requires family : ∀ i ∈ n, CubeFunctionContinuous(G(i), n)140requires hx : x ∈ unitCube(n)141requires positive : 0 < E142shows m ∈ succ(n) →∃ D ∈ ℝ, (0:R) < D ∧ (∀ y ∈ unitCube(n), ((∀ j ∈ n, dist(y(j), x(j)) < D) → (∀ i ∈ m, dist(G(i)(y), G(i)(x)) < E)))143proof144induction m145| zero =>146intro hm147have oneIn : (1:R) ∈ ℝ bytypecheck148have onePos : (0:R) < (1:R) bylinarith149witness (1:R)150constructor151 . exact oneIn152 . constructor153 . exact onePos154 . intros y hy near i hi155exfalso156rw nat.zero at hi157 simpa using hi158| succ(j) using ih =>159intro hm160have jNat : Nat(j) bytypecheck161have jIn : j ∈ succ(n)162have less := Less.of_membership with hm163have below : j < succ(n) byarith164exact membership.of_Less with below165have jInN : j ∈ n166have less := Less.of_membership with hm167have below : j < n byarith168exact membership.of_Less with below169obtain⟨D1, D1Real, D1Pos, previous⟩:= ih with jIn170have jAt := CubeFunctionContinuous.at with (family with jInN), hx171have EReal : E ∈ ℝ bytypecheck172obtain⟨D2, D2Real, D2Pos, close⟩:= jAt with EReal, positive173obtain⟨D, DReal, DPos, DBelow1, DBelow2⟩:= positive.below_both with D1Pos, D2Pos174have shrunk := CubeFunctionContinuousAt.shrink at G(j), n, x, E, D2, D with hx, close, (realLess.implies_le with DBelow2)175witness D176constructor177 . exact DReal178 . constructor179 . exact DPos180 . intros y hy near i hi181have nearD1 : ∀ k ∈ n, dist(y(k), x(k)) < D1182intros k hk183have step := near with hk184have yReal := unitCube.coordinate_real with hy, hk185have xReal := unitCube.coordinate_real with hx, hk186have distReal := dist.real at y(k), x(k)187linarith188havesplit:= bootstrap.succ.membership.forward with hi189casessplitwith below | same190 . have forY := previous with hy, nearD1191exact forY with below192 . rw same193exact shrunk with hy, near194195theorem assembledMap.continuous (n : Nat) (G : Set)196requires family : ∀ i ∈ n, CubeFunctionContinuous(G(i), n)197requires bounds : ∀ i ∈ n, ∀ x ∈ unitCube(n), (0:R) ≤ (G(i)(x):R) ∧ (G(i)(x):R) ≤ (1:R)198shows CubeContinuous(assembledMap(n, G), n)199proof200have functions : ∀ i ∈ n, CubeFunction(G(i), n)201intros i hi202exact CubeFunctionContinuous.function with (family with hi)203have function := assembledMap.function with functions, bounds204constructor205 . exact function206 . intros x hx E EReal Epos207have all := assembledMap.common_tolerance with family, hx, Epos208obtain⟨D, DReal, DPos, close⟩:= all with (bootstrap.succ.self at n)209witness D210constructor211 . exact DReal212 . constructor213 . exact DPos214 . intros y hy near i hi215have yValue := assembledMap.coordinate with functions, bounds, hy, hi216have xValue := assembledMap.coordinate with functions, bounds, hx, hi217rw [yValue, xValue]218have forY := close with hy, near219exact forY with hi220