// // CholeskySolver.swift // Metrika // // Author: Simon-Pierre Boucher // Contact: contact@spboucher.ai // Copyright © 2026 Simon-Pierre Boucher. All rights reserved. // /// Plain-Swift Cholesky solve for the k×k normal equations of the GPU /// bootstrap. k is the regressor count (single digits in practice), so a /// dependency-free O(k³) routine beats reaching for LAPACK — and ZQStats /// is the only module allowed to import Accelerate (CLAUDE.md §3). enum CholeskySolver { struct SingularMatrix: Error {} /// Solves A·x = b for symmetric positive-definite A (column-major, /// k×k). Throws on non-positive-definite input. static func solve(_ a: [Double], k: Int, rhs: [Double]) throws -> [Double] { precondition(a.count == k * k && rhs.count == k) // Lower-triangular factor L with A = L·Lᵀ. var l = [Double](repeating: 0, count: k * k) for j in 0.. 0 else { throw SingularMatrix() } let root = diagonal.squareRoot() l[j * k + j] = root for i in (j + 1)..