Introduction:
In this article, i am going to demonstrate how to create a Complex numbers using asp.net and C# 4.0.
Main:
Many engineering and mathematical problems require the
use of imaginary numbers, often represented in the form 4+3i, where i is the square
root of -1.
See this below simple examble,
Use the System.Numerics.Complex class to represent imaginary numbers.
Complex wraps both the real and imaginary parts using doubles as the underlying
format.
Complex a = new Complex(2, 1);
Complex b = new Complex(3, 2);
Console.WriteLine(“a = {0}”, a);
Console.WriteLine(“b = {0}”, b);
Console.WriteLine(“a + b = {0}”, a + b);
Console.WriteLine(“pow(a,2) = {0}”, Complex.Pow(a,2));
Console.WriteLine(“a / 0 = {0}”, a / Complex.Zero);
//this will assign -1 to the real part, and 0 to the imaginary part
Complex c = -1;
Console.WriteLine(“c = {0}”, c);
Console.WriteLine(“Sqrt(c) = {0}”, Complex.Sqrt(c));
This has the output
a = (2, 1)
b = (3, 2)
a + b = (5, 3)
pow(a,2) = (3, 4)
a / 0 = (NaN, NaN)
c = (-1, 0)
Sqrt(c) = (0, 1)
Complex a = new Complex(2, 1); Complex b = new Complex(3, 2); Console.WriteLine(“a = {0}”, a); Console.WriteLine(“b = {0}”, b); Console.WriteLine(“a + b = {0}”, a + b); Console.WriteLine(“pow(a,2) = {0}”, Complex.Pow(a,2)); Console.WriteLine(“a / 0 = {0}”, a / Complex.Zero); //this will assign -1 to the real part, and 0 to the imaginary part Complex c = -1; Console.WriteLine(“c = {0}”, c); Console.WriteLine(“Sqrt(c) = {0}”, Complex.Sqrt(c)); This has the output a = (2, 1) b = (3, 2) a + b = (5, 3) pow(a,2) = (3, 4) a / 0 = (NaN, NaN) c = (-1, 0) Sqrt(c) = (0, 1) |
How to format a Complex Number?
class ComplexFormatter : IFormatProvider, ICustomFormatter
{
//accepts two format specifiers: i and j
public string Format(string format, object arg, IFormatProvider
formatProvider)
{
if (arg is Complex)
{
Complex c = (Complex)arg;
if (format.Equals(“i”, StringComparison.OrdinalIgnoreCase))
{
return c.Real.ToString(“N2”) + “ + “ +
c.Imaginary.ToString(“N2”) + “i”;
}
else if (format.Equals(“j”,
StringComparison.OrdinalIgnoreCase))
{
return c.Real.ToString(“N2”) + “ + “ +
c.Imaginary.ToString(“N2”) + “j”;
}
else
{
return c.ToString(format, formatProvider);
}
}
else
{
if (arg is IFormattable)
{
return ((IFormattable)arg).ToString(format,
formatProvider);
}
else if (arg != null)
{
return arg.ToString();
}
else
{
return string.Empty;
}
}
}
public object GetFormat(Type formatType)
{
82 CHAPTER 5 Numbers
if (formatType == typeof(ICustomFormatter))
{
return this;
}
else
{
return
System.Threading.Thread.CurrentThread.
CurrentCulture.GetFormat(formatType);
}
}
}
Example usage:
Complex c = -1;
Console.WriteLine(“Sqrt(c) = {0}”,
string.Format(new ComplexFormatter(), “{0:i}”, Complex.Sqrt(c)));
This gives the output:
Sqrt(c) = 0.00 + 1.00i
class ComplexFormatter : IFormatProvider, ICustomFormatter { //accepts two format specifiers: i and j public string Format(string format, object arg, IFormatProvider formatProvider) { if (arg is Complex) { Complex c = (Complex)arg; if (format.Equals(“i”, StringComparison.OrdinalIgnoreCase)) { return c.Real.ToString(“N2”) + “ + “ + c.Imaginary.ToString(“N2”) + “i”; } else if (format.Equals(“j”, StringComparison.OrdinalIgnoreCase)) { return c.Real.ToString(“N2”) + “ + “ + c.Imaginary.ToString(“N2”) + “j”; } else { return c.ToString(format, formatProvider); } } else { if (arg is IFormattable) { return ((IFormattable)arg).ToString(format, formatProvider); } else if (arg != null) { return arg.ToString(); } else { return string.Empty; } } } public object GetFormat(Type formatType) { 82 CHAPTER 5 Numbers if (formatType == typeof(ICustomFormatter)) { return this; } else { return System.Threading.Thread.CurrentThread. CurrentCulture.GetFormat(formatType); } } } Example usage: Complex c = -1; Console.WriteLine(“Sqrt(c) = {0}”, string.Format(new ComplexFormatter(), “{0:i}”, Complex.Sqrt(c))); This gives the output: Sqrt(c) = 0.00 + 1.00i |
Conclusion:
Hope this helps,
Happy Coding.
great post as usual!