/* nag_mv_canon_corr (g03adc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 8 revised, 2004. * */ #include #include #include #include #define Z(I,J) z[(I)*tdz + J] #define CVX(I,J) cvx[(I)*tdcvx + J] #define CVY(I,J) cvy[(I)*tdcvy + J] #define E(I,J) e[(I)*tde + J] int main(void) { Integer exit_status=0, i, *isz=0, j, m, n, ncv, nx, ny, tdcvx, tdcvy, tde=6; Integer tdz; NagError fail; char weight[2]; double *cvx=0, *cvy=0, *e=0, tol, *wt=0, *wtptr, *z=0; INIT_FAIL(fail); Vprintf("nag_mv_canon_corr (g03adc) Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld",&n); Vscanf("%ld",&m); Vscanf("%ld",&nx); Vscanf("%ld",&ny); Vscanf("%s",weight); if (nx>=1 && ny>=1 && n > nx+ny && m>= nx+ny) { if ( !( z = NAG_ALLOC(n*m, double)) || !( wt = NAG_ALLOC(n, double)) || !( isz = NAG_ALLOC(m, Integer)) || !( cvx = NAG_ALLOC(nx*(MIN(nx,ny)), double)) || !( cvy = NAG_ALLOC(ny*(MIN(nx,ny)), double)) || !( e = NAG_ALLOC((MIN(nx,ny))*6, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tdz = m; tdcvx = MIN(nx,ny); tdcvy = MIN(nx,ny); tde = 6; } else { Vprintf("Invalid nx or ny or n or m.\n"); exit_status = 1; return exit_status; } if (*weight == 'W') { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&Z(i,j)); Vscanf("%lf",&wt[i]); } wtptr = wt; } else { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&Z(i,j)); } wtptr = 0; } for (j = 0; j < m; ++j) Vscanf("%ld",&isz[j]); tol = 1e-6; /* nag_mv_canon_corr (g03adc). * Canonical correlation analysis */ nag_mv_canon_corr(n, m, z, tdz, isz, nx, ny, wtptr, e, tde, &ncv, cvx, tdcvx, cvy, tdcvy, tol, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_mv_canon_corr (g03adc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n%s%2ld%s%2ld\n\n", "Rank of x = ", nx, " Rank of y = ",ny); 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 coefficients for y\n"); for (i = 0; i < ny; ++i) { for (j = 0; j < ncv; ++j) Vprintf("%9.4f",CVY(i,j)); Vprintf("\n"); } END: if (z) NAG_FREE(z); if (wt) NAG_FREE(wt); if (isz) NAG_FREE(isz); if (cvx) NAG_FREE(cvx); if (cvy) NAG_FREE(cvy); if (e) NAG_FREE(e); return exit_status; }