Thursday, May 5, 2011

QR decomposition with numpy

We will see how to compute the QR decomposition of a matrix A and how to use Q and R to solve the linear equation system Ax=b using the from described here.
from numpy import *

A = floor(random.rand(4,4)*20-10) # random matrix

Q,R = linalg.qr(A) # qr decomposition of A

b = floor(random.rand(4,1)*20-10)
# solve Ax = b using the standard numpy function
x = linalg.solve(A,b)

# solve Ax = b using Q and R
y = dot(Q.T,b)
xQR = linalg.solve(R,y) 

print "\nSolution compared"
print x.T,'Ax=b'
print xQR.T,'Rx=y'
And now we can compare the solutions obtained
Solution compared
[[ 0.69207502  0.05565638  0.68965517 -0.2183908 ]] Ax=b
[[ 0.69207502  0.05565638  0.68965517 -0.2183908 ]] Rx=y

Tuesday, May 3, 2011

How to synchronize threads using locks

In this post we will extend a previous example about multithreading. Here's how to synchronize two threads using a simple lock:
import threading
import thread
import time

class MyThread(threading.Thread):
 def __init__(self, myName, lock):
  threading.Thread.__init__(self)
  self.myName = myName
  self.lock = lock

 def run(self):
  while True:
   self.lock.acquire()
   print self.myName,'is in the critical section the lock'
   time.sleep(1) # wait 1 second
   print self.myName,'releasing the lock'
   self.lock.release()
   

if __name__=="__main__":
 lock=thread.allocate_lock()
 thread1 = MyThread("1",lock)
 thread1.start()
 thread2 = MyThread("2",lock)
 thread2.start()
 while True: pass
A thread can't print in the console until he acquires the lock. The output will be similar to this:
1 is in the critical section the lock
1 releasing the lock
2 is in the critical section the lock
2 releasing the lock
1 is in the critical section the lock
1 releasing the lock
2 is in the critical section the lock
2 releasing the lock
2 is in the critical section the lock
2 releasing the lock
1 is in the critical section the lock
1 releasing the lock
2 is in the critical section the lock
2 releasing the lock
...
WARNING: This is a simple example with two threads and only one critical section, more complicated situation need other synchronization mechanisms.

Monday, May 2, 2011

How to create a chart with Google Chart API

The example shows how to create a scatter plot using the Google Chart API.
import random
import urllib

def list2String(x):
 """ from a list like [1,2,5]
     return a string like '1,2,5' """
 data = ""
 for i in x:
  data += str(i)+","
 return data[0:len(data)-1]

def makeChart(x,y,filename):
 query_url = "http://chart.apis.google.com/chart?chxt=x,y&chs=300x200&cht=s&chd=t:"
 query_url += list2String(x)+"|"+list2String(y)
 chart = urllib.urlopen(query_url) # retrieve the chart
 print "saving",query_url
 f = open(filename,"wb")
 f.write(chart.read()) # save the pic
 f.close()

x = random.sample(range(0,100),10) # list with
y = random.sample(range(0,100),10) # random values in [0 100[
makeChart(x,y,"chart.png")
You can embed the picture in a web page:
<img alt="Google chart example" src="http://chart.apis.google.com/chart?chxt=x,y&amp;chs=300x200&amp;cht=s&amp;chd=t:64,10,18,42,49,83,73,27,44,51|77,89,13,87,27,34,38,44,22,42" />
Or use it from the disk.

Google chart example