numpy.trace(a,offset=0,axis1=0,axis2=1,dtype=None,out=None)[source]¶Return the sum along diagonals of the array.
Ifa is 2-D, the sum along its diagonal with the given offsetis returned, i.e., the sum of elementsa[i,i+offset] for all i.
Ifa has more than two dimensions, then the axes specified by axis1 andaxis2 are used to determine the 2-D sub-arrays whose traces are returned.The shape of the resulting array is the same as that ofa withaxis1andaxis2 removed.
| Parameters: |
|
|---|---|
| Returns: |
|
Examples
>>>np.trace(np.eye(3))3.0>>>a=np.arange(8).reshape((2,2,2))>>>np.trace(a)array([6, 8])
>>>a=np.arange(24).reshape((2,2,2,3))>>>np.trace(a).shape(2, 3)