完整代碼下載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));
}
}