public static Covariance ( Matrix source, VectorType t = VectorType.Col ) : Matrix | ||
source | Matrix | Source for the. |
t | VectorType | (Optional) Row or Column sum. |
return | Matrix |
public static Matrix Covariance(Matrix source, VectorType t = VectorType.Col)
{
int length = t == VectorType.Row ? source.Rows : source.Cols;
Matrix m = new Matrix(length);
//for (int i = 0; i < length; i++)
for (int i = 0; i < length; i++)
{
//for (int j = i; j < length; j++) // symmetric matrix
for (int j = i; j < length; j++)
m[i, j] = m[j, i] = source[i, t].Covariance(source[j, t]);
}
return m;
}
/// <summary>Generates.</summary> /// <param name="matrix">The matrix.</param> public void Generate(Matrix matrix) { // generate centered matrix // (using a copy since centering is in place) X = matrix .Copy() .Center(VectorType.Col); // compute eigen-decomposition // of covariance matrix var eigs = X.Covariance().Eigs(); Eigenvalues = eigs.Item1; Eigenvectors = eigs.Item2; }