using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 数学文件库
{
class Complex
{
private double R;//实数部分
private double I;//虚数部分
//数学复函数对应的数据处理类
//构造函数
public Complex(double real,double image) {
this.R = real;
this.I = image;
}
//符号重载(+)
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.R + c2.R, c1.I + c2.I);
}
//符号重载(-)
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.R - c2.R, c1.I - c2.I);
}
//符号重载(*)
public static Complex operator *(Complex c1, Complex c2) {
double p = c1.R * c2.R;
double q = c1.I * c2.I;
double s = (c1.R + c1.I) * (c2.R + c2.I);
double u = p - q;
double v = s - p - q;
return (new Complex(u, v));
}
//符号重载(/)
public static Complex operator /(Complex c1,Complex c2)
{
double p = c1.R * c2.R;
double q = -c1.I * c2.I;
double s = (c1.R + c1.I) * (c2.R -c2.I);
double omega =c2.R * c2.R + c2.I * c2.I;
double u = (p - q) / omega;
double v = (s - p - q) / omega;
return (new Complex(u, v));
}
//重载符号 乘方
public static Complex operator ^(Complex c, int n)
{
double q = Math.Atan2(c.I, c.R);
double r =Math.Sqrt(c.R * c.R + c.I * c.I);
if (r + 1.0 != 1.0)
{
r = n * Math.Log(r);
r = Math.Exp(r);
}
return (new Complex(r * Math.Cos(n * q), r * Math.Sin(n * q)));
}
//定义二次方根:调用sqrt方法
public List<Complex> Sqrt()
{
return (this.Sqrt(2));
}
//定义函数 开方 :sqrt(C,n)
public List<Complex> Sqrt( int n)
{
if (n <= 0)
throw new Exception("开方次数为负数或0");
List<Complex> Container = new List<Complex>();
double q = Math.Atan2(this.I, this.R);
double r = Math.Sqrt(this.I * this .I + this .R * this.R);
if (r + 1.0 != 1.0)
{
r = (1.0 / n) * Math.Log(r);
r = Math.Exp(r);
}
double t;
for(int k = 0; k < n; k++)
{
t = (2.0 * k * Math.PI + q) / n;
Container.Add(new Complex(r * Math.Cos(t), r * Math.Sin(t)));
}
return Container;
}
//定义指数
public Complex Exp()
{
double r = Math.Exp(this.R);
return (new Complex(r * Math.Cos(this.I), r * Math.Sin(this.I)));
}
//定义对数
public Complex Log()
{
double p =Math. Log(Math. Sqrt(this.R * this.R + this.I * this.I));
return (new Complex(p, Math.Atan2(this.I, this.R)));
}
//定义正弦
public Complex Sin()
{
double p = Math.Exp(this.I);
double q = Math.Exp(-this.I);
double u = Math.Sin(this.R) * (p + q) / 2;
double v = Math.Cos(this.R) * (p - q) / 2;
return new Complex(u, v);
}
//定义余弦
public Complex Cos()
{
double p = Math.Exp(this.I);
double q = Math.Exp(-this.I);
double u =Math. Cos(this.R) * (p + q) / 2;
double v =- Math.Sin(this.R) * (p - q) / 2;
return new Complex(u, v);
}
//复数的显示
public override string ToString()
{
if (this.R == 0)
return string.Format("{0}i",this.I);
if (this.I == 0)
return this.R.ToString();
if(this.I>0)
return string.Format("{0}+{1}i", this.R, this.I);
else
return string.Format("{0}{1}i", this.R, this.I);
}
}
}
【工程C#】复数类与复数运算函数集
最新推荐文章于 2022-11-07 15:25:02 发布