spectral math

Spectral Math

Universal mathematical library for language clarity analysis. TypeScript, JavaScript, Python, Cloudflare Workers.

Reads how a sentence is put together, from the two largest eigenvalues of its parse graph. No model, no EEG. See also: clarity →

the math

Reading scale: f = 5 + 2.5·log(Δλ + 1) Range: 4–12, a bounded scale over Δλ — no unit, and not Hz Confidence score: C = 1 / (1 + exp(-10·(Δλ - 0.5))) Sigmoid centered at Δλ = 0.5, range [0, 1] Spectral gap: Δλ = λ₁ - λ₂ Structure clarity: largest two eigenvalues

λ₁, λ₂ computed via power iteration on the adjacency matrix. Identical implementation across all platforms.

The 4–12 range was labelled Hz here and read as the EEG theta–alpha bands. It is not. Nothing in this has touched brain data, and /clarity carries the retraction. Held out against sentences we did not write, the reading tracks whether a sentence parses — not what it means.

installation

npm install spectral-math

Available on npm. Works in Node.js, browsers, Cloudflare Workers, and Deno.

usage

import { computeEigenvalues, predictFrequency } from 'spectral-math'

const matrix = [
  [0, 1, 0.5, 0],
  [1, 0, 1, 0.5],
  [0.5, 1, 0, 1],
  [0, 0.5, 1, 0]
]

const eigenvalues = computeEigenvalues(matrix)  // [2.081, 0.0]
const delta = eigenvalues[0] - (eigenvalues[1] || 0)
const frequency = predictFrequency(delta)       // 7.81 Hz

api reference

computeEigenvalues(matrix: number[][]): number[]

Power iteration algorithm for largest two eigenvalues.

predictFrequency(deltaLambda: number): number

Brain oscillation frequency from spectral gap (Hz).

predictConfidence(deltaLambda: number): number

Analysis confidence score [0, 1].

spectralGap(eigenvalues: number[]): number

Structure clarity measure (λ₁ - λ₂).

analyze(matrix: number[][]): SpectralAnalysis

Full pipeline: eigenvalues → frequency → confidence.

implementation notes

Power Iteration — Efficient eigenvalue computation that works in constrained environments (Cloudflare Workers, browsers).

Deflation Method — Extract second eigenvalue via matrix deflation, identical to dense solvers.

Convergence — 20 iterations typically sufficient; adaptive tolerance (1e-6).

Numerical Stability — Clamping and normalization prevent overflow/underflow.

Cross-Platform — Identical TypeScript implementation used in Workers, browsers, Node.js, Python (via porting).

Spectral grammar reads how a sentence parses. Same math across all platforms. Open, transparent, universal.

try clarity →