80 int ff_pca(
PCA *pca,
double *eigenvector,
double *eigenvalue){
86 memset(eigenvector, 0,
sizeof(
double)*n*n);
90 eigenvector[j + j*
n] = 1.0;
100 for(pass=0; pass < 50; pass++){
111 if(eigenvalue[j] > maxvalue){
112 maxvalue= eigenvalue[j];
116 eigenvalue[k]= eigenvalue[i];
117 eigenvalue[i]= maxvalue;
119 double tmp= eigenvector[k + j*
n];
120 eigenvector[k + j*
n]= eigenvector[i + j*
n];
121 eigenvector[i + j*
n]= tmp;
128 for(j=i+1; j<
n; j++){
130 double t,
c,
s,tau,theta,
h;
132 if(pass < 3 && fabs(covar) < sum / (5*n*n))
134 if(fabs(covar) == 0.0)
136 if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){
141 h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]);
143 t=1.0/(fabs(theta)+sqrt(1.0+theta*theta));
144 if(theta < 0.0) t = -t;
152 #define ROTATE(a,i,j,k,l) {\
153 double g=a[j + i*n];\
154 double h=a[l + k*n];\
155 a[j + i*n]=g-s*(h+g*tau);\
156 a[l + k*n]=h+s*(g-h*tau); }
161 ROTATE(eigenvector,k,i,k,j)
166 for (i=0; i<
n; i++) {
167 eigenvalue[i] += z[i];
186 double eigenvector[
LEN*
LEN];
187 double eigenvalue[
LEN];
194 for(i=0; i<9000000; i++){
201 if(j<=pos) v[j]= v[0];
217 ff_pca(pca, eigenvector, eigenvalue);
218 for(i=0; i<
LEN; i++){
226 for(j=i; j<
LEN; j++){
232 for(i=0; i<
LEN; i++){
235 memset(v, 0,
sizeof(v));
236 for(j=0; j<
LEN; j++){
237 for(k=0; k<
LEN; k++){
240 v[j] /= eigenvalue[i];
241 error += fabs(v[j] - eigenvector[i + j*LEN]);
243 printf(
"%f ", error);
247 for(i=0; i<
LEN; i++){
248 for(j=0; j<
LEN; j++){
249 printf(
"%9.6f ", eigenvector[i + j*LEN]);
251 printf(
" %9.1f %f\n", eigenvalue[i], eigenvalue[i]/eigenvalue[0]);
#define ROTATE(a, i, j, k, l)
int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue)
void ff_pca_free(PCA *pca)
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
void ff_pca_add(PCA *pca, const double *v)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
principal component analysis (PCA)
common internal and external API header
#define av_malloc_array(a, b)
int main(int argc, char **argv)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...