merged matrix.py from wangweinan/20130212