<commit_before>from ppb_vector import Vector2

from math import sqrt
import pytest  # type: ignore
from hypothesis import assume, given, note
from utils import angles, floats, isclose, vectors


@given(vector=vectors())
def test_dot_axis(vector: Vector2):
    assert vector * (1, 0) == vector.x
    assert vector * (0, 1) == vector.y

@given(x=vectors(), y=vectors())
def test_dot_commutes(x: Vector2, y: Vector2):
    assert x * y == y * x

@given(x=vectors())
def test_dot_length(x: Vector2):
    assert isclose(x * x, x.length * x.length)

@given(x=vectors(), y=vectors(), angle=angles())
def test_dot_rotational_invariance(x: Vector2, y: Vector2, angle: float):
    """Test that rotating vectors doesn't change their dot product."""
    t = x.angle(y)
    cos_t, _ = Vector2._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)


MAGNITUDE=1e10
@given(x=vectors(max_magnitude=MAGNITUDE), z=vectors(max_magnitude=MAGNITUDE),
       y=vectors(max_magnitude=sqrt(MAGNITUDE)),
       scalar=floats(max_magnitude=sqrt(MAGNITUDE)))<commit_msg>tests/dot: Add a test based on the Cauchy-Schwarz inequality<commit_after>from ppb_vector import Vector2

from math import sqrt
import pytest  # type: ignore
from hypothesis import assume, given, note
from utils import angles, floats, isclose, vectors


@given(vector=vectors())
def test_dot_axis(vector: Vector2):
    assert vector * (1, 0) == vector.x
    assert vector * (0, 1) == vector.y

@given(x=vectors(), y=vectors())
def test_dot_commutes(x: Vector2, y: Vector2):
    assert x * y == y * x

@given(x=vectors())
def test_dot_length(x: Vector2):
    assert isclose(x * x, x.length * x.length)

@given(x=vectors(), y=vectors())
def test_cauchy_schwarz(x: Vector2, y: Vector2):
    """Test the Cauchy-Schwarz inequality: |x·y| ⩽ |x| |y|"""
    assert abs(x * y) <= (1 + 1e-12) * x.length * y.length

@given(x=vectors(), y=vectors(), angle=angles())
def test_dot_rotational_invariance(x: Vector2, y: Vector2, angle: float):
    """Test that rotating vectors doesn't change their dot product."""
    t = x.angle(y)
    cos_t, _ = Vector2._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)


MAGNITUDE=1e10
@given(x=vectors(max_magnitude=MAGNITUDE), z=vectors(max_magnitude=MAGNITUDE),
       y=vectors(max_magnitude=sqrt(MAGNITUDE)),
       scalar=floats(max_magnitude=sqrt(MAGNITUDE)))
{'fraction_non_alphanumeric': 0.14894405335309374, 'fraction_numerical': 0.015561319007039644, 'mean_word_length': 4.389221556886228, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2980323', 'n_tokens_mistral': 1063, 'n_tokens_neox': 991, 'n_words': 243}