/* nag_mv_discrim_mahaldist (g03dbc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 8 revised, 2004. * */ #include #include #include #include #define D(I,J) d[(I)*tdd + J] #define GMEAN(I,J) gmean[(I)*tdgmean + J] #define X(I,J) x[(I)*tdx + J] int main(void) { Integer exit_status=0, i, *ing=0, *isx=0, j, m, n, ng, *nig=0, nobs, nvar; Integer tdd, tdgmean, tdx; NagError fail; Nag_GroupCovars equal; char char_equal[2], weight[2]; double *d=0, *det=0, df, *gc=0, *gmean=0, sig, stat, *wt=0, *wtptr=0, *x=0; INIT_FAIL(fail); Vprintf("nag_mv_discrim_mahaldist (g03dbc) Example Program Results\n\n"); /* Skip headings in data file */ Vscanf("%*[^\n]"); Vscanf("%ld",&n); Vscanf("%ld",&m); Vscanf("%ld",&nvar); Vscanf("%ld",&ng); Vscanf("%s",weight); if (n>=1 && nvar>=1 && m>=nvar && ng>=2) { if ( !( det = NAG_ALLOC(ng, double)) || !( gc = NAG_ALLOC((ng+1)*nvar*(nvar+1)/2, double)) || !( gmean = NAG_ALLOC(ng*nvar, double)) || !( wt = NAG_ALLOC(n, double)) || !( x = NAG_ALLOC(n*m, double)) || !( ing = NAG_ALLOC(n, Integer)) || !( isx = NAG_ALLOC(m, Integer)) || !( nig = NAG_ALLOC(ng, Integer)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tdgmean=nvar; tdx=m; } else { Vprintf("Invalid n or nvar or m or ng.\n"); exit_status = 1; return exit_status; } if (*weight == 'W') { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&X(i,j)); Vscanf("%ld",&ing[i]); Vscanf("%lf",&wt[i]); } wtptr = wt; } else { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&X(i,j)); Vscanf("%ld",&ing[i]); } } for (j = 0; j < m; ++j) Vscanf("%ld",&isx[j]); /* nag_mv_discrim (g03dac). * Test for equality of within-group covariance matrices */ nag_mv_discrim(n, m, x, tdx, isx, nvar, ing, ng, wtptr, nig, gmean, tdgmean, det, gc, &stat, &df, &sig, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_mv_discrim (g03dac).\n%s\n", fail.message); exit_status = 1; goto END; } Vscanf("%ld",&nobs); Vscanf("%s",char_equal); if (nobs >=1) { if ( !( d = NAG_ALLOC(nobs*ng, double))) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tdd=ng; for (i = 0; i < nobs; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&X(i,j)); } if (*char_equal == 'E') { equal = Nag_EqualCovar; } else if (*char_equal == 'U') { equal = Nag_NotEqualCovar; } /* nag_mv_discrim_mahaldist (g03dbc). * Mahalanobis squared distances, following nag_mv_discrim * (g03dac) */ nag_mv_discrim_mahaldist(equal, Nag_SamplePoints, nvar, ng, gmean, tdgmean, gc, nobs, m, isx, x, tdx, d, tdd, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_mv_discrim_mahaldist (g03dbc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n Obs Distances\n\n"); for (i = 0; i < nobs; ++i) { Vprintf(" %3ld",i+1); for (j = 0; j < ng; ++j) Vprintf("%10.3f",D(i,j)); Vprintf("\n"); } } END: if (d) NAG_FREE(d); if (det) NAG_FREE(det); if (gc) NAG_FREE(gc); if (gmean) NAG_FREE(gmean); if (wt) NAG_FREE(wt); if (x) NAG_FREE(x); if (ing) NAG_FREE(ing); if (isx) NAG_FREE(isx); if (nig) NAG_FREE(nig); return exit_status; }