#!/usr/bin/env python

class Fibo:
	"Class-based Fibonacci recursion"
	def __init__(self, n):
		if n == 1:
			self.value = 1
		elif n == 2:
			self.value = 1
		else:
			A = Fibo(n-1)
			B = Fibo(n-2)
			self.value = A.value + B.value
	def __repr__(self):
		return str(self.value)

if __name__ == "__main__":
	print Fibo(1)
	print Fibo(2)
	print Fibo(3)
	print Fibo(4)
	print Fibo(5)
	print Fibo(6)
	print Fibo(7)
	print Fibo(8)
	print Fibo(9)
	print Fibo(10)
Comments Off on Fibonacci Recursion implemented by Python class

Let’s check CRS (Compressed Row Storage) in Python NumPy.

[yuy121@cyberstar: ~]# module list
Currently Loaded Modulefiles:
  1) realvnc/4.5r21561   2) java/1.6.0_20    3) intel/11.1.073    4) pgi/10.9

Load Python module.

[yuy121@cyberstar: ~]# module load python
[yuy121@cyberstar: ~]# module list
Currently Loaded Modulefiles:
  1) realvnc/4.5r21561   6) gmp/5.0.1          11) gcc/4.5.0
  2) java/1.6.0_20       7) mpfr/2.4.2         12) openmpi/gnu/1.4.2
  3) intel/11.1.073      8) ppl/0.10.2         13) python/2.6.5
  4) pgi/10.9            9) cloog-ppl/0.15.9
  5) mkl/10.2.4.032     10) mpc/0.8.1
[yuy121@cyberstar: ~]# python
Python 2.6.5 (r265:79063, Jul 14 2011, 09:22:34) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Check CSR format in NumPy.

\mathbf{A}=\begin{bmatrix}1 & 1 & 2\\ 3 & 4 & 0 \\ 5 & 0 & 7 \\ 0 & 0 & 0\end{bmatrix}
value 1 1 2 3 4 5 7
col_ind 0 1 2 0 1 0 2
row_ptr 0 3 5 7 7
Python 2.6.5 (r265:79063, Jul 14 2011, 09:22:34) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import array, matrix
>>> from scipy.sparse import csr_matrix
>>> A=[[1, 1, 2], [3, 4, 0], [5, 0, 7], [0, 0, 0]]
>>> c = csr_matrix(A)
>>> c
<4x3 sparse matrix of type '<type 'numpy.int64'>'
	with 7 stored elements in Compressed Sparse Row format>
>>> c.__dict__
{'format': 'csr',
 '_shape': (4, 3),
 'indptr': array([0, 3, 5, 7, 7], dtype=int32),
 'indices': array([0, 1, 2, 0, 1, 0, 2], dtype=int32),
 'maxprint': 50,
 'data': array([1, 1, 2, 3, 4, 5, 7])}
>>> c.data
array([1, 1, 2, 3, 4, 5, 7])
>>> c.indices
array([0, 1, 2, 0, 1, 0, 2], dtype=int32)
>>> c.indptr
array([0, 3, 5, 7, 7], dtype=int32)
Comments Off on Compressed Row Storage Format in Python