added functions to calculate and diagonalize the covariance matrix