Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test the trigonometry routine on remarkable angles #164

Merged
merged 4 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions tests/test_rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,58 @@ def test_exact_rotations(input, angle, expected):
# values from 0 to 45°
# lifted from https://en.wikibooks.org/wiki/Trigonometry/Selected_Angles_Reference
remarkable_angles = {
15: ((sqrt(6) - sqrt(2)) / 4, (sqrt(6) + sqrt(2)) / 4),
22.5: (sqrt(2 - sqrt(2)) / 2, sqrt(2 + sqrt(2)) / 2),
30: (0.5, sqrt(3) / 2),
15: ((sqrt(6) + sqrt(2)) / 4, (sqrt(6) - sqrt(2)) / 4),
22.5: (sqrt(2 + sqrt(2)) / 2, sqrt(2 - sqrt(2)) / 2),
30: (sqrt(3) / 2, 0.5),
45: (sqrt(2) / 2, sqrt(2) / 2),
}

# extend up to 90°
remarkable_angles.update({
90 - angle: (cos_t, sin_t)
for angle, (sin_t, cos_t) in remarkable_angles.items()
90 - angle: (sin_t, cos_t)
for angle, (cos_t, sin_t) in remarkable_angles.items()
})

# extend up to 180°
remarkable_angles.update({
angle + 90: (cos_t, -sin_t)
for angle, (sin_t, cos_t) in remarkable_angles.items()
angle + 90: (-sin_t, cos_t)
for angle, (cos_t, sin_t) in remarkable_angles.items()
})

# extend up to 360°
remarkable_angles.update({
angle + 180: (-sin_t, -cos_t)
for angle, (sin_t, cos_t) in remarkable_angles.items()
angle + 180: (-cos_t, -sin_t)
for angle, (cos_t, sin_t) in remarkable_angles.items()
})

# extend to negative angles
remarkable_angles.update({
-angle: (-sin_t, cos_t)
for angle, (sin_t, cos_t) in remarkable_angles.items()
-angle: (cos_t, -sin_t)
for angle, (cos_t, sin_t) in remarkable_angles.items()
})


@pytest.mark.parametrize("angle, trig", remarkable_angles.items(),
ids=[str(x) for x in remarkable_angles])
def test_remarkable_angles(angle, trig):
_angle = math.radians(angle)
sin_t, cos_t = trig
sin_m, cos_m = math.sin(_angle), math.cos(_angle)
"""Test that our table of remarkable angles agrees with Vector._trig.

assert math.isclose(sin_t, sin_m)
assert math.isclose(cos_t, cos_m)
This is useful both as a consistency test of the table,
and as a test of Vector._trig (which Vector.rotate uses).
"""
cos_t, sin_t = trig
cos_m, sin_m = Vector._trig(angle)

assert isclose(sin_t, sin_m, abs_tol=0, rel_tol=1e-14)
assert isclose(cos_t, cos_m, abs_tol=0, rel_tol=1e-14)


data_close = [
(Vector(1, 0), angle, Vector(cos_t, sin_t))
for (angle, (sin_t, cos_t)) in remarkable_angles.items()
for (angle, (cos_t, sin_t)) in remarkable_angles.items()
] + [
(Vector(1, 1), angle, Vector(cos_t - sin_t, cos_t + sin_t))
for (angle, (sin_t, cos_t)) in remarkable_angles.items()
for (angle, (cos_t, sin_t)) in remarkable_angles.items()
]


Expand Down
6 changes: 0 additions & 6 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Sequence, Union

import hypothesis.strategies as st
from hypothesis import note

from ppb_vector import Vector

Expand Down Expand Up @@ -56,11 +55,6 @@ def isclose(
*(abs(z) ** rel_exp for z in rel_to if isinstance(z, float)),
*(z.length ** rel_exp for z in rel_to if isinstance(z, Vector)),
)
note(f"rel_max = {rel_max}")
if rel_max > 0:
note(f"diff = {diff} = {diff/rel_max} * rel_max")
else:
note(f"diff = {diff}")

return diff <= rel_max * rel_tol or diff <= abs_tol

Expand Down