MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaLang DesignEmacsUnicode ♥

Python, Ruby, Perl: Complex Numbers

, , …,

Python

complex number ad thumbnail
A New Coordinate
System for Complex Numbers

Python supports complex numbers. A complete complex number can be written as complex(3,4) or 3 + 4j. A number with “j” appended ⁖ 4j is the same as complex(0,4).

# -*- coding: utf-8 -*-
# python

# a complex number
cc = complex(3, 4)

# alternative notation
cc2 = 3 + 4j                    # same as complex(3, 4)

# complex numbers are printed with a parenthesis
print(cc)                        # (3+4j)
print(cc2)                       # (3+4j)

print(cc==cc2)                   # True

print(cc.real)                    # 3.0
print(cc.imag)                    # 4.0

http://docs.python.org/lib/typesnumeric.html

Basic arithmetic operations.

# -*- coding: utf-8 -*-
# python

# length of a complex number. That is, Sqrt[ i^2 + j^2]
print( abs(complex(3, 4)) )                    # 5.0

# complex number addition. (same as vector addition)
print ( complex(2, 3) + complex(4, 5) ) # (6+8j)

# multiplication of complex numbers
print ( complex(1, 0) * complex(0, 1) ) # (1j)

# scalar multiplication. That is, scale it.
print ( complex(3, 4) * 2)     # (6+8j)

# adding a scalar adds to the real part
print ( complex(3, 4) + 1)      # (4+4j)

More more advanced operations, you'll need to use module “cmath”. Examples:

# -*- coding: utf-8 -*-
# python

import cmath

z1 = complex(0, 1)

# gets length
print( abs(z1) )                # 1.0

# gets the angle. return in radians, between  [-π, π]
print( cmath.phase(z1) )        # 1.57079632679

# get polar coordinates. Returns this form (length, angle).
print( cmath.polar(z1) )        # (1.0, 1.57079632679)

# polar to rectangular. Input is (length, ‹angle in radians›). Returns a complex number
z2 = cmath.rect(1, cmath.pi)
print(z2)                        # (-1+1.22464679915e-16j) really is just -1 + 0j

# constant π
print(cmath.pi)                 #  3.14159265359

# constant e
print(cmath.e)                 #  2.71828182846

http://docs.python.org/2/library/cmath.html

Ruby

In Ruby, Complex number is represented by the object “Complex”. Example:

# -*- coding: utf-8 -*-
# ruby

# a complex number
cc = Complex(3, 4)

# when printed, it's shown as (‹a›+‹b›i)
p cc                            # (3+4i)

# input comlex number in polar form. The input is (length, ‹angle in radians›)
p Complex.polar(1, Math::PI)    # ⇒ (-1.0+1.2246467991473532e-16i)

p cc.real                    # ⇒ 3.0
p cc.imag                    # ⇒ 4.0

Basic arithmetic operations.

# -*- coding: utf-8 -*-
# ruby

# length of a Complex number. That is, Sqrt[ i^2 + j^2]
p Complex(3, 4).abs                    # ⇒ 5.0

# Complex number addition. (same as vector addition)
p Complex(2, 3) + Complex(4, 5)        # ⇒ (6+8i)

# multiplication of complex numbers
p Complex(1, 0) * Complex(0, 1)        # ⇒ (0+1i)

# scalar multiplication. That is, scale it.
p Complex(3, 4) * 2             # ⇒ (6+8j)

# adding a scalar adds to the real part
p Complex(3, 4) + 1             # ⇒ (4+4i)

http://www.ruby-doc.org/core-1.9.3/Complex.html

More advanced operations. Examples:

# -*- coding: utf-8 -*-
# ruby

z1 = Complex(0, 1)

# get length
p z1.abs                        # ⇒ 1.0

# get the angle. return in radians
p z1.angle                      # ⇒ 1.5707963267948966

# get polar coordinates. Returns a array [length, angle].
p z1.polar                      # ⇒ [1, 1.5707963267948966]

# polar to rectangular. Input is (length, ‹angle in radians›). Returns a complex number
p Complex.polar(1, Math::PI)   # ⇒ (-1.0+1.2246467991473532e-16i) really is just (-1+0i)

# constant π
p Math::PI                       # ⇒ 3.141592653589793

# constant e
p Math::E                       # ⇒ 2.718281828459045

http://www.ruby-doc.org/core-1.9.3/Math.html

Perl

Perl doesn't support complex numbers. But there are packages for it. One of them is “Math::Complex”.

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 observe 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.

perldoc Math::Complex

blog comments powered by Disqus