static float f14_maxcorr (float ** const p, unsigned int const ng) { /*---------------------------------------------------------------------------- The Maximal Correlation Coefficient -----------------------------------------------------------------------------*/ unsigned int i; float *px, *py; float ** q; float * x; float * iy; float tmp; px = vector(0, ng); py = vector(0, ng); q = matrix(1, ng + 1, 1, ng + 1); x = vector(1, ng); iy = vector(1, ng); /* * px[i] is the (i-1)th entry in the marginal probability matrix obtained * by summing the rows of p[i][j] */ for (i = 0; i < ng; ++i) { unsigned int j; for (j = 0; j < ng; ++j) { px[i] += p[i][j]; py[j] += p[i][j]; } } /* Compute the Q matrix */ for (i = 0; i < ng; ++i) { unsigned int j; for (j = 0; j < ng; ++j) { unsigned int k; q[i + 1][j + 1] = 0; for (k = 0; k < ng; ++k) q[i + 1][j + 1] += p[i][k] * p[j][k] / px[i] / py[k]; } } /* Balance the matrix */ mkbalanced(q, ng); /* Reduction to Hessenberg Form */ reduction(q, ng); /* Finding eigenvalue for nonsymetric matrix using QR algorithm */ hessenberg(q, ng, x, iy); if (sortit) simplesrt(ng, x); /* Return the sqrt of the second largest eigenvalue of q */ for (i = 2, tmp = x[1]; i <= ng; ++i) tmp = (tmp > x[i]) ? tmp : x[i]; return sqrt(x[ng - 1]); }
int main(){ int m,n,i,j; scanf("%d %d",&n,&m); mat A(n,m), hessenberg_form(n,m); printf(" Reading an %d x %d matrix...\n",n,m); for(i=0;i<A.size1();i++) for(j=0;j<A.size2();j++) scanf("%lf", &A(i,j)); printf(" A= \n");matprintf(A); hessenberg_form = hessenberg(A,n,m); printf(" Matrix reduced to Hessenberg form:\n");matprintf(hessenberg_form); exit(0); }
void v::eig(vectorn& eigenValues, const matrixn& mat) { //#define SYMMETRIC_TRIDIAGONAL #ifdef SYMMETRIC_TRIDIAGONAL ASSERT(mat.isValid()); matrixn symData(mat); ASSERT(symData.isValid()); vectorn e; eigenValues.setSize(mat.rows()); e.setSize(mat.rows()); NR::tred2(symData, eigenValues, e); ASSERT(symData.isValid()); NR::tqli(eigenValues, e,symData); int dim=eigenValues.size(); #else matrixn hessenberg (mat); // balance matrix //NR::balanc(hessenberg); // reduce to hessenberg form *****" NR::elmhes(hessenberg); Vec_CPLX_DP wri(hessenberg.rows()); NR::hqr(hessenberg,wri); eigenValues.setSize(wri.size()); for (int i=0;i<wri.size();i++) eigenValues[i]=wri[i].real(); #endif /* matrixn& EVectors=*this; EVectors.setSize(dim,dim); // Now each row contains EigenVector for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { EVectors[i][j] = symData[j][i]; } }*/ }
/* Returns the Maximal Correlation Coefficient */ double f14_maxcorr (double **P, int Ng) { int i, j, k; double *px, *py, **Q; double *x, *iy, tmp; double f; px = allocate_vector (0, Ng); py = allocate_vector (0, Ng); Q = allocate_matrix (1, Ng + 1, 1, Ng + 1); x = allocate_vector (1, Ng); iy = allocate_vector (1, Ng); /* * px[i] is the (i-1)th entry in the marginal probability matrix obtained * by summing the rows of p[i][j] */ for (i = 0; i < Ng; ++i) { for (j = 0; j < Ng; ++j) { px[i] += P[i][j]; py[j] += P[i][j]; } } /* Find the Q matrix */ for (i = 0; i < Ng; ++i) { for (j = 0; j < Ng; ++j) { Q[i + 1][j + 1] = 0; for (k = 0; k < Ng; ++k) Q[i + 1][j + 1] += P[i][k] * P[j][k] / px[i] / py[k]; } } /* Balance the matrix */ mkbalanced (Q, Ng); /* Reduction to Hessenberg Form */ reduction (Q, Ng); /* Finding eigenvalue for nonsymetric matrix using QR algorithm */ if (!hessenberg (Q, Ng, x, iy)) { /* Memmory cleanup */ for (i=1; i<=Ng+1; i++) free(Q[i]+1); free(Q+1); free((char *)px); free((char *)py); free((x+1)); free((iy+1)); /* computation failed ! */ return 0.0; } /* simplesrt(Ng,x); */ /* Returns the sqrt of the second largest eigenvalue of Q */ for (i = 2, tmp = x[1]; i <= Ng; ++i) tmp = (tmp > x[i]) ? tmp : x[i]; f = sqrt(x[Ng - 1]); for (i=1; i<=Ng+1; i++) free(Q[i]+1); free(Q+1); free((char *)px); free((char *)py); free((x+1)); free((iy+1)); return f; }