CSharp - Essential Types Complex struct

Introduction

Complex struct represents complex numbers with real and imaginary components of type double.

Complex resides in the System.Numerics.dll assembly.

To use Complex, instantiate the struct, specifying the real and imaginary values:

var c1 = new Complex (2, 3.5);
var c2 = new Complex (3, 0);

The Complex struct exposes properties for the real and imaginary values, as well as the phase and magnitude:

Console.WriteLine (c1.Real);       // 2
Console.WriteLine (c1.Imaginary);  // 3.5
Console.WriteLine (c1.Phase);      // 1.05
Console.WriteLine (c1.Magnitude);  // 4.03

You can also construct a Complex number by specifying magnitude and phase:

Complex c3 = Complex.FromPolarCoordinates (1.3, 5);

The standard arithmetic operators are overloaded to work on Complex numbers:

Console.WriteLine (c1 + c2);    // (5, 3.5)
Console.WriteLine (c1 * c2);    // (6, 10.5)

The Complex struct exposes static methods for more advanced functions, including:

  • Trigonometric (Sin, Asin, Sinh, Tan, etc.)
  • Logarithms and exponentiations
  • Conjugate

Demo

using System;
using System.Numerics;
class MainClass/*from  www .j  a v a2  s . c  om*/
{
   public static void Main(string[] args)
   {
       var c1 = new Complex (2, 3.5);

        Console.WriteLine (c1.Real);       // 2
        Console.WriteLine (c1.Imaginary);  // 3.5
        Console.WriteLine (c1.Phase);      // 1.05
        Console.WriteLine (c1.Magnitude);  // 4.03


   }
}

Result