Complex Numbers in IronPython QuickStart Sample
Illustrates how to work with complex numbers using the DoubleComplex structure in IronPython.
View this sample in: C# Visual Basic F#
```Python import numerics # The Complex<double> class resides in the Extreme.Mathematics namespace. from Extreme.Mathematics import * # Illustrates the use of the Complex<double> class in # Numerics.NET. # # Complex<double> constants: # print "Complex<double>.Zero =", Complex<double>.Zero print "Complex<double>.One =", Complex<double>.One # The imaginary unit is given by Complex<double>.I: print "Complex<double>.I =", Complex<double>.I print # # Construct some complex numbers # # Real and imaginary parts: # a = 2 + 4i a = Complex<double>(2, 4) print "a =", a # b = 1 - 3i b = Complex<double>(1, -3) print "b =", b.ToString() # From a real number: # c = -3 + 0i c = Complex<double>(-3) print "c =", c.ToString() # Polar form: # d = 2 (cos(Pi/3) + i sin(Pi/3)) d = Complex<double>.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 = Complex<double>.Exp(a) print "Exp(a) =", e e = Complex<double>.Log(a) print "Log(a) =", e e = Complex<double>.Log10(a) print "Log10(a) =", e # You can get a point on the unit circle by calling # the ExpI method: e = Complex<double>.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 = Complex<double>.RootOfUnity(6, 2) print "RootOfUnity(6, 2) =", e # The Pow method is overloaded for integer, double, # and complex argument: e = Complex<double>.Pow(a, 3) print "Pow(a,3) =", e e = Complex<double>.Pow(a, 1.5) print "Pow(a,1.5) =", e e = Complex<double>.Pow(a, b) print "Pow(a,b) =", e # Square root e = Complex<double>.Sqrt(a) print "Sqrt(a) =", e # The Sqrt method is overloaded. Here's the square # root of a negative double: e = Complex<double>.Sqrt(-4) print "Sqrt(-4) =", e print # # Trigonometric functions: # print "Trigonometric function:" e = Complex<double>.Sin(a) print "Sin(a) =", e e = Complex<double>.Cos(a) print "Cos(a) =", e e = Complex<double>.Tan(a) print "Tan(a) =", e # Inverse Trigonometric functions: e = Complex<double>.Asin(a) print "Asin(a) =", e e = Complex<double>.Acos(a) print "Acos(a) =", e e = Complex<double>.Atan(a) print "Atan(a) =", e # Asin and Acos have overloads with real argument # not restricted to [-1,1]: e = Complex<double>.Asin(2) print "Asin(2) =", e e = Complex<double>.Acos(2) print "Acos(2) =", e print # # Hyperbolic and inverse hyperbolic functions: # print "Hyperbolic function:" e = Complex<double>.Sinh(a) print "Sinh(a) =", e e = Complex<double>.Cosh(a) print "Cosh(a) =", e e = Complex<double>.Tanh(a) print "Tanh(a) =", e e = Complex<double>.Asinh(a) print "Asinh(a) =", e e = Complex<double>.Acosh(a) print "Acosh(a) =", e e = Complex<double>.Atanh(a) print "Atanh(a) =", e ```