// math.js // Linear-algebra helpers + sample averaging. // Pure functions; no DOM; no side effects. // ---- Sample averaging (V2) ---- // Averages all the per-frame features collected during calibration. Old samples // without ear/pitch/yaw simply contribute 0 to the corresponding sums (we // branch on the field being defined), so the function degrades gracefully if // it's ever called on a partially-populated sample batch. export function averageSamples(samples) { if (!samples.length) { return { ratioX: 0.5, ratioY: 0.5, ear: 0, pitch: 0, yaw: 0 }; } let sx = 0, sy = 0; let eSum = 0, eN = 0; let pSum = 0, pN = 0; let wSum = 0, wN = 0; for (let i = 0; i < samples.length; i++) { const s = samples[i]; sx += s.ratioX; sy += s.ratioY; if (s.ear !== undefined && s.ear !== null) { eSum += s.ear; eN++; } if (s.pitch !== undefined && s.pitch !== null) { pSum += s.pitch; pN++; } if (s.yaw !== undefined && s.yaw !== null) { wSum += s.yaw; wN++; } } const n = samples.length; return { ratioX: sx / n, ratioY: sy / n, ear: eN ? eSum / eN : 0, pitch: pN ? pSum / pN : 0, yaw: wN ? wSum / wN : 0 }; } // ---- NxN linear solver (Gaussian elimination with partial pivoting) ---- // Replaces the old Cramer's-rule 6x6 solver when we need a 9x9 (or any other // size) normal equation. Cramer's is O(n!) and becomes both slow and // numerically unstable past ~6x6, so we switch to LU-with-pivoting here. // // A is an n×n matrix (array of n row-arrays of length n). // b is an n-vector. // Returns the n-vector x such that A·x = b, or null if A is singular // (rank-deficient to machine precision, |pivot| < 1e-12). export function solveNxN(n, A, b) { // Build the augmented [A|b] matrix in row-major form. const M = new Array(n); for (let i = 0; i < n; i++) { const row = new Array(n + 1); for (let j = 0; j < n; j++) row[j] = A[i][j]; row[n] = b[i]; M[i] = row; } // Forward elimination. for (let k = 0; k < n; k++) { // Partial pivoting: pick the row with the largest |M[i][k]| for i >= k. let maxVal = Math.abs(M[k][k]); let maxRow = k; for (let i = k + 1; i < n; i++) { const v = Math.abs(M[i][k]); if (v > maxVal) { maxVal = v; maxRow = i; } } if (maxVal < 1e-12) return null; // singular if (maxRow !== k) { // swap rows const tmp = M[k]; M[k] = M[maxRow]; M[maxRow] = tmp; } // Eliminate below the pivot. for (let i = k + 1; i < n; i++) { const factor = M[i][k] / M[k][k]; if (factor === 0) continue; for (let j = k; j <= n; j++) { M[i][j] -= factor * M[k][j]; } } } // Back substitution. const x = new Array(n); for (let i = n - 1; i >= 0; i--) { let sum = M[i][n]; for (let j = i + 1; j < n; j++) sum -= M[i][j] * x[j]; x[i] = sum / M[i][i]; } return x; } // ============================================================ // Old 6-dim helpers (kept for back-compat / regression testing) // ============================================================ // Solve a 3×3 linear system A x = b via Cramer's rule. // Returns [x0, x1, x2] or null if A is singular. export function solve3x3(A, b) { const det = ( A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) - A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) + A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]) ); if (Math.abs(det) < 1e-9) return null; const det0 = ( b[0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) - A[0][1] * (b[1] * A[2][2] - A[1][2] * b[2]) + A[0][2] * (b[1] * A[2][1] - A[1][1] * b[2]) ); const det1 = ( A[0][0] * (b[1] * A[2][2] - A[1][2] * b[2]) - b[0] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) + A[0][2] * (A[1][0] * b[2] - b[1] * A[2][0]) ); const det2 = ( A[0][0] * (A[1][1] * b[2] - b[1] * A[2][1]) - A[0][1] * (A[1][0] * b[2] - b[1] * A[2][0]) + b[0] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]) ); return [det0 / det, det1 / det, det2 / det]; } // 6x6 Cramer's rule: solve Ax=b where A is 6x6, b is 6x1 // A is passed as array of 6 rows, each row is [a0..a5] export function solve6x6(A, b) { function det6(m) { // Compute 6x6 determinant via cofactor expansion along first row let d = 0; for (let j = 0; j < 6; j++) { const minor = []; for (let i = 1; i < 6; i++) { const row = []; for (let k = 0; k < 6; k++) { if (k !== j) row.push(m[i][k]); } minor.push(row); } const sign = (j % 2 === 0) ? 1 : -1; d += sign * m[0][j] * det5(minor); } return d; } function det5(m) { let d = 0; for (let j = 0; j < 5; j++) { const minor = []; for (let i = 1; i < 5; i++) { const row = []; for (let k = 0; k < 5; k++) { if (k !== j) row.push(m[i][k]); } minor.push(row); } const sign = (j % 2 === 0) ? 1 : -1; d += sign * m[0][j] * det4(minor); } return d; } function det4(m) { // 4x4 determinant via cofactor expansion let d = 0; for (let j = 0; j < 4; j++) { const minor = []; for (let i = 1; i < 4; i++) { const row = []; for (let k = 0; k < 4; k++) { if (k !== j) row.push(m[i][k]); } minor.push(row); } const sign = (j % 2 === 0) ? 1 : -1; d += sign * m[0][j] * det3(minor); } return d; } function det3(m) { return m[0][0]*(m[1][1]*m[2][2]-m[1][2]*m[2][1]) - m[0][1]*(m[1][0]*m[2][2]-m[1][2]*m[2][0]) + m[0][2]*(m[1][0]*m[2][1]-m[1][1]*m[2][0]); } const D = det6(A); if (Math.abs(D) < 1e-12) return null; // singular const x = []; for (let j = 0; j < 6; j++) { const Aj = A.map((row, i) => { const r = row.slice(); r[j] = b[i]; return r; }); x.push(det6(Aj) / D); } return x; } // Build 6-dim polynomial design row from iris ratios export function buildPolyFeatures(rX, rY) { return [1, rX, rY, rX*rX, rY*rY, rX*rY]; } // Build the design matrix for [a0, a1, …, a5] with rows [1, rX, rY, rX², rY², rX*rY]. export function buildDesignMatrix(points) { const M = []; for (let i = 0; i < points.length; i++) { M.push(buildPolyFeatures(points[i].ratioX, points[i].ratioY)); } return M; } // Ordinary least squares for y = X * beta. Returns beta = [b0, …, b5] // (we use Cramer's rule on X^T X and X^T y — small, dense, exact). export function olsFit(X, y) { const n = X.length; // X^T X (6x6) const A = [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]; // X^T y (6x1) const b = [0, 0, 0, 0, 0, 0]; for (let i = 0; i < n; i++) { const xi = X[i]; for (let r = 0; r < 6; r++) { for (let c = 0; c < 6; c++) A[r][c] += xi[r] * xi[c]; b[r] += xi[r] * y[i]; } } return solve6x6(A, b); } // ============================================================ // V2 (9-dim) helpers: [1, rX, rY, ear, pitch, yaw, rX², rY², rX*rY] // ============================================================ // The V2 feature layout. Head-pose features (ear, pitch, yaw) enter // linearly; the polynomial cross-terms (rX², rY², rX*rY) only involve // the iris ratios, which is what the literature converges on (eye-contour // + iris is dominant, with head pose as an auxiliary). export const N_FEATURES_V2 = 9; // Build one 9-dim design row from a (rX, rY, ear, pitch, yaw) sample. export function buildPolyFeaturesV2(rX, rY, ear, pitch, yaw) { return [1, rX, rY, ear, pitch, yaw, rX*rX, rY*rY, rX*rY]; } // Build a 9-dim design matrix from a list of points that carry the V2 features. // `points` is expected to be [{ ratioX, ratioY, ear, pitch, yaw }, ...]. export function buildDesignMatrixV2(points) { const M = []; for (let i = 0; i < points.length; i++) { const p = points[i]; M.push(buildPolyFeaturesV2( p.ratioX, p.ratioY, p.ear != null ? p.ear : 0, p.pitch != null ? p.pitch : 0, p.yaw != null ? p.yaw : 0 )); } return M; } // Ordinary least squares for an arbitrary-dim design matrix. // X is m × nFeatures (m >= nFeatures), y is m × 1. // Solves (X^T X) beta = X^T y via Gaussian elimination with partial pivoting. export function olsFitV2(X, y, nFeatures) { const n = X.length; // X^T X (nFeatures x nFeatures) const A = new Array(nFeatures); for (let r = 0; r < nFeatures; r++) A[r] = new Array(nFeatures).fill(0); // X^T y (nFeatures x 1) const b = new Array(nFeatures).fill(0); for (let i = 0; i < n; i++) { const xi = X[i]; for (let r = 0; r < nFeatures; r++) { for (let c = 0; c < nFeatures; c++) A[r][c] += xi[r] * xi[c]; b[r] += xi[r] * y[i]; } } return solveNxN(nFeatures, A, b); }