From fe3a3738077e41406491e45c794974704c4b569d Mon Sep 17 00:00:00 2001 From: jan Date: Wed, 12 Apr 2023 18:21:18 -0700 Subject: [PATCH] Make rotation matrix immutable and cache the value --- masque/utils/transform.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/masque/utils/transform.py b/masque/utils/transform.py index bc364f9..ccdf3d2 100644 --- a/masque/utils/transform.py +++ b/masque/utils/transform.py @@ -2,11 +2,13 @@ Geometric transforms """ from typing import Sequence +from functools import lru_cache import numpy from numpy.typing import NDArray +@lru_cache def rotation_matrix_2d(theta: float) -> NDArray[numpy.float64]: """ 2D rotation matrix for rotating counterclockwise around the origin. @@ -17,8 +19,10 @@ def rotation_matrix_2d(theta: float) -> NDArray[numpy.float64]: Returns: rotation matrix """ - return numpy.array([[numpy.cos(theta), -numpy.sin(theta)], - [numpy.sin(theta), +numpy.cos(theta)]]) + arr = numpy.array([[numpy.cos(theta), -numpy.sin(theta)], + [numpy.sin(theta), +numpy.cos(theta)]]) + arr.flags.writeable = False + return arr def normalize_mirror(mirrored: Sequence[bool]) -> tuple[bool, float]: