def test_two_sided_orthogonal_single_transformation_rot_reflect_padded(): # define an arbitrary symmetric array array = np.array([[5, 2, 1], [4, 6, 1], [1, 6, 3]]) array_a = np.dot(array, array.T) # define transformation arrays as a combination of rotation and reflection theta = 16. * np.pi / 5. rot = np.array([[np.cos(theta), -np.sin(theta), 0.], [np.sin(theta), np.cos(theta), 0.], [0., 0., 1.]]) ref = 1. / 3 * np.array([[1, -2, -2], [-2, 1, -2], [-2, -2, 1]]) trans = np.dot(rot, ref) # define array_b by transforming array_a and padding with zero array_b = np.dot(np.dot(trans.T, array_a), trans) array_b = np.concatenate((array_b, np.zeros((3, 5))), axis=1) array_b = np.concatenate((array_b, np.zeros((5, 8))), axis=0) # test the "exact" mode # compute approximate Procrustes transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, single_transform=True, mode='exact') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8) assert_almost_equal(e_opt, 0, decimal=8) # test the "approx" mode # compute approximate procrustes transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, single_transform=True, mode='approx') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8)
def test_two_sided_orthogonal_single_transformation_idential(): # define an arbitrary symmetric array array_a = np.array([[2, 5, 4, 1], [5, 3, 1, 2], [8, 9, 1, 0], [1, 5, 6, 7]]) array_a = np.dot(array_a, array_a.T) array_b = np.copy(array_a) # test the "exact" mode # compute exact 2sided orthogonal Procrustes with one transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, single_transform=True, mode='exact') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(4), decimal=8) # the rotations might not be unique # assert_almost_equal(abs(array_u), np.eye(4), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8) assert_almost_equal(e_opt, 0, decimal=8) # test the "approx" mode # compute exact 2sided orthogonal Procrustes with one transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, translate=False, scale=False, single_transform=True, mode='approx') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(4), decimal=8) assert_almost_equal(abs(array_u), np.eye(4), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8) assert_almost_equal(e_opt, 0, decimal=8)
def test_two_sided_orthogonal_single_transformation_scale_rot_ref_3by3(): # define an arbitrary symmetric array array_a = (np.random.rand(3, 3) * 100).astype(int) array_a = np.dot(array_a, array_a.T) # define transformation composed of rotation and reflection theta = 5.7 * np.pi / 21.95 rot = np.array([[np.cos(theta), -np.sin(theta), 0.], [np.sin(theta), np.cos(theta), 0.], [0., 0., 1.]]) ref = np.array([[1., 0., 0.], [0., -1., 0.], [0., 0., 1.]]) trans = np.dot(ref, rot) # define array_b by transforming scaled array_a array_b = np.dot(np.dot(trans.T, 6.9 * array_a), trans) # test the "exact" mode # compute approximate procrustes transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, translate=False, scale=True, single_transform=True, mode='exact') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8) assert_almost_equal(e_opt, 0, decimal=8) # test the "approx" mode # compute approximate procrustes transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, single_transform=True, mode='approx') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8)
def test_two_sided_orthogonal_translate_scale_rotate_reflect(): r"""Test 2sided orthogonal by 3by3 array with translation, rotation and reflection.""" # define an arbitrary array array_a = np.array([[1, 3, 5], [3, 5, 7], [8, 11, 15]]) # define rotation and reflection arrays rot = make_rotation_array(1.8 * np.pi / 34.) ref = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, -1]]) # define array_b by transforming scaled-and-traslated array_a shift = np.array([[16., 41., 33.], [16., 41., 33.], [16., 41., 33.]]) array_b = np.dot(np.dot(ref, 23.5 * array_a + shift), rot) # compute procrustes transformation result = orthogonal_2sided(array_a, array_b, translate=True, scale=True, single_transform=False) # check transformation array and error assert_almost_equal(np.dot(result["array_p"], result["array_p"].T), np.eye(3), decimal=6) assert_almost_equal(np.dot(result["array_q"], result["array_q"].T), np.eye(3), decimal=6) assert_almost_equal(abs(np.linalg.det(result["array_p"])), 1.0, decimal=6) assert_almost_equal(abs(np.linalg.det(result["array_q"])), 1.0, decimal=6) # transformation should return zero error assert_almost_equal(result["error"], 0, decimal=6)
def test_two_sided_orthogonal_rotate_reflect_pad(): # define an arbitrary array array_a = np.array([[1., 4.], [6., 7]]) # rotation by 30 degrees theta = np.pi / 6 rot1 = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) array_b = np.dot(array_a, rot1) # reflection 1 in x-axis ref1 = np.array([[1, 0], [0, -1]]) array_b = np.dot(ref1, array_b) # rotation by -45 degrees theta = -np.pi / 4 rot2 = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) array_b = np.dot(array_a, rot2) array_b = np.concatenate((array_b, np.zeros((2, 4))), axis=1) array_b = np.concatenate((array_b, np.zeros((2, 6))), axis=0) # compute Procrustes transformation new_a, new_b, array_u1, array_u2, e_opt = orthogonal_2sided( array_a, array_b, translate=True, scale=True, single_transform=False) # check transformation array and error # Check orthogonality # product is identity matrix and determinant is 1 or -1 assert_almost_equal(np.dot(array_u1, array_u1.T), np.eye(2), decimal=6) assert_almost_equal(np.dot(array_u2, array_u2.T), np.eye(2), decimal=6) assert_almost_equal(abs(np.linalg.det(array_u1)), 1.0, decimal=6) assert_almost_equal(abs(np.linalg.det(array_u2)), 1.0, decimal=6) # transformation should return zero error assert_almost_equal(e_opt, 0, decimal=6)
def test_two_sided_orthogonal_rotate_reflect(): r"""Test 2sided orthogonal by 3by3 array with rotation and reflection.""" # define an arbitrary array array_a = np.array([[41.8, 15.5, 24.4], [53.5, 55.2, 57.1], [58.2, 31.6, 35.9]]) # define rotation and reflection arrays rot = make_rotation_array(-np.pi / 6) ref = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, -1]]) # define array_b by transforming array_a array_b = np.dot(np.dot(ref, array_a), rot) # compute procrustes transformation result = orthogonal_2sided(array_a, array_b, translate=True, scale=True, single_transform=False) # check transformation array orthogonality assert_almost_equal(np.dot(result["array_p"], result["array_p"].T), np.eye(3), decimal=6) assert_almost_equal(np.dot(result["array_q"], result["array_q"].T), np.eye(3), decimal=6) assert_almost_equal(np.abs(np.linalg.det(result["array_p"])), 1.0, decimal=6) assert_almost_equal(np.abs(np.linalg.det(result["array_q"])), 1.0, decimal=6) # transformation should return zero error assert_almost_equal(result["error"], 0, decimal=6)
def test_two_sided_orthogonal_single_transformation_scale_3by3(): r"""Test 2sided orthogonal by 3by3 array with translation and scaling.""" # define an arbitrary symmetric array array_a = np.array([[12.43, 16.15, 17.61], [11.4, 21.5, 16.7], [16.4, 19.4, 14.9]]) array_a = np.dot(array_a, array_a.T) # define transformation composed of rotation and reflection theta = np.pi / 2 rot = np.array([[np.cos(theta), -np.sin(theta), 0.], [np.sin(theta), np.cos(theta), 0.], [0., 0., 1.]]) ref = np.array([[1, -2, -2], [-2, 1, -2], [-2, -2, 1]]) trans = np.dot(ref, rot) / 3 # define array_b by transforming scaled-and-translated array_a array_b = np.dot(np.dot(trans.T, 6.9 * array_a), trans) # check transformation array and error result = orthogonal_2sided(array_a, array_b, translate=False, scale=True, single_transform=True) assert_almost_equal(np.dot(result["array_u"], result["array_u"].T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(result["array_u"])), 1.0, decimal=8) assert_almost_equal(result["error"], 0, decimal=8)
def test_two_sided_orthogonal_single_transformation_scale_rot_ref_2by2(): r"""Test 2sided orthogonal by 3by3 array with scaling, rotation and reflection.""" # define an arbitrary symmetric array array_a = np.array([[124.72, 147.93], [120.5, 59.41]]) array_a = np.dot(array_a, array_a.T) # define transformation composed of rotation and reflection theta = 5.5 * np.pi / 6.5 rot = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) ref = np.array([[1., 0.], [0., -1.]]) trans = np.dot(ref, rot) # define array_b by transforming scaled array_a array_b = np.dot(np.dot(trans.T, 88.89 * array_a), trans) # check transformation array and error result = orthogonal_2sided(array_a, array_b, translate=False, scale=True, single_transform=True) assert_almost_equal(np.dot(result["array_u"], result["array_u"].T), np.eye(2), decimal=8) assert_almost_equal(abs(np.linalg.det(result["array_u"])), 1.0, decimal=8) assert_almost_equal(result["error"], 0, decimal=8)
def test_two_sided_orthogonal_single_transformation_scale_rot_ref_3by3(): r"""Test 2sided orthogonal by 3by3 array with single translation, scling and rotation.""" # define an arbitrary symmetric array array_a = (np.random.rand(3, 3) * 100).astype(int) array_a = np.dot(array_a, array_a.T) # define transformation composed of rotation and reflection theta = 5.7 * np.pi / 21.95 rot = np.array([[np.cos(theta), -np.sin(theta), 0.], [np.sin(theta), np.cos(theta), 0.], [0., 0., 1.]]) ref = np.array([[1., 0., 0.], [0., -1., 0.], [0., 0., 1.]]) trans = np.dot(ref, rot) # define array_b by transforming scaled array_a array_b = np.dot(np.dot(trans.T, 6.9 * array_a), trans) # check transformation array and error result = orthogonal_2sided(array_a, array_b, translate=False, scale=True, single_transform=True) assert_almost_equal(np.dot(result["array_u"], result["array_u"].T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(result["array_u"])), 1.0, decimal=8) assert_almost_equal(result["error"], 0, decimal=8)
def test_two_sided_orthogonal_translate_scale_rotate_reflect_3by3(): r"""Test 2sided orthogonal by a another 3by3 array with translation, rotation and reflection.""" # define an arbitrary array array_a = np.array([[141.58, 315.25, 524.14], [253.25, 255.52, 357.51], [358.2, 131.6, 135.59]]) # define rotation and reflection arrays rot = make_rotation_array(17.54 * np.pi / 6.89) ref = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, -1]]) # define array_b by transforming scaled-and-translated array_a shift = np.array([[146.56, 441.67, 343.56], [146.56, 441.67, 343.56], [146.56, 441.67, 343.56]]) array_b = np.dot(np.dot(ref, 79.89 * array_a + shift), rot) # compute procrustes transformation result = orthogonal_2sided(array_a, array_b, translate=True, scale=True, single_transform=False) # check transformation array and error assert_almost_equal(np.dot(result["array_p"], result["array_p"].T), np.eye(3), decimal=6) assert_almost_equal(np.dot(result["array_q"], result["array_q"].T), np.eye(3), decimal=6) assert_almost_equal(abs(np.linalg.det(result["array_p"])), 1.0, decimal=6) assert_almost_equal(abs(np.linalg.det(result["array_q"])), 1.0, decimal=6) assert_almost_equal(result["error"], 0, decimal=6)
def test_two_sided_orthogonal_rotate_reflect_pad(): r"""Test 2sided orthogonal by 3by3 array with rotation, reflection and zero padding.""" # define an arbitrary array array_a = np.array([[1., 4.], [6., 7]]) # rotation by -45 degrees theta = -np.pi / 4 rot2 = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) array_b = np.dot(array_a, rot2) array_b = np.concatenate((array_b, np.zeros((2, 4))), axis=1) array_b = np.concatenate((array_b, np.zeros((2, 6))), axis=0) # compute Procrustes transformation result = orthogonal_2sided(array_a, array_b, translate=True, scale=True, single_transform=False) # check transformation array and error assert_almost_equal(np.dot(result["array_p"], result["array_p"].T), np.eye(2), decimal=6) assert_almost_equal(np.dot(result["array_q"], result["array_q"].T), np.eye(2), decimal=6) assert_almost_equal(abs(np.linalg.det(result["array_p"])), 1.0, decimal=6) assert_almost_equal(abs(np.linalg.det(result["array_q"])), 1.0, decimal=6) assert_almost_equal(result["error"], 0, decimal=6)
def test_two_sided_orthogonal_identical(): r"""Test 2-sided orthogonal with identical matrix.""" # case of identical square arrays array_a = np.arange(16).reshape(4, 4) array_b = np.copy(array_a) result = orthogonal_2sided(array_a, array_b, single_transform=False) # check transformation array is identity assert_almost_equal(np.linalg.det(result["array_p"]), 1.0, decimal=6) assert_almost_equal(np.linalg.det(result["array_q"]), 1.0, decimal=6) assert_almost_equal(result["array_p"], np.eye(4), decimal=6) assert_almost_equal(result["array_q"], np.eye(4), decimal=6) assert_almost_equal(result["error"], 0., decimal=6)
def test_two_sided_orthogonal_identical(n): r"""Test 2-sided orthogonal with identical matrix.""" # case of identical square arrays array_a = np.random.uniform(-10.0, 10.0, (n, n)) array_b = np.copy(array_a) result = orthogonal_2sided(array_a, array_b, single=False) # check transformation array is identity assert_almost_equal(np.linalg.det(result.s), 1.0, decimal=6) assert_almost_equal(np.linalg.det(result.t), 1.0, decimal=6) assert_almost_equal(result.s, np.eye(n), decimal=6) assert_almost_equal(result.t, np.eye(n), decimal=6) assert_almost_equal(result.error, 0., decimal=6)
def test_two_sided_orthogonal_single_transformation_identical(n): r"""Test 2sided orthogonal with identical arrays.""" # define an arbitrary symmetric array array_a = np.random.uniform(-10.0, 10.0, (n, n)) array_a = np.dot(array_a, array_a.T) array_b = np.copy(array_a) result = orthogonal_2sided(array_a, array_b, single=True) # check transformation array and error assert_almost_equal(np.dot(result.t, result.t.T), np.eye(n), decimal=8) assert_almost_equal(abs(result.t), np.eye(n), decimal=8) assert_almost_equal(abs(np.linalg.det(result.t)), 1.0, decimal=8) assert_almost_equal(result.error, 0, decimal=8)
def test_two_sided_orthogonal_identical(): # case of identical square arrays array_a = np.arange(16).reshape(4, 4) array_b = np.copy(array_a) new_a, new_b, array_u1, array_u2, e_opt = orthogonal_2sided( array_a, array_b, single_transform=False) # check transformation array is identity assert_almost_equal(np.linalg.det(array_u1), 1.0, decimal=6) assert_almost_equal(np.linalg.det(array_u2), 1.0, decimal=6) assert_almost_equal(array_u1, np.eye(4), decimal=6) assert_almost_equal(array_u2, np.eye(4), decimal=6) assert_almost_equal(e_opt, 0., decimal=6)
def test_two_sided_orthogonal_single_transformation_scale_3by3(): # define an arbitrary symmetric array array_a = np.array([[12.43, 16.15, 17.61], [11.4, 21.5, 16.7], [16.4, 19.4, 14.9]]) array_a = np.dot(array_a, array_a.T) # define transformation composed of rotation and reflection theta = np.pi / 2 rot = np.array([[np.cos(theta), -np.sin(theta), 0.], [np.sin(theta), np.cos(theta), 0.], [0., 0., 1.]]) ref = np.array([[1, -2, -2], [-2, 1, -2], [-2, -2, 1]]) trans = np.dot(ref, rot) / 3 # define array_b by transforming scaled-and-translated array_a array_b = np.dot(np.dot(trans.T, 6.9 * array_a), trans) # test the "exact" mode # compute approximate procrustes transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, translate=False, scale=True, single_transform=True, mode='exact') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8) assert_almost_equal(e_opt, 0, decimal=8) # test the "approx" mode # compute approximate procrustes transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, translate=True, scale=True, single_transform=True, mode='approx') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8)
def test_two_sided_orthogonal_single_transformation_random_orthogonal(): r"""Test 2sided orthogonal by 3by3 array.""" # define random array array_a = np.array([[0, 5, 8, 6], [5, 0, 5, 1], [8, 5, 0, 2], [6, 1, 2, 0]]) ortho = np.array([[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]) array_b = np.dot(np.dot(ortho.T, array_a), ortho) # check transformation array and error result = orthogonal_2sided(array_a, array_b, single_transform=True) assert_almost_equal(np.dot(result["array_u"], result["array_u"].T), np.eye(4), decimal=8) assert_almost_equal(abs(np.linalg.det(result["array_u"])), 1.0, decimal=8) assert_almost_equal(result["error"], 0, decimal=8)
def test_two_sided_orthogonal_single_transformation_random_orthogonal(n): r"""Test 2sided orthogonal by 3by3 array.""" # define random symmetric array array_a = np.random.uniform(-10.0, 10.0, (n, n)) array_a = (array_a + array_a.T) / 2.0 # Obtain random orthogonal matrix. ortho = ortho_group.rvs(n) array_b = np.dot(np.dot(ortho.T, array_a), ortho) # check transformation array and error result = orthogonal_2sided(array_a, array_b, single=True) assert_almost_equal(np.dot(result.t, result.t.T), np.eye(n), decimal=8) assert_almost_equal(abs(np.linalg.det(result.t)), 1.0, decimal=8) assert_almost_equal(result.error, 0, decimal=8)
def test_two_sided_orthogonal_rotate_reflect(m, n): r"""Test two sided orthogonal with rotation and reflected.""" # define an arbitrary array array_a = np.random.uniform(-10.0, 10.0, (m, n)) # define rotation and reflection arrays rot = ortho_group.rvs(n) ref = generate_random_reflection_matrix(m) # define array_b by transforming array_a array_b = np.dot(np.dot(ref, array_a), rot) # compute procrustes transformation result = orthogonal_2sided(array_a, array_b, single=False) # check transformation array orthogonality assert_almost_equal(np.dot(result.s, result.s.T), np.eye(m), decimal=6) assert_almost_equal(np.dot(result.t, result.t.T), np.eye(n), decimal=6) assert_almost_equal(result.error, 0, decimal=6)
def test_two_sided_orthogonal_single_transformation_identical(): r"""Test 2sided orthogonal with identical arrays.""" # define an arbitrary symmetric array array_a = np.array([[2, 5, 4, 1], [5, 3, 1, 2], [8, 9, 1, 0], [1, 5, 6, 7]]) array_a = np.dot(array_a, array_a.T) array_b = np.copy(array_a) result = orthogonal_2sided(array_a, array_b, single_transform=True) # check transformation array and error assert_almost_equal(np.dot(result["array_u"], result["array_u"].T), np.eye(4), decimal=8) assert_almost_equal(abs(result["array_u"]), np.eye(4), decimal=8) assert_almost_equal(abs(np.linalg.det(result["array_u"])), 1.0, decimal=8) assert_almost_equal(result["error"], 0, decimal=8)
def test_two_sided_orthogonal_single_transformation_random_orthogonal(): # define random array array_a = np.array([[0, 5, 8, 6], [5, 0, 5, 1], [8, 5, 0, 2], [6, 1, 2, 0]]) ortho = np.array([[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]) array_b = np.dot(np.dot(ortho.T, array_a), ortho) # test the "exact" mode # compute approximate Procrustes transformation new_a, new_b, array_u, e_opt = orthogonal_2sided(array_a, array_b, single_transform=True, mode='exact') # check transformation array and error assert_almost_equal(np.dot(array_u, array_u.T), np.eye(4), decimal=8) assert_almost_equal(abs(np.linalg.det(array_u)), 1.0, decimal=8) assert_almost_equal(e_opt, 0, decimal=8)
def test_two_sided_orthogonal_rotate_reflect(): # define an arbitrary array array_a = np.array([[41.8, 15.5, 24.4], [53.5, 55.2, 57.1], [58.2, 31.6, 35.9]]) # define rotation and reflection arrays rot = make_rotation_array(-np.pi / 6) ref = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, -1]]) # define array_b by transforming array_a array_b = np.dot(np.dot(ref, array_a), rot) # compute procrustes transformation new_a, new_b, array_u1, array_u2, e_opt = orthogonal_2sided( array_a, array_b, translate=True, scale=True, single_transform=False) # check transformation array orthogonality assert_almost_equal(np.dot(array_u1, array_u1.T), np.eye(3), decimal=6) assert_almost_equal(np.dot(array_u2, array_u2.T), np.eye(3), decimal=6) assert_almost_equal(np.linalg.det(array_u1), 1.0, decimal=6) assert_almost_equal(np.linalg.det(array_u2), 1.0, decimal=6) # transformation should return zero error assert_almost_equal(e_opt, 0, decimal=6)
def test_two_sided_orthogonal_single_transformation_with_scaling(n): r"""Test 2sided orthogonal by array with scaling, rotation and reflection.""" # define an arbitrary symmetric array array_a = np.random.uniform(-10.0, 10.0, (n, n)) array_a = np.dot(array_a, array_a.T) # define transformation composed of rotation and reflection rot = ortho_group.rvs(n) ref = generate_random_reflection_matrix(n) trans = np.dot(ref, rot) # define array_b by transforming scaled array_a array_b = np.dot(np.dot(trans.T, 88.89 * array_a), trans) # check transformation array and error result = orthogonal_2sided(array_a, array_b, single=True, translate=False, scale=True) assert_almost_equal(np.dot(result.t, result.t.T), np.eye(n), decimal=8) assert_almost_equal(abs(np.linalg.det(result.t)), 1.0, decimal=8) assert_almost_equal(result.error, 0, decimal=8)
def test_two_sided_orthogonal_translate_scale_rotate_reflect_3by3(): # define an arbitrary array array_a = np.array([[141.58, 315.25, 524.14], [253.25, 255.52, 357.51], [358.2, 131.6, 135.59]]) # define rotation and reflection arrays rot = make_rotation_array(17.54 * np.pi / 6.89) ref = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, -1]]) # define array_b by transforming scaled-and-translated array_a shift = np.array([[146.56, 441.67, 343.56], [146.56, 441.67, 343.56], [146.56, 441.67, 343.56]]) array_b = np.dot(np.dot(ref, 79.89 * array_a + shift), rot) # compute procrustes transformation new_a, new_b, array_u1, array_u2, e_opt = orthogonal_2sided( array_a, array_b, translate=True, scale=True, single_transform=False) # check transformation array and error assert_almost_equal(np.dot(array_u1, array_u1.T), np.eye(3), decimal=6) assert_almost_equal(np.dot(array_u2, array_u2.T), np.eye(3), decimal=6) assert_almost_equal(abs(np.linalg.det(array_u1)), 1.0, decimal=6) assert_almost_equal(abs(np.linalg.det(array_u2)), 1.0, decimal=6) # transformation should return zero error assert_almost_equal(e_opt, 0, decimal=6)
def test_two_sided_orthogonal_single_transformation_rot_reflect_padded(): r"""Test 2sided orthogonal by array with translation, rotation, reflection and zero padding.""" # define an arbitrary symmetric array array = np.array([[5, 2, 1], [4, 6, 1], [1, 6, 3]]) array_a = np.dot(array, array.T) # define transformation arrays as a combination of rotation and reflection theta = 16. * np.pi / 5. rot = np.array([[np.cos(theta), -np.sin(theta), 0.], [np.sin(theta), np.cos(theta), 0.], [0., 0., 1.]]) ref = 1. / 3 * np.array([[1, -2, -2], [-2, 1, -2], [-2, -2, 1]]) trans = np.dot(rot, ref) # define array_b by transforming array_a and padding with zero array_b = np.dot(np.dot(trans.T, array_a), trans) array_b = np.concatenate((array_b, np.zeros((3, 5))), axis=1) array_b = np.concatenate((array_b, np.zeros((5, 8))), axis=0) # check transformation array and error. result = orthogonal_2sided(array_a, array_b, single_transform=True) assert_almost_equal(np.dot(result["array_u"], result["array_u"].T), np.eye(3), decimal=8) assert_almost_equal(abs(np.linalg.det(result["array_u"])), 1.0, decimal=8) assert_almost_equal(result["error"], 0, decimal=8)
def test_two_sided_orthogonal_with_translation_and_scaling(m, n): r"""Test two sided orthogonal with translation and scaling.""" # define an arbitrary array array_a = np.random.uniform(-10.0, 10.0, (m, n)) # define rotation and reflection arrays rot = ortho_group.rvs(n) ref = generate_random_reflection_matrix(m) # define array_b by transforming scaled-and-translated array_a shift = np.random.uniform(-10.0, 10.0, n) array_b = 23.4 * np.dot(np.dot(ref, array_a), rot) + shift # compute procrustes transformation result = orthogonal_2sided(array_a, array_b, single=False, translate=True, scale=True) # check transformation array and error assert_almost_equal(np.dot(result.s, result.s.T), np.eye(m), decimal=6) assert_almost_equal(np.dot(result.t, result.t.T), np.eye(n), decimal=6) assert_almost_equal(abs(np.linalg.det(result.s)), 1.0, decimal=6) assert_almost_equal(abs(np.linalg.det(result.t)), 1.0, decimal=6) # transformation should return zero error assert_almost_equal(result.error, 0, decimal=4)
def test_two_sided_orthogonal_rotate_with_unpadding(m, n, ncols, nrows): r"""Test two sided orthogonal with unpadding.""" # define an arbitrary array array_a = np.random.uniform(-10.0, 10.0, (m, n)) # define rotation and reflection arrays rot = ortho_group.rvs(n) array_b = np.dot(array_a, rot) array_b = np.concatenate((array_b, np.zeros((m, ncols))), axis=1) array_b = np.concatenate((array_b, np.zeros((nrows, n + ncols))), axis=0) # compute Procrustes transformation result = orthogonal_2sided(array_a, array_b, single=False, translate=True, scale=True, unpad_col=True, unpad_row=True) # check transformation array and error assert_almost_equal(np.dot(result.s, result.s.T), np.eye(m), decimal=6) assert_almost_equal(np.dot(result.t, result.t.T), np.eye(n), decimal=6) assert_almost_equal(abs(np.linalg.det(result.s)), 1.0, decimal=6) assert_almost_equal(abs(np.linalg.det(result.t)), 1.0, decimal=6) assert_almost_equal(result.error, 0, decimal=6)
def test_two_sided_orthogonal_single_transformation_rot_reflect_unpadded( n, ncol, nrow): r"""Test 2sided orthogonal by array with translation, rotation, reflection and unpadding.""" # define an arbitrary symmetric array array = np.random.uniform(-10.0, 10.0, (n, n)) array_a = np.dot(array, array.T) # define transformation arrays as a combination of rotation and reflection rot = ortho_group.rvs(n) ref = generate_random_reflection_matrix(n) trans = np.dot(rot, ref) # define array_b by transforming array_a and padding with zero array_b = np.dot(np.dot(trans.T, array_a), trans) 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 transformation array and error. result = orthogonal_2sided(array_a, array_b, single=True, unpad_col=True, unpad_row=True) assert_almost_equal(np.dot(result.t, result.t.T), np.eye(n), decimal=8) assert_almost_equal(abs(np.linalg.det(result.t)), 1.0, decimal=8) assert_almost_equal(result.error, 0, decimal=8)