1-- Explicit solutions of two real linear equations.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.LinearAlgebra.TwoByTwo8import Real.Inverse910namespace LinearSystem21112-- These total expressions are solutions only when the determinant is nonzero.13def solutionX (a b c d u v : Real) : Real:=14 realInvValue(a * d - b * c) * (d * u - b * v)1516def solutionY (a b c d u v : Real) : Real:=17 realInvValue(a * d - b * c) * (a * v - c * u)1819theorem solutionX.real (a b c d u v : Real)20showsReal(solutionX(a, b, c, d, u, v))21proof22unfold solutionX23typecheck2425theorem solutionY.real (a b c d u v : Real)26showsReal(solutionY(a, b, c, d, u, v))27proof28unfold solutionY29typecheck3031theorem correct (a b c d u v : Real)32requires nonsingular : a * d - b * c ≠033shows a * solutionX(a, b, c, d, u, v) + b * solutionY(a, b, c, d, u, v) = u ∧34 c * solutionX(a, b, c, d, u, v) + d * solutionY(a, b, c, d, u, v) = v35proof36have inverse := realInvValue.mul_left with*37constructor38 . unfold solutionX39unfold solutionY40linear_combination u * inverse41 . unfold solutionX42unfold solutionY43linear_combination v * inverse4445-- Uniqueness applies directly to any two solutions, without unfolding Cramer formulas.46theorem unique (a b c d u v x y s t : Real)47requires nonsingular : a * d - b * c ≠048requires first : a * x + b * y = u49requires second : c * x + d * y = v50requires otherFirst : a * s + b * t = u51requires otherSecond : c * s + d * t = v52shows x = s ∧ y = t53proof54have inverse := realInvValue.mul_left with*55constructor56 . linear_combination (realInvValue(a * d - b * c) * d) * first - (realInvValue(a * d - b * c) * b) * second - (realInvValue(a * d - b * c) * d) * otherFirst + (realInvValue(a * d - b * c) * b) * otherSecond - (x - s) * inverse57 . linear_combination (realInvValue(a * d - b * c) * a) * second - (realInvValue(a * d - b * c) * c) * first - (realInvValue(a * d - b * c) * a) * otherSecond + (realInvValue(a * d - b * c) * c) * otherFirst - (y - t) * inverse5859end LinearSystem260