inline static void f( char * jobu, char * jobvt, INTEGER * m, INTEGER * n, double * A, INTEGER * LDA, double * S, double * U, INTEGER * LDU, double * VT, INTEGER * LDVT, double * WORK, INTEGER * LWORK, INTEGER * INFO) { DGESVD(jobu, jobvt, m, n, A, LDA, S, U, LDU, VT, LDVT, WORK, LWORK, INFO); }
void SpinAdapted::svd(Matrix& M, DiagonalMatrix& d, Matrix& U, Matrix& V) { int nrows = M.Nrows(); int ncols = M.Ncols(); assert(nrows >= ncols); int minmn = min(nrows, ncols); int maxmn = max(nrows, ncols); int eigenrows = min(minmn, minmn); d.ReSize(minmn); Matrix Ut; Ut.ReSize(nrows, nrows); V.ReSize(ncols, ncols); int lwork = maxmn * maxmn + 100; double* workspace = new double[lwork]; // first transpose matrix Matrix Mt; Mt = M.t(); int info = 0; DGESVD('A', 'A', nrows, ncols, Mt.Store(), nrows, d.Store(), Ut.Store(), nrows, V.Store(), ncols, workspace, lwork, info); U.ReSize(nrows, ncols); SpinAdapted::Clear(U); for (int i = 0; i < nrows; ++i) for (int j = 0; j < ncols; ++j) U(i+1,j+1) = Ut(j+1,i+1); delete[] workspace; }
void wrap_dgesvd(char jobu, char jobvt, int m, int n, double *a, int lda, double *sing, double *u, int ldu, double *vt, int ldvt, int *info) { #ifdef HAVE_ACML DGESVD(jobu, jobvt, m, n, a, lda, sing, u, ldu, vt, ldvt, info); #else int lwork=-1; double work1; DGESVD(&jobu, &jobvt, &m, &n, a, &lda, sing, u, &ldu, vt, &ldvt, &work1, &lwork, info); ASSERT(*info==0); ASSERT(work1>0); lwork=(int) work1; double* work=new double[lwork]; DGESVD(&jobu, &jobvt, &m, &n, a, &lda, sing, u, &ldu, vt, &ldvt, work, &lwork, info); delete[] work; #endif }
double cond(double * A, int n, int m) { //#ifdef COMPLETE_LAPACK_LIBRARIES int dimS = m < n ? m : n; double * S = (double *)malloc(dimS * sizeof(double)); char JOBU = 'N'; int LDU = 1; double *U = NULL; char JOBVT = 'N'; int LDVT = 1; double *VT = NULL; size_t size = n * m * sizeof(double); double *Atmp = (double *)malloc(size); memcpy(Atmp, A, size); int InfoDGSVD = -1; double * superb = (double *)malloc((min(m, n) - 1)* sizeof(double)); DGESVD(JOBU, JOBVT, n, m, A, n, S, U, LDU, VT, LDVT, superb, &InfoDGSVD); /* #else */ /* int LWORK = -1; */ /* double * WORK; */ /* WORK = malloc(sizeof(*WORK)); */ /* assert(WORK); */ /* DGESVD(&JOBU, &JOBVT, n, m, A, m, S, U, LDU, VT, LDVT, WORK, LWORK, InfoDGSVD); */ /* LWORK = (int)(WORK[0]); */ /* WORK = realloc(WORK, LWORK * sizeof * WORK); */ /* DGESVD(&JOBU, &JOBVT, n, m, A, m, S, U, LDU, VT, LDVT, WORK, LWORK, InfoDGSVD); */ /* free(WORK); */ /* #endif */ printf("SVD of A :\n "); printf("[\t "); for (int i = 0; i < dimS ; i++) { printf("%14.7e\t", S[i]); } printf("]\n "); memcpy(A, Atmp, size); double conditioning = S[0] / S[dimS - 1]; free(superb); free(Atmp); free(S); return conditioning; /* #else */ /* fprintf(stderr, "Numerics. cond.c dgesvd not found\n"); */ /* return 0.0; */ /* #endif */ }
int MatrixPCA(ThresholdingSpecification * thresholdingSpecification, int m, int n, double * A, int * r, double * weights) { if (!A) { printf("Error: input matrix is NULL.\n"); return -1; } if (weights != NULL) { // transform row i of A by sqrt(weights[i]) for(int row=0; row<m; row++) { double rowWeight = sqrt(weights[row]); for(int column=0; column<n; column++) A[ELT(m, row, column)] *= rowWeight; } } bool transpose = false; if (m > n) { transpose = true; InPlaceTransposeMatrix(m,n,A); // swap m,n int bufferi = m; m = n; n = bufferi; } // do SVD char jobu = 'O';//overwrites A with U (left singular vectors) //char jobu = 'N'; char jobvt = 'S';//all rows returned in VT //char jobvt = 'N'; int ldA = m; int ldU = m; int lwork = 64*MAX(3*MIN( m, n)+MAX(m,n), 5*MIN(m,n)-4); double * work = (double*) malloc (sizeof(double) * lwork); if (!work) { printf("Error: failed to allocate workspace.\n"); return -2; } //printf("Workspace size is: %G Mb .\n",1.0 * lwork * sizeof(int) / 1024 / 1024); // allocate array for singular vectors double * S = (double *) malloc (sizeof(double) * MIN(m,n)); if (!S) { printf("Error: failed to allocate singular vectors.\n"); return -2; } double * dummyU = NULL; // allocate array for VT int ldVT = MIN(m,n); double * VT = (double *) malloc (sizeof(double) * ldVT * n); if (!VT) { printf("Error: failed to allocate VT.\n"); return -2; } #ifdef __APPLE__ #define DGESVD dgesvd_ #define INTEGER __CLPK_integer #else #define DGESVD dgesvd #define INTEGER int #endif INTEGER M = m; INTEGER N = n; INTEGER LDA = ldA; INTEGER LDU = ldU; INTEGER LDVT = ldVT; INTEGER LWORK = lwork; INTEGER INFO; //printf("Calling LAPACK dgesvd routine...\n");fflush(NULL); //SUBROUTINE SGESVD( JOBU, JOBVT, M, N, A, LDA, S, U, LDU, VT, LDVT, WORK, LWORK, INFO ) DGESVD (&jobu, &jobvt, &M, &N, A, &LDA, S, dummyU, &LDU, VT, &LDVT, work, &LWORK, &INFO); if (INFO != 0) { int code = INFO; printf("Error: SVD solver returned non-zero exit code: %d.\n", code); free(VT); free(S); free(work); return code; } free(work); if (transpose) { InPlaceTransposeMatrix(m,n,VT); memcpy(A, VT, sizeof(double) * m * n); // swap m and n int bufferii = m; m = n; n = bufferii; } free(VT); if (weights != NULL) { // transform row i of A by sqrt(weights[i])^{-1} for(int row=0; row<m; row++) { double rowWeight = 1.0 / sqrt(weights[row]); for(int column=0; column<n; column++) A[ELT(m, row, column)] *= rowWeight; } } //double totalEnergy = 0; //printf ("Singular values:\n");fflush(NULL); //for (int i=0; i< MIN(m,n); i++) //printf("%f ",S[i]); //printf ("\n"); // discard unneccesary modes //printf("Discarding unnecessary components...\n");fflush(NULL); if (thresholdingSpecification->tresholdingType == ThresholdingSpecification::epsilonBased) DoTresholding_Epsilon(S,MIN(m,n),r,thresholdingSpecification->epsilon); else DoTresholding_NumberOfModes(S,MIN(m,n),r,thresholdingSpecification->rDesired); // now, variable r has been set to the number of retained modes free(S); return 0; }
void n_svm(double *A, double *w, double *gamma, DOC *d_p, DOC *d_n){ int i,j,k,m,n,*im,in,jn,nn,iter; double alpha, *H, *Q, xx,current_xx; // double **a, **v, *star, *hu, *u; double *star, *hu, *u; int ldu,ldvt,lwork,lda,ldb,info,*ipiv,emm,enn,rsh=1; double *s,*ut,*vt,*work, *HH; char ch1='N'; //str[99], // // m=num_tot; lda=NM_MAX; ldb=lda; n=num_SVs_tot; im = new int [m+4]; for(i=0;i<(m+4);i++)im[i]=i*m; // // create H & Q matrices // H = new double [(m+1)*(m+1)]; Q = new double [(m+1)*(m+1)]; nn=n+1; for(i=0;i<num_p;i++){ k=i*nn; in=i*n; for(j=0;j<n;j++)H[k+j]=A[in+j]; H[k+n]=-1.0; } for(i=0;i<num_n;i++){ k=(i+num_p)*nn; in=(i+num_p)*n; for(j=0;j<n;j++)H[k+j]=-A[in+j]; H[k+n]=1.0; } for(i=0;i<m;i++){ in=i*nn; for(j=0;j<m;j++){ if(i==j) { Q[im[i]+j] = 1.0 / nu; if ( i<num_p ) Q[im[i]+j] = Q[im[i]+j] / J_nu; } else Q[im[i]+j]=0.0; jn=j*nn; for(k=0;k<nn;k++) Q[im[i]+j]=Q[im[i]+j]+H[in+k]*H[jn+k]; // printf("%8.4lf",Q[im[i]+j]); } // printf("\n"); } lwork = 8*lda; HH = (double *)malloc((lda*m)*sizeof(double)); hu = (double *)malloc(ldb*sizeof(double)); ipiv = (int *)malloc(lda*sizeof(int)); s = (double *)malloc((lda)*sizeof(double)); ut = (double *)malloc(sizeof(double)); vt = (double *)malloc(sizeof(double)); work = (double *)malloc((lwork)*sizeof(double)); // a=H', a=UWV; w is svd(a); after call svdcmp a was changed to U; // output the H' to file and run SVD program by system command // then then reading the norm(H',2) back from SVD runbig results emm=nn; enn=m; /* if ( (fpw=fopen("Matrix_H__T.dat","wt")) != NULL ) { fprintf(fpw, "%d , %d\n", nn,m); */ for(i=0;i<nn;i++) for(j=0;j<m;j++) { // fprintf(fpw,"%26.16lf\n",H[j*nn+i]); HH[i+j*lda]=H[j*nn+i]; } /* fclose(fpw); } */ if(debug){ #ifdef WIN32 printf("Printing time before svd the %d * %d matrix: ",nn,m); fflush(stdout); system("date /T & time /T"); #else printf("Printing time before svd the %d * %d matrix: ",nn,m); fflush(stdout); system("date"); #endif } delete[] H; ldu = 1; ldvt = 1; #ifdef WIN32 DGESVD(&ch1,&ch1, &emm, &enn, HH, &lda, s, ut, &ldu, vt, &ldvt, work, &lwork,&info); #else dgesvd_(&ch1,&ch1, &emm, &enn, HH, &lda, s, ut, &ldu, vt, &ldvt, work, &lwork,&info); #endif alpha = s[0] ; if (debug){ #ifdef WIN32 printf("Norm(H^^T)=%20.16lf\nPrinting time after svd : ",alpha); fflush(stdout); system("date /T & time /T"); #else printf("Norm(H^^T)=%20.16lf\nPrinting time after svd : ",alpha); fflush(stdout); system("date"); #endif } /* fpw=fopen("svd_sss","wt"); for(i=0;i<nn;i++)fprintf(fpw,"%26.16lf\n",s[i]); fclose(fpw); */ free(s); free(ut); free(vt); free(work); star = new double[1+m]; u = new double[1+m]; // hu = new double[1+m]; /* system("SVD_lapack_C Matrix_H__T.dat SVD_of_H__T.dat"); if ( (fpr=fopen("SVD_of_H__T.dat","rt")) != NULL ) { fscanf(fpr,"%lf",&alpha); fclose(fpr); } */ alpha = alpha*alpha*1.1+1.1/nu; // in matlab: hu=-max(((Q*u-e)-alpha*u),0)+Q*u-e; // at this step, hu = [-1](1:m) column vector; for(i=0;i<m;i++){ // a[i][1] =-1.0; u[i] = 0.0; // hu[i]=a[i][1]; hu[i] = - 1.0; } xx = vector_norm(m,hu); // printf("m= %d | hu[0]=%lf | norm(hu)=%lf\n",m,hu[0],xx); iter=1; current_xx=1.0e99; while( (xx>stop_criteria) && ( fabs(current_xx-xx) >= stop_criteria ) ){ current_xx = xx; for(i=0;i<m;i++){ star[i]=(Q[im[i]+i]-alpha)*u[i]-1; for(j=0;j<i;j++)star[i]=star[i]+Q[im[i]+j]*u[j]; for(j=i+1;j<m;j++)star[i]=star[i]+Q[im[i]+j]*u[j]; if(star[i]>TINY) star[i]=1.0; else star[i]=0.0; } /* if(iter<10)sprintf(str,"Matrix_A_and_B_0%d.dat",iter); else sprintf(str,"Matrix_A_and_B_%d.dat",iter); fpw=fopen(str,"wt") ; fprintf(fpw, "%d\n", m); */ for(i=0;i<m;i++){ // for(j=1;j<=m;j++)a[i][j]=Q[im[i-1]+j-1]*(1-star[i]); for(j=0;j<m;j++)w[j]=Q[im[i]+j]*(1-star[i]); // a[i][i]=a[i][i]+alpha*star[i]; w[i]=w[i]+alpha*star[i]; for(j=0;j<m;j++){ // fprintf(fpw,"%26.16lf\n",w[j]); HH[i+j*lda] = w[j] ; } } /* for ( i=0;i<m;i++ ){ for(j=0;j<m;j++){ fprintf(fpw,"%20.14lf\n",HH[i+j*lda]); } } for(i=0;i<m;i++){ fprintf(fpw,"%20.14lf\n",hu[i]); } fclose(fpw); */ if (debug){ #ifdef WIN32 printf("\nPrinting time before solving the %d-D linear equation: ",m); fflush(stdout); system("date /T & time /T"); #else printf("\nPrinting time before solving the %d-D linear equation: ",m); fflush(stdout); system("date"); #endif } // system("SOL_lin_eq_lapack_C Matrix_A_and_B.dat X_of_AX_eq_B.dat"); enn=m; rsh=1; #ifdef WIN32 DGESV( &enn, &rsh, HH, &lda, ipiv, hu, &ldb, &info); #else dgesv_( &enn, &rsh, HH, &lda, ipiv, hu, &ldb, &info); #endif /* fpw=fopen("xxxx","wt"); for(i=0;i<m;i++)fprintf(fpw,"%26.16lf\n",hu[i]); fclose(fpw); if ( (fpr=fopen("X_of_AX_eq_B.dat","rt")) != NULL ) { for(i=0;i<m;i++) fscanf(fpr,"%lf",&hu[i]); fclose(fpr); } */ if (debug){ #ifdef WIN32 printf("Printing time after solving equation: "); fflush(stdout); system("date /T & time /T"); #else printf("Printing time after solving equation: "); fflush(stdout); system("date"); #endif } for(i=0;i<m;i++) u[i]=u[i]-hu[i]; for(i=0;i<m;i++){ hu[i]=-1.0; for(j=0;j<m;j++) hu[i]=hu[i]+u[j]*Q[im[i]+j]; xx=hu[i]-u[i]*alpha; if(xx<TINY)xx=0.0; hu[i]=hu[i]-xx; // a[i][1]=hu[i]; // printf("%10.4lf\n",hu[i]); } xx = vector_norm(m,hu); printf("iteration %d : xx = %1.2le\n",iter,xx); fflush(stdout); iter++; } printf(" iteration done !\n"); fflush(stdout); gamma[1]=0.0; for(i=0;i<num_p;i++) gamma[1]=gamma[1]+u[i]; for(i=num_p;i<m;i++){ u[i]=-u[i]; gamma[1]=gamma[1]+u[i]; } gamma[1]=-gamma[1]; for(i=0;i<n;i++){ w[i]=0.0; for(j=0;j<m;j++) w[i]=w[i]+A[j*n+i]*u[j]; // printf("%lf\n",w[i]); } // Classification for the traing set k=0; for(i=0;i<num_p;i++){ xx=-gamma[1];; for(j=0;j<num_SVs_tot;j++)xx=xx+w[j]*A[i*num_SVs_tot+j]; if(xx>0.0)k++; } printf("Total %d of %d positive were classified correctly\n",k,num_p);fflush(stdout); /* nn=0; fpw=fopen("tmp.de.pre","wt"); for(i=0;i<num_n;i++){ xx=-gamma[1];; for(j=0;j<num_SVs_tot;j++)xx=xx+w[j]*A[(i+num_p)*num_SVs_tot+j]; strcpy(str,d_n[i].nam); if(str[6]=='.') str[6]='\0'; else str[4]='\0'; fprintf(fpw,"%d %s %lf\n",d_n[i].ind,str,xx); if(xx<0.0)nn++; } printf("Total %d of %d negative were classified correctly\n",nn,num_n);fflush(stdout); fclose(fpw); */ nn=0; for(i=0;i<num_n;i++){ xx=-gamma[1];; for(j=0;j<num_SVs_tot;j++)xx=xx+w[j]*A[(i+num_p)*num_SVs_tot+j]; if(xx<0.0)nn++; } printf("Total %d of %d negative were classified correctly\n",nn,num_n);fflush(stdout); nn=nn+k; xx = (double) nn / (double) num_tot; xx=xx*100.0; printf("Total classification rate %d / %d (%6.2lf %% )\n",nn,num_tot,xx); fflush(stdout); delete[] star; free(hu); delete[] u; delete[] Q; free(HH); // free(bb); free(ipiv); }