# `BB.Math.Defn`
[🔗](https://github.com/beam-bots/bb/blob/main/lib/bb/math/defn.ex#L5)

Composable `defn` numerical kernels operating on raw Nx tensors.

This is the computational core of the math layer. The struct wrappers
(`BB.Math.Vec3`, `BB.Math.Quaternion`, `BB.Math.Transform`) delegate their
hot operations here, and higher-level algorithms (forward kinematics,
Jacobians, IK iterations) are expected to compose these kernels inside their
own `defn` rather than threading values back through eager per-op struct
calls.

Keeping the maths in `defn` is the point: a `defn` can be JIT-compiled,
vectorised over a leading batch axis, and composed into a larger computation,
none of which is possible for eager `Nx.*` calls dispatched one operation at
a time.

All inputs and outputs are raw `:f64` tensors with these conventions:

- quaternions are `{4}` tensors in WXYZ (scalar-first) order
- transforms are `{4, 4}` homogeneous matrices

# `normalise_quaternion`

Normalise a `{4}` quaternion tensor to unit length.

Falls back to the identity quaternion when the input is near-zero, so the
result is always a valid unit rotation.

# `quaternion_multiply`

Hamilton product of two quaternions, returning a normalised unit quaternion.

`quaternion_multiply(q1, q2)` composes the rotations: `q2` is applied first,
then `q1`. Inputs and output are `{4}` WXYZ tensors.

# `transform_compose`

Compose two `{4, 4}` homogeneous transforms.

`transform_compose(a, b)` returns the transform that applies `a` first, then
`b`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
