@tf_export('linalg.trace', v1=['linalg.trace', 'trace'])
@deprecation.deprecated_endpoints('trace')
def trace(x, name=None):
    'Compute the trace of a tensor `x`.\n\n  `trace(x)` returns the sum along the main diagonal of each inner-most matrix\n  in x. If x is of rank `k` with shape `[I, J, K, ..., L, M, N]`, then output\n  is a tensor of rank `k-2` with dimensions `[I, J, K, ..., L]` where\n\n  `output[i, j, k, ..., l] = trace(x[i, j, i, ..., l, :, :])`\n\n  For example:\n\n  ```python\n  x = tf.constant([[1, 2], [3, 4]])\n  tf.linalg.trace(x)  # 5\n\n  x = tf.constant([[1, 2, 3],\n                   [4, 5, 6],\n                   [7, 8, 9]])\n  tf.linalg.trace(x)  # 15\n\n  x = tf.constant([[[1, 2, 3],\n                    [4, 5, 6],\n                    [7, 8, 9]],\n                   [[-1, -2, -3],\n                    [-4, -5, -6],\n                    [-7, -8, -9]]])\n  tf.linalg.trace(x)  # [15, -15]\n  ```\n\n  Args:\n    x: tensor.\n    name: A name for the operation (optional).\n\n  Returns:\n    The trace of input tensor.\n  '
    with ops.name_scope(name, 'Trace', [x]) as name:
        x = ops.convert_to_tensor(x, name='x')
        return reduce_sum(array_ops.matrix_diag_part(x), [(- 1)], name=name)
Compute the trace of a tensor `x`.

`trace(x)` returns the sum along the main diagonal of each inner-most matrix
in x. If x is of rank `k` with shape `[I, J, K, ..., L, M, N]`, then output
is a tensor of rank `k-2` with dimensions `[I, J, K, ..., L]` where

`output[i, j, k, ..., l] = trace(x[i, j, i, ..., l, :, :])`

For example:

```python
x = tf.constant([[1, 2], [3, 4]])
tf.linalg.trace(x)  # 5

x = tf.constant([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
tf.linalg.trace(x)  # 15

x = tf.constant([[[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]],
                 [[-1, -2, -3],
                  [-4, -5, -6],
                  [-7, -8, -9]]])
tf.linalg.trace(x)  # [15, -15]
```

Args:
  x: tensor.
  name: A name for the operation (optional).

Returns:
  The trace of input tensor.
@tf_export('linalg.trace', v1=['linalg.trace', 'trace'])
@deprecation.deprecated_endpoints('trace')
def trace(x, name=None):
    'Compute the trace of a tensor `x`.\n\n  `trace(x)` returns the sum along the main diagonal of each inner-most matrix\n  in x. If x is of rank `k` with shape `[I, J, K, ..., L, M, N]`, then output\n  is a tensor of rank `k-2` with dimensions `[I, J, K, ..., L]` where\n\n  `output[i, j, k, ..., l] = trace(x[i, j, i, ..., l, :, :])`\n\n  For example:\n\n  ```python\n  x = tf.constant([[1, 2], [3, 4]])\n  tf.linalg.trace(x)  # 5\n\n  x = tf.constant([[1, 2, 3],\n                   [4, 5, 6],\n                   [7, 8, 9]])\n  tf.linalg.trace(x)  # 15\n\n  x = tf.constant([[[1, 2, 3],\n                    [4, 5, 6],\n                    [7, 8, 9]],\n                   [[-1, -2, -3],\n                    [-4, -5, -6],\n                    [-7, -8, -9]]])\n  tf.linalg.trace(x)  # [15, -15]\n  ```\n\n  Args:\n    x: tensor.\n    name: A name for the operation (optional).\n\n  Returns:\n    The trace of input tensor.\n  '
    with ops.name_scope(name, 'Trace', [x]) as name:
        x = ops.convert_to_tensor(x, name='x')
        return reduce_sum(array_ops.matrix_diag_part(x), [(- 1)], name=name)