/* compute the mean, the covariance matrix, and the eigenvectors. They are stored in the structure itself */ void pca_online_complete (struct pca_online_s * pca) { int d = pca->d; int n = pca->n; fvec_div_by (pca->mu, d, n); fvec_div_by (pca->cov, d * d, n); float * mumut = fvec_new (d*d); fmat_mul_tr (pca->mu, pca->mu, d, d, 1, mumut); fvec_sub (pca->cov, mumut, d*d); free (mumut); assert(fvec_all_finite(pca->cov,d*d)); pca->eigvec = fmat_new_pca_from_covariance (d, pca->cov, pca->eigval); }
/* compute the mean and covariance matrix */ void pca_online_cov (struct pca_online_s * pca) { int d = pca->d; int n = pca->n; fvec_div_by (pca->mu, d, n); fvec_div_by (pca->cov, d * (long)d, n); float * mumut = fvec_new (d*(long)d); fmat_mul_tr (pca->mu, pca->mu, d, d, 1, mumut); fvec_sub (pca->cov, mumut, d*(long)d); free (mumut); fvec_mul_by (pca->cov, d * (long)d, n / (double) (n-1)); assert(fvec_all_finite(pca->cov,d*(long)d)); pca->n = -pca->n; }
float *fmat_new_pca(int d,int n,const float *v, float *singvals) { float *cov=fmat_new_covariance(d,n,v,NULL,1); assert(fvec_all_finite(cov,d*d)); float *evals=singvals; if(!singvals) evals=fvec_new(d); float *ret=fmat_new_pca_from_covariance(d,cov,evals); if(!singvals) free(evals); free(cov); return ret; }