dpnp.trace

dpnp.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)[source]

Return the sum along diagonals of the array.

For full documentation refer to numpy.trace.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- Input array, from which the diagonals are taken.

  • offset (int, optional) -- Offset of the diagonal from the main diagonal. Can be both positive and negative. Default: 0.

  • axis1 (int, optional) -- Axes to be used as the first and second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults are the first two axes of a.

  • axis2 (int, optional) -- Axes to be used as the first and second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults are the first two axes of a.

  • dtype (dtype, optional) -- Determines the data-type of the returned array and of the accumulator where the elements are summed. If dtype has the value None and a is of integer type of precision less than the default integer precision, then the default integer precision is used. Otherwise, the precision is the same as that of a. Default: None.

  • out ({dpnp.ndarray, usm_ndarray}, optional) -- Array into which the output is placed. Its type is preserved and it must be of the right shape to hold the output. Default: None.

Returns:

sum_along_diagonals -- If a is 2-D, the sum along the diagonal is returned. If a has larger dimensions, then an array of sums along diagonals is returned.

Return type:

dpnp.ndarray

See also

dpnp.linalg.trace

Array API compatible version.

dpnp.diag

Extract a diagonal or construct a diagonal array.

dpnp.diagonal

Return specified diagonals.

dpnp.diagflat

Create a 2-D array with the flattened input as a diagonal.

Examples

>>> import dpnp as np
>>> np.trace(np.eye(3))
array(3.)
>>> 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)