def test_mat_from_diag_triu_tril(self): diag = gs.array([9., 9., 9.]) triu = gs.array([1., 2., 3.]) tril = -1 * triu mat = gs.mat_from_diag_triu_tril(diag, triu, tril) expected = gs.array([[9., 1., 2.], [ -1., 9., 3., ], [-2., -3., 9.]]) result = mat batch_diag = gs.eye(3) batch_triu = gs.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]) batch_tril = -1 * batch_triu batch_mat = gs.mat_from_diag_triu_tril(batch_diag, batch_triu, batch_tril) batch_expected = gs.array([[[1., 1., 2.], [-1., 0., 3.], [-2., -3., 0.]], [[0., 4., 5.], [-4., 1., 6.], [-5., -6., 0.]], [[0., 7., 8.], [-7., 0., 9.], [-8., -9., 1.]]]) batch_result = batch_mat self.assertAllClose(expected, result) self.assertAllClose(batch_expected, batch_result)
def test_mat_from_diag_triu_tril(self): diag = gs.array([9.0, 9.0, 9.0]) triu = gs.array([1.0, 2.0, 3.0]) tril = -1 * triu mat = gs.mat_from_diag_triu_tril(diag, triu, tril) expected = gs.array([ [9.0, 1.0, 2.0], [ -1.0, 9.0, 3.0, ], [-2.0, -3.0, 9.0], ]) result = mat batch_diag = gs.eye(3) batch_triu = gs.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) batch_tril = -1 * batch_triu batch_mat = gs.mat_from_diag_triu_tril(batch_diag, batch_triu, batch_tril) batch_expected = gs.array([ [[1.0, 1.0, 2.0], [-1.0, 0.0, 3.0], [-2.0, -3.0, 0.0]], [[0.0, 4.0, 5.0], [-4.0, 1.0, 6.0], [-5.0, -6.0, 0.0]], [[0.0, 7.0, 8.0], [-7.0, 0.0, 9.0], [-8.0, -9.0, 1.0]], ]) batch_result = batch_mat self.assertAllClose(expected, result) self.assertAllClose(batch_expected, batch_result)
def samples_sym(self, mean_vec, cov, n_samples): """Generate symmetric matrices.""" n = self.mean.shape[-1] samples_euclidean = gs.random.multivariate_normal(mean_vec, cov, (n_samples,)) diag = samples_euclidean[:, :n] off_diag = samples_euclidean[:, n:] / gs.sqrt(2.0) samples_sym = gs.mat_from_diag_triu_tril( diag=diag, tri_upp=off_diag, tri_low=off_diag ) return samples_sym
def __sample_spd(self, samples): n = self.mean.shape[-1] sym_matrix = self.manifold.logm(self.mean) mean_euclidean = gs.hstack( (gs.diagonal(sym_matrix)[None, :], gs.sqrt(2.0) * gs.triu_to_vec(sym_matrix, k=1)[None, :]))[0] samples_euclidean = gs.random.multivariate_normal( mean_euclidean, self.cov, (samples, )) diag = samples_euclidean[:, :n] off_diag = samples_euclidean[:, n:] / gs.sqrt(2.0) samples_sym = gs.mat_from_diag_triu_tril(diag=diag, tri_upp=off_diag, tri_low=off_diag) samples_spd = self.manifold.expm(samples_sym) return samples_spd