📄 complex.cs
字号:
using System;
namespace Mymath
{
public struct Complex
{
/// <summary>
/// Real part of the complex number
/// </summary>
public Double Re;
/// <summary>
/// Imaginary part of the complex number
/// </summary>
public Double Im;
/// <summary>
/// Initializes a new instance of the Complex struct
/// </summary>
/// <param name="re">Real part</param>
/// <param name="im">Imaginary part</param>
public Complex(Double re, Double im)
{
this.Re = re;
this.Im = im;
}
/// <summary>
/// Initializes a new instance of the Complex struct by copy assignment
/// </summary>
/// <param name="rhs">The copy source</param>
public Complex(Complex rhs)
{
this.Re = rhs.Re;
this.Im = rhs.Im;
}
/// <summary>
/// Represents complex zero
/// </summary>
/// <remarks>Represents complex number with both real and imaginary parts equal to zero.</remarks>
public static Complex Zero
{
get { return new Complex(0, 0); }
}
/// <summary>
/// Magnitude value of the complex number
/// </summary>
public Double Magnitude
{
get { return System.Math.Sqrt(Re * Re + Im * Im); }
}
/// <summary>
/// Squared magnitude value of the complex number
/// </summary>
public Double SquaredMagnitude
{
get { return (Re * Re + Im * Im); }
}
/// <summary>
/// Phase value of the complex number
/// </summary>
public Double Phase
{
get { return System.Math.Atan(Im / Re); }
}
/// <summary>
/// Returns string representation of the complex number
/// </summary>
/// <returns>String representation of the complex number</returns>
public override string ToString()
{
return String.Format("{0}{1}{2}i", Re, ((Im < 0) ? '-' : '+'), Math.Abs(Im));
}
/// <summary>
/// Addition operator
/// </summary>
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
/// <returns>Result complex number</returns>
public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.Re + b.Re, a.Im + b.Im);
}
/// <summary>
/// Subtraction operator
/// </summary>
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
/// <returns>Result complex number</returns>
public static Complex operator -(Complex a, Complex b)
{
return new Complex(a.Re - b.Re, a.Im - b.Im);
}
/// <summary>
/// Multiplication operator
/// </summary>
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
/// <returns>Result complex number</returns>
public static Complex operator *(Complex a, Complex b)
{
return new Complex(
a.Re * b.Re - a.Im * b.Im,
a.Re * b.Im + a.Im * b.Re);
}
/// <summary>
/// Division operator
/// </summary>
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
/// <returns>Result complex number</returns>
public static Complex operator /(Complex a, Complex b)
{
double divider = b.Re * b.Re + b.Im * b.Im;
if (divider == 0)
throw new DivideByZeroException();
return new Complex(
(a.Re * b.Re + a.Im * b.Im) / divider,
(a.Im * b.Re - a.Re * b.Im) / divider);
}
/// <summary>
/// Conjugate operator
/// </summary>
/// <param name="a">Source complex operand</param>
/// <returns>Result complex conjugate</returns>
public static Complex operator ~(Complex a)
{
return new Complex(a.Re, -a.Im);
}
/// <summary>
/// Equation operator
/// </summary>
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
/// <returns>The two complex operands are equal or not</returns>
public static Boolean operator ==(Complex a, Complex b)
{
return a.Re == b.Re && a.Im == b.Im;
}
public static Boolean operator !=(Complex a, Complex b)
{
return a.Re != b.Re || a.Im != b.Im;
}
public override bool Equals(object obj)
{
return this == (Complex)obj;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -