please help me how to normalize a complex matrix by python?
We have to first define what you mean by normalize. Especially in terms of complex numbers, where there are multiple ways you can approach normalization.
For instance, we can approach it from a strictly cartesian method, treating real and imaginary values separately to apply a min-max feature scaling.
Or, the more natural approach, in my opinion, would be with a polar method, maintaining angles while scaling only the radius.
Here are both approaches in PyTorch, where we scale the values to -1 and 1 for the cartesian approach and a radius of 0 to 1 for the polar approach:
import torch
from copy import deepcopy
A = torch.randn((3,4), dtype = torch.cfloat)*10
B = deepcopy(A)
print(A)
def complex_norm_cart(x, lower, upper):
max_r = torch.max(x.real)
min_r = torch.min(x.real)
max_i = torch.max(x.imag)
min_i = torch.min(x.imag)
x.real = lower +(x.real - min_r)*(upper-lower)/(max_r-min_r)
x.imag = lower + (x.imag - min_i) * (upper - lower) / (max_i - min_i)
return x
print(complex_norm_cart(A, -1, 1))
def complex_norm_polar(x, upper):
rad = torch.abs(x)
max_rad = torch.max(rad)
norm_rad = rad*upper/max_rad
x_angle = x.angle()
return torch.view_as_complex(torch.stack([norm_rad*torch.cos(x_angle), norm_rad*torch.sin(x_angle)], dim=-1))
print(complex_norm_polar(B, 1))