import Mathlib namespace PermutohedronMatching open scoped BigOperators section variable {Row Col : Type*} [Fintype Row] [Fintype Col] def corrected (base : Row → Col → ℝ) (potential : Col → ℝ) (average : Row → ℝ) (r : Row) (c : Col) : ℝ := base r c * (1 + potential c - average r) /-- A row-preserving, column-equalizing transport, including its support and positivity. -/ theorem correct_flow (base : Row → Col → ℝ) (potential : Col → ℝ) (average : Row → ℝ) (goal : Col → ℝ) (hbase : ∀ r c, 0 ≤ base r c) (hrow : ∀ r, ∑ c, base r c * (potential c - average r) = 0) (hcol : ∀ c, ∑ r, base r c * (potential c - average r) = goal c - ∑ r, base r c) (hbound : ∀ r c, base r c ≠ 0 → average r - potential c ≤ 1) : (∀ r c, 0 ≤ corrected base potential average r c) ∧ (∀ r c, base r c = 0 → corrected base potential average r c = 0) ∧ (∀ r, ∑ c, corrected base potential average r c = ∑ c, base r c) ∧ (∀ c, ∑ r, corrected base potential average r c = goal c) := by have he (r : Row) (c : Col) : corrected base potential average r c = base r c + base r c * (potential c - average r) := by unfold corrected ring refine ⟨?_, ?_, ?_, ?_⟩ · intro r c by_cases h : base r c = 0 · simp [corrected, h] · apply mul_nonneg (hbase r c) linarith [hbound r c h] · intro r c h simp [corrected, h] · intro r simp only [he, Finset.sum_add_distrib, hrow, add_zero] · intro c simp only [he, Finset.sum_add_distrib, hcol] ring end end PermutohedronMatching