def testRandom(self): m = Matrix(self.size) m.random(3) q, r = m.QRHouseholder() one = Matrix(self.size) one.one() self.assertTrue(m.isAlmostEqual(q*r)) self.assertTrue(one.isAlmostEqual(q*q.transpose()))
def testRandom(self): m = Matrix(5) m.random(3) q, r = m.QRSimple() one = Matrix(5) one.one() self.assertTrue(m.isAlmostEqual(q*r)) self.assertTrue(one.isAlmostEqual(q*q.transpose()))
def testKnown(self): m = Matrix([[1,2,3,4], [1,4,9,16], [1,8,27,64], [1,16,81,256]]) l = Matrix([[1,0,0,0], [1,1,0,0], [1,3,1,0], [1,7,6,1]]) u = Matrix([[1,2,3,4], [0,2,6,12], [0,0,6,24], [0 ,0,0,24]]) (x,y) = m.lu() self.assertTrue(x.isAlmostEqual(l)) self.assertTrue(y.isAlmostEqual(u)) self.assertTrue(m.isAlmostEqual(x*y))
def testRandom(self): l = Matrix(self.size) u = Matrix(self.size) l.randomLowerTriangular(5) u.randomUpperTriangular(5) m = l * u one = Matrix(self.size) one.one() self.assertTrue(one.isAlmostEqual(m * m.invertLU()))
def testUpperTriangularInversion(self): a = Matrix(15) a.randomUpperTriangular(87) b = a.invertUpperTriangular() c = Matrix(15) c.one() print (c - a * b).frobeniusNorm() self.assertTrue(c.isAlmostEqual(a * b)) self.assertTrue(c.isAlmostEqual(b * a))
def testTransposition(self): m = Matrix([[1,2],[3,4]]) tr = Matrix([[1,3], [2,4]]) self.assertTrue(tr.isAlmostEqual(m.transpose()))
def testClone(self): m = Matrix(7) a = m.copy() self.assertTrue(m.isAlmostEqual(a))
def testColumnNormalization(self): m = Matrix([[3,3],[4,4]]) norm = Matrix([[0.6, 0.6],[0.8,0.8]]) m.normalizeColumn(0) m.normalizeColumn(1) self.assertTrue(norm.isAlmostEqual(m))
def testHilbert(self): m = Matrix(50) m.hilbert() (l,u) = m.lu() self.assertTrue(m.isAlmostEqual(l*u))
def testUnit(self): one = Matrix(self.size) one.one() self.assertTrue(one.isAlmostEqual(one.invertLU()))
def testRandom(self): m = Matrix(self.size) m.random(3); one = Matrix(self.size) one.one() self.assertTrue(one.isAlmostEqual(m * m.invertQRHouseholder()))