Add your own alternative version
Stats
201.7K 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.





Can this run with Windows Forms instead of console? I tried adding complex.dll as a reference (in Visual Studio 2005) but got the following error ar runtime:
LoaderLock was detected
DLL 'C:\Documents and Settings\Bill\My Documents\Visual Studio 2005\Projects\TestComplex\TestComplex\bin\Release\complex.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.





Bill,
I recompiled with VS 2005, and it fixes the problems.
The version on codeproject was compiled with the old VS 2002.
You might want to try the Nmath library. It is pure managd code and can be downloaded from nmath.sourceforge.net It has a good complex math library, see the file src\Complex.cs (MathNet.Numericsv0.3)
 modified at 10:18 Monday 2nd January, 2006





Looks very promising however:
I tried the test app in Visual Studio 2005 console mode and while no errors were generated it did not display the line of output at all.
Is it supposed to work with Visual Studio 2005?
Thanks
Bill.





Sorry this library has not been tested with visual studio 2005.
I will test and make a new version for 2005 if needed. VB now supports operator overloading so that should eliminate the need for the methods Add() Subract(), Multiply(), And Divide().
Thanks,
Karl





Thank you Karl, appreciated. I'm using C# and Windows Forms.
 Bill.





I have been writing NUMERICAL METHOD algorithm ( finding roots of polynomials ) and your dll saved my from rewriting program into C++
Gratz





You don't have to write this to allow vb to use this.






I'm having serious trouble getting the standard <complex> library to work in a c++.net DLL. I can't use the one here because speed is a priority in my
application. But any time I use #include <complex>, my code won't compile. I get errors like:
"wasteoftime error LNK2020: unresolved token (0A000006) _CxxThrowException"
and
"wasteoftime error LNK2020: unresolved token (0A000016) delete".
As a test I even started a new class library project, and pasted every bit of code from the download here into mine, and still got the same problem. (the original compiles fine)
Help!!
And make it simple please. I'm not a Programmer by trade. I'm just a Physicist.





First of all, it's managed C++. This could be written entirely using straight C#.
Secondly, why are you using std::complex? . You're instantiated the template using a double in the C++ section, so the C# and VB side doesn't benefit from the point of a template, which is to instantiate whatever type you want. Therefore, this supports the previous point that this should have been written in C# to begin with.
BTW, C++ doesn't inherently support complex types (last time I checked). This is provided by STL.
You might want to look at this link for some simplistic examples:
http://www.csharphelp.com/archives/archive135.html[^]
As this article discusses, you might also want to overload the equality operators so we can compare complex numbers.
Finally, as you say, there's a major performance hit using complex numbers with managed C++. If I were writing a complicated math function with lots of complex number calculations, I really wouldn't want to incur these performance hits.
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator.
sensitivity and ethnic diversity means celebrating difference, not hiding from it.  Christian Graus





Thanks for the comment.
You are right it could be in straight C# and that would be nice.
My goal was full functionality, which is where managed C++ is great. C# and VB get to use the STL indirectly to get full functionality. The tradeoff is losing some flexibility and performance in non trivial cases. For the easy cases I am NOT using STL!
I use STL to handle nontrivial cases letting it worry about significant bits, as needed. With time more functions could be converted away from the STL wrapper. At that point it would be an easy port to C# if desired.
I HAVE overloaded the equality and other operators. I was hoping for feedback on why I needed to create redundant functions like Equals() to make it work in VB. The overloaded stuff works great in C++ and C#, but not everything maps to VB.
Ok STL is not “built” into the language but any good C++ compiler has STL, hence you can do complex math with C++ and FORTRAN.
Karl





I HAVE overloaded the equality and other operators.
Oops, sorry!
Not to be nitpicky:
Ok STL is not “built” into the language but any good C++ compiler has STL, hence you can do complex math with C++ and FORTRAN.
Any good C++ compiler supports templates, which therefore allows you to use STL.
And not to be sarcastic:
I was hoping for feedback on why I needed to create redundant functions like Equals() to make it work in VB. The overloaded stuff works great in C++ and C#, but not everything maps to VB.
Another reason why VB is pathetic (Marc tones it down from a vast selection of words beginning with the letter 's').
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator. Sensitivity and ethnic diversity means celebrating difference, not hiding from it.  Christian Graus Every line of code is a liability  Taka Muraoka







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.

