<- previous index next ->
There may be times when you have to do numerical computation on complex values (scalars, vectors, or matrices). If you are programming in Fortran, no problem, the types complex and complex*16 or double complex are built in. In Ada 95, the full set of complex arithmetic and functions come with the compiler as packages. MatLab uses complex as needed, automatically. In other programming languages you need to know how to do complex computation and how to choose the appropriate numerical method. Background: A complex number is stored in the computer as an ordered pair of floating point numbers, the real part and the imaginary part. The floating point numbers may be single or double precision as needed by the application. It is suggested that you use double precision as the default. There are called Cartesian coordinates. Polar coordinates are seldom used for computation, yet, are usually made available by a conversion function to magnitude and angle. Note that numerical analyst call the angle by the name "argument". The naming convention depends on the programming language and personal (or customer) choice. Basic complex arithmetic is covered first and then complex functions: sin, cos, tan, sinh, cosh, tanh, exp, log, sqrt, pow are covered in the next lecture. The simplest need for complex numbers is solving for the roots of the polynomial equation x^2 + 1 = 0 . There must be exactly two roots and they are sqrt(-1) and -sqrt(-1) that are named "i" and "-i". The quadratic equation for finding roots of a second order polynomial should use the complex sqrt, even for real coefficients a, b, and c, because the roots may be complex. given: a x^2 + b x + c = 0 find the roots b +/- sqrt(b^2 - 4 a c) solution x = ----------------------- 2 a that computes complex roots if 4 a c > b^2 Of course, the equation correctly computes the roots when a, b, and c are complex numbers. The basic complex arithmetic (including some functions for the next lecture) are in Complex.java The automatically generated documentation is Complex.html The basic complex arithmetic in C uses simple named functions as shown in the header file complex.h and the body complex.c with a test program test_complex.c The built in Ada package generic_complex_types.ads provides complex arithmetic. Note the many operator definitions. The use of complex in Ada is shown in this small program: complx.adb The use of complex in Fortran 95 is shown in this small program: complx.f90 C++ has the STL class Complex and can be used as shown test_complex.cpp Complex numbers define a plane and are typically Cartesian coordinates. Polar coordinates also define a plane in terms of radius, r and angle θ. x = r * cos(θ) r = sqrt(x*x+y*y) y = r * sin(θ) θ = arctan(y/x) or atan2 Other coordinate systems are Cylindrical coordinates in terms of radius r, angle θ and height z. x = r * cos(θ) r = sqrt(x*x+y*y) y = r * sin(θ) θ = arctan(y/x) or atan2 z = z z = z Spherical coordinates in terms of radius r, angles θ and φ x = r * sin(φ) * cos(θ) r = sqrt(x*x+y*y+z*z) y = r * sin(φ) * sin(θ) θ = arctan(y/x) or atan2 z = r * cos(φ) φ = arctan(sqrt(x*x+y*y)/z) A simple implementation in C is demonstrated in coordinate.c coordinate.out Beware of your choice of angle ranges when converting the above radians to degrees.Cartesian Cylindrical Spherical
<- previous index next ->