/* nag_cov_to_corr (g02bwc) Example Program. * * Copyright 2002 Numerical Algorithms Group. * * Mark 7, 2002. */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; /* Arrays */ char nag_enum_mean[40], nag_enum_weight[40]; double *c = 0, *wmean = 0, *wt = 0, *x = 0; double *wtptr = 0; /* Scalars */ double sw; Integer exit_status, j, k, m, n, pdx; Nag_OrderType order; Nag_SumSquare mean; Nag_Boolean weight; NagError fail; #ifdef NAG_COLUMN_MAJOR #define X(I, J) x[(J-1)*pdx + I - 1] order = Nag_ColMajor; #else #define X(I, J) x[(I-1)*pdx + J - 1] order = Nag_RowMajor; #endif 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); (void) nag_example_file_io(argc, argv, "-nag_write", &outfile); exit_status = 0; fprintf(fpout, "nag_cov_to_corr (g02bwc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); while (fscanf(fpin, "%s %s %ld%ld%*[^\n]", nag_enum_mean, nag_enum_weight, &m, &n) != EOF) { /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ mean = (Nag_SumSquare) nag_enum_name_to_value(nag_enum_mean); weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_weight); /* Allocate memory */ if (!(c = NAG_ALLOC((m*(m+1))/2, double)) || !(wmean = NAG_ALLOC(m, double)) || !(wt = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n * m, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } #ifdef NAG_COLUMN_MAJOR pdx = n; #else pdx = m; #endif for (j = 1; j <= n; ++j) fscanf(fpin, "%lf", &wt[j-1]); fscanf(fpin, "%*[^\n] "); for (j = 1; j <= n; ++j) { for (k = 1; k <= m; ++k) fscanf(fpin, "%lf", &X(j, k)); } fscanf(fpin, "%*[^\n] "); if (weight) wtptr = wt; /* Calculate the sums of squares and cross-products matrix */ /* nag_sum_sqs (g02buc). * Computes a weighted sum of squares matrix */ nag_sum_sqs(order, mean, n, m, x, pdx, wtptr, &sw, wmean, c, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_sum_sqs (g02buc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Calculate the correlation matrix */ /* nag_cov_to_corr (g02bwc). * Computes a correlation matrix from a sum of squares * matrix */ nag_cov_to_corr(m, c, &fail); /* Print the correlation matrix */ if (fail.code == NE_NOERROR) { fprintf(fpout, "\n"); /* nag_pack_real_mat_print (x04ccc). * Print real packed triangular matrix (easy-to-use) */ if (outfile) fclose(fpout); nag_pack_real_mat_print(Nag_ColMajor, Nag_Upper, Nag_NonUnitDiag, m, c, "Correlation matrix", outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_pack_real_mat_print (x04ccc).\n%s\n", fail.message); exit_status = 1; goto END; } } else if (fail.code == NE_ZERO_VARIANCE) { fprintf(fpout, "\n"); fprintf(fpout, "NOTE: some variances are zero\n\n"); /* nag_pack_real_mat_print (x04ccc), see above. */ if (outfile) fclose(fpout); nag_pack_real_mat_print(Nag_ColMajor, Nag_Upper, Nag_NonUnitDiag, m, c, "Correlation matrix", outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_pack_real_mat_print (x04ccc).\n%s\n", fail.message); exit_status = 1; goto END; } } else { fprintf(fpout, "Error from nag_cov_to_corr (g02bwc).\n%s\n", fail.message); exit_status = 1; goto END; } if (c) NAG_FREE(c); if (wmean) NAG_FREE(wmean); if (wt) NAG_FREE(wt); if (x) NAG_FREE(x); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (c) NAG_FREE(c); if (wmean) NAG_FREE(wmean); if (wt) NAG_FREE(wt); if (x) NAG_FREE(x); return exit_status; }