def test_dot_rotational_invariance(x: Vector, y: Vector, angle: float): """Test that rotating vectors doesn't change their dot product.""" t = x.angle(y) cos_t, _ = Vector._trig(t) note(f"θ: {t}") note(f"cos θ: {cos_t}") # Exclude near-orthogonal test inputs assume(abs(cos_t) > 1e-6) assert isclose(x * y, x.rotate(angle) * y.rotate(angle), rel_to=(x, y), rel_exp=2)
def test_rotation_stability(angle, loops): """Rotating loops times by angle is equivalent to rotating by loops*angle.""" initial = Vector(1, 0) fellswoop = initial.rotate(angle * loops) note(f"One Fell Swoop: {fellswoop}") stepwise = initial for _ in range(loops): stepwise = stepwise.rotate(angle) note(f"Step-wise: {stepwise}") assert fellswoop.isclose(stepwise, rel_tol=1e-8) assert math.isclose(fellswoop.length, initial.length, rel_tol=1e-15)
def test_rotation_invariance(v: Vector, angle: float, n: int): """Check that rotating by angle and angle + n×360° have the same result.""" rot_once = v.rotate(angle) rot_many = v.rotate(angle + 360 * n) note(f"δ: {(rot_once - rot_many).length}") assert rot_once.isclose(rot_many, rel_tol=n / 1e9)