Add your own alternative version
Stats
197.5K views 3.2K downloads 39 bookmarked
Posted
15 Dec 2002

Comments and Discussions



//======================================================================== // Copyright (c) 20052006, Boxing Wang. All rights reserved. //========================================================================
using System; using System.Diagnostics;
namespace Wbx.Math { [DebuggerDisplay("{Real} + {Imag}i")] public struct Complex { public double Real;
public double Imag;
[DebuggerStepThrough()] public Complex(double real, double imag) { this.Real = real; this.Imag = imag; }
[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)] public Complex Conjugate { [DebuggerStepThrough()] get { return new Complex(Real, Imag); } }
[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)] public double Norm { [DebuggerStepThrough()] get { return Real * Real + Imag * Imag; } }
[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)] public double Modulus { [DebuggerStepThrough()] get { return System.Math.Sqrt(Real * Real + Imag * Imag); } }
[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)] public double Argument { [DebuggerStepThrough()] get { return System.Math.Atan2(Imag, Real); } }
[DebuggerStepThrough()] public static Complex Polar(double modulus, double argument) { return new Complex( modulus * System.Math.Cos(argument), modulus * System.Math.Sin(argument)); }
public static Complex operator +(Complex z1, Complex z2) { return new Complex(z1.Real + z2.Real, z1.Imag + z2.Imag); }
public static Complex operator (Complex z1, Complex z2) { return new Complex(z1.Real  z2.Real, z1.Imag  z2.Imag); }
public static Complex operator *(Complex z1, Complex z2) { return new Complex( z1.Real * z2.Real  z1.Imag * z2.Imag, z1.Real * z2.Imag + z1.Imag * z2.Real); }
public static Complex operator /(Complex z1, Complex z2) { double value = z2.Real * z2.Real + z2.Imag * z2.Imag;
return new Complex( (z1.Real * z2.Real + z1.Imag * z2.Imag) / value, (z1.Imag * z2.Real  z1.Real * z2.Imag) / value); }
public static bool operator ==(Complex z1, Complex z2) { return (z1.Real == z2.Real && z1.Imag == z2.Imag); }
public static bool operator !=(Complex z1, Complex z2) { return (z1.Real != z2.Real  z1.Imag == z2.Imag); }
public static Complex Cos(Complex z) { Complex z1 = Exp(new Complex(z.Imag, z.Real)); Complex z2 = Exp(new Complex(z.Imag, z.Real));
return new Complex(0.5 * (z1.Real + z2.Real), 0.5 * (z1.Imag + z2.Imag)); }
public static Complex Cosh(Complex z) { Complex z1 = Exp(z); Complex z2 = Exp(new Complex(z.Real, z.Imag));
return new Complex(0.5 * (z1.Real + z2.Real), 0.5 * (z1.Imag + z2.Imag)); }
public static Complex Sin(Complex z) { Complex z1 = Exp(new Complex(z.Imag, z.Real)); Complex z2 = Exp(new Complex(z.Imag, z.Real));
return new Complex(0.5 * (z1.Imag  z2.Imag), 0.5 * (z2.Real  z1.Real)); }
public static Complex Sinh(Complex z) { Complex z1 = Exp(z); Complex z2 = Exp(new Complex(z.Real, z.Imag));
return new Complex(0.5 * (z1.Real  z2.Real), 0.5 * (z1.Imag  z2.Imag)); }
public static Complex Tan(Complex z) { return Sin(z) / Cos(z); }
public static Complex Tanh(Complex z) { return Sinh(z) / Cosh(z); }
public static Complex Exp(Complex z) { double value = System.Math.Exp(z.Real);
return new Complex( value * System.Math.Cos(z.Imag), value * System.Math.Sin(z.Imag)); }
public static Complex Log(Complex z) { return new Complex(System.Math.Log(z.Modulus), z.Argument); }
public static Complex Log10(Complex z) { const double log10 = 2.3025850929940459;
Complex value = Log(z);
value.Real /= log10; value.Imag /= log10;
return value; }
public static Complex Pow(Complex baseNumber, Complex index) { return Exp(index * Log(baseNumber)); }
public static Complex Sqrt(Complex z) { double value = System.Math.Sqrt(z.Real * z.Real + z.Imag * z.Imag) + z.Real;
return new Complex( System.Math.Sqrt(0.5 * value), System.Math.Sqrt(0.5 / value) * z.Imag); }
public override string ToString() { return (String.Format("{0} + {1}i", Real, Imag)); }
public override bool Equals(object obj) { return base.Equals(obj); }
public override int GetHashCode() { return Real.GetHashCode() ^ Imag.GetHashCode(); } } }





Very nice. Note the error in the != operator: should be:
public static bool operator !=(Complex z1, Complex z2) { return (z1.Real != z2.Real  z1.Imag != z2.Imag); }





Your sqrt function is also wrong, but thanks for the rest, proved very helpful
Correct sqrt:
public static Complex Sqrt(Complex z)
{
return Polar(System.Math.Sqrt(z.Modulus), z.Argument * 0.5);
}





You should either mark the Real and Imag fields as readonly, or make them public properties with private setters. You can append " : this()" to the constructor's signature to make C# allow property assignments in the constructor.







General News Suggestion Question Bug Answer Joke Praise Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

