/* nag_mv_canon_var (g03acc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 8 revised, 2004. * */ #include #include #include #include #include #define X(I, J) x[(I) *tdx + J] #define E(I, J) e[(I) *tde + J] #define CVM(I, J) cvm[(I) *tdcvm + J] #define CVX(I, J) cvx[(I) *tdcvx + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, irx, j, m, n, ncv, ng; Integer nx, tdcvm, tdcvx, tde, tdx; Integer *ing = 0, *isx = 0, *nig = 0; double *cvm = 0, *cvx = 0, *e = 0, tol, *wt = 0, *x = 0; char nag_enum_arg[40]; Nag_Weightstype weight; NagError fail; 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_mv_canon_var (g03acc) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld", &n); fscanf(fpin, "%ld", &m); fscanf(fpin, "%ld", &nx); fscanf(fpin, "%ld", &ng); fscanf(fpin, "%s", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ weight = (Nag_Weightstype) nag_enum_name_to_value(nag_enum_arg); if (n >= nx+ng && m >= nx) { if (!(x = NAG_ALLOC(n*m, double)) || !(wt = NAG_ALLOC(n, double)) || !(ing = NAG_ALLOC(n, Integer)) || !(e = NAG_ALLOC((MIN(nx, ng-1))*6, double)) || !(cvm = NAG_ALLOC(ng*nx, double)) || !(cvx = NAG_ALLOC(nx*(ng-1), double)) || !(nig = NAG_ALLOC(ng, Integer)) || !(isx = NAG_ALLOC(m, Integer)) ) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdx = m; tde = 6; tdcvm = nx; tdcvx = ng-1; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } if (weight == Nag_Weightsfreq || weight == Nag_Weightsvar) { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf", &wt[i]); fscanf(fpin, "%ld", &ing[i]); } } else { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%ld", &ing[i]); } } for (j = 0; j < m; ++j) fscanf(fpin, "%ld", &isx[j]); tol = 1e-6; /* nag_mv_canon_var (g03acc). * Canonical variate analysis */ nag_mv_canon_var(weight, n, m, x, tdx, isx, nx, ing, ng, wt, nig, cvm, tdcvm, e, tde, &ncv, cvx, tdcvx, tol, &irx, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_mv_canon_var (g03acc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "%s%2ld\n\n", "Rank of x = ", irx); fprintf(fpout, "Canonical Eigenvalues Percentage CHISQ" " DF SIG \n"); fprintf(fpout, "Correlations Variation\n"); for (i = 0; i < ncv; ++i) { for (j = 0; j < 6; ++j) fprintf(fpout, "%12.4f", E(i, j)); fprintf(fpout, "\n"); } fprintf(fpout, "\nCanonical Coefficients for X\n"); for (i = 0; i < nx; ++i) { for (j = 0; j < ncv; ++j) fprintf(fpout, "%9.4f", CVX(i, j)); fprintf(fpout, "\n"); } fprintf(fpout, "\nCanonical variate means\n"); for (i = 0; i < ng; ++i) { for (j = 0; j < ncv; ++j) fprintf(fpout, "%9.4f", CVM(i, j)); fprintf(fpout, "\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (wt) NAG_FREE(wt); if (ing) NAG_FREE(ing); if (e) NAG_FREE(e); if (cvm) NAG_FREE(cvm); if (cvx) NAG_FREE(cvx); if (nig) NAG_FREE(nig); if (isx) NAG_FREE(isx); return exit_status; }