def test_mul_with_hmat(self): self.assertRaises(ValueError, self.hmat.__mul__, self.hmat) self.assertRaises(ValueError, self.hmat1.__mul__, self.hmat3) hmat = HMat(content=self.content3, shape=(4, 2), parent_index=(0, 0)) check = HMat(content=numpy.matrix(numpy.zeros((3, 2))), shape=(3, 2), parent_index=(0, 0)) self.assertEqual(self.hmat1 * hmat, check) rmat = RMat(numpy.matrix(numpy.ones((3, 1))), right_mat=numpy.matrix(numpy.ones((3, 1)))) hmat = HMat(content=numpy.matrix(numpy.ones((3, 3))), shape=(3, 3), parent_index=(0, 3)) hmat1 = HMat(content=rmat, shape=(3, 3), parent_index=(3, 0)) check_rmat = RMat(numpy.matrix(3*numpy.ones((3, 1))), right_mat=numpy.matrix(numpy.ones((3, 1)))) check = HMat(content=3*numpy.matrix(numpy.ones((3, 3))), shape=(3, 3), parent_index=(0, 0)) self.assertEqual(hmat * hmat1, check) blocks = [HMat(content=numpy.matrix(1), shape=(1, 1), parent_index=(i, j)) for i in range(3) for j in range(3)] block_mat = HMat(blocks=blocks, shape=(3, 3), parent_index=(0, 0)) hmat = HMat(content=numpy.matrix(numpy.ones((3, 3))), shape=(3, 3), parent_index=(0, 0)) hmat1 = HMat(content=rmat, shape=(3, 3), parent_index=(0, 0)) self.assertTrue(numpy.array_equal((hmat1 * block_mat).to_matrix(), (3*hmat1).to_matrix())) self.assertTrue(numpy.array_equal((block_mat * hmat1).to_matrix(), (3 * hmat1).to_matrix())) res1 = HMat(content=numpy.matrix(6 * numpy.ones((3, 3))), shape=(3, 3), parent_index=(0, 0)) res2 = HMat(content=numpy.matrix(6 * numpy.ones((3, 2))), shape=(3, 2), parent_index=(0, 3)) res3 = HMat(content=numpy.matrix(6 * numpy.ones((2, 3))), shape=(2, 3), parent_index=(3, 0)) res4 = HMat(content=numpy.matrix(6 * numpy.ones((2, 2))), shape=(2, 2), parent_index=(3, 3)) res = HMat(blocks=[res1, res2, res3, res4], shape=(5, 5), parent_index=(0, 0)) check = self.consistent1 * self.consistent2 self.assertEqual(check, res) hmat = HMat(content=numpy.matrix(numpy.ones((3, 3))), shape=(3, 3), parent_index=(0, 0)) hmat2 = HMat(content=numpy.matrix(numpy.ones((3, 2))), shape=(3, 2), parent_index=(0, 3)) hmat_1 = HMat(blocks=[hmat, hmat2], shape=(3, 5), parent_index=(0, 0)) hmat3 = HMat(content=numpy.matrix(numpy.ones((2, 3))), shape=(2, 3), parent_index=(0, 0)) hmat4 = HMat(content=numpy.matrix(numpy.ones((3, 3))), shape=(3, 3), parent_index=(2, 0)) hmat_2 = HMat(blocks=[hmat3, hmat4], shape=(5, 3), parent_index=(0, 0)) self.assertRaises(ValueError, hmat_1.__mul__, hmat_2)
def test_transpose(self): left_block = numpy.matrix([[1, 2, 3], [3, 2, 1], [2, 3, 1]]) right_block = numpy.matrix([[2, 3, 4], [4, 3, 2], [3, 4, 2]]) rmat = RMat(left_block, right_block) self.assertEqual(rmat.transpose(), RMat(left_mat=right_block, right_mat=left_block)) trans = rmat.transpose() full = rmat.to_matrix() self.assertTrue(numpy.array_equal(trans.to_matrix(), full.transpose()))
def test_ne(self): left_block = numpy.matrix([[1, 2, 3], [3, 2, 1], [2, 3, 1]]) right_block = numpy.matrix([[2, 3, 4], [4, 3, 2], [3, 4, 2]]) rmat1 = RMat(left_block, right_block) left_block = numpy.matrix([[3, 2, 1], [3, 2, 1], [2, 3, 1]]) right_block = numpy.matrix([[2, 1, 4], [4, 3, 2], [3, 4, 2]]) rmat2 = RMat(left_block, right_block) self.assertNotEqual(rmat1, rmat2) self.assertFalse(rmat1 != rmat1)
def test_setup(self): left_block = numpy.matrix([[1, 2, 3], [3, 2, 1], [2, 3, 1]]) right_block = numpy.matrix([[2, 3, 4], [4, 3, 2], [3, 4, 2]]) rmat = RMat(left_block, right_block) self.assertIsInstance(rmat, RMat) rmat = RMat(left_block, right_block, 1) self.assertIsInstance(rmat, RMat) rmat = RMat(left_block, right_block, 4) self.assertIsInstance(rmat, RMat)
def test_split(self): self.assertRaises(NotImplementedError, self.hmat.restructure, self.hmat.block_structure()) check = HMat(content='bla', shape=(2, 2), parent_index=(0, 0)) self.assertRaises(NotImplementedError, check.restructure, {(0, 0): (1, 1)}) splitter = HMat(content=RMat(numpy.matrix(numpy.ones((2, 1))), numpy.matrix(numpy.ones((2, 1)))), shape=(2, 2), parent_index=(0, 0)) check_blocks = [HMat(content=RMat(numpy.matrix(numpy.ones((1, 1))), numpy.matrix(numpy.ones((1, 1)))), shape=(1, 1), parent_index=(i, j)) for i in range(2) for j in range(2)] check = HMat(blocks=check_blocks, shape=(2, 2), parent_index=(0, 0)) self.assertEqual(splitter.restructure(check.block_structure()), check)
def test_mul_with_rmat(self): rmat = RMat(self.content1, self.content1) hmat = HMat(content=rmat, shape=(3, 3), parent_index=(0, 0)) prod = hmat * rmat self.assertEqual(prod, hmat) rmat = RMat(self.content3, self.content3) prod = self.hmat1 * rmat self.assertTrue(numpy.array_equal(prod.to_matrix(), self.hmat1.to_matrix())) rmat = RMat(numpy.matrix(numpy.ones((7, 3))), numpy.matrix(numpy.ones((6, 3)))) self.assertRaises(TypeError, self.hmat_lvl2.__mul__, rmat)
def test_split(self): left_block = numpy.matrix([[1, 2, 3], [3, 2, 1], [2, 3, 1]]) right_block = numpy.matrix([[2, 3, 4], [4, 3, 2], [3, 4, 2]]) rmat = RMat(left_block, right_block) check1 = RMat(numpy.matrix([[1, 2, 3]]), numpy.matrix([[2, 3, 4]])) check2 = RMat(numpy.matrix([[3, 2, 1], [2, 3, 1]]), numpy.matrix([[4, 3, 2], [3, 4, 2]])) check3 = RMat(numpy.matrix([[1, 2, 3], [3, 2, 1]]), numpy.matrix([[4, 3, 2], [3, 4, 2]])) self.assertEqual(rmat.split(0, 0, 1, 1), check1) self.assertEqual(rmat.split(1, 1, 3, 3), check2) self.assertEqual(rmat.split(0, 1, 2, 3), check3)
def test_minus(self): left1 = numpy.matrix([[1], [2], [3]]) left2 = numpy.matrix([[2], [3], [4]]) sub_left = numpy.matrix([[1, -2], [2, -3], [3, -4]]) right1 = numpy.matrix([[5], [6], [7]]) right2 = numpy.matrix([[4], [5], [6]]) sub_right = numpy.matrix([[5, 4], [6, 5], [7, 6]]) rmat1 = RMat(left1, right1, 1) rmat2 = RMat(left2, right2, 1) sub_mat = RMat(sub_left, sub_right, 2) minus = rmat1 - rmat2 res = numpy.matrix([[-3, -4, -5], [-2, -3, -4], [-1, -2, -3]]) self.assertEqual(minus, sub_mat.reduce(1))
def test_reduce(self): left_block = numpy.matrix([[1, 2, 3], [3, 2, 1], [2, 3, 1]]) right_block = numpy.matrix([[2, 3, 4], [4, 3, 2], [3, 4, 2]]) rmat = RMat(left_block, right_block, 3) red_rmat = rmat.reduce(2) res_a = numpy.matrix([[30.52901104, -3.60036909], [31.80619274, 2.24569436], [32.3747334, 1.18885057]]) res_b = numpy.matrix([[0.16125158, -0.98446933], [0.85768556, 0.17458427], [0.488235, 0.01845182]]) res = RMat(res_a, res_b, 2) self.assertAlmostEqual(red_rmat, res, places=6)
def test_to_matrix(self): block1 = numpy.matrix([numpy.arange(i, i + 5) for i in range(1, 6)]) block2 = numpy.matrix([numpy.arange(i, i + 5) for i in range(6, 11)]) block4 = numpy.matrix([numpy.arange(i, i + 5) for i in range(11, 16)]) hmat1 = HMat(content=block1, shape=(5, 5), parent_index=(0, 0)) hmat2 = HMat(content=block2, shape=(5, 5), parent_index=(5, 0)) hmat3 = HMat(content=block2, shape=(5, 5), parent_index=(0, 5)) hmat4 = HMat(content=block4, shape=(5, 5), parent_index=(5, 5)) hmat = HMat(blocks=[hmat1, hmat2, hmat3, hmat4], shape=(10, 10), parent_index=(0, 0)) res = numpy.matrix([numpy.arange(i, i + 10) for i in range(1, 11)]) self.assertTrue(numpy.array_equal(hmat.to_matrix(), res)) self.assertTrue(numpy.array_equal(self.hmat.to_matrix(), numpy.zeros((7, 6)))) check_lvl2 = numpy.zeros((7, 6)) check_lvl2[0:3, 0:4] = 1 self.assertTrue(numpy.array_equal(self.hmat_lvl2.to_matrix(), check_lvl2)) rmat = RMat(numpy.matrix(numpy.ones((3, 1))), numpy.matrix(numpy.ones((3, 1)))) hmat1 = HMat(content=rmat, shape=(3, 3), parent_index=(0, 0)) hmat2 = HMat(content=rmat, shape=(3, 3), parent_index=(0, 3)) hmat3 = HMat(content=rmat, shape=(3, 3), parent_index=(3, 0)) hmat4 = HMat(content=rmat, shape=(3, 3), parent_index=(3, 3)) hmat = HMat(blocks=[hmat1, hmat2, hmat3, hmat4], shape=(6, 6), parent_index=(0, 0)) self.assertTrue(numpy.array_equal(hmat.to_matrix(), numpy.matrix(numpy.ones((6, 6))))) blocks1 = [HMat(content=rmat, shape=(3, 3), parent_index=(i, j)) for i in range(0, 4, 3) for j in range(0, 4, 3)] block_mat1 = HMat(blocks=blocks1, shape=(6, 6), parent_index=(0, 0)) block_mat2 = HMat(blocks=blocks1, shape=(6, 6), parent_index=(6, 0)) block_mat3 = HMat(blocks=blocks1, shape=(6, 6), parent_index=(0, 6)) block_mat4 = HMat(blocks=blocks1, shape=(6, 6), parent_index=(6, 6)) outer_block = HMat(blocks=[block_mat1, block_mat2, block_mat3, block_mat4], shape=(12, 12), parent_index=(0, 0)) self.assertTrue(numpy.array_equal(outer_block.to_matrix(), numpy.matrix(numpy.ones((12, 12)))))
def test_eq(self): self.assertEqual(self.hmat1, self.hmat1) self.assertEqual(self.hmat, self.hmat) self.assertEqual(self.hmat_lvl2, self.hmat_lvl2) self.assertFalse(self.hmat2 == self.hmat1) self.assertFalse(self.hmat20 == self.hmat1) self.assertFalse(HMat(content=self.content1, shape=(4, 4), parent_index=(0, 0)) == self.hmat1) self.assertFalse(HMat(content=self.content1, shape=(3, 4), parent_index=(1, 0)) == self.hmat1) self.assertFalse(HMat(blocks=[self.hmat1], shape=(3, 4), parent_index=(0, 0)) == self.hmat20) self.assertFalse(HMat(content=numpy.ones((3, 4)), shape=(3, 4), parent_index=(0, 0)) == self.hmat1) rmat = RMat(self.content1, self.content1) hmat = HMat(content=rmat, shape=(3, 3), parent_index=(0, 0)) rmat2 = RMat(self.content2, self.content2) hmat2 = HMat(content=rmat2, shape=(3, 3), parent_index=(0, 0)) self.assertFalse(hmat == hmat2) self.assertFalse(hmat == rmat)
def test_radd_(self): left1 = numpy.matrix([[1], [2], [3]]) right1 = numpy.matrix([[5], [6], [7]]) rmat1 = RMat(left1, right1) addend = numpy.matrix(numpy.ones((3, 3))) res = rmat1.__radd__(addend) check = rmat1.to_matrix() + numpy.matrix(numpy.ones((3, 3))) self.assertEqual(res.norm(), numpy.linalg.norm(check))
def test_form_add(self): left1 = numpy.matrix([[1, 2], [2, 2], [4, 3]]) right1 = numpy.matrix([[2, 3], [1, 5], [5, 1]]) left2 = numpy.matrix([[4, 1, 3], [5, 1, 1], [1, 3, 1]]) right2 = numpy.matrix([[1, 1, 1], [2, 1, 3], [5, 1, 5]]) rmat1 = RMat(left1, right1) rmat2 = RMat(left2, right2) added = rmat1 + rmat2 added2 = rmat2 + rmat1 self.assertEqual(added.reduce(2), rmat1.form_add(rmat2, 2)) self.assertEqual(added.reduce(1), rmat1.form_add(rmat2, 1)) self.assertEqual(added2.reduce(2), rmat2.form_add(rmat1, 2)) self.assertEqual(added2.reduce(1), rmat2.form_add(rmat1, 1)) rmat1 = RMat(left1, right1, 2) rmat2 = RMat(left2, right2, 3) added = rmat1 + rmat2 self.assertEqual(added, rmat1.form_add(rmat2))
def test_add(self): left1 = numpy.matrix([[1], [2], [3]]) left2 = numpy.matrix([[2], [3], [4]]) add_left = numpy.matrix([[1, 2], [2, 3], [3, 4]]) right1 = numpy.matrix([[5], [6], [7]]) right2 = numpy.matrix([[4], [5], [6]]) add_right = numpy.matrix([[5, 4], [6, 5], [7, 6]]) rmat1 = RMat(left1, right1) rmat2 = RMat(left2, right2) add_mat = RMat(add_left, add_right) res = rmat1 + rmat2 self.assertEqual(res, add_mat) self.assertAlmostEqual(abs(rmat1 + rmat2), abs(rmat2 + rmat1), 6) self.assertRaises(ValueError, rmat1.__add__, numpy.ones((2, 4))) self.assertRaises(ValueError, rmat1.__add__, 1) res = rmat1 + numpy.matrix(numpy.ones((3, 3))) check = rmat1.to_matrix() + numpy.matrix(numpy.ones((3, 3))) self.assertEqual(res.norm(), numpy.linalg.norm(check)) self.assertRaises(NotImplementedError, rmat1.__add__, 'bla') self.assertRaises(NotImplementedError, rmat1.__add__, numpy.ones( (3, 3)))
def test_str(self): check = '''Rank-k matrix with left block: [[1] [2] [3]] and right block: [[4] [5] [6]]''' rmat = RMat(numpy.matrix([[1], [2], [3]]), numpy.matrix([[4], [5], [6]]), 1) self.assertEqual(str(rmat), check)
def test_build_hmatrix(self): full_func = lambda x: numpy.matrix(numpy.ones(x.shape())) block_func = lambda x: RMat(numpy.matrix(numpy.ones((x.shape()[0], 1))), numpy.matrix(numpy.ones((x.shape()[1], 1))), max_rank=1) lim1 = 2 link_num = 4 points1 = [numpy.array([float(i) / lim1]) for i in range(lim1)] links1 = [[points1[l] for l in [random.randint(0, lim1 - 1) for x in range(link_num)]] for i in range(lim1)] grid1 = Grid(points1, links1) cluster1 = Cluster(grid1) rc1 = RegularCuboid(cluster1) ct1 = build_cluster_tree(rc1, max_leaf_size=4) bct1 = build_block_cluster_tree(ct1, right_cluster_tree=ct1, admissible_function=admissible) hmat = build_hmatrix(bct1, generate_rmat_function=block_func, generate_full_matrix_function=block_func) check_rmat = RMat(numpy.matrix(numpy.ones((2, 1))), numpy.matrix(numpy.ones((2, 1))), max_rank=1) check = HMat(content=check_rmat, shape=(2, 2), parent_index=(0, 0)) self.assertEqual(hmat, check) lim1 = 8 link_num = 4 points1 = [numpy.array([float(i) / lim1]) for i in range(lim1)] links1 = [[points1[l] for l in [random.randint(0, lim1 - 1) for x in range(link_num)]] for i in range(lim1)] grid1 = Grid(points1, links1) cluster1 = Cluster(grid1) rc1 = RegularCuboid(cluster1) ct1 = build_cluster_tree(rc1, max_leaf_size=4) bct1 = build_block_cluster_tree(ct1, right_cluster_tree=ct1, admissible_function=admissible) hmat = build_hmatrix(bct1, generate_rmat_function=block_func, generate_full_matrix_function=block_func) check_rmat = RMat(numpy.matrix(numpy.ones((4, 1))), numpy.matrix(numpy.ones((4, 1))), max_rank=1) check1 = HMat(content=check_rmat, shape=(4, 4), parent_index=(0, 0)) check2 = HMat(content=check_rmat, shape=(4, 4), parent_index=(0, 4)) check3 = HMat(content=check_rmat, shape=(4, 4), parent_index=(4, 0)) check4 = HMat(content=check_rmat, shape=(4, 4), parent_index=(4, 4)) check = HMat(blocks=[check1, check2, check3, check4], shape=(8, 8), parent_index=(0, 0)) self.assertEqual(hmat, check) bct1 = build_block_cluster_tree(ct1, right_cluster_tree=ct1, admissible_function=lambda x, y: True) hmat = build_hmatrix(bct1, generate_rmat_function=block_func, generate_full_matrix_function=block_func) self.assertIsInstance(hmat, HMat)
def test_add(self): addend1 = HMat(content=numpy.matrix(numpy.ones((3, 4))), shape=(3, 4), parent_index=(0, 0)) addend2 = HMat(content=numpy.matrix(numpy.ones((3, 2))), shape=(3, 2), parent_index=(0, 4)) addend3 = HMat(content=numpy.matrix(numpy.ones((4, 2))), shape=(4, 2), parent_index=(3, 0)) addend4 = HMat(content=numpy.matrix(numpy.ones((4, 4))), shape=(4, 4), parent_index=(3, 2)) addend_hmat = HMat(blocks=[addend1, addend2, addend3, addend4], shape=(7, 6), parent_index=(0, 0)) splitter_mat = HMat(content=numpy.matrix(numpy.zeros((7, 6))), shape=(7, 6), parent_index=(0, 0)) self.assertEqual(addend_hmat + splitter_mat, addend_hmat) self.assertEqual(splitter_mat + addend_hmat, addend_hmat) res = addend_hmat + self.hmat self.assertEqual(res, addend_hmat) self.assertRaises(ValueError, addend1.__add__, addend2) addend = HMat(content=numpy.matrix(numpy.ones((3, 2))), shape=(3, 2), parent_index=(0, 0)) self.assertRaises(ValueError, addend.__add__, addend2) self.assertRaises(NotImplementedError, addend_hmat.__add__, 'bla') self.assertRaises(NotImplementedError, addend_hmat.__add__, numpy.ones((7, 6))) addend_hmat = HMat(content=numpy.matrix(1), shape=(1, 1), parent_index=(0, 0)) self.assertEqual(addend_hmat + 0, addend_hmat) addend_hmat = HMat(blocks=[addend1, addend2, addend3], shape=(7, 6), parent_index=(0, 0)) self.assertRaises(ValueError, self.hmat.__add__, addend_hmat) check = HMat(content=numpy.matrix(2 * numpy.ones((3, 4))), shape=(3, 4), parent_index=(0, 0)) res = addend1 + numpy.matrix(numpy.ones((3, 4))) self.assertEqual(res, check) self.assertRaises(ValueError, addend1._add_matrix, numpy.matrix(numpy.ones((3, 2)))) rmat = RMat(numpy.matrix(numpy.ones((3, 2))), numpy.matrix(numpy.ones((3, 2))), 2) hmat = HMat(content=rmat, shape=(3, 3), parent_index=(0, 0)) self.assertRaises(NotImplementedError, hmat.__add__, rmat) mat = numpy.matrix(numpy.zeros((7, 6))) res = addend_hmat + mat self.assertEqual(addend_hmat, res) mat = numpy.matrix(numpy.ones((7, 6))) res = addend_hmat + mat check = 2 * addend_hmat self.assertEqual(check, res) left = RMat(numpy.matrix(numpy.zeros((3, 1))), numpy.matrix(numpy.zeros((3, 1))), max_rank=1) left_mat = HMat(content=left, shape=(3, 3), parent_index=(0, 0)) addend = HMat(content=numpy.matrix(numpy.ones((3, 3))), shape=(3, 3), parent_index=(0, 0)) self.assertAlmostEqual(numpy.linalg.norm((addend + left_mat).to_matrix()), numpy.linalg.norm(addend.to_matrix()))
def test_abs_norm(self): self.assertEqual(abs(self.cmat3), 3.0) self.assertEqual(abs(self.cmat4), 2.0) self.assertEqual(abs(self.consistent1), 5.4772255750516612) rmat1 = RMat(numpy.matrix(numpy.ones((3, 1))), numpy.matrix(numpy.ones((3, 1))), max_rank=1) hmat1 = HMat(content=rmat1, shape=(3, 3), parent_index=(0, 0)) hmat2 = HMat(content=rmat1, shape=(3, 3), parent_index=(0, 3)) hmat3 = HMat(content=rmat1, shape=(3, 3), parent_index=(3, 0)) hmat4 = HMat(content=rmat1, shape=(3, 3), parent_index=(3, 3)) hmat = HMat(blocks=[hmat1, hmat2, hmat3, hmat4], shape=(6, 6), parent_index=(0, 0)) self.assertEqual(abs(hmat), 6.0) self.assertEqual(abs(hmat), hmat.norm()) self.assertEqual(abs(hmat), hmat.norm('fro')) self.assertRaises(NotImplementedError, hmat.norm, 2)
def test_mul(self): left1 = numpy.matrix([[1, 2, 3], [3, 2, 1], [2, 3, 1]]) right1 = numpy.matrix([[2, 3, 4], [4, 3, 2], [3, 4, 2]]) rmat1 = RMat(left1, right1, 3) left2 = numpy.matrix([[1, 2], [2, 2], [4, 3]]) right2 = numpy.matrix([[2, 3], [1, 5], [5, 1]]) rmat2 = RMat(left2, right2, 2) left3 = numpy.matrix([[1], [2], [3], [4]]) right3 = numpy.matrix([[4], [5], [6], [7]]) rmat3 = RMat(left3, right3, 1) left4 = numpy.matrix([[1], [2], [3]]) right4 = numpy.matrix([[4], [5], [6]]) rmat4 = RMat(left4, right4, 1) res1 = rmat1 * rmat2 res2 = rmat4 * rmat2 self.assertEqual(res1.max_rank, 3) self.assertEqual(res2.max_rank, 2) self.assertRaises(ValueError, rmat2.__mul__, rmat3) self.assertRaises(ValueError, rmat1.__mul__, rmat3) mat = numpy.matrix(numpy.ones((3, 1))) res = RMat(left1, mat.T * right1, 3) self.assertEqual(res, rmat1 * mat) self.assertRaises(ValueError, rmat1.__rmul__, mat) mat = numpy.matrix(numpy.ones((1, 3))) check = mat * left1 * right1.T self.assertTrue(numpy.array_equal(mat * rmat1, check)) mat = numpy.matrix(numpy.ones((4, 1))) self.assertRaises(ValueError, rmat1.__mul__, mat) mat = numpy.ones((3, 1)) res = left1 * (right1.T * mat) self.assertTrue(numpy.array_equal(res, rmat1 * mat)) mat = numpy.ones((4, 1)) self.assertRaises(ValueError, rmat1.__mul__, mat) self.assertRaises(NotImplementedError, rmat1.__mul__, 'a') self.assertRaises(NotImplementedError, rmat1.__rmul__, 'a') self.assertEqual(rmat1 * 1, rmat1) self.assertEqual(1 * rmat1, rmat1)
def test_norm(self): left = numpy.matrix([[1], [2], [3]]) right = numpy.matrix([[2], [3], [4]]) rmat = RMat(left, right, 1) self.assertEqual(rmat.norm(), abs(rmat))
def test_repr(self): check = '<RMat with left_mat: matrix([[1],[2],[3]]), right_mat: matrix([[4],[5],[6]]) and max_rank: 1>' rmat = RMat(numpy.matrix([[1], [2], [3]]), numpy.matrix([[4], [5], [6]]), 1) self.assertEqual(rmat.__repr__(), check)
def test_from_matrix(self): mat = numpy.matrix(numpy.ones((3, 3))) check = RMat(mat, max_rank=1) self.assertAlmostEqual(check.norm(), numpy.linalg.norm(mat)) check = RMat(mat, max_rank=4) self.assertAlmostEqual(check.norm(), numpy.linalg.norm(mat))
def build_rmat(x): return RMat(numpy.matrix(numpy.ones((x.shape()[0], 1))), numpy.matrix(numpy.ones((x.shape()[1], 1))), max_rank=1)
def test_neg(self): left1 = numpy.matrix([[1], [2], [3]]) right1 = numpy.matrix([[2], [3], [4]]) rmat_pos = RMat(left1, right1, 1) rmat_neg = RMat(-left1, right1, 1) self.assertEqual(-rmat_pos, rmat_neg)
def test_type(self): left1 = numpy.matrix([[1, 2], [2, 2], [4, 3]]) right1 = numpy.matrix([[2, 3], [1, 5], [5, 1]]) rmat1 = RMat(left1, right1, 2) self.assertEqual(type(rmat1), RMat)
def test_abs(self): left = numpy.matrix([[1], [2], [3]]) right = numpy.matrix([[4], [5], [6]]) rmat = RMat(left, right, 1) self.assertAlmostEqual(abs(rmat), 32.832910319, places=8)
def test_toMatrix(self): left = numpy.matrix([[1], [2], [3]]) right = numpy.matrix([[2], [3], [4]]) rmat = RMat(left, right, 1) check_mat = numpy.matrix([[2, 3, 4], [4, 6, 8], [6, 9, 12]]) self.assertTrue(numpy.array_equal(rmat.to_matrix(), check_mat))