YouTip LogoYouTip

Pytorch Torch Angle

## PyTorch `torch.angle` Function The `torch.angle` function in PyTorch computes the element-wise angle (also known as the phase or argument) of a complex tensor. The returned values are in radians and fall within the range $[-\pi, \pi]$. --- ### Function Signature ```python torch.angle(input, *, out=None) -> Tensor ``` ### Parameters * **`input` (Tensor)**: The input tensor containing real or complex numbers. * **`out` (Tensor, optional)**: The output tensor. Defaults to `None`. ### Return Value * Returns a new tensor containing the phase angles of the elements in `input`. * If the input is a complex tensor, the output is a real-valued float tensor representing the phase angle in radians. * If the input is a real tensor, the phase angle is $0$ for positive values and $\pi$ (approximately $3.14159$) for negative values. --- ## Code Examples ### Example 1: Computing the Phase Angle of a Complex Tensor This example demonstrates how to calculate the phase angle for various complex numbers representing different quadrants of the complex plane. ```python import torch # Create a complex tensor # 1+1j (Quadrant I), 1+0j (Positive Real Axis), 0+1j (Positive Imaginary Axis), -1+0j (Negative Real Axis) x = torch.tensor([1+1j, 1+0j, 0+1j, -1+0j]) # Compute the phase angle (argument) result = torch.angle(x) print("Input Tensor:") print(x) print("\nPhase Angles (in radians):") print(result) ``` **Output:** ```text Input Tensor: tensor([ 1.+1.j, 1.+0.j, 0.+1.j, -1.+0.j]) Phase Angles (in radians): tensor([ 0.7854, 0.0000, 1.5708, 3.1416]) ``` * `1 + 1j` corresponds to $\pi/4 \approx 0.7854$ radians ($45^\circ$). * `1 + 0j` corresponds to $0$ radians ($0^\circ$). * `0 + 1j` corresponds to $\pi/2 \approx 1.5708$ radians ($90^\circ$). * `-1 + 0j` corresponds to $\pi \approx 3.1416$ radians ($180^\circ$). --- ### Example 2: Handling Real-Valued Tensors When passed a real-valued tensor, `torch.angle` treats the elements as complex numbers with an imaginary part of $0$. ```python import torch # Create a real-valued tensor with positive and negative numbers x = torch.tensor([2.0, -2.0, 0.0]) # Compute the phase angle result = torch.angle(x) print(result) ``` **Output:** ```text tensor([0.0000, 3.1416, 0.0000]) ``` --- ## Technical Considerations 1. **Mathematical Definition**: For a complex number $z = x + iy$, the angle $\theta$ is calculated using the two-argument arctangent function: $$\theta = \text{atan2}(y, x)$$ 2. **Data Types**: * If the input is complex (`torch.complex64` or `torch.complex128`), the output will be a real float tensor (`torch.float32` or `torch.float64` respectively). * If the input is real-valued, the output will have the same float data type as the input. 3. **Gradient Support**: `torch.angle` is fully differentiable, making it suitable for use in backpropagation within neural networks (e.g., in signal processing or Fourier-transform-based architectures).
← Pytorch Torch ArangeClaude Code Subagent β†’