def test_equals(self): """ Test equality operator """ vec_space1 = vspc.VectorSpaceArrays(weights=np.array([1., 1., 1.])) vec_space2 = vspc.VectorSpaceArrays(weights=[1., 1., 1.]) vec_space3 = vspc.VectorSpaceArrays(weights=[2., 4., 6.]) vec_space4 = vspc.VectorSpaceArrays(weights=np.diag(np.ones(3) * 2.)) self.assertEqual(vec_space1, vec_space2) self.assertNotEqual(vec_space1, vec_space3) self.assertNotEqual(vec_space1, vec_space4)
def test_lin_combine(self): """ Test that linear combinations are correctly computed """ # Set test tolerances rtol = 1e-10 atol = 1e-12 # Generate data num_states = 100 num_vecs = 30 num_modes = 10 vecs_array = ( np.random.random((num_states, num_vecs)) + 1j * np.random.random((num_states, num_vecs))) coeffs_array = ( np.random.random((num_vecs, num_modes)) + 1j * np.random.random((num_vecs, num_modes))) modes_array_true = vecs_array.dot(coeffs_array) # Do computation using a vector space object. Check an explicit # mode_indices argument, as well as a None value. vec_space = vspc.VectorSpaceArrays() mode_indices_trunc = np.random.randint( 0, high=num_modes, size=num_modes // 2) for mode_idxs_arg, mode_idxs_vals in zip( [None, mode_indices_trunc], [range(num_modes), mode_indices_trunc]): modes_array = vec_space.lin_combine( vecs_array, coeffs_array, coeff_array_col_indices=mode_idxs_arg) np.testing.assert_allclose( modes_array, modes_array_true[:, mode_idxs_vals], rtol=rtol, atol=atol)
def test_inner_product_arrays(self): """ Test that inner product arrays are correctly computed """ # Set test tolerances rtol = 1e-10 atol = 1e-12 # Generate data num_states = 10 num_rows = 2 num_cols = 3 row_array = (np.random.random( (num_states, num_rows)) + 1j * np.random.random( (num_states, num_rows))) col_array = (np.random.random( (num_states, num_cols)) + 1j * np.random.random( (num_states, num_cols))) # Test different weights weights_1d = np.random.random(num_states) weights_2d = np.random.random((num_states, num_states)) weights_2d = 0.5 * (weights_2d + weights_2d.T) for weights in [None, weights_1d, weights_2d]: # Reshape weights if weights is None: weights_array = np.eye(num_states) elif weights.ndim == 1: weights_array = np.diag(weights) else: weights_array = weights # Correct inner product values ip_array_true = row_array.conj().T.dot( weights_array.dot(col_array)) ip_array_symm_true = row_array.conj().T.dot( weights_array.dot(row_array)) # Compute inner products using vec space object vec_space = vspc.VectorSpaceArrays(weights=weights) ip_array = vec_space.compute_inner_product_array( row_array, col_array) ip_array_symm = vec_space.compute_symm_inner_product_array( row_array) np.testing.assert_allclose(ip_array, ip_array_true, rtol=rtol, atol=atol)