torch.is_complex
function is_complex(input: TensorClass): booleanCheck if tensor has a complex numeric dtype.
Returns whether the tensor's data type is a complex number type (e.g., complex64 or complex128). Complex tensors represent numbers with real and imaginary parts. Useful for signal processing, FFT operations, and numerical algorithms that work with complex-valued data.
Complex dtypes in PyTorch:
complex64: Complex number with 32-bit (float32) real and imaginary partscomplex128: Complex number with 64-bit (float64) real and imaginary parts
Current torch.js support: torch.js supports complex64 dtype. This function returns true for tensors with complex dtype. Complex support is still early - some operations may not be fully implemented yet.
When complex numbers are needed:
- FFT (Fast Fourier Transform) operations
- Signal processing and filtering
- Quantum computing simulations
- Numerical methods (eigenvalue decomposition, etc.)
- Fourier analysis and spectral methods
Using complex numbers in torch.js:
- Create complex tensors with dtype: 'complex64'
- Access real and imaginary parts via tensor.real and tensor.imag
- Many operations support complex tensors; check documentation for specifics
- complex64 supported: torch.js supports complex64 dtype with float32 real/imaginary parts
- Early support: Complex dtype support is still evolving; some operations may have limited support
- Related check: Use
is_floating_pointto check for float dtypes - Components: Access real and imaginary parts via tensor.real and tensor.imag
Parameters
inputTensorClass- Tensor to check for complex dtype
Returns
boolean– True if tensor dtype is a complex type (complex64), false otherwiseExamples
// Check if a tensor has complex dtype
const real = torch.randn(4, 4); // float32 tensor
torch.is_complex(real); // false
// Create a complex tensor
const complex = torch.tensor([[1, 2], [3, 4]], { dtype: 'complex64' });
torch.is_complex(complex); // true// Type guard for complex operations
function processComplexTensor(x: torch.Tensor) {
if (torch.is_complex(x)) {
// Handle complex operations
return x.real.sum().add(x.imag.sum());
} else if (torch.is_floating_point(x)) {
return x.sum();
} else {
return x.float().sum();
}
}See Also
- PyTorch torch.is_complex()
- is_floating_point - Check if tensor is floating point
- torch.is_tensor - Check if object is a tensor
- torch.fft - FFT module (real FFT only)
- torch.core - Core utilities