/* nag_mv_canon_var (g03acc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 8 revised, 2004. * */ #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(void) { Integer exit_status=0, i, *ing=0, irx, *isx=0, j, m, n, ncv, ng, *nig=0; Integer nx, tdcvm, tdcvx, tde, tdx; NagError fail; Nag_Weightstype weight; char wtchar[2]; double *cvm=0, *cvx=0, *e=0, tol, *wt=0, *x=0; INIT_FAIL(fail); Vprintf("nag_mv_canon_var (g03acc) Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld",&n); Vscanf("%ld",&m); Vscanf("%ld",&nx); Vscanf("%ld",&ng); Vscanf("%s",wtchar); 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)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tdx = m; tde = 6; tdcvm = nx; tdcvx = ng-1; } else { Vprintf("Invalid n or m.\n"); exit_status = 1; return exit_status; } if (*wtchar == 'W' || *wtchar == 'V') { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&X(i,j)); Vscanf("%lf",&wt[i]); Vscanf("%ld",&ing[i]); } if (*wtchar == 'W') weight = Nag_Weightsfreq; else weight = Nag_Weightsvar; } else { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&X(i,j)); Vscanf("%ld",&ing[i]); } weight = Nag_NoWeights; } for (j = 0; j < m; ++j) Vscanf("%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) { Vprintf("Error from nag_mv_canon_var (g03acc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("%s%2ld\n\n","Rank of x = ",irx); Vprintf("Canonical Eigenvalues Percentage CHISQ" " DF SIG \n"); Vprintf("Correlations Variation\n"); for (i = 0; i < ncv; ++i) { for (j = 0; j < 6; ++j) Vprintf("%12.4f",E(i,j)); Vprintf("\n"); } Vprintf("\nCanonical Coefficients for X\n"); for (i = 0; i < nx; ++i) { for (j = 0; j < ncv; ++j) Vprintf("%9.4f",CVX(i,j)); Vprintf("\n"); } Vprintf("\nCanonical variate means\n"); for (i = 0; i < ng; ++i) { for (j = 0; j < ncv; ++j) Vprintf("%9.4f",CVM(i,j)); Vprintf("\n"); } END: 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; }