next up previous contents
Next: Tests of elementwise operations Up: Getting started and examples Previous: Tests of slicing into   Contents

Tests of assembling from block matrices

The file test_block.py tests making a large matrix from blocks of submatrices. Results are at 3.5


#!/usr/bin/env python
# (C) 2000 Huaiyu Zhu <hzhu@users.sourceforge.net>.  Licence: GPL
# $Id: test_block.py,v 1.6 2000/09/13 01:22:57 hzhu Exp $
"""
Test constructing block matrices for Matrix module
"""
from MatPy.Matrix import *
#------------------------------------------------------------------
print "-"*40, "Assemble block matrices"

print "For proper matrices"
a = ones((2,3))
b = [a*1, a*2, a*3]
print Matrix_c(b)
print Matrix_r(b)

print "For row vectors"
a = ones(3)
b = [a*1, a*2, a*3]
print Matrix_c(b)
print Matrix_r(b)

print "For col vectors"
a = ones(2).T
b = [a*1, a*2, a*3]
print Matrix_c(b)
print Matrix_r(b)

#------------------------------------------------------------------

print "For different shapes"
a = zeros((0,0))
b = ones(3)
c = ones((2,3))*2
print a, b, c
print Matrix_c((a, b, c))
print Matrix_r((a.T, b.T, c.T))

#------------------------------------------------------------------
print "-"*40, "Split into block matrices"

print "For proper matrices"
a = rand((2,3))
print len(rows(a)), rows(a)
print len(cols(a)), cols(a)

print "For row vectors"
a = rand(3)
print len(rows(a)), rows(a)
print len(cols(a)), cols(a)

print "For col vectors"
a = rand(2).T
print len(rows(a)), rows(a)
print len(cols(a)), cols(a)


#------------------------------------------------------------------
print "-"*40, "Split and assemble"
a = rand((2,3))
print a

b = rows(a)
print b
c = Matrix_c(b)
print c
assert norm(a - c) == 0

d = cols(a)
print d
e = Matrix_r(d)
print e
assert norm(a - e) == 0


Result obtained with
>>> from MatPy.tests import test_block


---------------------------------------- Assemble block matrices
For proper matrices
[ 1   1   1 
  1   1   1 
  2   2   2 
  2   2   2 
  3   3   3 
  3   3   3  ]
[ 1   1   1   2   2   2   3   3   3 
  1   1   1   2   2   2   3   3   3  ]
For row vectors
[ 1   1   1 
  2   2   2 
  3   3   3  ]
[ 1   1   1   2   2   2   3   3   3  ]
For col vectors
[ 1 
  1 
  2 
  2 
  3 
  3  ]
[ 1   2   3 
  1   2   3  ]
For different shapes
[ ] [ 1   1   1  ] [ 2   2   2 
  2   2   2  ]
[ 1   1   1 
  2   2   2 
  2   2   2  ]
[ 1   2   2 
  1   2   2 
  1   2   2  ]
---------------------------------------- Split into block matrices
For proper matrices
2 [[[  0.829,  0.355,  0.802 ]], [[  0.24 ,  0.704,  0.917 ]]]
3 [[[  0.829 ],
 [  0.24  ]], [[  0.355 ],
 [  0.704 ]], [[  0.802 ],
 [  0.917 ]]]
For row vectors
1 [[[  0.596,  0.169,  0.664 ]]]
3 [[[  0.596 ]], [[  0.169 ]], [[  0.664 ]]]
For col vectors
2 [[[  0.63  ]], [[  0.527 ]]]
1 [[[  0.63  ],
 [  0.527 ]]]
---------------------------------------- Split and assemble
[ 0.528  0.829  0.289
  0.611  0.525  0.862 ]
[[[  0.528,  0.829,  0.289 ]], [[  0.611,  0.525,  0.862 ]]]
[ 0.528  0.829  0.289
  0.611  0.525  0.862 ]
[[[  0.528 ],
 [  0.611 ]], [[  0.829 ],
 [  0.525 ]], [[  0.289 ],
 [  0.862 ]]]
[ 0.528  0.829  0.289
  0.611  0.525  0.862 ]



Huaiyu Zhu
2002-03-23