Skip to content

Commit

Permalink
Micro-optimizations for Rotation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
scottshambaugh committed May 6, 2024
1 parent ee8c655 commit 68140e6
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions scipy/spatial/transform/_rotation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,10 @@ cdef class Rotation:
@cython.wraparound(False)
def __init__(self, quat, normalize=True, copy=True, scalar_first=False):
self._single = False
quat = np.asarray(quat, dtype=float)
if normalize or copy:
quat = np.array(quat, copy=True, dtype=float)
else:
quat = np.asarray(quat, dtype=float)

if quat.ndim not in [1, 2] or quat.shape[len(quat.shape) - 1] != 4:
raise ValueError("Expected `quat` to have shape (4,) or (N, 4), "
Expand All @@ -846,8 +849,6 @@ cdef class Rotation:

if scalar_first:
quat = np.roll(quat, -1, axis=1)
elif normalize or copy:
quat = quat.copy()

if normalize:
for ind in range(num_rotations):
Expand Down Expand Up @@ -2699,9 +2700,9 @@ cdef class Rotation:
return self.inv()
elif n == 1:
if self._single:
return self.__class__(self._quat[0], copy=True)
return self.__class__(self._quat[0], normalize=False, copy=True)
else:
return self.__class__(self._quat, copy=True)
return self.__class__(self._quat, normalize=False, copy=True)
else: # general scaling of rotation angle
return Rotation.from_rotvec(n * self.as_rotvec())

Expand Down Expand Up @@ -2744,7 +2745,7 @@ cdef class Rotation:
quat[:, 2] *= -1
if self._single:
quat = quat[0]
return self.__class__(quat, copy=False)
return self.__class__(quat, normalize=False, copy=False)

@cython.embedsignature(True)
@cython.boundscheck(False)
Expand Down

0 comments on commit 68140e6

Please sign in to comment.