Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les compléments
Voir le programme détaillé
Module « numpy.linalg »

Fonction trace - module numpy.linalg

Signature de la fonction trace

def trace(x, /, *, offset=0, dtype=None) 

Description

help(numpy.linalg.trace)

Returns the sum along the specified diagonals of a matrix
(or a stack of matrices) ``x``.

This function is Array API compatible, contrary to
:py:func:`numpy.trace`.

Parameters
----------
x : (...,M,N) array_like
    Input array having shape (..., M, N) and whose innermost two
    dimensions form MxN matrices.
offset : int, optional
    Offset specifying the off-diagonal relative to the main diagonal,
    where::

        * offset = 0: the main diagonal.
        * offset > 0: off-diagonal above the main diagonal.
        * offset < 0: off-diagonal below the main diagonal.

dtype : dtype, optional
    Data type of the returned array.

Returns
-------
out : ndarray
    An array containing the traces and whose shape is determined by
    removing the last two dimensions and storing the traces in the last
    array dimension. For example, if x has rank k and shape:
    (I, J, K, ..., L, M, N), then an output array has rank k-2 and shape:
    (I, J, K, ..., L) where::

        out[i, j, k, ..., l] = trace(a[i, j, k, ..., l, :, :])

    The returned array must have a data type as described by the dtype
    parameter above.

See Also
--------
numpy.trace

Examples
--------
>>> np.linalg.trace(np.eye(3))
3.0
>>> a = np.arange(8).reshape((2, 2, 2))
>>> np.linalg.trace(a)
array([3, 11])

Trace is computed with the last two axes as the 2-d sub-arrays.
This behavior differs from :py:func:`numpy.trace` which uses the first two
axes by default.

>>> a = np.arange(24).reshape((3, 2, 2, 2))
>>> np.linalg.trace(a).shape
(3, 2)

Traces adjacent to the main diagonal can be obtained by using the
`offset` argument:

>>> a = np.arange(9).reshape((3, 3)); a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> np.linalg.trace(a, offset=1)  # First superdiagonal
6
>>> np.linalg.trace(a, offset=2)  # Second superdiagonal
2
>>> np.linalg.trace(a, offset=-1)  # First subdiagonal
10
>>> np.linalg.trace(a, offset=-2)  # Second subdiagonal
6



Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les compléments
Voir le programme détaillé