this.means = MathArrays.copyOf(means);
covarianceMatrix = new Array2DRowRealMatrix(covariances);
// Covariance matrix eigen decomposition.
final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix);
// Compute and store the inverse.
covarianceMatrixInverse = covMatDec.getSolver().getInverse();
// Compute and store the determinant.
covarianceMatrixDeterminant = covMatDec.getDeterminant();
// Eigenvalues of the covariance matrix.
final double[] covMatEigenvalues = covMatDec.getRealEigenvalues();
for (int i = 0; i < covMatEigenvalues.length; i++) {
if (covMatEigenvalues[i] < 0) {
throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0);
}
}
// Matrix where each column is an eigenvector of the covariance matrix.
final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim);
for (int v = 0; v < dim; v++) {
final double[] evec = covMatDec.getEigenvector(v).toArray();
covMatEigenvectors.setColumn(v, evec);
}
final RealMatrix tmpMatrix = covMatEigenvectors.transpose();