def test_permutation_2sided_single_transform_umeyama_approx_trans_scale_zero_padding( n, ncol, nrow): r"""Test 2sided-perm single transf with "umeyama_approx" by arrays with translate, scaling.""" # define a random, symmetric matrix array_a = np.random.uniform(-10.0, 10.0, (n, n)) array_a = (array_a + array_a.T) / 2.0 # check with all possible permutation matrices perm = generate_random_permutation_matrix(n) # Compute the translated, scaled matrix padded with zeros array_b = np.dot(perm.T, np.dot(20 * array_a + 9, perm)) # pad the matrices with zeros array_b = np.concatenate((array_b, np.zeros((n, ncol))), axis=1) array_b = np.concatenate((array_b, np.zeros((nrow, n + ncol))), axis=0) # Check res = permutation_2sided( array_a, array_b, single=True, method="approx-umeyama", unpad_col=True, unpad_row=True, translate=True, scale=True, ) assert_almost_equal(res.t, perm, decimal=6) assert_almost_equal(res.error, 0, decimal=6)
def test_permutation_2sided_2trans_regular(): r"""Test regular 2sided-perm by practical example.""" # Example taken from page 64 in parallel solution of # svd-related problems, with applications # vummath.ma.man.ac.uk/~higham/links/theses/papad93.pdf b = np.array([ [32, 14, 3, 63, 50], [24, 22, 1, 56, 4], [94, 16, 28, 75, 81], [19, 72, 42, 90, 54], [71, 85, 10, 96, 58], ]) a = np.array([ [58, 96, 85, 10, 71], [81, 75, 16, 28, 94], [4, 56, 22, 1, 24], [54, 90, 72, 42, 19], [50, 63, 14, 3, 32], ]) p1 = np.array([[0, 0, 0, 0, 1], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 1, 0], [1, 0, 0, 0, 0]]) p2 = np.array([[0, 0, 0, 0, 1], [0, 0, 0, 1, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [1, 0, 0, 0, 0]]) result = permutation_2sided(a, b, single=False, method="flip-flop") assert_almost_equal(result.s, p1, decimal=6) assert_almost_equal(result.t, p2, decimal=6) assert_almost_equal(result.error, 0.0, decimal=6)
def test_permutation_2sided_1trans_directed(n): r"""Test 2sided-perm with single transform and directed.""" # A random array a = np.random.uniform(-10.0, 10.0, (n, n)) p = generate_random_permutation_matrix(n) b = np.dot(p.T, np.dot(a, p)) # Procrustes with no translate and scale res = permutation_2sided(a, b, single=True, method="approx-umeyama") res = permutation_2sided(res.new_a, res.new_b, single=True, method="nmf", guess_p2=res.t) assert_almost_equal(res.t, p, decimal=6) assert_almost_equal(res.s, p.T, decimal=6) assert_almost_equal(res.error, 0.0, decimal=6)
def test_permutation_2sided_1trans_small_matrices_umeyama_all_permutations(n): r"""Test 2sided-perm single transform with Umeyama guess for all permutations.""" # define a random matrix a = np.random.uniform(-10.0, 10.0, (n, n)) # check with all possible permutation matrices for comb in itertools.permutations(np.arange(n)): p = np.zeros((n, n)) p[np.arange(n), comb] = 1 # get array_b by permutation b = np.dot(p.T, np.dot(a, p)) res = permutation_2sided(a, b, single=True, method="approx-umeyama") res = permutation_2sided(res.new_a, res.new_b, single=True, method="nmf", guess_p2=res.t) assert_almost_equal(res.t, p, decimal=6) assert_almost_equal(res.error, 0, decimal=6)
def test_permutation_2sided_1trans_directed_all_permutations(n): r"""Test 2sided-perm with "directed" over all permutations.""" # define a random matrix a = np.random.uniform(-10.0, 10.0, (n, n)) # check with all possible permutation matrices for comb in itertools.permutations(np.arange(n)): perm = np.zeros((n, n)) perm[np.arange(n), comb] = 1 b = np.dot(perm.T, np.dot(a, perm)) res = permutation_2sided(a, b, single=True, method="approx-umeyama") res = permutation_2sided(res.new_a, res.new_b, single=True, method="nmf", guess_p2=res.t) assert_almost_equal(res.t, perm, decimal=6) assert_almost_equal(res.s, perm.T, decimal=6) assert_almost_equal(res.error, 0, decimal=6)
def test_permutation_2sided_2trans_flipflop_rectangular(): r"""Test regular 2sided-perm by rectangular 4by2 random arrays.""" a = np.array([[6, 8], [10, 8], [5, 8], [5, 7]]) p = np.array([[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 0, 1]]) q = np.array([[0, 1], [1, 0]]) b = np.linalg.multi_dot([p, a, q]) result = permutation_2sided(a, b, single=False, method="flip-flop") assert_almost_equal(result.s, p, decimal=6) assert_almost_equal(result.t, q, decimal=6) assert_almost_equal(result.error, 0.0, decimal=6)
def test_permutation_2sided_regular_unsquared(): r"""Test regular 2sided-perm by unsquared 4by2 random arrays.""" N = np.array([[6, 8], [10, 8], [5, 8], [5, 7]]) perm_P = np.array([[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 0, 1]]) perm_Q = np.array([[0, 1], [1, 0]]) M = np.linalg.multi_dot([perm_P, N, perm_Q]) new_M, new_N, new_P, new_Q, e_opt = permutation_2sided( M, N, transform_mode='double', iteration=500) assert_almost_equal(new_P, perm_P, decimal=6) assert_almost_equal(new_Q, perm_Q, decimal=6) assert_almost_equal(e_opt, 0, decimal=6)
def test_permutation_2sided_single_transform_normal2(n): r"""Test 2sided-perm with "normal2".""" # define a random, symmetric matrix a = np.random.uniform(-10.0, 10.0, (n, n)) a = (a + a.T) / 2.0 # define b by permuting a p = generate_random_permutation_matrix(n) b = np.dot(p.T, np.dot(a, p)) res = permutation_2sided(a, b, single=True, method="approx-normal2") assert_almost_equal(res.t, p, decimal=6) assert_almost_equal(res.error, 0, decimal=6)
def test_permutation_2sided_1trans_umeyama(n): r"""Test 2sided-permutation with single transform with umeyama guess.""" # define a random matrix array_a = np.random.uniform(-10.0, 10.0, (n, n)) array_a = (array_a + array_a.T) / 2.0 # define array_b by permuting array_a perm = generate_random_permutation_matrix(n) array_b = np.dot(perm.T, np.dot(array_a, perm)) # Check res = permutation_2sided(array_a, array_b, single=True, method="approx-umeyama") res = permutation_2sided(res.new_a, res.new_b, single=True, method="nmf", guess_p2=res.t) assert_almost_equal(res.t, perm, decimal=6) assert_almost_equal(res.error, 0, decimal=6)
def test_permutation_2sided_2trans_rectangular_negative(): r"""Test regular 2sided-perm by unsquared negative 6by4 random arrays.""" # build random matrix by seed 999 np.random.seed(999) a = np.random.randint(-5, 6, size=(6, 4)).astype(float) p1 = np.random.permutation(np.eye(6, 6)) p2 = np.random.permutation(np.eye(4, 4)) b = np.linalg.multi_dot([p1, a, p2]) result = permutation_2sided(b, a, single=False, method="flip-flop") assert_almost_equal(result.s, p1, decimal=6) assert_almost_equal(result.t, p2, decimal=6) assert_almost_equal(result.error, 0.0, decimal=6)
def test_permutation_2sided_4by4_directed(): r"""Test 2sided-perm with "directed" by 4by4 arrays.""" # A random array array_a = np.array([[29, 79, 95, 83], [37, 86, 67, 93], [72, 85, 15, 3], [38, 39, 58, 24]]) # permutation perm = np.array([[0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0]]) # permuted array_b array_b = np.dot(perm.T, np.dot(array_a, perm)) # Procrustes with no translate and scale result = permutation_2sided(array_a, array_b, transform_mode="single") assert_almost_equal(result["array_u"], perm, decimal=6) assert_almost_equal(result["error"], 0., decimal=6)
def test_permutation_2sided_regular_unsquared(): r"""Test regular 2sided-perm by unsquared 4by2 random arrays.""" array_n = np.array([[6, 8], [10, 8], [5, 8], [5, 7]]) perm_p = np.array([[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 0, 1]]) perm_q = np.array([[0, 1], [1, 0]]) array_m = np.linalg.multi_dot([perm_p, array_n, perm_q]) result = permutation_2sided(array_m, array_n, transform_mode="double", iteration=500) assert_almost_equal(result[2], perm_p, decimal=6) assert_almost_equal(result[3], perm_q, decimal=6) assert_almost_equal(result[4], 0, decimal=6)
def test_permutation_2sided_4by4_directed_symmetric(): r"""Test 2sided-perm with "directed" by 4by4 symmetric arrays.""" # A random array array_a = np.array([[4, 5, 3, 3], [5, 7, 3, 5], [3, 3, 2, 2], [3, 5, 2, 5]]) # permutation perm = np.array([[0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0]]) # permuted array_b array_b = np.dot(perm.T, np.dot(array_a, perm)) # Procrustes with no translate and scale result = permutation_2sided(array_a, array_b, transform_mode="single") assert_almost_equal(result["array_u"], perm, decimal=6) assert_almost_equal(result["error"], 0., decimal=6)
def test_permutation_2sided_1trans_umeyama_approx(n): r"""Test 2sided-perm, single transform with "umeyama_approx" mode.""" # define a random, symmetric matrix array_a = np.random.uniform(-10.0, 10.0, (n, n)) array_a = (array_a + array_a.T) / 2.0 # define array_b by permuting array_a perm = generate_random_permutation_matrix(n) array_b = np.dot(perm.T, np.dot(array_a, perm)) res = permutation_2sided(array_a, array_b, single=True, method="approx-umeyama-svd") assert_almost_equal(res.t, perm, decimal=6) assert_almost_equal(res.error, 0, decimal=6)
def test_permutation_2sided_regular_unsquared_negative(): r"""Test regular 2sided-perm by unsquared negative 6by4 random arrays.""" # build random matrix by seed 999 np.random.seed(999) N = np.random.randint(-5, 6, size=(6, 4)) N = np.float_(N) perm_P = np.random.permutation(np.eye(6, 6)) perm_Q = np.random.permutation(np.eye(4, 4)) M = np.linalg.multi_dot([perm_P, N, perm_Q]) new_M, new_N, new_P, new_Q, e_opt = permutation_2sided( M, N, transform_mode='double', iteration=500) assert_almost_equal(new_P, perm_P, decimal=6) assert_almost_equal(new_Q, perm_Q, decimal=6) assert_almost_equal(e_opt, 0, decimal=6)
def test_permutation_2sided_1trans_normal1_translate_scale(n): r"""Test 2sided-perm with "normal1" with translation and scaling.""" # define a random, symmetric matrix a = np.random.uniform(-10.0, 10.0, (n, n)) a = np.dot(a, a.T) p = generate_random_permutation_matrix(n) b = np.dot(p.T, np.dot((14.7 * a + 3.14), p)) res = permutation_2sided(a, b, single=True, method="approx-umeyama", translate=True, scale=True) res = permutation_2sided(res.new_a, res.new_b, single=True, method="nmf", guess_p2=res.t, translate=True, scale=True) assert_almost_equal(res.t, p, decimal=6) assert_almost_equal(res.s, p.T, decimal=6) assert_almost_equal(res.error, 0.0, decimal=6)
def test_permutation_2sided_1trans_normal1_loop(n): r"""Test 2sided-perm with "normal1" by small arrays with all permutations.""" # define a random matrix & symmetrize it a = np.random.uniform(-10.0, 10.0, (n, n)) a = (a + a.T) / 2.0 # check with all possible permutation matrices for comb in itertools.permutations(np.arange(n)): p = np.zeros((n, n)) p[np.arange(n), comb] = 1 b = np.dot(p.T, np.dot(a, p)) res = permutation_2sided(a, b, single=True, method="approx-normal1") assert_almost_equal(res.t, p, decimal=6) assert_almost_equal(res.s, p.T, decimal=6) assert_almost_equal(res.error, 0.0, decimal=6)
def test_permutation_2sided_4by4_directed(): r"""Test 2sided-perm with 'directed' by 4by4 arrays.""" # A random array array_a = np.array([[29, 79, 95, 83], [37, 86, 67, 93], [72, 85, 15, 3], [38, 39, 58, 24]]) # permutation perm = np.array([[0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0]]) # permuted array_b array_b = np.dot(perm.T, np.dot(array_a, perm)) # Procrustes with no translate and scale new_a, new_b, U, e_opt = permutation_2sided( array_a, array_b, transform_mode='single_directed') assert_almost_equal(U, perm, decimal=6) assert_almost_equal(e_opt, 0., decimal=6)
def test_permutation_2sided_4by4_directed_symmetric(): r"""Test 2sided-perm with 'directed' by 4by4 symmetric arrays.""" # A random array array_a = np.array([[4, 5, 3, 3], [5, 7, 3, 5], [3, 3, 2, 2], [3, 5, 2, 5]]) # permutation perm = np.array([[0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0]]) # permuted array_b array_b = np.dot(perm.T, np.dot(array_a, perm)) # Procrustes with no translate and scale new_a, new_b, U, e_opt = permutation_2sided( array_a, array_b, transform_mode='single_directed') assert_almost_equal(U, perm, decimal=6) assert_almost_equal(e_opt, 0., decimal=6)
def test_permutation_2sided_1trans_umeyama_nmf_translate_scale(n): r"""Test 2sided-perm single transform with "normal2" with translation and scaling.""" # generate random symmetric matrix. a = np.random.uniform(-10.0, 10.0, (n, n)) a = np.dot(a, a.T) / 2.0 # define array_b by scale-translate array_a and permuting p = generate_random_permutation_matrix(n) shift = np.random.uniform(-10.0, 10.0, n) b = np.dot(p.T, np.dot((14.7 * a + shift), p)) res = permutation_2sided(a, b, single=True, method="approx-umeyama", translate=True, scale=True) res = permutation_2sided(res.new_a, res.new_b, single=True, method="nmf", guess_p2=res.t) assert_almost_equal(res.t, p, decimal=6) assert_almost_equal(res.s, p.T, decimal=6) assert_almost_equal(res.error, 0, decimal=6)
def test_permutation_2sided_4by4_normal2(): r"""Test 2sided-perm with "normal2" by 4by4 arrays.""" # define a random matrix array_a = np.array([[4, 5, 3, 3], [5, 7, 3, 5], [3, 3, 2, 2], [3, 5, 2, 5]]) # define array_b by permuting array_a perm = np.array([[0., 0., 1., 0.], [1., 0., 0., 0.], [0., 0., 0., 1.], [0., 1., 0., 0.]]) array_b = np.dot(perm.T, np.dot(array_a, perm)) # Check _, _, array_u, e_opt = permutation_2sided( array_a, array_b, transform_mode="single_undirected", mode="normal2") assert_almost_equal(array_u, perm, decimal=6) assert_almost_equal(e_opt, 0, decimal=6)
def test_permutation_2sided_4by4_umeyama(): r"""Test 2sided-perm with umeyama guess by 4by4 arrays.""" # define a random matrix array_a = np.array([[4, 5, 3, 3], [5, 7, 3, 5], [3, 3, 2, 2], [3, 5, 2, 5]]) # define array_b by permuting array_a perm = np.array([[0., 0., 1., 0.], [1., 0., 0., 0.], [0., 0., 0., 1.], [0., 1., 0., 0.]]) array_b = np.dot(perm.T, np.dot(array_a, perm)) # Check new_a, new_b, U, e_opt = permutation_2sided( array_a, array_b, transform_mode='single_undirected', mode='umeyama') assert_almost_equal(U, perm, decimal=6) assert_almost_equal(e_opt, 0, decimal=6)
def test_permutation_2sided_2trans_kopt(n): r"""Test regular 2sided permutation with kopt.""" a = np.random.uniform(-10.0, 10.0, (n, n)) p1 = generate_random_permutation_matrix(n) p2 = generate_random_permutation_matrix(n) b = p2.dot(a.dot(p1)) result = permutation_2sided(b, a, single=False, method="k-opt", options={"k": n}) assert_almost_equal(result.s, p2, decimal=6) assert_almost_equal(result.t, p1.T, decimal=6) assert_almost_equal(result.error, 0, decimal=6)
def test_permutation_2sided_1trans_umeyama_nmf_translate_scale_pad( n, ncol, nrow, ncol2, nrow2): r"""Test "normal1" by arrays by translation and scaling and zero padding.""" # define a random matrix & symmetrize it a = np.random.uniform(-10.0, 10.0, (n, n)) a = (a + a.T) / 2.0 p = generate_random_permutation_matrix(n) b = np.dot(p.T, np.dot(a, p)) # pad the matrices with zeros a = np.concatenate((a, np.zeros((n, ncol))), axis=1) a = np.concatenate((a, np.zeros((nrow, n + ncol))), axis=0) b = np.concatenate((b, np.zeros((n, ncol2))), axis=1) b = np.concatenate((b, np.zeros((nrow2, n + ncol2))), axis=0) res = permutation_2sided( a, b, single=True, method="approx-umeyama", unpad_col=True, unpad_row=True, translate=True, scale=True, ) res = permutation_2sided( res.new_a, res.new_b, single=True, method="nmf", guess_p2=res.t, unpad_col=True, unpad_row=True, translate=True, scale=True, ) assert_almost_equal(res.t, p, decimal=6) assert_almost_equal(res.s, p.T, decimal=6) assert_almost_equal(res.error, 0, decimal=6)
def test_permutation_2sided_4by4_directed_netative_loop(): r"""Test 2sided-perm with "directed" by negative 4by4 arrays with all permutations.""" # define a random matrix array_a = np.array([[29, 79, 95, 83], [37, -86, 67, 93], [72, 85, 15, 3], [38, 39, -58, 24]]) # check with all possible permutation matrices for comb in itertools.permutations(np.arange(4)): perm = np.zeros((4, 4)) perm[np.arange(4), comb] = 1 # get array_b by permutation array_b = np.dot(perm.T, np.dot(array_a, perm)) # check result = permutation_2sided(array_a, array_b, transform_mode="single") assert_almost_equal(result["array_u"], perm, decimal=6) assert_almost_equal(result["error"], 0, decimal=6)
def test_permutation_2sided_regular2(): r"""Test regular 2sided-perm by 4by4 random arrays.""" # define a random matrix array_n = np.array([[0.74163916, 0.82661152, 0.26856538, 0.23777467], [0.06530971, 0.28429819, 0.44244327, 0.79478503], [0.83645105, 0.49704302, 0.34292989, 0.01406331], [0.04351473, 0.85459821, 0.00663386, 0.62464223]]) array_p = np.array([[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) array_q = array_p.T array_m = np.dot(np.dot(array_p, array_n), array_q) result = permutation_2sided(array_m, array_n, transform_mode="double") assert_almost_equal(result[2], array_p, decimal=6) assert_almost_equal(result[3], array_q, decimal=6) assert_almost_equal(result[4], 0, decimal=6)
def test_permutation_2sided_regular2(): r"""Test regular 2sided-perm by 4by4 random arrays.""" # define a random matrix N = np.array([[0.74163916, 0.82661152, 0.26856538, 0.23777467], [0.06530971, 0.28429819, 0.44244327, 0.79478503], [0.83645105, 0.49704302, 0.34292989, 0.01406331], [0.04351473, 0.85459821, 0.00663386, 0.62464223]]) P = np.array([[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) Q = P.T M = np.dot(np.dot(P, N), Q) new_M, new_N, new_P, new_Q, e_opt = permutation_2sided( M, N, transform_mode='double') assert_almost_equal(new_P, P, decimal=6) assert_almost_equal(new_Q, Q, decimal=6) assert_almost_equal(e_opt, 0, decimal=6)
def test_permutation_2sided_2trans_flipflop(): r"""Test regular 2sided-perm by 4by4 random arrays.""" # define a random matrix a = np.array([ [0.74163916, 0.82661152, 0.26856538, 0.23777467], [0.06530971, 0.28429819, 0.44244327, 0.79478503], [0.83645105, 0.49704302, 0.34292989, 0.01406331], [0.04351473, 0.85459821, 0.00663386, 0.62464223], ]) p = np.array([[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) b = np.dot(np.dot(p, a), p.T) result = permutation_2sided(b, a, single=False, method="flip-flop") assert_almost_equal(result.s, p.T, decimal=6) assert_almost_equal(result.t, p, decimal=6) assert_almost_equal(result.error, 0.0, decimal=6)
def test_permutation_2sided_1trans_directed_translate_scale(n): r"""Test 2sided-perm single transform with "directed" and translation, and scaling.""" a = np.random.uniform(-10.0, 10.0, (n, n)) p = generate_random_permutation_matrix(n) b = np.dot(p.T, np.dot(15.3 * a + 5.45, p)) res = permutation_2sided(a, b, single=True, method="approx-umeyama", translate=True, scale=True) # res = permutation_2sided(res.new_a, res.new_b, single=True, method="nmf", guess_p2=res.t, # translate=True, scale=True) assert_almost_equal(res.t, p, decimal=6) assert_almost_equal(res.s, p.T, decimal=6) assert_almost_equal(res.error, 0.0, decimal=6)
def test_permutation_2sided_regular_unsquared_negative(): r"""Test regular 2sided-perm by unsquared negative 6by4 random arrays.""" # build random matrix by seed 999 np.random.seed(999) array_n = np.random.randint(-5, 6, size=(6, 4)) array_n = np.float_(array_n) perm_p = np.random.permutation(np.eye(6, 6)) perm_q = np.random.permutation(np.eye(4, 4)) array_m = np.linalg.multi_dot([perm_p, array_n, perm_q]) result = permutation_2sided(array_m, array_n, transform_mode="double", iteration=500) assert_almost_equal(result[2], perm_p, decimal=6) assert_almost_equal(result[3], perm_q, decimal=6) assert_almost_equal(result[4], 0, decimal=6)