/* nag_mv_discrim_distance_mat (g03eac) 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] int main(void) { Integer exit_status=0, i, *isx=0, j, m, n, tdx; NagError fail; Nag_DistanceType dist; Nag_MatUpdate update; Nag_VarScaleType scale; char char_dist[2], char_scale[2], char_update[2]; double *d=0, *s=0, *x=0; INIT_FAIL(fail); Vprintf("nag_mv_distance_mat (g03eac) Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld",&n); Vscanf("%ld",&m); if (n>=2 && m>=1) { if ( !( d = NAG_ALLOC(n*(n-1)/2, double)) || !( s = NAG_ALLOC(m, double)) || !( x = NAG_ALLOC((n)*(m), double)) || !( isx = NAG_ALLOC(m, Integer)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tdx = m; } else { Vprintf("Invalid n or m.\n"); exit_status = 1; return exit_status; } Vscanf("%s",char_update); Vscanf("%s",char_dist); Vscanf("%s",char_scale); for (j = 0; j < n; ++j) { for (i = 0; i < m; ++i) Vscanf("%lf",&X(j,i)); } for (i = 0; i < m; ++i) Vscanf("%ld",&isx[i]); for (i = 0; i < m; ++i) Vscanf("%lf",&s[i]); /* Compute the distance matrix */ if (*char_update == 'U') update = Nag_MatUp; else if (*char_update == 'I') update = Nag_NoMatUp; if (*char_dist == 'A') dist = Nag_DistAbs; else if (*char_dist == 'E') dist = Nag_DistEuclid; else if (*char_dist == 'S') dist = Nag_DistSquared; if (*char_scale == 'S') scale = Nag_VarScaleStd; else if (*char_scale == 'R') scale = Nag_VarScaleRange; else if (*char_scale == 'G') scale = Nag_VarScaleUser; else if (*char_scale == 'U') scale = Nag_NoVarScale; /* nag_mv_distance_mat (g03eac). * Compute distance (dissimilarity) matrix */ nag_mv_distance_mat(update, dist, scale, n, m, x, tdx, isx, s, d, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_mv_distance_mat (g03eac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the distance matrix */ Vprintf("\n"); Vprintf(" Distance Matrix "); Vprintf("\n"); Vprintf("\n"); Vprintf(" %s\n"," 1 2 3 4"); Vprintf("\n"); for (i = 2; i <= n; ++i) { Vprintf("%2ld ",i); for (j=(i-1)*(i-2)/2+1; j<=i*(i - 1)/2; ++j) Vprintf("%5.2f ",d[j-1]); Vprintf("\n"); } END: if (d) NAG_FREE(d); if (s) NAG_FREE(s); if (x) NAG_FREE(x); if (isx) NAG_FREE(isx); return exit_status; }