espnet2.enh.layers.complex_utils.inverse
Less than 1 minute
espnet2.enh.layers.complex_utils.inverse
espnet2.enh.layers.complex_utils.inverse(c: Tensor | ComplexTensor) → Tensor | ComplexTensor
Compute the inverse of a complex or real tensor.
This function calculates the inverse of a given tensor, which can be either a real tensor or a complex tensor (using ComplexTensor). If the input is a ComplexTensor, the function will use the inverse2() method. For real tensors, it will use the standard inverse() method.
- Parameters:c – A tensor that can either be a torch.Tensor or a ComplexTensor. It is expected to be a square matrix for the inverse operation.
- Returns: A tensor that is the inverse of the input tensor c. The type of the returned tensor will match the input tensor type.
- Raises:ValueError – If the input tensor is not square or of unsupported type.
Examples
>>> import torch
>>> from torch_complex.tensor import ComplexTensor
>>> real_tensor = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> complex_tensor = ComplexTensor(torch.tensor([[1.0, 2.0], [3.0, 4.0]]),
... torch.tensor([[0.0, 0.0], [0.0, 0.0]]))
>>> inverse_real = inverse(real_tensor)
>>> inverse_complex = inverse(complex_tensor)
>>> print(inverse_real)
tensor([[-2.0000, 1.0000],
[1.5000, -0.5000]])
>>> print(inverse_complex)
ComplexTensor(real=tensor([[-2.0000, 1.0000],
[1.5000, -0.5000]]),
imag=tensor([[0.0, 0.0],
[0.0, 0.0]]))
NOTE
The input tensor must be a square matrix for the inverse to be computed. Ensure that the dimensions of the input tensor are valid to avoid runtime errors.