Least-squares functional, normal system and Gram matrix
DefinitionMetodosNumericos_ajusteDefsThe sum of squared residuals , the normal system of equations , and the Gram matrix .
import Mathlib
namespace MetodosNumericos
/-- The least-squares functional of the MMQ (Método dos Mínimos Quadrados):
for data `(xᵢ, fᵢ)`, `i = 0, …, m`, a family of base functions `φ₀, …, φₙ` and
coefficients `c₀, …, cₙ`,
`S(c) = ∑ᵢ (c₀φ₀(xᵢ) + ⋯ + cₙφₙ(xᵢ) - fᵢ)²`. -/
noncomputable def sqError {m n : ℕ} (phi : Fin (n + 1) → ℝ → ℝ)
(x f : Fin (m + 1) → ℝ) (c : Fin (n + 1) → ℝ) : ℝ :=
∑ i : Fin (m + 1), (∑ k : Fin (n + 1), c k * phi k (x i) - f i) ^ 2
/-- The normal system (sistema normal) of the MMQ: the `n + 1` equations
`∑ᵢ φₖ(xᵢ)(c₀φ₀(xᵢ) + ⋯ + cₙφₙ(xᵢ)) = ∑ᵢ φₖ(xᵢ) fᵢ`, `k = 0, …, n`. -/
def NormalSystem {m n : ℕ} (phi : Fin (n + 1) → ℝ → ℝ)
(x f : Fin (m + 1) → ℝ) (c : Fin (n + 1) → ℝ) : Prop :=
∀ k : Fin (n + 1),
∑ i : Fin (m + 1), phi k (x i) * (∑ j : Fin (n + 1), c j * phi j (x i)) =
∑ i : Fin (m + 1), phi k (x i) * f i
/-- The Gram matrix `A = (a_{kj})` of the normal system, `a_{kj} = ∑ᵢ φₖ(xᵢ) φⱼ(xᵢ)`. -/
noncomputable def gramMatrix {m n : ℕ} (phi : Fin (n + 1) → ℝ → ℝ) (x : Fin (m + 1) → ℝ) :
Matrix (Fin (n + 1)) (Fin (n + 1)) ℝ :=
Matrix.of fun k j => ∑ i : Fin (m + 1), phi k (x i) * phi j (x i)
end MetodosNumericosRead-back
What the Lean code literally says, in plain math · self-authored-by-drafting-agent (non-blind)
Disclosure: this read-back is not blind. It was written by the same agent that drafted the Lean code, at the explicit instruction of the mission's human owner, and not by an independent auditor with fresh context.
All three definitions are parameterised by natural numbers and , a family of real functions indexed by a type with elements, and real families and indexed by a type with elements. Both index types are nonempty.
The first definition sends a coefficient family to the real number
The second is a proposition about a coefficient family : for every index ,
The third is the square matrix indexed by the coefficient index type whose entry is .
No assumption is made that the nodes are distinct, that the functions are linearly independent, or that .