def test_skew_symmetrize_handles_blocksizes(blocksize): dim = 3 # decides when to transpose vector = np.random.randn(dim, blocksize) input_matrix = skew_symmetrize(vector) test_matrix = _skew_symmetrize(vector) assert_allclose(input_matrix, test_matrix)
def test_skew_symmetrize_transposes_for_two_dimensions(): dim = 3 blocksize = 32 in_vector = np.random.randn(blocksize, dim) # switched order vector = in_vector.T input_matrix = skew_symmetrize(in_vector) test_matrix = _skew_symmetrize(vector) assert_allclose(input_matrix, test_matrix)
def test_inv_skew_symmetrize_correctness(blocksize): dim = 3 vector = np.random.randn(dim, blocksize) input_matrix = _skew_symmetrize(vector) # reshape and squeeze because we are testing a single vector test_vector = _inv_skew_symmetrize(input_matrix) assert test_vector.shape == (3, blocksize) assert_allclose(test_vector, vector)
def test_skew_symmetrize_sq_correctness_in_three_dimensions(blocksize): dim = 3 vector = np.random.randn(dim, blocksize) correct_matrix = _skew_symmetrize(vector).reshape(dim, dim, -1) correct_matrix = np.einsum("ijk,jlk->ilk", correct_matrix, correct_matrix) # reshape and squeeze because we are testing a single vector test_matrix = _skew_symmetrize_sq(vector) assert test_matrix.shape == (3, 3, blocksize) assert_allclose(test_matrix, correct_matrix)
def test_skew_symmetrize_correctness_in_two_dimensions(): dim = 3 vector = np.hstack((np.random.randn(2), 1)) test_matrix = _skew_symmetrize(vector[:, np.newaxis]) # Reshape for checking correctness and drop the last dimension test_matrix = test_matrix.reshape(dim, dim, -1) test_matrix = test_matrix[:-1, :-1, 0] correct_matrix = np.array([[0.0, -1.0], [1.0, 0.0]]) assert_allclose(test_matrix, correct_matrix)
def test_skew_symmetrize_against_input_shapes(ndim): dim = 3 blocksize = 32 if ndim == 1: in_vector = np.random.randn(dim) vector = in_vector.reshape(dim, 1) elif ndim == 2: in_vector = np.random.randn(dim, blocksize) vector = in_vector input_matrix = skew_symmetrize(in_vector) test_matrix = _skew_symmetrize(vector) assert_allclose(input_matrix, test_matrix)
def test_skew_symmetrize_correctness_in_three_dimensions(blocksize): dim = 3 vector = np.random.randn(dim, blocksize) correct_matrix = np.zeros((dim * dim, blocksize)) correct_matrix[1] = -vector[2] correct_matrix[2] = vector[1] correct_matrix[3] = vector[2] correct_matrix[5] = -vector[0] correct_matrix[6] = -vector[1] correct_matrix[7] = vector[0] correct_matrix = correct_matrix.reshape(dim, dim, -1) # reshape and squeeze because we are testing a single vector test_matrix = _skew_symmetrize(vector) assert test_matrix.shape == (3, 3, blocksize) assert_allclose(test_matrix, correct_matrix)
def test_inv_skew_symmetrize_against_input_shapes(ndim): dim = 3 # First generate a skew-symmetric matrix from vector if ndim == 1: blocksize = 1 else: blocksize = 16 input_vector = np.random.randn(dim, blocksize) input_matrix = _skew_symmetrize(input_vector) if ndim == 1: input_matrix = input_matrix.reshape(dim ** 2) elif ndim == 2: input_matrix = input_matrix.reshape(dim ** 2, blocksize) elif ndim == 3: input_matrix = input_matrix.reshape(dim, dim, blocksize) test_vector = inv_skew_symmetrize(input_matrix) assert_allclose(test_vector, input_vector)
def skew_symmetrize(vector): vector = format_vector_shape(vector) return _skew_symmetrize(vector)