1-- Grouping weighted finite sums by their index map (ZF).2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Algebra.Ring.WeightedRegrouping8import Regrouping9import FiniteLinearity1011section Rings12variable (D C A add zero neg mul one h w t a v : Set)13notationring R := (A, add, zero, neg, mul, one)14hypothesisring : IsCommRing(R)15hypothesis finiteD : Finite(D)16hypothesis finiteC : Finite(C)17hypothesis indexMap : h : D → C18hypothesis weights : w : D → A19hypothesis terms : t : D → A20hypothesis scalars : a : C → A21hypothesis outputs : v : C → A22hypothesis factors : ∀ d ∈ D, t(d) = a(h(d)) * w(d)23hypothesis values : ∀ c ∈ C, v(c) = a(c) * finitePushforward(D, C, A, add, zero, neg, mul, one, h, w)(c)2425theorem finitePushforward.weighted_sum26shows finiteRingSum(D, R, t) = finiteRingSum(C, R, v)27proof28have ordinary := IsCommRing.ring withring29have zeroIn := IsRing.zero_in_carrier with ordinary30have grouped := finitePushforward.function at D, C, A, add, zero, neg, mul, one, h, t with*31have equations : ∀ c ∈ C, finitePushforward(D, C, A, add, zero, neg, mul, one, h, t)(c) = v(c)32intros c hc33have scalar := FunctionBetween.apply_closed at a, C, A, c with*34have selectedW := pushforwardWeights.function at D, A, add, zero, neg, mul, one, h, w, c with*35have selectedT := pushforwardWeights.function at D, A, add, zero, neg, mul, one, h, t, c with*36have pointwise : ∀ d ∈ D, pushforwardWeights(D, A, add, zero, neg, mul, one, h, t, c)(d) = a(c) * pushforwardWeights(D, A, add, zero, neg, mul, one, h, w, c)(d)37intros d hd38have wIn := FunctionBetween.apply_closed at w, D, A, d with*39have tIn := FunctionBetween.apply_closed at t, D, A, d with*40haveleft:= pushforwardWeights.value at D, A, add, zero, neg, mul, one, h, t, c, d with*41haveright:= pushforwardWeights.value at D, A, add, zero, neg, mul, one, h, w, c, d with*42have alternatives : c = h(d) ∨ c ≠ h(d) bytauto43cases alternatives with same | different44 . have selectedLeft := singleValue.same at A, zero, t(d), h(d), c with*45have selectedRight := singleValue.same at A, zero, w(d), h(d), c with*46have factor := factors with d, hd47cc48 . have selectedLeft := singleValue.other at A, zero, t(d), h(d), c with*49have selectedRight := singleValue.other at A, zero, w(d), h(d), c with*50rw [left, right, selectedLeft, selectedRight]51ring52have scale := finiteRingSum.scale_eq at D, A, add, zero, neg, mul, one, pushforwardWeights(D, A, add, zero, neg, mul, one, h, w, c), pushforwardWeights(D, A, add, zero, neg, mul, one, h, t, c), a(c) with*53haveleft:= finitePushforward.value at D, C, A, add, zero, neg, mul, one, h, t, c with*54haveright:= finitePushforward.value at D, C, A, add, zero, neg, mul, one, h, w, c with*55have output := values with c, hc56cc57have groupedSum := finitePushforward.sum at D, C, A, add, zero, neg, mul, one, h, t with*58have equal := finiteRingSum.congr at C, A, add, zero, neg, mul, one, finitePushforward(D, C, A, add, zero, neg, mul, one, h, t), v with*59cc6061end Rings62