完整代码下载https://download.csdn.net/download/u012737193/10359608
我们这里将复数类表示为complx = real + imag * i
那么要如何计算complex ^ n呢,这里的n是double类型,可以是任何数
首先进行如下转换
因此complex = r *(cos(mu)+ sin(mu)*i) = r*(e ^ (mu * i))(欧拉公式)
因此complex ^ n = (r^ n) * (e ^ (mu * n * i))
因此r2 = r ^ n ,mu2 = mu * n
real2 = r2 * cos(mu2) = (r ^ n) *cos(mu *n)
imag2 = r2 * cos(mu2) = (r ^ n) *sin(mu *n)
complex2 = complex ^ n = real2 + imag2 * i
至此得到结果complex类C#的实现如下
classcomplex
{
double imag_;
double real_;
double r()
{
return Math.Sqrt(Math.Pow(imag_, 2) + Math.Pow(real_, 2));
}
double mu()
{
returnMath.Atan2(imag_, real_);
}
public complex()
{
this.imag_ = 0;
this.real_ = 0;
}
public complex(double real,double imag)
{
this.imag_ = imag;
this.real_ = real;
}
publicdouble imag()
{
return imag_;
}
publicdouble real()
{
return real_;
}
publiccomplex Pow(double comp)
{
double ther = r();
double sita = mu();
double R = Math.Pow(ther, comp) * Math.Cos(comp * sita);
double I = Math.Pow(ther, comp) * Math.Sin(comp * sita);
real_ = R;
imag_ = I;
return this;
}
//^的重载
public static complex operator ^(complex z1, double z2)
{
return (z1.Pow(z2));
}
}