New Version 5.0!

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

Download now!

QuickStart Samples

Complex Numbers QuickStart Sample (F#)

Illustrates hwo to work with complex numbers using the DoubleComplex structure in F#.

C# code Visual Basic code IronPython code Back to QuickStart Samples

// Illustrates the use of the DoubleComplex class in the
// Extreme Optimization Mathematics Library for .NET.

#light

open System

// The DoubleComplex class resides in the Extreme.Mathematics namespace.
open Extreme.Mathematics

//
// DoubleComplex constants:
//
printfn "DoubleComplex.Zero = %A" DoubleComplex.Zero
printfn "DoubleComplex.One = %A" DoubleComplex.One
// The imaginary unit is given by DoubleComplex.I:
printfn "DoubleComplex.I = %A" DoubleComplex.I
printfn ""

//
// Construct some complex numbers
//
// Real and imaginary parts:
//   a = 2 + 4i
let a = DoubleComplex(2.0, 4.0)
printfn "a = %A" a
//   b = 1 - 3i
let b = DoubleComplex(1.0, -3.0)
printfn "b = %A" b
// From a real number:
//   c = -3 + 0i
let c = DoubleComplex(-3.0)
printfn "c = %A" c
// Polar form:
//   d = 2 (cos(Pi/3) + i sin(Pi/3))
let d = DoubleComplex.FromPolar(2.0, Constants.Pi/3.0)
// To print this number, use the overloaded ToString
// method and specify the format let for the real 
// and imaginary parts:
printfn "d = %A" d
printfn ""

//
// Parts of complex numbers
//
printfn "Parts of a = %A:" a
printfn "Real part of a = %A" a.Re
printfn "Imaginary part of a = %A" a.Im
printfn "Modulus of a = %A" a.Modulus
printfn "Argument of a = %A" a.Argument
printfn ""

//
// Basic arithmetic:
//
printfn "Basic arithmetic:"
printfn "-a = %A" (-a)
printfn "a + b = %A" (a + b)
printfn "a - b = %A" (a - b)
printfn "a * b = %A" (a * b)
printfn "a / b = %A" (a / b)
// The conjugate of a complex number corresponds to
// the "Conjugate" method:
printfn "Conjugate(a) = ~a = %A" (a.Conjugate())
printfn ""

//
// Functions of complex numbers
//
// Most of these have corresponding static methods 
// in the System.Math class, but are extended to complex 
// arguments.
printfn "Functions of complex numbers:"

// Exponentials and logarithms
printfn "Exponentials and logarithms:"

printfn "Exp(a) = %A" (DoubleComplex.Exp(a))
printfn "Log(a) = %A" (DoubleComplex.Log(a))
printfn "Log10(a) = %A" (DoubleComplex.Log10(a))
// You can get a point on the unit circle by calling
// the ExpI method:
printfn "ExpI(2*Pi/3) = %A" (DoubleComplex.ExpI(2.0*Constants.Pi/3.0))
// The RootOfUnity method also returns points on the
// unit circle. The above is equivalent to the second
// root of z^6 = 1:
printfn "RootOfUnity(6, 2) = %A" (DoubleComplex.RootOfUnity(6, 2))


// The Pow method is overloaded for integer, double,
// and complex argument:
printfn "Pow(a,3) = %A" (DoubleComplex.Pow(a, 3))
printfn "Pow(a,1.5) = %A" (DoubleComplex.Pow(a, 1.5))
printfn "Pow(a,b) = %A" (DoubleComplex.Pow(a, b))

// Square root
printfn "Sqrt(a) = %A" (DoubleComplex.Sqrt(a))
// The Sqrt method is overloaded. Here's the square 
// root of a negative double:
printfn "Sqrt(-4) = %A" (DoubleComplex.Sqrt(-4.0))
printfn ""

//
// Trigonometric functions:
//
printfn "Trigonometric function:"
printfn "Sin(a) = %A" (DoubleComplex.Sin(a))
printfn "Cos(a) = %A" (DoubleComplex.Cos(a))
printfn "Tan(a) = %A" (DoubleComplex.Tan(a))

// Inverse Trigonometric functions:
printfn "Asin(a) = %A" (DoubleComplex.Asin(a))
printfn "Acos(a) = %A" (DoubleComplex.Acos(a))
printfn "Atan(a) = %A" (DoubleComplex.Atan(a))

// Asin and Acos have overloads with real argument
// not restricted to [-1,1]:
printfn "Asin(2) = %A" (DoubleComplex.Asin(2.0))
printfn "Acos(2) = %A" (DoubleComplex.Acos(2.0))
printfn ""

//
// Hyperbolic and inverse hyperbolic functions:
//
printfn "Hyperbolic function:"
printfn "Sinh(a) = %A" (DoubleComplex.Sinh(a))
printfn "Cosh(a) = %A" (DoubleComplex.Cosh(a))
printfn "Tanh(a) = %A" (DoubleComplex.Tanh(a))
printfn "Asinh(a) = %A" (DoubleComplex.Asinh(a))
printfn "Acosh(a) = %A" (DoubleComplex.Acosh(a))
printfn "Atanh(a) = %A" (DoubleComplex.Atanh(a))
printfn ""

printf "Press Enter key to exit..."
Console.ReadLine() |> ignore