Section 5.4. Complex Numbers


5.4. Complex Numbers

A long time ago, mathematicians were absorbed by the following equation:

x2 = -1

The reason for this is that any real number (positive or negative) multiplied by itself results in a positive number. How can you multiply any number with itself to get a negative number? No such real number exists. So in the eighteenth century, mathematicians invented something called an imaginary number i (or j, depending on what math book you are reading) such that:

Basically a new branch of mathematics was created around this special number (or concept), and now imaginary numbers are used in numerical and mathematical applications. Combining a real number with an imaginary number forms a single entity known as a complex number. A complex number is any ordered pair of floating point real numbers (x, y) denoted by x + yj where x is the real part and y is the imaginary part of a complex number.

It turns out that complex numbers are used a lot in everyday math, engineering, electronics, etc. Because it became clear that many researchers were reinventing this wheel quite often, complex numbers became a real Python data type long ago in version 1.4.

Here are some facts about Python's support of complex numbers:

  • Imaginary numbers by themselves are not supported in Python (they are paired with a real part of 0.0 to make a complex number)

  • Complex numbers are made up of real and imaginary parts

  • Syntax for a complex number: real+imagj

  • Both real and imaginary components are floating point values

  • Imaginary part is suffixed with letter "J" lowercase (j) or uppercase (J)

The following are examples of complex numbers:

64.375+1j  4.23-8.5j   0.23-8.55j  1.23e-045+6.7e+089j 6.23+1.5j  -1.23-875J 0+1j  9.80665-8.31441J   -.0224+0j


5.4.1. Complex Number Built-in Attributes

Complex numbers are one example of objects with data attributes (Section 4.1.1). The data attributes are the real and imaginary components of the complex number object they belong to. Complex numbers also have a method attribute that can be invoked, returning the complex conjugate of the object.

>>> aComplex = -8.333-1.47j >>> aComplex (-8.333-1.47j) >>> aComplex.real -8.333 >>> aComplex.imag -1.47 >>> aComplex.conjugate() (-8.333+1.47j)


Table 5.1 describes the attributes of complex numbers.

Table 5.1. Complex Number Attributes

Attribute

Description

num.real

Real component of complex number num

num.imag

Imaginary component of complex number num

num.conjugate()

Returns complex conjugate of num




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net