#!/usr/bin/env python3
# Source: q3-deep-learning.html
import numpy as np

def rope(x, pos, base=10000.0):
    # x: (n, d) with d even. pos: (n,) integer positions.
    x, pos = np.asarray(x, dtype=float), np.asarray(pos)
    if x.ndim != 2 or x.shape[1] % 2 or pos.shape != (len(x),):
        raise ValueError("Use a two-dimensional array with even width and one position per row")
    if base <= 0:
        raise ValueError("RoPE base must be positive")
    d = x.shape[-1]
    inv_freq = base ** (-np.arange(0, d, 2) / d)       # (d/2,)
    ang = pos[:, None] * inv_freq[None, :]              # (n, d/2)
    cos, sin = np.cos(ang), np.sin(ang)
    x1, x2 = x[:, 0::2], x[:, 1::2]                     # pairs
    out = np.empty_like(x)
    out[:, 0::2] = x1 * cos - x2 * sin
    out[:, 1::2] = x1 * sin + x2 * cos
    return out
