Complex Numbers Library

Mathematical Software. Mathematical Research. Mathematical Education. Tvalx Products.

 

We offer for public mathematical programming and use the following library of complex numbers. The library (namespace) is written in C# MS Visual Studio 2005-2008 by Vitali Tihomirov in 2007.

namespace ComplexNumbers
{
public class Complex
{
public double Re;
public double Im;

public Complex() : this(0, 0)
{
}
public Complex(double real, double imaginary)
{
this.Re = real;
this.Im = imaginary;
}
public Complex(Complex c)
{
this.Re = c.Re;
this.Im = c.Im;
}
public Complex(ComplexPolar c)
{
this.Re = c.Modulus * Math.Cos(c.Argument);
this.Im = c.Modulus * Math.Sin(c.Argument);
}
public static bool operator ==(Complex a, Complex b)
{
return (a.Re == b.Re) && (a.Im == b.Im);
}
public static bool operator !=(Complex a, Complex b)
{
return (a.Re != b.Re) || (a.Im != b.Im);
}
public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.Re + b.Re, a.Im + b.Im);
}
public static Complex operator -(Complex a, Complex b)
{
return new Complex(a.Re - b.Re, a.Im - b.Im);
}
public static Complex operator *(Complex a, Complex b)
{
return new Complex(a.Re*b.Re - a.Im*b.Im, a.Im*b.Re + a.Re*b.Im);
}
public static Complex operator /(Complex a, Complex b)
{
return new Complex( (a.Re*b.Re + a.Im*b.Im) / (b.Re * b.Re + b.Im * b.Im), (a.Im*b.Re - a.Re*b.Im) / (b.Re * b.Re + b.Im * b.Im));
}
}

public class ComplexPolar
{
public double Modulus;
public double Argument;
public ComplexPolar(double modulus, double argument)
{
this.Modulus = modulus;
this.Argument = argument;
}
public ComplexPolar(ComplexPolar c)
{
this.Modulus = c.Modulus;
this.Argument = c.Argument;
}
public ComplexPolar(Complex c)
{
this.Modulus = Math.Sqrt(c.Re * c.Re + c.Im * c.Im);
this.Argument = Math.Atan(c.Im / c.Re); //NaN if c.Re=0
}
public static bool operator ==(ComplexPolar a, ComplexPolar b)
{
return (a.Modulus == b.Modulus) && (a.Argument == b.Argument);
}
public static bool operator !=(ComplexPolar a, ComplexPolar b)
{
return (a.Modulus != b.Modulus) || (a.Argument != b.Argument);
}
}
}

 

 

© 2008 Tvalx