Build your own semantic search
Vectors & the dot product·12 min

The dot product

0 / 1
# grade this stage (from your course directory)
npx theinference test --stage dot-product --watch

The dot product is the atom of everything that follows. a · b = Σ aᵢbᵢ — multiply elementwise, sum it up. Geometrically it measures alignment: big when two vectors point the same way, zero when perpendicular, negative when opposed.

Dot product
a · b = a₀b₀ + a₁b₁ + … + a₍ₙ₋₁₎b₍ₙ₋₁₎

What to build

Your task

Implement dot

Implement dot(a, b) in src/vecmath.ts. Both are Float32Array of equal length; return their dot product as a number. One loop, one accumulator.

src/vecmath.ts
export function dot(a: Float32Array, b: Float32Array): number {
  // Σ aᵢ·bᵢ
}
How it's graded

adapter — the harness calls your runDot (which calls dot) on a seeded vector pair and compares to the reference with allclose, atol=1e-5, rtol=1e-4. Your adapter wraps the scalar as [value]; that wiring is already done in tests/adapters.ts.

Accumulate in a `number`, not back into f32

Float32Array elements are 32-bit, but a JS number is float64. Summing into a plain let s = 0 (float64) is exactly what the reference does — read each a[i]/b[i] as needed, but never truncate the running sum back to float32.