Showing posts with label Python Basics. Show all posts
Showing posts with label Python Basics. Show all posts

Tuesday, March 01, 2011

Python Basics: Python Complex Numbers

Python supports Complex Numbers and are built-in to the language. You can create complex numbers either by specifying the number in (real + imagJ) form or using a built-in method complex(a, b).

Complex Numbers:
  1. For Electronic Engineers python uses j or J instead of an i for imaginary numbers.
  2. j or J means square root of -1.
  3. You can perform arithmetic operations on Complex Numbers.
  4. Both real and imagJ part are floating-point numbers.
  5. Complex Number has a nonzero real part.
  6. cmath library. (for your reference.)
(Post in your comments if i left out something important so i can add in here.)

How Complex Numbers Works.

  1 #!/usr/local/bin/python3.2
  2 # Filename: ComplexNum.py
  3
  4 # How Imaginary Number Arithmetic Works.
  5 print('1j + 1j :', 1j + 1j)
  6 print('3j - 1j :', 3j - 1j)
  7 print('2j * 1j :', 2j * 1j)
  8 print('2j / 1j :', 2j / 1j)
  9
 10 # complex(real, imagJ)
 11 print('complex(1, 1) :', complex(1, 1))
 12
 13 # Make Complex Numbers bit More Complex. Arithmetic on real and imagJ parts. :)
 14 print('3 * complex(2, 3) :', 3 * complex(2, 3))
 15 print('2j * complex(4, 6) :', 2j * complex(4, 6))
 16 print('(2 + 4j) * complex(4, 2) :', (2 + 4j) * complex(4, 2))
 17 print('(2 + 4j) * (4 + 2j) :', (2 + 4j) * (4 + 2j))
 18 print('complex(2, 4) * complex(4, 2) :', complex(2, 4) * complex(4, 2))

You can download the file here: ComplexNum.py

Output:

1j + 1j : 2j
3j - 1j : 2j
2j * 1j : (-2+0j)
2j / 1j : (2+0j)
complex(1, 1) : (1+1j)
3 * complex(2, 3) : (6+9j)
2j * complex(4, 6) : (-12+8j)
(2 + 4j) * complex(4, 2) : 20j
(2 + 4j) * (4 + 2j) : 20j
complex(2, 4) * complex(4, 2) : 20j

Explanation:
  1. Line #5 & #6 are pretty much self-explained.
  2. Line #7 2j * 1j = 2j^2. As we know that j here means square root of -1. Now j^2 equals -1. leads to the output of (-2 + 0j). As we know that a complex number has a nonzero real part.
  3. Line #8 2j / 1j = (2+0j). Complex Part division is a bit harder maybe or not. Because we need to multiply but top and bottom by what we call complex conjugate. This is what you get when you change the sign of the imaginary part but not the real part. 2j/1j * -1j/-1j, i.e, -2j^2/-1j^2. Minus signs cancels out we are left with (2 + 0j).
  4. Line #11 uses python interpreter built-in function complex(a, b). But the output has nothing much that i can explain.
I believe rest of it is more or less self -explainable. But if you think they need explanation post in your comments. :)

Hope this Helps! Please write your comments it will help me improve.

Monday, February 14, 2011

Python Basics: Built-in function abs usage

First of all Built-in functions are those functions that are available to us all the time without importing any python modules. Today we'll see how to use abs(x) function.
So, Let's get Started:
abs(x) function takes an argument that can be a integer or a floating point number. It returns you the absolute value of a given number. A argument can be complex number, in that case the magnitude is returned.
Simple Case:

Let, say if x is less than zero, i.e, x < 0, than -x is returned. Otherwise, if x is greater than or equal to zero, i.e, x >= 0, than x is returned.

Complex Case:

Let, say if x is complex, than in that case abs returns a square root of  x.imag**2 + x.real**2 (a.k.a magnitude) of a number.

We'll be discussing complex numbers in another post. But in any case the formula to compute them remains same.

Hope this Helps! Please write your comments it will help me improve.