def test_01_01_permute_one(self): np.random.seed(11) a = [np.random.uniform()] b = [p for p in F.permutations(a)] self.assertEqual(len(b), 1) self.assertEqual(len(b[0]), 1) self.assertEqual(b[0][0], a[0])
def test_01_02_permute_two(self): np.random.seed(12) a = np.random.uniform(size=2) b = [p for p in F.permutations(a)] self.assertEqual(len(b), 2) self.assertEqual(len(b[0]), 2) self.assertTrue(np.all(np.array(b) == a[np.array([[0, 1], [1, 0]])]))
def test_01_03_permute_three(self): np.random.seed(13) a = np.random.uniform(size=3) b = [p for p in F.permutations(a)] self.assertEqual(len(b), 6) self.assertEqual(len(b[0]), 3) expected = np.array([[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]) self.assertTrue(np.all(np.array(b) == a[expected]))
def test_01_02(self): r = np.random.RandomState() r.seed(11) for reductions in [0,2]: for _ in range(100): c = r.randint(1,10,(5,5)) i,j = np.mgrid[0:5,0:5] i = i.flatten() j = j.flatten() x,y,u,v = LAPJV.lapjv(i, j, c.flatten(), True, reductions) min_cost = np.sum(c) best = None for permutation in permutations([0,1,2,3,4]): cost = sum([c[i,permutation[i]] for i in range(5)]) if cost < min_cost: best = list(permutation) min_cost = cost result_cost = sum([c[i,x[i]] for i in range(5)]) self.assertAlmostEqual(min_cost, result_cost)