28 std::vector<double> Sample(Eigen::VectorXd& mean, Eigen::MatrixXd& covariance, std::mt19937& gen) {
30 int mean_cols = mean.size();
31 int cov_rows = covariance.rows();
32 int cov_cols = covariance.cols();
33 CHECK_EQ(mean_cols, cov_cols);
36 Eigen::LLT<Eigen::MatrixXd> decomposition(covariance);
37 Eigen::MatrixXd covariance_chol = decomposition.matrixL();
40 Eigen::VectorXd std_norm_vec(cov_rows);
41 for (
int i = 0; i < cov_rows; i++) {
42 std_norm_vec(i) = std_normal_dist_(gen);
46 Eigen::VectorXd sampled_values_raw = mean + covariance_chol * std_norm_vec;
47 std::vector<double> result(cov_rows);
48 for (
int i = 0; i < cov_rows; i++) {
49 result[i] = sampled_values_raw(i, 0);
53 Eigen::VectorXd SampleEigen(Eigen::VectorXd& mean, Eigen::MatrixXd& covariance, std::mt19937& gen) {
55 int mean_cols = mean.size();
56 int cov_rows = covariance.rows();
57 int cov_cols = covariance.cols();
58 CHECK_EQ(mean_cols, cov_cols);
61 Eigen::LLT<Eigen::MatrixXd> decomposition(covariance);
62 Eigen::MatrixXd covariance_chol = decomposition.matrixL();
65 Eigen::VectorXd std_norm_vec(cov_rows);
66 for (
int i = 0; i < cov_rows; i++) {
67 std_norm_vec(i) = std_normal_dist_(gen);
71 return mean + covariance_chol * std_norm_vec;
75 std::normal_distribution<double> std_normal_dist_;