next up previous contents
Next: Tests of probability distributions Up: Getting started and examples Previous: Tests of various transpose   Contents

Tests of elementwise comparison

The file test_compare.py tests elementwise comparisons and summarizations. Results are at 3.15


#!/usr/bin/env python
# (C) 2000 Huaiyu Zhu <hzhu@users.sourceforge.net>.  Licence: GPL
# $Id: test_compare.py,v 1.2 2000/09/13 01:52:36 hzhu Exp $
"""
Test comparisons for Matrix module
"""

from MatPy.Matrix import r_range, c_range, rand, any, all, find, sum2
from MatPy.Matrix import max, min, sum, cumsum

a = rand((2,5))
a[1,2] = .5
print a

print "-"*40, "\nComparing two matrices"
b = 1 -a
print a.e_cmp(b)
print a.gt(b)
print a.ge(b)
print a.le(b)
print a.lt(b)


print "-"*40, "\nComparing matrix with number"
b = 0.5
print a.e_cmp(b)
print a.gt(b)
print a.ge(b)
print a.le(b)
print a.lt(b)

print any(a.lt(.1)), any(a.lt(.9))
print all(a.lt(.1)), all(a.lt(.9)) 
print sum2(a.lt(b))

print "-"*40, "\nComparing row vectors"
a = r_range(5)
b = 4-a
print a, b
print a.e_cmp(b)
print a.gt(b)
print a.ge(b)
print a.le(b)
print a.lt(b)

print any(a.lt(1)), any(a.lt(5))
print all(a.lt(1)), all(a.lt(5)) 
print sum2(a.lt(b))

print "-"*40, "\nComparing col vectors"
a = c_range(5)
b = 4-a
print a, b
print a.e_cmp(b)
print a.gt(b)
print a.ge(b)
print a.le(b)
print a.lt(b)

print any(a.lt(1)), any(a.lt(5))
print all(a.lt(1)), all(a.lt(5)) 
print sum2(a.lt(b))

print "-"*40, "max, min, sum, cumsum"
X = rand((9,4))
print X
print max(X)
print min(X)
print sum(X)
print cumsum(X)

print "-"*40, "max, min for multiple matrices"
Y = rand((4,4))
a = max(Y, Y.T, Y)
b = min(Y, Y.T, Y)
print a
print b
assert a == a.T
assert b == b.T
assert all(a.ge(Y))
assert all(Y.ge(b))

print "-"*40, "max, min for matrices and numbers"
print max(Y, 0.4, 0.6)
print min(Y, 0.4, 0.6)

print "-"*40, "max, min for multiple numbers"
print max(1, 0.5, -1)
print min(1, 0.5, -1)

print "-"*40, "find indices of true elements"
T = Y.lt(0.5)
print find(T)


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


[ 0.828  0.718  0.911  0.165  0.286
  0.15   0.134  0.5    0.891  0.0222 ]
---------------------------------------- 
Comparing two matrices
[ 1   1   1  -1  -1 
 -1  -1   0   1  -1  ]
[ 1   1   1   0   0 
  0   0   0   1   0  ]
[ 1   1   1   0   0 
  0   0   1   1   0  ]
[ 0   0   0   1   1 
  1   1   1   0   1  ]
[ 0   0   0   1   1 
  1   1   0   0   1  ]
---------------------------------------- 
Comparing matrix with number
[ 1   1   1  -1  -1 
 -1  -1   0   1  -1  ]
[ 1   1   1   0   0 
  0   0   0   1   0  ]
[ 1   1   1   0   0 
  0   0   1   1   0  ]
[ 0   0   0   1   1 
  1   1   1   0   1  ]
[ 0   0   0   1   1 
  1   1   0   0   1  ]
[ 0   0   0   0   1  ] [ 1   1   1   1   1  ]
[ 0   0   0   0   0  ] [ 1   1   0   1   1  ]
5
---------------------------------------- 
Comparing row vectors
[ 0   1   2   3   4  ] [ 4   3   2   1   0  ]
[-1  -1   0   1   1  ]
[ 0   0   0   1   1  ]
[ 0   0   1   1   1  ]
[ 1   1   1   0   0  ]
[ 1   1   0   0   0  ]
[ 1   0   0   0   0  ] [ 1   1   1   1   1  ]
[ 1   0   0   0   0  ] [ 1   1   1   1   1  ]
2
---------------------------------------- 
Comparing col vectors
[ 0 
  1 
  2 
  3 
  4  ] [ 4 
  3 
  2 
  1 
  0  ]
[-1 
 -1 
  0 
  1 
  1  ]
[ 0 
  0 
  0 
  1 
  1  ]
[ 0 
  0 
  1 
  1 
  1  ]
[ 1 
  1 
  1 
  0 
  0  ]
[ 1 
  1 
  0 
  0 
  0  ]
[ 1  ] [ 1  ]
[ 0  ] [ 1  ]
2
---------------------------------------- max, min, sum, cumsum
[ 0.996  0.14   0.342  0.482
  0.347  0.426  0.602  0.345
  0.58   0.0796  0.435  0.373
  0.487  0.0976  0.244  0.637
  0.186  0.579  0.485  0.659
  0.243  0.785  0.412  0.747
  0.439  0.472  0.574  0.367
  0.837  0.659  0.704  0.481
  0.625  0.427  0.184  0.382 ]
[ 0.996  0.785  0.704  0.747 ]
[ 0.186  0.0796  0.184  0.345 ]
[ 4.74   3.66   3.98   4.47  ]
[ 0.996  0.14   0.342  0.482
  1.34   0.566  0.944  0.827
  1.92   0.645  1.38   1.2  
  2.41   0.743  1.62   1.84 
  2.6    1.32   2.11   2.5  
  2.84   2.11   2.52   3.24 
  3.28   2.58   3.09   3.61 
  4.12   3.24   3.8    4.09 
  4.74   3.66   3.98   4.47  ]
---------------------------------------- max, min for multiple matrices
[ 0.321  0.712  0.346  0.658
  0.712  0.226  0.654  0.931
  0.346  0.654  0.275  0.316
  0.658  0.931  0.316  0.305 ]
[ 0.321  0.619  0.0865  0.302
  0.619  0.226  0.636  0.0598
  0.0865  0.636  0.275  0.259
  0.302  0.0598  0.259  0.305 ]
---------------------------------------- max, min for matrices and numbers
[ 0.6    0.712  0.6    0.6  
  0.619  0.6    0.654  0.931
  0.6    0.636  0.6    0.6  
  0.658  0.6    0.6    0.6   ]
[ 0.321  0.4    0.0865  0.302
  0.4    0.226  0.4    0.4  
  0.346  0.4    0.275  0.259
  0.4    0.0598  0.316  0.305 ]
---------------------------------------- max, min for multiple numbers
1.0
-1.0
---------------------------------------- find indices of true elements
[(0, 0), (0, 2), (0, 3), (1, 1), (2, 0), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]



Huaiyu Zhu
2002-03-23