New Version 6.0!

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

Download now!

QuickStart Samples

Complex Numbers QuickStart Sample (IronPython)

Illustrates how to work with complex numbers using the DoubleComplex structure in IronPython.

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

import numerics

# The DoubleComplex class resides in the Extreme.Mathematics namespace.
from Extreme.Mathematics import *

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

#
# DoubleComplex constants:
#
print "DoubleComplex.Zero =", DoubleComplex.Zero
print "DoubleComplex.One =", DoubleComplex.One
# The imaginary unit is given by DoubleComplex.I:
print "DoubleComplex.I =", DoubleComplex.I
print 

#
# Construct some complex numbers
#
# Real and imaginary parts:
#   a = 2 + 4i
a = DoubleComplex(2, 4)
print "a =", a
#   b = 1 - 3i
b = DoubleComplex(1, -3)
print "b =", b.ToString()
# From a real number:
#   c = -3 + 0i
c = DoubleComplex(-3)
print "c =", c.ToString()
# Polar form:
#   d = 2 (cos(Pi/3) + i sin(Pi/3))
d = 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:
print "d =", d
print 

#
# Parts of complex numbers
#
print "Parts of a = {0}:", a
print "Real part of a =", a.Re
print "Imaginary part of a =", a.Im
print "Modulus of a =", a.Modulus
print "Argument of a =", a.Argument
print 

#
# Basic arithmetic:
#
print "Basic arithmetic:"
e = -a
print "-a =", e
e = a + b
print "a + b =", e
e = a - b
print "a - b =", e
e = a * b
print "a * b =", e
e = a / b
print "a / b =", e
# The conjugate of a complex number corresponds to
# the "Conjugate" method:
e = a.Conjugate()
print "Conjugate(a) = ~a =", e
print 

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

# Exponentials and logarithms
print "Exponentials and logarithms:"
e = DoubleComplex.Exp(a)
print "Exp(a) =", e
e = DoubleComplex.Log(a)
print "Log(a) =", e
e = DoubleComplex.Log10(a)
print "Log10(a) =", e
# You can get a point on the unit circle by calling
# the ExpI method:
e = DoubleComplex.ExpI(2*Constants.Pi/3)
print "ExpI(2*Pi/3) =", e
# 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)
print "RootOfUnity(6, 2) =", e


# The Pow method is overloaded for integer, double, # and complex argument:
e = DoubleComplex.Pow(a, 3)
print "Pow(a,3) =", e
e = DoubleComplex.Pow(a, 1.5)
print "Pow(a,1.5) =", e
e = DoubleComplex.Pow(a, b)
print "Pow(a,b) =", e

# Square root
e = DoubleComplex.Sqrt(a)
print "Sqrt(a) =", e
# The Sqrt method is overloaded. Here's the square 
# root of a negative double:
e = DoubleComplex.Sqrt(-4)
print "Sqrt(-4) =", e
print 

#
# Trigonometric functions:
#
print "Trigonometric function:"
e = DoubleComplex.Sin(a)
print "Sin(a) =", e
e = DoubleComplex.Cos(a)
print "Cos(a) =", e
e = DoubleComplex.Tan(a)
print "Tan(a) =", e

# Inverse Trigonometric functions:
e = DoubleComplex.Asin(a)
print "Asin(a) =", e
e = DoubleComplex.Acos(a)
print "Acos(a) =", e
e = DoubleComplex.Atan(a)
print "Atan(a) =", e

# Asin and Acos have overloads with real argument
# not restricted to [-1,1]:
e = DoubleComplex.Asin(2)
print "Asin(2) =", e
e = DoubleComplex.Acos(2)
print "Acos(2) =", e
print 
			
#
# Hyperbolic and inverse hyperbolic functions:
#
print "Hyperbolic function:"
e = DoubleComplex.Sinh(a)
print "Sinh(a) =", e
e = DoubleComplex.Cosh(a)
print "Cosh(a) =", e
e = DoubleComplex.Tanh(a)
print "Tanh(a) =", e
e = DoubleComplex.Asinh(a)
print "Asinh(a) =", e
e = DoubleComplex.Acosh(a)
print "Acosh(a) =", e
e = DoubleComplex.Atanh(a)
print "Atanh(a) =", e