Use local variable for performance.
@@ -3026,19 +3026,20 @@ cpdef _scatter_op(ndarray a, slices, value, op):
cpdef ndarray _diagonal(ndarray a, Py_ssize_t offset=0, Py_ssize_t axis1=0,
Py_ssize_t axis2=1):
- if (axis1 >= a.ndim or abs(axis2) >= a.ndim or
- axis1 < - a.ndim or axis2 < - a.ndim):
+ cdef Py_ssize_t ndim = a.ndim
+ if (axis1 >= ndim or abs(axis2) >= ndim or
+ axis1 < - ndim or axis2 < - ndim):
raise ValueError('axis1(={0}) and axis2(={1}) must be within range '
- '(ndim={2})'.format(axis1, axis2, a.ndim))
+ '(ndim={2})'.format(axis1, axis2, ndim))
- axis1 %= a.ndim
- axis2 %= a.ndim
+ axis1 %= ndim
+ axis2 %= ndim
if axis1 < axis2:
min_axis, max_axis = axis1, axis2
else:
min_axis, max_axis = axis2, axis1
- tr = list(range(a.ndim))
+ tr = list(range(ndim))
del tr[max_axis]
del tr[min_axis]
if offset >= 0:
