YouTip LogoYouTip

Pytorch Torch Linalg Det

## PyTorch torch.linalg.det `torch.linalg.det` is the standard function in PyTorch's linear algebra module (`torch.linalg`) used to compute the determinant of a square matrix or a batch of square matrices. It is the recommended replacement for the legacy `torch.det` function. --- ### Syntax and Definition ```python torch.linalg.det(A, *, out=None) -> Tensor ``` #### Parameters: * **`A` (Tensor)**: A tensor of shape `(*, U, U)` where `*` represents zero or more batch dimensions, and `U` is the size of the square matrix. The tensor must have a floating-point or complex data type (e.g., `float32`, `float64`, `complex64`, `complex128`). * **`out` (Tensor, optional)**: The output tensor. It is ignored if set to `None`. #### Returns: * **`Tensor`**: A tensor containing the determinant(s). If `A` has shape `(*, U, U)`, the returned tensor will have shape `(*)`. --- ### Code Examples #### Example 1: Computing the Determinant of a 2D Square Matrix The following example demonstrates how to calculate the determinant of a $3 \times 3$ upper triangular matrix. For triangular matrices, the determinant is simply the product of the diagonal elements ($1.0 \times 4.0 \times 6.0 = 24.0$). ```python import torch # Create a 3x3 square matrix A = torch.tensor([[1.0, 2.0, 3.0], [0.0, 4.0, 5.0], [0.0, 0.0, 6.0]]) # Compute the determinant det_A = torch.linalg.det(A) print("Matrix A:") print(A) print("\nDeterminant of A:") print(det_A) ``` **Output:** ```text Matrix A: tensor([[1., 2., 3.], [0., 4., 5.], [0., 0., 6.]]) Determinant of A: tensor(24.) ``` --- #### Example 2: Batch Matrix Determinant Calculation `torch.linalg.det` supports batch operations. If you pass a 3D tensor representing a batch of square matrices, it will compute the determinant for each matrix in the batch simultaneously. ```python import torch # Create a batch of two 2x2 matrices (Shape: 2 x 2 x 2) B = torch.tensor([[[2.0, 1.0], [3.0, 4.0]], [[1.0, 5.0], [0.0, 3.0]]]) # Compute batch determinants det_B = torch.linalg.det(B) print("Batch Matrices B:") print(B) print("\nDeterminants of Batch B:") print(det_B) ``` **Output:** ```text Batch Matrices B: tensor([[[2., 1.], [3., 4.]], [[1., 5.], [0., 3.]]]) Determinants of Batch B: tensor([5., 3.]) ``` --- ### Key Considerations 1. **Matrix Dimensions**: The input tensor `A` must be square along its last two dimensions (i.e., `A.shape == A.shape`). Passing a non-square matrix will raise a `RuntimeError`. 2. **Data Type Requirements**: The input tensor must have a floating-point or complex data type. Integer tensors are not supported and must be cast using `.float()` or `.double()` before calling the function. 3. **Numerical Stability**: For large matrices, computing the determinant directly can lead to numerical underflow or overflow. If you only need the determinant for solving linear systems or computing log-likelihoods, consider using `torch.linalg.slogdet` (which returns the sign and the natural logarithm of the absolute value of the determinant) for better numerical stability.
← Pytorch Torch Linalg EighPytorch Torch Less β†’