#!/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)
|