?? complex.cs
字號(hào):
/*
* 操作復(fù)數(shù)的類Complex
*
* 周長(zhǎng)發(fā)編制
*/
using System;
namespace CSharpAlgorithm.Algorithm
{
/**
* 操作復(fù)數(shù)的類Complex
* @author 周長(zhǎng)發(fā)
* @version 1.0
*/
public class Complex
{
private double real = 0.0; // 復(fù)數(shù)的實(shí)部
private double imaginary = 0.0; // 復(fù)數(shù)的虛部
private double eps = 0.0; // 缺省精度
/**
* 屬性: 實(shí)部
*/
public double Real
{
get
{
return real;
}
set
{
real = value;
}
}
/**
* 屬性: 虛部
*/
public double Imaginary
{
get
{
return imaginary;
}
set
{
imaginary = value;
}
}
/**
* 屬性: Eps
*/
public double Eps
{
get
{
return eps;
}
set
{
eps = value;
}
}
/**
* 基本構(gòu)造函數(shù)
*/
public Complex()
{
}
/**
* 指定值構(gòu)造函數(shù)
*
* @param dblX - 指定的實(shí)部
* @param dblY - 指定的虛部
*/
public Complex(double dblX, double dblY)
{
real = dblX;
imaginary = dblY;
}
/**
* 拷貝構(gòu)造函數(shù)
*
* @param other - 源復(fù)數(shù)
*/
public Complex(Complex other)
{
real = other.real;
imaginary = other.imaginary;
}
/**
* 根據(jù)"a,b"形式的字符串來構(gòu)造復(fù)數(shù),以a為復(fù)數(shù)的實(shí)部,b為復(fù)數(shù)的虛部
*
* @param s - "a,b"形式的字符串,a為復(fù)數(shù)的實(shí)部,b為復(fù)數(shù)的虛部
* @param sDelim - a, b之間的分隔符
*/
public Complex(string s, string sDelim)
{
SetValue(s, sDelim);
}
/**
* 設(shè)置復(fù)數(shù)運(yùn)算的精度
*
* @param newEps - 新的精度值
*/
public void SetEps(double newEps)
{
eps = newEps;
}
/**
* 取復(fù)數(shù)的精度值
*
* @return double型,復(fù)數(shù)的精度值
*/
public double GetEps()
{
return eps;
}
/**
* 指定復(fù)數(shù)的實(shí)部
*
* @param dblX - 復(fù)數(shù)的實(shí)部
*/
public void SetReal(double dblX)
{
real = dblX;
}
/**
* 指定復(fù)數(shù)的虛部
*
* @param dblY - 復(fù)數(shù)的虛部
*/
public void SetImag(double dblY)
{
imaginary = dblY;
}
/**
* 取復(fù)數(shù)的實(shí)部
*
* @return double 型,復(fù)數(shù)的實(shí)部
*/
public double GetReal()
{
return real;
}
/**
* 取復(fù)數(shù)的虛部
*
* @return double 型,復(fù)數(shù)的虛部
*/
public double GetImag()
{
return imaginary;
}
/**
* 指定復(fù)數(shù)的實(shí)部和虛部值
*
* @param real - 指定的實(shí)部
* @param imag - 指定的虛部
*/
public void SetValue(double real, double imag)
{
SetReal(real);
SetImag(imag);
}
/**
* 將"a,b"形式的字符串轉(zhuǎn)化為復(fù)數(shù),以a為復(fù)數(shù)的實(shí)部,b為復(fù)數(shù)的虛部
*
* @param s - "a,b"形式的字符串,a為復(fù)數(shù)的實(shí)部,b為復(fù)數(shù)的虛部
* @param sDelim - a, b之間的分隔符
*/
public void SetValue(string s, string sDelim)
{
int nPos = s.IndexOf(sDelim);
if (nPos == -1)
{
s = s.Trim();
real = Double.Parse(s);
imaginary = 0;
}
else
{
int nLen = s.Length;
string sLeft = s.Substring(0, nPos);
string sRight = s.Substring(nPos+1, nLen-nPos-1);
sLeft = sLeft.Trim();
sRight = sRight.Trim();
real = Double.Parse(sLeft);
imaginary = Double.Parse(sRight);
}
}
/**
* 重載 + 運(yùn)算符
*
* @return Complex對(duì)象
*/
public static Complex operator +(Complex cpx1, Complex cpx2)
{
return cpx1.Add(cpx2);
}
/**
* 重載 - 運(yùn)算符
*
* @return Complex對(duì)象
*/
public static Complex operator -(Complex cpx1, Complex cpx2)
{
return cpx1.Subtract(cpx2);
}
/**
* 重載 * 運(yùn)算符
*
* @return Complex對(duì)象
*/
public static Complex operator *(Complex cpx1, Complex cpx2)
{
return cpx1.Multiply(cpx2);
}
/**
* 重載 / 運(yùn)算符
*
* @return Complex對(duì)象
*/
public static Complex operator /(Complex cpx1, Complex cpx2)
{
return cpx1.Divide(cpx2);
}
/**
* 重載 double 運(yùn)算符
*
* @return double值
*/
public static implicit operator double(Complex cpx)
{
return cpx.Abs();
}
/**
* 將復(fù)數(shù)轉(zhuǎn)化為"a+bj"形式的字符串
*
* @return string 型,"a+bj"形式的字符串
*/
public override string ToString()
{
string s;
if (real != 0.0)
{
if (imaginary > 0)
s = real.ToString("F") + "+" + imaginary.ToString("F") + "j";
else if (imaginary < 0)
{
double absImag = -1*imaginary;
s = real.ToString("F") + "-" + absImag.ToString("F") + "j";
}
else
s = real.ToString("F");
}
else
{
if (imaginary > 0)
s = imaginary.ToString("F") + "j";
else if (imaginary < 0)
{
double absImag = -1*imaginary;
s = absImag.ToString("F") + "j";
}
else
s = real.ToString("F");
}
return s;
}
/**
* 比較兩個(gè)復(fù)數(shù)是否相等
*
* @param other - 用于比較的復(fù)數(shù)
* @return bool型,相等則為true,否則為false
*/
public override bool Equals(object other)
{
Complex cpxX = other as Complex;
if (cpxX == null)
return false;
return Math.Abs(real - cpxX.real) <= eps &&
Math.Abs(imaginary - cpxX.imaginary) <= eps;
}
/**
* 因?yàn)橹貙懥薊quals,因此必須重寫GetHashCode
*
* @return int型,返回復(fù)數(shù)對(duì)象散列碼
*/
public override int GetHashCode()
{
return (int)Math.Sqrt(real * real + imaginary * imaginary);
}
/**
* 給復(fù)數(shù)賦值
*
* @param cpxX - 用于給復(fù)數(shù)賦值的源復(fù)數(shù)
* @return Complex型,與cpxX相等的復(fù)數(shù)
*/
public Complex SetValue(Complex cpxX)
{
real = cpxX.real;
imaginary = cpxX.imaginary;
return this;
}
/**
* 實(shí)現(xiàn)復(fù)數(shù)的加法
*
* @param cpxX - 與指定復(fù)數(shù)相加的復(fù)數(shù)
* @return Complex型,指定復(fù)數(shù)與cpxX相加之和
*/
public Complex Add(Complex cpxX)
{
double x = real + cpxX.real;
double y = imaginary + cpxX.imaginary;
return new Complex(x, y);
}
/**
* 實(shí)現(xiàn)復(fù)數(shù)的減法
*
* @param cpxX - 與指定復(fù)數(shù)相減的復(fù)數(shù)
* @return Complex型,指定復(fù)數(shù)減去cpxX之差
*/
public Complex Subtract(Complex cpxX)
{
double x = real - cpxX.real;
double y = imaginary - cpxX.imaginary;
return new Complex(x, y);
}
/**
* 實(shí)現(xiàn)復(fù)數(shù)的乘法
*
* @param cpxX - 與指定復(fù)數(shù)相乘的復(fù)數(shù)
* @return Complex型,指定復(fù)數(shù)與cpxX相乘之積
*/
public Complex Multiply(Complex cpxX)
{
double x = real * cpxX.real - imaginary * cpxX.imaginary;
double y = real * cpxX.imaginary + imaginary * cpxX.real;
return new Complex(x, y);
}
/**
* 實(shí)現(xiàn)復(fù)數(shù)的除法
*
* @param cpxX - 與指定復(fù)數(shù)相除的復(fù)數(shù)
* @return Complex型,指定復(fù)數(shù)除與cpxX之商
*/
public Complex Divide(Complex cpxX)
{
double e, f, x, y;
if (Math.Abs(cpxX.real) >= Math.Abs(cpxX.imaginary))
{
e = cpxX.imaginary / cpxX.real;
f = cpxX.real + e * cpxX.imaginary;
x = (real + imaginary * e) / f;
y = (imaginary - real * e) / f;
}
else
{
e = cpxX.real / cpxX.imaginary;
f = cpxX.imaginary + e * cpxX.real;
x = (real * e + imaginary) / f;
y = (imaginary * e - real) / f;
}
return new Complex(x, y);
}
/**
* 計(jì)算復(fù)數(shù)的模
*
* @return double型,指定復(fù)數(shù)的模
*/
public double Abs()
{
// 求取實(shí)部和虛部的絕對(duì)值
double x = Math.Abs(real);
double y = Math.Abs(imaginary);
if (real == 0)
return y;
if (imaginary == 0)
return x;
// 計(jì)算模
if (x > y)
return (x * Math.Sqrt(1 + (y / x) * (y / x)));
return (y * Math.Sqrt(1 + (x / y) * (x / y)));
}
/**
* 計(jì)算復(fù)數(shù)的根
*
* @param n - 待求根的根次
* @param cpxR - Complex型數(shù)組,長(zhǎng)度為n,返回復(fù)數(shù)的所有根
*/
public void Root(int n, Complex[] cpxR)
{
if (n<1)
return;
double q = Math.Atan2(imaginary, real);
double r = Math.Sqrt(real*real + imaginary*imaginary);
if (r != 0)
{
r = (1.0/n)*Math.Log(r);
r = Math.Exp(r);
}
for (int k=0; k<=n-1; k++)
{
double t = (2.0*k*3.1415926+q)/n;
cpxR[k] = new Complex(r*Math.Cos(t), r*Math.Sin(t));
}
}
/**
* 計(jì)算復(fù)數(shù)的實(shí)冪指數(shù)
*
* @param dblW - 待求實(shí)冪指數(shù)的冪次
* @return Complex型,復(fù)數(shù)的實(shí)冪指數(shù)值
*/
public Complex Pow(double dblW)
{
// 常量
const double PI = 3.14159265358979;
// 局部變量
double r, t;
// 特殊值處理
if ((real == 0) && (imaginary == 0))
return new Complex(0, 0);
// 冪運(yùn)算公式中的三角函數(shù)運(yùn)算
if (real == 0)
{
if (imaginary > 0)
t = 1.5707963268;
else
t = -1.5707963268;
}
else
{
if (real > 0)
t = Math.Atan2(imaginary, real);
else
{
if (imaginary >= 0)
t = Math.Atan2(imaginary, real) + PI;
else
t = Math.Atan2(imaginary, real) - PI;
}
}
// 模的冪
r = Math.Exp(dblW * Math.Log(Math.Sqrt(real * real + imaginary * imaginary)));
// 復(fù)數(shù)的實(shí)冪指數(shù)
return new Complex(r * Math.Cos(dblW * t), r * Math.Sin(dblW * t));
}
/**
* 計(jì)算復(fù)數(shù)的復(fù)冪指數(shù)
*
* @param cpxW - 待求復(fù)冪指數(shù)的冪次
* @param n - 控制參數(shù),默認(rèn)值為0。當(dāng)n=0時(shí),求得的結(jié)果為復(fù)冪指數(shù)的主值
* @return Complex型,復(fù)數(shù)的復(fù)冪指數(shù)值
*/
public Complex Pow(Complex cpxW, int n)
{
// 常量
const double PI = 3.14159265358979;
// 局部變量
double r, s, u, v;
// 特殊值處理
if (real == 0)
{
if (imaginary == 0)
return new Complex(0, 0);
s = 1.5707963268 * (Math.Abs(imaginary) / imaginary + 4 * n);
}
else
{
s = 2 * PI * n + Math.Atan2(imaginary, real);
if (real < 0)
{
if (imaginary > 0)
s = s + PI;
else
s = s - PI;
}
}
// 求冪運(yùn)算公式
r = 0.5 * Math.Log(real * real + imaginary * imaginary);
v = cpxW.real * r + cpxW.imaginary * s;
u = Math.Exp(cpxW.real * r - cpxW.imaginary * s);
return new Complex(u * Math.Cos(v), u * Math.Sin(v));
}
/**
* 計(jì)算復(fù)數(shù)的自然對(duì)數(shù)
*
* @return Complex型,復(fù)數(shù)的自然對(duì)數(shù)值
*/
public Complex Log()
{
double p = Math.Log(Math.Sqrt(real*real + imaginary*imaginary));
return new Complex(p, Math.Atan2(imaginary, real));
}
/**
* 計(jì)算復(fù)數(shù)的正弦
*
* @return Complex型,復(fù)數(shù)的正弦值
*/
public Complex Sin()
{
int i;
double x, y, y1, br, b1, b2;
double[] c = new double[6];
// 切比雪夫公式的常數(shù)系數(shù)
c[0] = 1.13031820798497;
c[1] = 0.04433684984866;
c[2] = 0.00054292631191;
c[3] = 0.00000319843646;
c[4] = 0.00000001103607;
c[5] = 0.00000000002498;
y1 = Math.Exp(imaginary);
x = 0.5 * (y1 + 1 / y1);
br = 0;
if (Math.Abs(imaginary) >= 1)
y = 0.5 * (y1 - 1 / y1);
else
{
b1 = 0;
b2 = 0;
y1 = 2 * (2 * imaginary * imaginary - 1);
for (i = 5; i >=0; --i)
{
br = y1 * b1 - b2 - c[i];
if (i != 0)
{
b2 = b1;
b1 = br;
}
}
y = imaginary * (br - b1);
}
// 組合計(jì)算結(jié)果
x = x * Math.Sin(real);
y = y * Math.Cos(real);
return new Complex(x, y);
}
/**
* 計(jì)算復(fù)數(shù)的余弦
*
* @return Complex型,復(fù)數(shù)的余弦值
*/
public Complex Cos()
{
int i;
double x, y, y1, br, b1, b2;
double[] c = new double[6];
// 切比雪夫公式的常數(shù)系數(shù)
c[0] = 1.13031820798497;
c[1] = 0.04433684984866;
c[2] = 0.00054292631191;
c[3] = 0.00000319843646;
c[4] = 0.00000001103607;
c[5] = 0.00000000002498;
y1 = Math.Exp(imaginary);
x = 0.5 * (y1 + 1 / y1);
br = 0;
if (Math.Abs(imaginary) >= 1)
y = 0.5 * (y1 - 1 / y1);
else
{
b1 = 0;
b2 = 0;
y1 = 2 * (2 * imaginary * imaginary - 1);
for (i=5 ; i>=0; --i)
{
br = y1 * b1 - b2 - c[i];
if (i != 0)
{
b2 = b1;
b1 = br;
}
}
y = imaginary * (br - b1);
}
// 組合計(jì)算結(jié)果
x = x * Math.Cos(real);
y = -y * Math.Sin(real);
return new Complex(x, y);
}
/**
* 計(jì)算復(fù)數(shù)的正切
*
* @return Complex型,復(fù)數(shù)的正切值
*/
public Complex Tan()
{
return Sin().Divide(Cos());
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -