33 #ifndef __EIGENMULTIVARIATENORMAL_HPP
34 #define __EIGENMULTIVARIATENORMAL_HPP
36 #include <Eigen/Dense>
49 template<
typename Scalar>
52 static std::mt19937 rng;
53 mutable std::normal_distribution<Scalar> norm;
57 template<
typename Index>
58 inline const Scalar operator() (Index, Index = 0)
const {
return norm(rng); }
59 inline void seed(
const uint64_t &s) { rng.seed(s); }
62 template<
typename Scalar>
65 template<
typename Scalar>
67 {
enum { Cost = 50 * NumTraits<Scalar>::MulCost, PacketAccess =
false, IsRepeatable =
false }; };
75 template<
typename Scalar>
78 Matrix< Scalar, Dynamic, 1> _mean;
83 void set_covar(
const Matrix<Scalar,Dynamic,Dynamic> &covar) { _covar = covar; }
84 void set_transform(
const Matrix<Scalar,Dynamic,Dynamic> &transform) { _transform = transform; }
87 Matrix<Scalar,Dynamic,Dynamic> _covar;
88 Matrix<Scalar,Dynamic,Dynamic> _transform;
91 SelfAdjointEigenSolver<Matrix<Scalar,Dynamic,Dynamic> > _eigenSolver;
95 const uint64_t &seed=std::mt19937::default_seed)
96 :_use_cholesky(use_cholesky)
101 const bool &use_cholesky=
false,
const uint64_t &seed=std::mt19937::default_seed)
102 :_use_cholesky(use_cholesky)
109 void setMean(
const Matrix<Scalar,Dynamic,1>& mean) { _mean = mean; }
110 void setCovar(
const Matrix<Scalar,Dynamic,Dynamic>& covar)
120 Eigen::LLT<Eigen::Matrix<Scalar,Dynamic,Dynamic> > cholSolver(_covar);
125 if (cholSolver.info()==Eigen::Success)
128 _transform = cholSolver.matrixL();
132 throw std::runtime_error(
"Failed computing the Cholesky decomposition. Use solver instead");
137 _eigenSolver = SelfAdjointEigenSolver<Matrix<Scalar,Dynamic,Dynamic> >(_covar);
138 _transform = _eigenSolver.eigenvectors()*_eigenSolver.eigenvalues().cwiseMax(0).cwiseSqrt().asDiagonal();
144 Matrix<Scalar,Dynamic,-1>
samples(
int nn,
double factor)
146 return ((_transform * Matrix<Scalar,Dynamic,-1>::NullaryExpr(_covar.rows(),nn,randN))*factor).colwise() + _mean;
149 Matrix<Scalar,Dynamic,-1> samples_ind(
int nn,
double factor)
151 dMat pop = (Matrix<Scalar,Dynamic,-1>::NullaryExpr(_covar.rows(),nn,randN))*factor;
152 for (
int i=0;i<pop.cols();i++)
154 pop.col(i) = pop.col(i).cwiseProduct(_transform) + _mean;
159 Matrix<Scalar,Dynamic,-1> samples_ind(
int nn)
161 return (Matrix<Scalar,Dynamic,-1>::NullaryExpr(_covar.rows(),nn,randN));
Definition: eigenmvn.h:76
Matrix< Scalar, Dynamic,-1 > samples(int nn, double factor)
Definition: eigenmvn.h:144
Definition: eigenmvn.h:47
Definition: eigenmvn.h:50