#region Copyright © 2009 Jose Antonio De Santiago-Castillo.
//Copyright © 2009 Jose Antonio De Santiago-Castillo
//E-mail:JAntonioDeSantiago@gmail.com
//Web: www.DotNumerics.com
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace DotNumerics
{
//[DebuggerDisplay("{_real} + {_imaginary} i")]
///
/// Represents a Complex number.
///
public struct Complex: IFormattable
{
#region Fields
private double _real;
private double _imaginary;
#endregion
#region Constructor
///
/// Initializes a new instance of the Complex class.
///
/// The real part of the Complex number.
/// The imaginary part of the complex number.
[DebuggerStepThrough()]
public Complex(double real, double imaginary)
{
this._real = real;
this._imaginary = imaginary;
}
#endregion
#region Properties
///
/// Gets or sets the real value of the complex number.
///
public double Real
{
get { return _real; }
set { _real = value; }
}
///
/// Gets or sets the imaginary value of the complex number.
///
public double Imaginary
{
get { return _imaginary; }
set { _imaginary = value; }
}
#endregion
#region Methods
///
/// Returns the conjugate of this complex number.
///
[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)]
public Complex Conjugate
{
[DebuggerStepThrough()]
get { return new Complex(_real, -_imaginary); }
}
/////
/////
/////
//[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)]
//public double Norm
//{
// [DebuggerStepThrough()]
// get { return _real * _real + _imaginary * _imaginary; }
//}
///
/// Gets the modulus or absolute value of this complex number.
///
[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)]
public double Modulus
{
[DebuggerStepThrough()]
get { return System.Math.Sqrt(_real * _real + _imaginary * _imaginary); }
}
///
/// Gets or stes the argument of a this complex number.
///
[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)]
public double Argument
{
[DebuggerStepThrough()]
get { return System.Math.Atan2(_imaginary, _real); }
set
{
double modulus = Modulus;
this._real = Math.Cos(value) * modulus;
this._imaginary = Math.Sin(value) * modulus;
}
}
//[DebuggerStepThrough()]
//public static Complex Polar(double modulus, double argument)
//{
// return new Complex(
// modulus * System.Math.Cos(argument),
// modulus * System.Math.Sin(argument));
//}
#endregion
///
/// Complex addition.
///
/// The left side of the addition operator.
/// The right side matrix of the addition operator.
/// A value that represents the result of the addition.
public static Complex operator +(Complex z1, Complex z2)
{
return new Complex(z1._real + z2._real, z1._imaginary + z2._imaginary);
}
///
/// Unary addition.
///
/// The value.
/// The value.
public static Complex operator +(Complex c)
{
return c;
}
///
/// Unary minus.
///
/// The value
/// -value
public static Complex operator -(Complex c)
{
return new Complex(-c.Real, -c.Imaginary);
}
///
/// Complex subtraction.
///
/// The left side of the operator.
/// The right side of the operator.
/// A value that represents the result of the operation.
public static Complex operator -(Complex z1, Complex z2)
{
return new Complex(z1._real - z2._real, z1._imaginary - z2._imaginary);
}
///
/// Complex multiplication.
///
/// The left side of the operator.
/// The right side of the operator.
/// A value that represents the result of the operation.
public static Complex operator *(Complex z1, Complex z2)
{
return new Complex(
z1._real * z2._real - z1._imaginary * z2._imaginary,
z1._real * z2._imaginary + z1._imaginary * z2._real);
}
///
/// Complex multiplication.
///
/// The left side of the operator.
/// The right side of the operator.
/// A value that represents the result of the operation.
public static Complex operator *(double d1, Complex z2)
{
return new Complex(d1 * z2._real, d1 * z2._imaginary);
}
///
/// Complex multiplication.
///
/// The left side of the operator.
/// The right side of the operator.
/// A value that represents the result of the operation.
public static Complex operator *(Complex z1, double d2)
{
return d2 * z1;
}
///
/// Complex division.
///
/// The left side of the operator.
/// The right side of the operator.
/// A value that represents the result of the operation.
public static Complex operator /(Complex z1, Complex z2)
{
double value = z2._real * z2._real + z2._imaginary * z2._imaginary;
return new Complex(
(z1._real * z2._real + z1._imaginary * z2._imaginary) / value,
(z1._imaginary * z2._real - z1._real * z2._imaginary) / value);
}
///
/// Equality operator.
///
/// The left side of the operator.
/// The right side of the operator.
/// A value that represents the result of the operation.
public static bool operator ==(Complex z1, Complex z2)
{
return (z1._real == z2._real && z1._imaginary == z2._imaginary);
}
///
/// Inequality operator.
///
/// The left side of the operator.
/// The right side of the operator.
/// A value that represents the result of the operation.
public static bool operator !=(Complex z1, Complex z2)
{
return (z1._real != z2._real || z1._imaginary != z2._imaginary);
}
///
/// Indicates whether this instance and a specific object are equals.
///
/// Another object to compare to.
/// true if obj and this instance are the same type and represent the same value;
/// otherwise, false.
public override bool Equals(object obj)
{
return base.Equals(obj);
}
///
/// Returns the hash code for this instance.
///
/// A 32-bit signed integer that is the hash code for this instance.
public override int GetHashCode()
{
return _real.GetHashCode() ^ _imaginary.GetHashCode();
}
///
/// Converts this instance to its equivalent string representation.
///
/// The string representation.
public override string ToString()
{
//return (String.Format("{0} + {1}i", _real, _imaginary));
return (_imaginary >= 0) ? _real.ToString() + " +" + _imaginary.ToString() + " i" : _real.ToString() + " " + _imaginary.ToString() + " i";
}
///
/// Converts this instance to its equivalent string representation,
/// using the specified format.
///
/// A numeric format string.
/// The string representation of the value of this instance as specified by format.
public string ToString(string format)
{
//return (String.Format("{0} + {1}i", _real, _imaginary));
return (_imaginary >= 0) ? _real.ToString(format) + " +" + _imaginary.ToString(format) + " i" : _real.ToString(format) + " " + _imaginary.ToString(format) + " i";
}
#region IFormattable Members
///
/// Converts the numeric value of this instance to its equivalent string representation
/// using the specified format and culture-specific format information.
///
///
/// A numeric format string.
///
///
/// An System.IFormatProvider that supplies culture-specific formatting information.
///
///
/// The string representation of the value of this instance as specified by format and provider.
///
public string ToString(string format, IFormatProvider formatProvider)
{
string s = "";
if (_imaginary >= 0)
{
s = _real.ToString(format, formatProvider) + " +" + _imaginary.ToString(format, formatProvider) + " i";
}
else
{
s = _real.ToString(format, formatProvider) + " " + _imaginary.ToString(format, formatProvider) + " i";
}
return s;
}
#endregion
}
}