1 #ifndef HOPS_MULTIVARIATEGAUSSIAN_HPP
2 #define HOPS_MULTIVARIATEGAUSSIAN_HPP
4 #define _USE_MATH_DEFINES
6 #include <Eigen/Cholesky>
36 typename MatrixType::Scalar logNormalizationConstant;
40 mean(std::move(mean)),
41 covariance(std::move(covariance)) {
42 Eigen::LLT<MatrixType> solver(this->covariance);
43 if (!this->covariance.isApprox(this->covariance.transpose()) || solver.info() == Eigen::NumericalIssue) {
44 throw std::domain_error(
45 "Possibly non semi-positive definite covariance in initialization for MultivariateGaussian.");
47 Eigen::Matrix<typename MatrixType::Scalar, Eigen::Dynamic, Eigen::Dynamic> matrixL = solver.matrixL();
48 Eigen::Matrix<typename MatrixType::Scalar, Eigen::Dynamic, Eigen::Dynamic> inverseMatrixL = matrixL.inverse();
49 inverseCovariance = inverseMatrixL.transpose() * inverseMatrixL;
51 logNormalizationConstant = -
static_cast<typename MatrixType::Scalar
>(this->mean.rows()) / 2 *
53 - matrixL.diagonal().array().log().sum();
56 typename MatrixType::Scalar
58 return -logNormalizationConstant +
59 0.5 *
static_cast<typename MatrixType::Scalar
>((x - mean).transpose() * inverseCovariance * (x - mean));
63 return -inverseCovariance * (x - mean);
67 return inverseCovariance;
79 #endif //HOPS_MULTIVARIATEGAUSSIAN_HPP