/* nag_mv_hierar_cluster_analysis (g03ecc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 8 revised, 2004. * */ #include #include #include #include #include #define NMAX 10 #define X(I, J) x[(I) *tdx + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, m, n, nsym, tdx; Integer *ilc = 0, *iord = 0, *isx = 0, *iuc = 0; char **c = 0, name[NMAX][2]; double dmin_, dstep, ydist; double *cd = 0, *d = 0, *dord = 0, *s = 0, *x = 0; char nag_enum_arg[40]; Nag_ClusterMethod method; Nag_DistanceType dist; Nag_MatUpdate update; Nag_VarScaleType scale; 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_hierar_cluster_analysis (g03ecc) Example Program " "Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld", &n); fscanf(fpin, "%ld", &m); if (n >= 2 && m >= 1) { if (!(cd = NAG_ALLOC(n-1, double)) || !(d = NAG_ALLOC(n*(n-1)/2, double)) || !(dord = NAG_ALLOC(n, double)) || !(s = NAG_ALLOC(m, double)) || !(x = NAG_ALLOC(n*m, double)) || !(ilc = NAG_ALLOC(n-1, Integer)) || !(iord = NAG_ALLOC(n, Integer)) || !(isx = NAG_ALLOC(m, Integer)) || !(iuc = NAG_ALLOC(n-1, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdx = m; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } fscanf(fpin, "%s", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ method = (Nag_ClusterMethod) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%s", nag_enum_arg); update = (Nag_MatUpdate) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%s", nag_enum_arg); dist = (Nag_DistanceType) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%s", nag_enum_arg); scale = (Nag_VarScaleType) nag_enum_name_to_value(nag_enum_arg); for (j = 0; j < n; ++j) { for (i = 0; i < m; ++i) fscanf(fpin, "%lf", &X(j, i)); fscanf(fpin, "%s", name[j]); } for (i = 0; i < m; ++i) fscanf(fpin, "%ld", &isx[i]); for (i = 0; i < m; ++i) fscanf(fpin, "%lf", &s[i]); /* Compute the distance matrix */ /* 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) { fprintf(fpout, "Error from nag_mv_distance_mat (g03eac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Perform clustering */ /* nag_mv_hierar_cluster_analysis (g03ecc). * Hierarchical cluster analysis */ nag_mv_hierar_cluster_analysis(method, n, d, ilc, iuc, cd, iord, dord, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_mv_hierar_cluster_analysis (g03ecc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n Distance Clusters Joined\n\n"); for (i = 1; i <= n-1; ++i) fprintf(fpout, "%10.3f %3s%3s\n", cd[i-1], name[ilc[i-1]-1], name[iuc[i-1]-1]); /* Produce dendrogram */ nsym = 20; dmin_ = 0.0; dstep = cd[n - 2] / (double) nsym; /* nag_mv_dendrogram (g03ehc). * Construct dendogram following * nag_mv_hierar_cluster_analysis (g03ecc) */ nag_mv_dendrogram(Nag_DendSouth, n, dord, dmin_, dstep, nsym, &c, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_mv_dendrogram (g03ehc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); fprintf(fpout, "Dendrogram "); fprintf(fpout, "\n"); fprintf(fpout, "\n"); ydist = cd[n - 2]; for (i = 0; i < nsym; ++i) { if ((i+1) % 3 == 1) { fprintf(fpout, "%10.3f%6s", ydist, ""); fprintf(fpout, "%s", c[i]); fprintf(fpout, "\n"); } else { fprintf(fpout, "%16s%s", "", c[i]); fprintf(fpout, "\n"); } ydist -= dstep; } fprintf(fpout, "\n"); fprintf(fpout, "%14s", ""); for (i = 0; i < n; ++i) { fprintf(fpout, "%3s", name[iord[i]-1]); } fprintf(fpout, "\n"); /* nag_mv_dend_free (g03xzc). * Frees memory allocated to the dendrogram array in * nag_mv_dendrogram (g03ehc) */ nag_mv_dend_free(&c); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (cd) NAG_FREE(cd); if (d) NAG_FREE(d); if (dord) NAG_FREE(dord); if (s) NAG_FREE(s); if (x) NAG_FREE(x); if (ilc) NAG_FREE(ilc); if (iord) NAG_FREE(iord); if (isx) NAG_FREE(isx); if (iuc) NAG_FREE(iuc); return exit_status; }