Skip to content

Commit 788be79

Browse files
Optimise maths (#1963)
* Optimise maths * Update variable names --------- Co-authored-by: Paul Coe <119464076+CoePaul@users.noreply.github.com>
1 parent f788459 commit 788be79

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/dodal/common/maths.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,12 @@ def do_rotation(x: float, y: float, rotation_matrix: np.ndarray) -> tuple[float,
133133

134134

135135
def rotate_clockwise(theta: float, x: float, y: float) -> tuple[float, float]:
136+
cos_theta = np.cos(theta)
137+
sin_theta = np.sin(theta)
136138
rotation_matrix = np.array(
137139
[
138-
[np.cos(theta), np.sin(theta)],
139-
[-np.sin(theta), np.cos(theta)],
140+
[cos_theta, sin_theta],
141+
[-sin_theta, cos_theta],
140142
]
141143
)
142144
return do_rotation(x, y, rotation_matrix)

tests/common/test_maths.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from math import pi, sqrt
1+
from math import pi
22

33
import numpy as np
44
import pytest
@@ -167,19 +167,21 @@ def test_rotation_preserves_length():
167167
theta = 1.234
168168

169169
x_rot, y_rot = rotate_clockwise(theta, x, y)
170-
original_length = sqrt(x**2 + y**2)
171-
rotated_length = sqrt(x_rot**2 + y_rot**2)
170+
original = x**2 + y**2
171+
rotated = x_rot**2 + y_rot**2
172172

173-
assert rotated_length == pytest.approx(original_length)
173+
assert rotated == pytest.approx(original)
174174

175175

176176
def test_do_rotation_matches_manual_matrix_multiply() -> None:
177177
x, y = 1.2, -0.4
178178
theta = 0.5
179+
cos_theta = np.cos(theta)
180+
sin_theta = np.sin(theta)
179181
rotation_matrix = np.array(
180182
[
181-
[np.cos(theta), -np.sin(theta)],
182-
[np.sin(theta), np.cos(theta)],
183+
[cos_theta, -sin_theta],
184+
[sin_theta, cos_theta],
183185
]
184186
)
185187
x_new, y_new = do_rotation(x, y, rotation_matrix)

0 commit comments

Comments
 (0)