( ) (sample1.c) (sample1.c) 2 2 Nearest Neighbor 1 (2D-class1.dat) 2 (2D-class2.dat) (2D-test.dat) 3 Nearest Neighbor Nearest Neighbor ( 1) 2 1: NN 1
(sample1.c) /* ----------------------------------------------------------------- */ /* (2 2 ver.) */ /* ----------------------------------------------------------------- */ /* Nearest Neighbor */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define LDATA 20 // #define TDATA 40 // #define DIM 2 // double class1[ldata][dim]; double class2[ldata][dim]; // 1 // 2 double test[tdata][dim]; // ( ) int tans[tdata]; // int result[tdata]; // double met1[tdata]; // ( 1) double met2[tdata]; // ( 2) int main(int argc, char* argv[]){ /* ---------- ---------- */ int input(); // int nearest(); // Nearest Neighbor int output(); // /********** **********/ input(); /********** Nearest Neighbor **********/ nearest(); /********** **********/ output(); return(0); 2
/* ---------------------------------------------------- */ /* */ /* ---------------------------------------------------- */ int input(){ FILE *fin1, *fin2, *fin3; int i, j; // /* ----------- open ---------- */ fin1 = fopen("2d-class1.dat", "r"); if( fin1 == NULL){ fprintf(stderr, "file open error : 2D-class1.dat \n"); return -1; fin2 = fopen("2d-class2.dat", "r"); if( fin2 == NULL){ fprintf(stderr, "file open error : 2D-class2.dat \n"); return -1; fin3 = fopen("2d-test.dat", "r"); if( fin3 == NULL){ fprintf(stderr, "file open error : 2D-test.dat \n"); return -1; /* ---------- ---------- */ for(i=0;i<ldata;i++){ for(j=0;j<dim;j++){ fscanf(fin1, "%lf", &class1[i][j]); fscanf(fin2, "%lf", &class2[i][j]); for(i=0;i<tdata;i++){ for(j=0;j<dim;j++) fscanf(fin3, "%lf", &test[i][j]); fscanf(fin3, "%d", &tans[i]); /* ---------- close ---------- */ fclose(fin1); fclose(fin2); fclose(fin3); return 0; 3
/* ---------------------------------------------------- */ /* Nearest Neighbor */ /* ---------------------------------------------------- */ int nearest(){ double min1, min2, dist, tmp; /* min1 min2 */ /* */ int i, j, k; for(k=0;k<tdata;k++){ min1 = min2 = 9999999; // for(i=0;i<ldata;i++){ /* ---------- ---------- */ tmp = 0; for(j=0;j<dim;j++) tmp += (test[k][j] - class1[i][j]) * (test[k][j] - class1[i][j]); dist = sqrt(tmp); if(min1 > dist) min1 = dist; /* min1 */ /* ---------- ---------- */ tmp = 0; for(j=0;j<dim;j++) tmp += (test[k][j] - class2[i][j]) * (test[k][j] - class2[i][j]); dist = sqrt(tmp); if(min2 > dist) min2 = dist; /* min2 */ met1[k] = min1; met2[k] = min2; /* */ if(min1 <= min2) result[k] = 1; else result[k] = 2; return 0; /* ---------------------------------------------------- */ /* */ /* ---------------------------------------------------- */ int output(){ 4
FILE *fout; int k; /* ---------- open ---------- */ fout = fopen("2d-result.dat", "w"); if( fout == NULL){ fprintf(stderr, "file open error : result.dat \n"); return -1; /* ---------- ---------- */ fprintf(fout, "NUM MIN1 MIN2 RES ANS o/x\n"); fprintf(fout, "--------------------------------\n"); for(k=0;k<tdata;k++){ fprintf(fout, "%3d %4.2lf %4.2lf %3d %3d ", k+1, met1[k], met2[k], result[k], tans[k]); if(result[k] == tans[k]) fprintf(fout, "o \n"); else fprintf(fout, "x \n"); /* ---------- close ---------- */ fclose(fout); return 0; 5
input() nearest() Nearest Neighbor output() 3 1 1 /* ---------- ---------- */ int input(); // int nearest(); // Nearest Neighbor int output(); // 1 1 (x ) 2 1 (x ) 3 1 (x ). 1 2 (y ) 2 2 (y ) 3 2 (y ). i j class[i][j] 2 1 2 2 #define LDATA 20 // #define TDATA 40 // #define DIM 2 // double class1[ldata][dim]; double class2[ldata][dim]; // 1 // 2 double test[tdata][dim]; // ( ) 6
3 /* ---------- ---------- */ for(i=0;i<ldata;i++){ for(j=0;j<dim;j++){ fscanf(fin1, "%lf", &class1[i][j]); fscanf(fin2, "%lf", &class2[i][j]); for(i=0;i<tdata;i++){ for(j=0;j<dim;j++) fscanf(fin3, "%lf", &test[i][j]); fscanf(fin3, "%d", &tans[i]); 7
1. URL sample1.c 2D-class1.dat 2D-class2.dat 2D-test.dat http://www.it.ecei.tohoku.ac.jp/pattern/ 2. % cc sample1.c -lm 3. % a.out 4. % less result.dat 8
(u-eigen.c g-eigen.c) (u-eigen.c g-eigen.c) N CLAPACK u-eigen.c - Ax = λx g-eigen.c - Ax = λbx (u-eigen.c) /* Ax = x */ /* C interface example for dsyev hidehisa_2006/05/01 */ #include <stdio.h> #include <stdlib.h> #include <PRT.h> /* #define L (64) */ int main( void ){ FILE int *fp; ds; int N; // double A[L][L],E[L],V[L][L]; int i,j; /* ---------- ---------- */ fp=fopen( "NA4.dat", "r"); // if( fp==null ){ fprintf(stderr,"file open error\n"); return(-1); else{ fscanf(fp, "%d", &N ); // for(i=0;i<n;i++) for(j=0;j<n;j++) fscanf(fp,"%lf", &A[i][j] ); // fclose( fp ); 9
/* ---------- ---------- */ ds=dsyev(n,a,e,v); if( ds < 0 ){ perror("dsyev"); exit(2); /* ---------- ---------- */ fprintf(stderr,"*** Output ***\n\n"); for(i=0;i<n;i++) fprintf(stderr,"\t Eigenvalue"); for(i=0;i<n;i++) fprintf(stderr,"\t %9.6lf", E[i] ); fprintf(stderr,"\n\n"); for(i=0;i<n;i++) fprintf(stderr,"\t Eigenvector"); for(j=0;j<n;j++){ for(i=0;i<n;i++) fprintf(stderr,"\t %9.6lf", V[i][j] ); return( 0 ); (g-eigen.c) /* Ax = Bx */ /* C interface example for dsygv hidehisa_2006/05/01 */ #include <stdio.h> #include <stdlib.h> #include <PRT.h> /* #define L ( 64 ) */ int main( void ){ FILE int *fp; ds; int N; // double A[L][L],B[L][L],E[L],V[L][L]; int i,j; /* ---------- ---------- */ fp=fopen( "NAB4.dat", "r"); if( fp==null ){ fprintf(stderr,"file open error\n"); return(-1); else{ 10
fscanf(fp, "%d", &N ); // if( N > L ){ perror("input N"); exit(1); for(i=0;i<n;i++) for(j=0;j<n;j++) fscanf(fp,"%lf", &A[i][j] ); // A for(i=0;i<n;i++) for(j=0;j<n;j++) fscanf(fp,"%lf", &B[i][j] ); // B fclose( fp ); /* ---------- ---------- */ ds=dsygv(n,a,b,e,v); if( ds < 0 ){ perror("dsygv"); exit(2); /* ---------- ---------- */ fprintf(stderr,"*** Output ***\n\n"); for(i=0;i<n;i++) fprintf(stderr,"\t Eigenvalue"); for(i=0;i<n;i++) fprintf(stderr,"\t %9.6lf", E[i] ); fprintf(stderr,"\n\n"); for(i=0;i<n;i++) fprintf(stderr,"\t Eigenvector"); for(j=0;j<n;j++){ for(i=0;i<n;i++) fprintf(stderr,"\t %9.6lf", V[i][j] ); return( 0 ); 11
(NAB4.dat) 1 (g-eigen.c) )4 A = 2 1 1 2 1 1 1 1 1 1 2 2 2 1 2 4 B = 153 31 58 58 31 153 58 58 58 58 153 31 58 58 31 153 4 // 2 1 1 2 // A 1 1 1 1 1 1 2 2 2 1 2 4 153 31 58-58 // B 31 153-58 58 58-58 153 31-58 58 31 153 (u-eigen.c) A 12
1. URL u-eigen.c g-eigen.c NA4.dat NAB4.dat http://www.it.ecei.tohoku.ac.jp/pattern/ 2. (LAPACK) "$PRTFLAG" % cc u-eigen.c $PRTFLAG % cc g-eigen.c $PRTFLAG 3. % a.out 13