Complex Operations¶
spectrans.utils.complex ¶
Complex tensor operations for spectral transformations.
This module provides specialized complex number operations for PyTorch tensors, designed for spectral transformer implementations. The functions provide consistent error handling, mathematical safety for edge cases, and uniform interfaces for complex tensor operations in spectral transforms.
All functions support batch operations and proper broadcasting.
Functions:
| Name | Description |
|---|---|
complex_multiply |
Multiply two complex tensors element-wise. |
complex_conjugate |
Compute complex conjugate of input tensor. |
complex_modulus |
Compute magnitude (absolute value) of complex tensor. |
complex_phase |
Compute phase angle of complex tensor. |
complex_polar |
Construct complex tensor from magnitude and phase. |
complex_exp |
Compute complex exponential e^x. |
complex_log |
Compute complex natural logarithm. |
complex_divide |
Divide two complex tensors element-wise. |
make_complex |
Construct complex tensor from real and imaginary parts. |
split_complex |
Split complex tensor into real and imaginary parts. |
complex_relu |
Apply ReLU activation to complex tensor. |
complex_dropout |
Apply dropout to complex tensor. |
Functions¶
complex_multiply ¶
Multiply two complex tensors element-wise.
Performs (a_real + ia_imag) * (b_real + ib_imag) efficiently. Supports broadcasting according to PyTorch broadcasting rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Tensor
|
First complex tensor. |
required |
b
|
Tensor
|
Second complex tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex product tensor. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not complex tensors. |
RuntimeError
|
If tensors cannot be broadcast together. |
Source code in spectrans/utils/complex.py
complex_conjugate ¶
Compute complex conjugate of input tensor.
Essential operation for spectral transforms, particularly for ensuring Hermitian symmetry in frequency domain operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input complex tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex conjugate tensor. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a complex tensor. |
Source code in spectrans/utils/complex.py
complex_modulus ¶
Compute magnitude (absolute value) of complex tensor.
Critical for spectral analysis where magnitude represents signal energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input complex tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Real tensor containing magnitudes. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a complex tensor. |
Source code in spectrans/utils/complex.py
complex_phase ¶
Compute phase angle of complex tensor.
Phase information is crucial for spectral transformations and filter design.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input complex tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Real tensor containing phase angles in radians [-π, π]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a complex tensor. |
Source code in spectrans/utils/complex.py
complex_polar ¶
Construct complex tensor from magnitude and phase.
Fundamental for spectral operations where separate magnitude and phase processing is required. Includes validation for non-negative magnitudes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
magnitude
|
Tensor
|
Real tensor containing magnitudes (must be non-negative). |
required |
phase
|
Tensor
|
Real tensor containing phase angles in radians. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor constructed from polar coordinates. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not real tensors. |
ValueError
|
If magnitude contains negative values. |
RuntimeError
|
If tensors cannot be broadcast together. |
Source code in spectrans/utils/complex.py
complex_exp ¶
Compute complex exponential e^x.
Core operation for Fourier transforms and oscillatory functions. Accepts both real and complex inputs for flexibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor (can be real or complex). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex exponential tensor. |
Source code in spectrans/utils/complex.py
complex_log ¶
Compute complex natural logarithm.
Used in spectral domain operations and inverse transforms. Includes safety check for zeros where logarithm is undefined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input complex tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex logarithm tensor. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a complex tensor. |
ValueError
|
If input contains zeros (logarithm undefined). |
Source code in spectrans/utils/complex.py
complex_divide ¶
Divide two complex tensors element-wise.
Essential for spectral filtering operations. Includes safety checks for division by zero, which can occur in spectral nulls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Tensor
|
Numerator complex tensor. |
required |
b
|
Tensor
|
Denominator complex tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex division result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not complex tensors. |
ValueError
|
If denominator contains zeros. |
RuntimeError
|
If tensors cannot be broadcast together. |
Source code in spectrans/utils/complex.py
make_complex ¶
Construct complex tensor from real and imaginary parts.
Fundamental constructor for complex tensors in spectral transforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
real
|
Tensor
|
Real part tensor. |
required |
imag
|
Tensor
|
Imaginary part tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not real tensors. |
RuntimeError
|
If tensors cannot be broadcast together. |
Source code in spectrans/utils/complex.py
split_complex ¶
Split complex tensor into real and imaginary parts.
Useful for separate processing of real and imaginary components in spectral neural networks and filter implementations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input complex tensor. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of (real_part, imaginary_part) tensors. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a complex tensor. |
Source code in spectrans/utils/complex.py
complex_relu ¶
Apply ReLU activation to complex tensor.
Applies ReLU to both real and imaginary parts independently. Note: This is not holomorphic but useful for some neural architectures.
This specialized activation is designed for complex-valued neural networks in spectral transformers where non-linearity is needed in both components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input complex tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor with ReLU applied to each part. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a complex tensor. |
Source code in spectrans/utils/complex.py
complex_dropout ¶
Apply dropout to complex tensor.
Applies dropout to magnitude while preserving phase relationships. This is superior to independent real/imaginary dropout for spectral data.
This specialized dropout maintains the complex structure essential for spectral transformations while providing regularization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input complex tensor. |
required |
p
|
float
|
Dropout probability. |
0.5
|
training
|
bool
|
Whether in training mode. |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor with dropout applied. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a complex tensor. |
ValueError
|
If dropout probability is not in [0, 1]. |