Return the sum along diagonals of the array.
If a is 2-d, returns the sum along the diagonal of self with the given offset, i.e., the collection of elements of the form a[i,i+offset]. If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-d subarray whose trace is returned. The shape of the resulting array can be determined by removing axis1 and axis2 and appending an index to the right equal to the size of the resulting diagonals.
Parameters: | a : array_like
offset : integer, optional
axis1 : integer, optional
axis2 : integer, optional
dtype : dtype, optional
out : array, optional
|
---|---|
Returns: | sum_along_diagonals : ndarray
|
Examples
>>> np.trace(np.eye(3))
3.0
>>> a = np.arange(8).reshape((2,2,2))
>>> np.trace(a)
array([6, 8])