torch.complex
Create a complex tensor from separate real and imaginary tensors.
Combines two real-valued tensors into a complex tensor in Cartesian (rectangular) form. This is the primary way to create complex tensors from separate components when you have the real and imaginary parts already computed. The output is stored as [..., 2] with the final dimension containing [real, imaginary] pairs.
The real and imaginary input tensors must have identical shapes. They are automatically broadcast if compatible, similar to arithmetic operations.
- Component stacking: Stacks real and imag along final dimension
- Cartesian form: Creates rectangular (real + imag) not polar form
- Shape matching: real and imag must have identical shapes
- Broadcasting: Compatible shapes are automatically broadcast
- Zero-copy-like: Uses view operations when possible for efficiency
- Inverse of view_as_real:
view_as_real(complex(r, i)) = [r, i]
- Shape requirement: real and imag must have identical shapes (or broadcast to same)
- Not view_as_complex: Different from
view_as_complex()which interprets [..., 2] as complex - Broadcasting scope: Broadcasting rules follow standard tensor rules
Parameters
realTensor- Tensor containing the real part of complex numbers. Shape: [...]
imagTensor- Tensor containing the imaginary part of complex numbers. Shape: [...], must match real
Returns
Tensor– Complex tensor with shape [..., 2] where: - Element [..., 0] = real[...] - Element [..., 1] = imag[...]Examples
// Create simple complex numbers
const real = torch.tensor([1.0, 2.0, 3.0]);
const imag = torch.tensor([4.0, 5.0, 6.0]);
const z = torch.complex(real, imag);
// z -> [[1, 4], [2, 5], [3, 6]] representing [1+4i, 2+5i, 3+6i]
// Matrix of complex numbers
const real_mat = torch.randn(3, 4);
const imag_mat = torch.randn(3, 4);
const z_mat = torch.complex(real_mat, imag_mat); // [3, 4, 2]
// Using zero imaginary part
const pure_real = torch.tensor([1.0, 2.0, 3.0]);
const zeros = torch.zeros(3);
const z_real = torch.complex(pure_real, zeros); // [1+0i, 2+0i, 3+0i]
// Using zero real part
const zeros2 = torch.zeros(5);
const pure_imag = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0]);
const z_imag = torch.complex(zeros2, pure_imag); // [0+1i, 0+2i, ...]
// Signal processing: result of some computation on real/imag separately
const signal_real = torch.randn(1024); // Real component
const signal_imag = torch.randn(1024); // Imaginary component
const signal = torch.complex(signal_real, signal_imag); // [1024, 2]
// Broadcasting (real is [3, 1], imag is [1, 4])
const real_col = torch.randn(3, 1); // [3, 1]
const imag_row = torch.randn(1, 4); // [1, 4]
const z_broadcast = torch.complex(real_col, imag_row); // [3, 4, 2] - broadcasted
// Batch of complex numbers
const batch_size = 32;
const dim = 10;
const batch_real = torch.randn(batch_size, dim);
const batch_imag = torch.randn(batch_size, dim);
const batch_z = torch.complex(batch_real, batch_imag); // [32, 10, 2]See Also
- PyTorch torch.complex()
- view_as_complex - Create complex by interpreting [..., 2] tensor
- polar - Create complex from magnitude and phase angle instead
- view_as_real - Extract real and imaginary components