Complex Numbers in Python and Perl

Xah Lee, 2005-01-08.

Python

Python supports complex numbers. Append a “j” to a number and it represents the imaginary number. For example, 3j means 3 ⅈ. The full form (3,4) can be written as 3+4j. Another way to write it is complex(3,4). Arithmetic operations can be applied normally. To get the real part, imaginary part, or length, one can do:

# Python
a=complex(3,4)
print a.real
print a.imag
print abs(a)
print complex(3,4)+5+6j

Reference: Python Doc↗.

Perl

Perl doesn't support complex numbers. But there are packages that supports it. For example, Math::Complex. (http://search.cpan.org/~nwclark/perl/lib/Math/Complex.pm, 2006-08-30, v1.35)

Here's a excerpt from its documentation:

SYNOPSIS
            use Math::Complex;

            $z = Math::Complex->make(5, 6);
            $t = 4 - 3*i + $z;
            $j = cplxe(1, 2*pi/3);

DESCRIPTION
    This package lets you create and manipulate complex numbers. By default,
    *Perl* limits itself to real numbers, but an extra "use" statement
    brings full complex support, along with a full set of mathematical
    functions typically associated with and/or extended to complex numbers.

    If you wonder what complex numbers are, they were invented to be able to
    solve the following equation:

            x*x = -1

    ...

From this, we can garther some observations about the general Perl programer's understanding of mathematics, and also this package's author's understanding of it. And, as well as the fanaticality and maturity from the writing style.

Reference: perldoc Math::Complex↗.


See also:


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet