diff options
Diffstat (limited to 'SD-VBS/benchmarks/pca/src')
| -rwxr-xr-x | SD-VBS/benchmarks/pca/src/pca | bin | 0 -> 21446 bytes | |||
| -rw-r--r-- | SD-VBS/benchmarks/pca/src/pca.c | 694 |
2 files changed, 694 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/pca/src/pca b/SD-VBS/benchmarks/pca/src/pca new file mode 100755 index 0000000..15b5908 --- /dev/null +++ b/SD-VBS/benchmarks/pca/src/pca | |||
| Binary files differ | |||
diff --git a/SD-VBS/benchmarks/pca/src/pca.c b/SD-VBS/benchmarks/pca/src/pca.c new file mode 100644 index 0000000..4637e7c --- /dev/null +++ b/SD-VBS/benchmarks/pca/src/pca.c | |||
| @@ -0,0 +1,694 @@ | |||
| 1 | /*********************** Contents **************************************** | ||
| 2 | * Principal Components Analysis: C, 638 lines. **************************** | ||
| 3 | * Sample input data set (final 36 lines). ********************************* | ||
| 4 | *************************************************************************** | ||
| 5 | */ | ||
| 6 | |||
| 7 | /*********************************/ | ||
| 8 | /* Principal Components Analysis */ | ||
| 9 | /*********************************/ | ||
| 10 | |||
| 11 | /*********************************************************************/ | ||
| 12 | /* Principal Components Analysis or the Karhunen-Loeve expansion is a | ||
| 13 | classical method for dimensionality reduction or exploratory data | ||
| 14 | analysis. One reference among many is: F. Murtagh and A. Heck, | ||
| 15 | Multivariate Data Analysis, Kluwer Academic, Dordrecht, 1987. | ||
| 16 | |||
| 17 | Author: | ||
| 18 | F. Murtagh | ||
| 19 | Phone: + 49 89 32006298 (work) | ||
| 20 | + 49 89 965307 (home) | ||
| 21 | Earn/Bitnet: fionn@dgaeso51, fim@dgaipp1s, murtagh@stsci | ||
| 22 | Span: esomc1::fionn | ||
| 23 | Internet: murtagh@scivax.stsci.edu | ||
| 24 | |||
| 25 | F. Murtagh, Munich, 6 June 1989 */ | ||
| 26 | /*********************************************************************/ | ||
| 27 | |||
| 28 | #include <stdio.h> | ||
| 29 | #include <string.h> | ||
| 30 | #include <math.h> | ||
| 31 | |||
| 32 | #define SIGN(a, b) ( (b) < 0 ? -fabs(a) : fabs(a) ) | ||
| 33 | |||
| 34 | main(argc, argv) | ||
| 35 | int argc; | ||
| 36 | char *argv[]; | ||
| 37 | |||
| 38 | { | ||
| 39 | FILE *stream; | ||
| 40 | int n, m, i, j, k, k2; | ||
| 41 | float **data, **matrix(), **symmat, **symmat2, *vector(), *evals, *interm; | ||
| 42 | void free_matrix(), free_vector(), corcol(), covcol(), scpcol(); | ||
| 43 | void tred2(), tqli(); | ||
| 44 | float in_value; | ||
| 45 | char option, *strncpy(); | ||
| 46 | |||
| 47 | /********************************************************************* | ||
| 48 | Get from command line: | ||
| 49 | input data file name, #rows, #cols, option. | ||
| 50 | |||
| 51 | Open input file: fopen opens the file whose name is stored in the | ||
| 52 | pointer argv[argc-1]; if unsuccessful, error message is printed to | ||
| 53 | stderr. | ||
| 54 | *********************************************************************/ | ||
| 55 | |||
| 56 | if (argc != 5) | ||
| 57 | { | ||
| 58 | printf("Syntax help: PCA filename #rows #cols option\n\n"); | ||
| 59 | printf("(filename -- give full path name,\n"); | ||
| 60 | printf(" #rows \n"); | ||
| 61 | printf(" #cols -- integer values,\n"); | ||
| 62 | printf(" option -- R (recommended) for correlation analysis,\n"); | ||
| 63 | printf(" V for variance/covariance analysis\n"); | ||
| 64 | printf(" S for SSCP analysis.)\n"); | ||
| 65 | exit(1); | ||
| 66 | } | ||
| 67 | |||
| 68 | n = atoi(argv[2]); /* # rows */ | ||
| 69 | m = atoi(argv[3]); /* # columns */ | ||
| 70 | strncpy(&option,argv[4],1); /* Analysis option */ | ||
| 71 | |||
| 72 | printf("No. of rows: %d, no. of columns: %d.\n",n,m); | ||
| 73 | printf("Input file: %s.\n",argv[1]); | ||
| 74 | |||
| 75 | if ((stream = fopen(argv[1],"r")) == NULL) | ||
| 76 | { | ||
| 77 | fprintf(stderr, "Program %s : cannot open file %s\n", | ||
| 78 | argv[0], argv[1]); | ||
| 79 | fprintf(stderr, "Exiting to system."); | ||
| 80 | exit(1); | ||
| 81 | /* Note: in versions of DOS prior to 3.0, argv[0] contains the | ||
| 82 | string "C". */ | ||
| 83 | } | ||
| 84 | |||
| 85 | /* Now read in data. */ | ||
| 86 | |||
| 87 | data = matrix(n, m); /* Storage allocation for input data */ | ||
| 88 | |||
| 89 | for (i = 1; i <= n; i++) | ||
| 90 | { | ||
| 91 | for (j = 1; j <= m; j++) | ||
| 92 | { | ||
| 93 | fscanf(stream, "%f", &in_value); | ||
| 94 | data[i][j] = in_value; | ||
| 95 | printf("at row %d column %d is %lf\n",i,j,data[i][j]); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | /* Check on (part of) input data. | ||
| 102 | for (i = 1; i <= 18; i++) {for (j = 1; j <= 8; j++) { | ||
| 103 | printf("%7.1f", data[i][j]); } printf("\n"); } | ||
| 104 | */ | ||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | symmat = matrix(m, m); /* Allocation of correlation (etc.) matrix */ | ||
| 109 | |||
| 110 | /* Look at analysis option; branch in accordance with this. */ | ||
| 111 | |||
| 112 | switch(option) | ||
| 113 | { | ||
| 114 | case 'R': | ||
| 115 | case 'r': | ||
| 116 | printf("Analysis of correlations chosen.\n"); | ||
| 117 | corcol(data, n, m, symmat); | ||
| 118 | |||
| 119 | /* Output correlation matrix. | ||
| 120 | for (i = 1; i <= m; i++) { | ||
| 121 | for (j = 1; j <= 8; j++) { | ||
| 122 | printf("%7.4f", symmat[i][j]); } | ||
| 123 | printf("\n"); } | ||
| 124 | */ | ||
| 125 | break; | ||
| 126 | case 'V': | ||
| 127 | case 'v': | ||
| 128 | printf("Analysis of variances-covariances chosen.\n"); | ||
| 129 | covcol(data, n, m, symmat); | ||
| 130 | |||
| 131 | /* Output variance-covariance matrix. | ||
| 132 | for (i = 1; i <= m; i++) { | ||
| 133 | for (j = 1; j <= 8; j++) { | ||
| 134 | printf("%7.1f", symmat[i][j]); } | ||
| 135 | printf("\n"); } | ||
| 136 | */ | ||
| 137 | break; | ||
| 138 | case 'S': | ||
| 139 | case 's': | ||
| 140 | printf("Analysis of sums-of-squares-cross-products"); | ||
| 141 | printf(" matrix chosen.\n"); | ||
| 142 | scpcol(data, n, m, symmat); | ||
| 143 | |||
| 144 | /* Output SSCP matrix. | ||
| 145 | for (i = 1; i <= m; i++) { | ||
| 146 | for (j = 1; j <= 8; j++) { | ||
| 147 | printf("%7.1f", symmat[i][j]); } | ||
| 148 | printf("\n"); } | ||
| 149 | */ | ||
| 150 | break; | ||
| 151 | default: | ||
| 152 | printf("Option: %s\n",option); | ||
| 153 | printf("For option, please type R, V, or S\n"); | ||
| 154 | printf("(upper or lower case).\n"); | ||
| 155 | printf("Exiting to system.\n"); | ||
| 156 | exit(1); | ||
| 157 | break; | ||
| 158 | } | ||
| 159 | |||
| 160 | /********************************************************************* | ||
| 161 | Eigen-reduction | ||
| 162 | **********************************************************************/ | ||
| 163 | |||
| 164 | /* Allocate storage for dummy and new vectors. */ | ||
| 165 | evals = vector(m); /* Storage alloc. for vector of eigenvalues */ | ||
| 166 | printf("the vector storage size is %d\n",m); | ||
| 167 | interm = vector(m); /* Storage alloc. for 'intermediate' vector */ | ||
| 168 | symmat2 = matrix(m, m); /* Duplicate of correlation (etc.) matrix */ | ||
| 169 | for (i = 1; i <= m; i++) { | ||
| 170 | for (j = 1; j <= m; j++) { | ||
| 171 | symmat2[i][j] = symmat[i][j]; /* Needed below for col. projections */ | ||
| 172 | } | ||
| 173 | } | ||
| 174 | tred2(symmat, m, evals, interm); /* Triangular decomposition */ | ||
| 175 | printf("eval value at 0 is %lf\n",evals[0]); | ||
| 176 | printf("eval value at 1 is %lf\n",evals[1]); | ||
| 177 | printf("eval value at 2 is %lf\n",evals[2]); | ||
| 178 | |||
| 179 | printf("m/height is %lf \n",m); | ||
| 180 | |||
| 181 | printf("m/height is %d \n",m); | ||
| 182 | printf("m/height is %d \n",n); | ||
| 183 | printf("m/height is %d \n",n); | ||
| 184 | |||
| 185 | |||
| 186 | tqli(evals, interm, m, symmat); /* Reduction of sym. trid. matrix */ | ||
| 187 | /* evals now contains the eigenvalues, | ||
| 188 | columns of symmat now contain the associated eigenvectors. */ | ||
| 189 | |||
| 190 | printf("\nEigenvalues:\n"); | ||
| 191 | for (j = m; j >= 1; j--) { | ||
| 192 | printf("%18.5f\n", evals[j]); } | ||
| 193 | printf("\n(Eigenvalues should be strictly positive; limited\n"); | ||
| 194 | printf("precision machine arithmetic may affect this.\n"); | ||
| 195 | printf("Eigenvalues are often expressed as cumulative\n"); | ||
| 196 | printf("percentages, representing the 'percentage variance\n"); | ||
| 197 | printf("explained' by the associated axis or principal component.)\n"); | ||
| 198 | |||
| 199 | printf("\nEigenvectors:\n"); | ||
| 200 | |||
