New Version 5.1!

Try it for free with our fully functional 60-day trial version.

Download now!

QuickStart Samples

Complex Numbers QuickStart Sample (Visual Basic)

Illustrates hwo to work with complex numbers using the DoubleComplex structure in Visual Basic.

C# code F# code IronPython code Back to QuickStart Samples

' The DoubleComplex class resides in the Extreme.Mathematics namespace.
Imports Extreme.Mathematics

Namespace Extreme.Numerics.QuickStart.VB

    ' Illustrates the use of the DoubleComplex class in the
    ' Extreme Optimization Numerical Libraries for .NET.
    ' </summary>
    Module ComplexNumbers

        Sub Main()
            '
            ' DoubleComplex constants:
            '
            Console.WriteLine("DoubleComplex.Zero = {0}", DoubleComplex.Zero)
            Console.WriteLine("DoubleComplex.One = {0}", DoubleComplex.One)
            ' The imaginary unit is given by DoubleComplex.I:
            Console.WriteLine("DoubleComplex.I = {0}", DoubleComplex.I)
            Console.WriteLine()

            '
            ' Construct some complex numbers
            '
            ' Real and imaginary parts:
            '   a = 2 + 4i
            Dim a As DoubleComplex = New DoubleComplex(2, 4)
            Console.WriteLine("a = {0}", a)
            '   b = 1 - 3i
            Dim b As DoubleComplex = New DoubleComplex(1, -3)
            Console.WriteLine("b = {0}", b.ToString())
            ' From a real number:
            '   c = -3 + 0i
            Dim c As DoubleComplex = New DoubleComplex(-3)
            Console.WriteLine("c = {0}", c.ToString())
            ' Polar form:
            '   d = 2 (cos(Pi/3) + i sin(Pi/3))
            Dim d As DoubleComplex = DoubleComplex.FromPolar(2, Constants.Pi / 3)
            ' To print this number, use the overloaded ToString
            ' method and specify the format string for the real 
            ' and imaginary parts:
            Console.WriteLine("d = {0}", d.ToString("F4"))
            Console.WriteLine()

            '
            ' Parts of complex numbers
            '
            Console.WriteLine("Parts of a = {0}:", a)
            Console.WriteLine("Real part of a = {0}", a.Re)
            Console.WriteLine("Imaginary part of a = {0}", a.Im)
            Console.WriteLine("Modulus of a = {0}", a.Modulus)
            Console.WriteLine("Argument of a = {0}", a.Argument)
            Console.WriteLine()

            '
            ' Basic arithmetic:
            '
            Console.WriteLine("Basic arithmetic:")
            Dim e As DoubleComplex
            e = DoubleComplex.Negate(a)
            Console.WriteLine("-a = {0}", e)
            e = DoubleComplex.Add(a, b)
            Console.WriteLine("a + b = {0}", e)
            e = DoubleComplex.Subtract(a, b)
            Console.WriteLine("a - b = {0}", e)
            e = DoubleComplex.Multiply(a, b)
            Console.WriteLine("a * b = {0}", e)
            e = DoubleComplex.Divide(a, b)
            Console.WriteLine("a / b = {0}", e)
            'BUG e = DoubleComplex.Conjugate(a)
            e = a.Conjugate
            Console.WriteLine("Conjugate(a) = {0}", e)
            Console.WriteLine()

            '
            ' Functions of complex numbers
            '
            ' Most of these have corresponding Shared methods 
            ' in the System.Math class, but are extended to complex 
            ' arguments.
            Console.WriteLine("Functions of complex numbers:")

            ' Exponentials and logarithms
            Console.WriteLine("Exponentials and logarithms:")
            e = DoubleComplex.Exp(a)
            Console.WriteLine("Exp(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Log(a)
            Console.WriteLine("Log(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Log10(a)
            Console.WriteLine("Log10(a) = {0}", e.ToString("F4"))
            ' You can get a point on the unit circle by calling
            ' the ExpI method:
            e = DoubleComplex.ExpI(2 * Extreme.Mathematics.Constants.Pi / 3)
            Console.WriteLine("ExpI(2*Pi/3) = {0}", e.ToString("F4"))
            ' The RootOfUnity method also returns points on the
            ' unit circle. The above is equivalent to the second
            ' root of z^6 = 1:
            e = DoubleComplex.RootOfUnity(6, 2)
            Console.WriteLine("RootOfUnity(6, 2) = {0}", e.ToString("F4"))


            ' The Pow method is overloaded for integer, double,
            ' and complex argument:
            e = DoubleComplex.Pow(a, 3)
            Console.WriteLine("Pow(a,3) = {0}", e.ToString("F4"))
            e = DoubleComplex.Pow(a, 1.5)
            Console.WriteLine("Pow(a,1.5) = {0}", e.ToString("F4"))
            e = DoubleComplex.Pow(a, b)
            Console.WriteLine("Pow(a,b) = {0}", e.ToString("F4"))

            ' Square root
            e = DoubleComplex.Sqrt(a)
            Console.WriteLine("Sqrt(a) = {0}", e.ToString("F4"))
            ' The Sqrt method is overloaded. Here's the square 
            ' root of a negative double:
            e = DoubleComplex.Sqrt(-4)
            Console.WriteLine("Sqrt(-4) = {0}", e.ToString("F4"))
            Console.WriteLine()

            '
            ' Trigonometric functions:
            '
            Console.WriteLine("Trigonometric function:")
            e = DoubleComplex.Sin(a)
            Console.WriteLine("Sin(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Cos(a)
            Console.WriteLine("Cos(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Tan(a)
            Console.WriteLine("Tan(a) = {0}", e.ToString("F4"))

            ' GetInverse Trigonometric functions:
            e = DoubleComplex.Asin(a)
            Console.WriteLine("Asin(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Acos(a)
            Console.WriteLine("Acos(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Atan(a)
            Console.WriteLine("Atan(a) = {0}", e.ToString("F4"))

            ' Asin and Acos have overloads with real argument
            ' not restricted to [-1,1]:
            e = DoubleComplex.Asin(2)
            Console.WriteLine("Asin(2) = {0}", e.ToString("F4"))
            e = DoubleComplex.Acos(2)
            Console.WriteLine("Acos(2) = {0}", e.ToString("F4"))
            Console.WriteLine()

            '
            ' Hyperbolic and inverse hyperbolic functions:
            '
            Console.WriteLine("Hyperbolic function:")
            e = DoubleComplex.Sinh(a)
            Console.WriteLine("Sinh(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Cosh(a)
            Console.WriteLine("Cosh(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Tanh(a)
            Console.WriteLine("Tanh(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Asinh(a)
            Console.WriteLine("Asinh(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Acosh(a)
            Console.WriteLine("Acosh(a) = {0}", e.ToString("F4"))
            e = DoubleComplex.Atanh(a)
            Console.WriteLine("Atanh(a) = {0}", e.ToString("F4"))
            Console.WriteLine()

            Console.Write("Press Enter key to exit...")
            Console.ReadLine()
        End Sub

    End Module

End Namespace