using System; using System.Text; using System.Runtime.InteropServices; using NagLibrary; public class NagG03Functions { public static void Main() { int n = 20; int m = 5; int nvar = 5; int k = 3; int maxit = 10; int tdx = m; int tdc = nvar; double[] x = { 77.3, 13.0, 9.7, 1.5, 6.4, 82.5, 10.0, 7.5, 1.5, 6.5, 66.9, 20.6, 12.5, 2.3, 7.0, 47.2, 33.8, 19.0, 2.8, 5.8, 65.3, 20.5, 14.2, 1.9, 6.9, 83.3, 10.0, 6.7, 2.2, 7.0, 81.6, 12.7, 5.7, 2.9, 6.7, 47.8, 36.5, 15.7, 2.3, 7.2, 48.6, 37.1, 14.3, 2.1, 7.2, 61.6, 25.5, 12.9, 1.9, 7.3, 58.6, 26.5, 14.9, 2.4, 6.7, 69.3, 22.3, 8.4, 4.0, 7.0, 61.8, 30.8, 7.4, 2.7, 6.4, 67.7, 25.3, 7.0, 4.8, 7.3, 57.2, 31.2, 11.6, 2.4, 6.5, 67.2, 22.7, 10.1, 3.3, 6.2, 59.2, 31.2, 9.6, 2.4, 6.0, 80.2, 13.2, 6.6, 2.0, 5.8, 82.2, 11.1, 6.7, 2.2, 7.2, 69.7, 20.7, 9.6, 3.1, 5.9 }; double[] cmeans = { 82.5, 10.0, 7.5, 1.5, 6.5, 47.8, 36.5, 15.7, 2.3, 7.2, 67.2, 22.7, 10.1, 3.3, 6.2 }; double[] wt = new double[n]; int[] isx = new int[m]; int[] inc = new int[n]; int[] nic = new int[k]; double[] css = new double[k]; double[] csw = new double[k]; nag_declarations.NagError fail = new nag_declarations.NagError(); fail.message = new byte[512]; Encoding enc = Encoding.ASCII; for (int i = 0; i < n; i++) wt[i] = 1.0; for (int i = 0; i < m; i++) isx[i] = 1; /* nag_mv_kmeans_cluster_analysis (g03efc). * K-means */ nag_declarations.nag_mv_kmeans_cluster_analysis(n, m, x, tdx, isx, nvar, k, cmeans, tdc, wt, inc, nic, css, csw, maxit, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_mv_kmeans_cluster_analysis (g03efc):"); Console.WriteLine(enc.GetString(fail.message)); } else { Console.WriteLine(); Console.WriteLine("The cluster each point belongs to"); for (int i = 0; i < n; ++i) { Console.Write(" {0, 6:d}", inc[i]); if ((i + 1) % 10 == 0) Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("The number of points in each cluster"); for (int i = 0; i < k; ++i) Console.Write(" {0,6:d}", nic[i]); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("The within-cluster sum of weights of each cluster"); for (int i = 0; i < k; ++i) Console.Write(" {0,9:f2}", csw[i]); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("The within-cluster sum of squares of each cluster"); for (int i = 0; i < k; ++i) Console.Write(" {0,13:f4}", css[i]); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("The final cluster centres"); Console.WriteLine(" 1 2 3 4 5"); for (int i = 0; i < k; ++i) { Console.Write(" {0, 5:d} ", i + 1); for (int j = 0; j < nvar; ++j) Console.Write("{0,8:f4}", cmeans[i * tdc + j]); Console.WriteLine(); } } } }