/* nag_mv_cluster (g03ejc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * * Mark 6 revised, 2000. */ #include #include #include #include #define NMAX 10 #define MMAX 10 int main(void) { double cd[NMAX-1], d[NMAX*(NMAX-1)/2], dord[NMAX], s[MMAX], x[NMAX][MMAX]; double dmin_; double dstep, ydist; double dlevel; Integer ic[NMAX], ilc[NMAX-1], iord[NMAX], isx[MMAX], iuc[NMAX-1]; Integer nsym; Integer i, j, k; Integer m, n; Integer int_method; Integer tdx=MMAX; char **c = 0; char name[NMAX][3]; char char_dist[2]; char char_scale[2]; char char_update[2]; Nag_ClusterMethod method; Nag_MatUpdate update; Nag_DistanceType dist; Nag_VarScaleType scale; Vprintf("g03ejc Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld",&n); Vscanf("%ld",&m); if (n <= NMAX && m <= MMAX) { Vscanf("%ld",&int_method); if (int_method == 1) method = Nag_SingleLink; else if (int_method == 2) method = Nag_CompleteLink; else if (int_method == 3) method = Nag_GroupAverage; else if (int_method == 4) method = Nag_Centroid; else if (int_method == 5) method = Nag_Median; else method = Nag_MinVariance; Vscanf("%s",char_update); if (*char_update == 'U') update = Nag_MatUp; else update = Nag_NoMatUp; Vscanf("%s",char_dist); if (*char_dist == 'A') dist = Nag_DistAbs; else if (*char_dist == 'E') dist = Nag_DistEuclid; else dist = Nag_DistSquared; Vscanf("%s",char_scale); if (*char_scale == 'S') scale = Nag_VarScaleStd; else if (*char_scale == 'R') scale = Nag_VarScaleRange; else if (*char_scale == 'G') scale = Nag_VarScaleUser; else scale = Nag_NoVarScale; for (j = 0; j < n; ++j) { for (i = 0; i < m; ++i) Vscanf("%lf",&x[j][i]); Vscanf("%s",name[j]); } for (i = 0; i < m; ++i) Vscanf("%ld",&isx[i]); for (i = 0; i < m; ++i) Vscanf("%lf",&s[i]); Vscanf("%ld",&k); Vscanf("%lf",&dlevel); /* Compute the distance matrix */ g03eac(update, dist, scale, n, m, &x[0][0], tdx, isx, s, d, NAGERR_DEFAULT); /* Perform clustering */ g03ecc(method, n, d, ilc, iuc, cd, iord, dord, NAGERR_DEFAULT); Vprintf("\nDistance Clusters Joined\n\n"); for (i = 0; i < n-1; ++i) { Vprintf("%10.3f ",cd[i]); Vprintf("%3s",name[ilc[i]-1]); Vprintf("%3s",name[iuc[i]-1]); Vprintf("\n"); } /* Produce dendrogram */ nsym = 20; dmin_ = 0.0; dstep = cd[n - 2] / (double) nsym; g03ehc(Nag_DendSouth, n, dord, dmin_, dstep, nsym, &c, NAGERR_DEFAULT); Vprintf("\n"); Vprintf("Dendrogram "); Vprintf("\n"); Vprintf("\n"); ydist = cd[n - 2]; for (i = 0; i < nsym; ++i) { if ((i+1) % 3 == 1) { Vprintf("%10.3f%6s",ydist,""); Vprintf("%s",c[i]); Vprintf("\n"); } else { Vprintf("%16s%s","", c[i]); Vprintf("\n"); } ydist -= dstep; } Vprintf("\n"); Vprintf("%14s",""); for (i = 0; i < n; ++i) { Vprintf("%3s",name[iord[i]-1]); } Vprintf("\n"); g03xzc(&c); g03ejc(n, cd, iord, dord, &k, &dlevel, ic, NAGERR_DEFAULT); Vprintf("\n%s%2ld%s\n\n","Allocation to ",k," clusters"); Vprintf("Object Cluster\n\n"); for (i = 0; i < n; ++i) { Vprintf("%5s%s%5s","",name[i],""); Vprintf("%ld ",ic[i]); Vprintf("\n"); } return EXIT_SUCCESS; } else { Vprintf("Incorrect input value of n or m.\n"); return EXIT_FAILURE; } }