parry2d/utils/
cov.rs

1use crate::math::{Matrix, Real, Vector, VectorExt};
2
3/// Computes the covariance matrix of a set of points.
4pub fn cov(pts: &[Vector]) -> Matrix {
5    center_cov(pts).1
6}
7
8/// Computes the center and the covariance matrix of a set of points.
9pub fn center_cov(pts: &[Vector]) -> (Vector, Matrix) {
10    let center = crate::utils::center(pts);
11    let mut cov = Matrix::ZERO;
12    let normalizer: Real = 1.0 / (pts.len() as Real);
13
14    for p in pts.iter() {
15        let cp = *p - center;
16        let cp_scaled = cp * normalizer;
17        // Compute outer product: cp * cp_scaled^T
18        cov += cp.kronecker(cp_scaled);
19    }
20
21    (center, cov)
22}