/* nag_corr_cov (g02bxc) Example Program. * * Copyright 1992 Numerical Algorithms Group. * * Mark 3, 1992. * Mark 8 revised, 2004. */ #include #include #include #include #include #define X(I, J) x[(I) *tdx + J] #define R(I, J) r[(I) *tdr + J] #define V(I, J) v[(I) *tdv + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, m, n, tdr, tdv, tdx, test; NagError fail; char w; double *r = 0, *std = 0, sw, *v = 0, *wmean = 0, *wt = 0, *wtptr, *x = 0; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_corr_cov (g02bxc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); test = 0; while ((fscanf(fpin, "%ld%ld %c", &m, &n, &w) != EOF)) { if (m >= 1 && n >= 1) { if (!(x = NAG_ALLOC(n*m, double)) || !(r = NAG_ALLOC(m*m, double)) || !(v = NAG_ALLOC(m*m, double)) || !(wt = NAG_ALLOC(n, double)) || !(wmean = NAG_ALLOC(m, double)) || !(std = NAG_ALLOC(m, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdx = m; tdr = m; tdv = m; } else { fprintf(fpout, "Invalid m or n.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; i++) fscanf(fpin, "%lf", &wt[i]); for (i = 0; i < n; i++) for (j = 0; j < m; j++) fscanf(fpin, "%lf", &X(i, j)); if (w == 'w') wtptr = wt; else wtptr = (double *) 0; /* nag_corr_cov (g02bxc). * Product-moment correlation, unweighted/weighted * correlation and covariance matrix, allows variables to be * disregarded */ nag_corr_cov(n, m, x, tdx, (Integer *) 0, wtptr, &sw, wmean, std, r, tdr, v, tdv, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_corr_cov (g02bxc).\n%s\n", fail.message); exit_status = 1; goto END; } if (wtptr) fprintf(fpout, "\nCase %ld --- Using weights\n", ++test); else fprintf(fpout, "\nCase %ld --- Not using weights\n", ++test); fprintf(fpout, "\nInput data\n"); for (i = 0; i < n; i++) fprintf(fpout, "%6.1f%6.1f%6.1f%6.1f\n", X(i, 0), X(i, 1), X(i, 2), wt[i]); fprintf(fpout, "\n"); fprintf(fpout, "Sample means.\n"); for (i = 0; i < m; i++) fprintf(fpout, "%6.1f\n", wmean[i]); fprintf(fpout, "\nStandard deviation.\n"); for (i = 0; i < m; i++) fprintf(fpout, "%6.1f\n", std[i]); fprintf(fpout, "\nCorrelation matrix.\n"); for (i = 0; i < m; i++) { for (j = 0; j < m; j++) fprintf(fpout, " %7.4f ", R(i, j)); fprintf(fpout, "\n"); } fprintf(fpout, "\nVariance matrix.\n"); for (i = 0; i < m; i++) { for (j = 0; j < m; j++) fprintf(fpout, " %7.3f ", V(i, j)); fprintf(fpout, "\n"); } fprintf(fpout, "\nSum of weights %6.1f\n", sw); END: if (x) NAG_FREE(x); if (r) NAG_FREE(r); if (v) NAG_FREE(v); if (wt) NAG_FREE(wt); if (wmean) NAG_FREE(wmean); if (std) NAG_FREE(std); } if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); return exit_status; }