// g02ab Example Program Text // C# version, NAG Copyright 2012 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class G02ABE { static string datafile = "ExampleData/g02abe.d"; // Specify data file as a command line argument. // It defaults to the named file specified below otherwise. static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { try { DataReader sr = new DataReader(datafile); // g02ab Example Program Text // Mark 24 Release. NAG Copyright 2012. double alpha, errtol, nrmgrd; int feval = 0, i = 0, ifail = 0, iter = 0, ldg = 0, ldx = 0, lwork = 0, maxit = 0, maxits = 0, n = 0; string opt = ""; Console.WriteLine(" {0}", "g02ab Example Program Results"); Console.WriteLine(""); // Skip heading in data file sr.Reset(); // Read in the problem size, opt and alpha sr.Reset(); n = int.Parse(sr.Next()); opt = sr.Next(); alpha = double.Parse(sr.Next(), CultureInfo.InvariantCulture); ldg = (int)n; ldx = (int)n; lwork = 66 * n; // allocate // Read in the matrix G double[] eig = new double[n]; double[,] g = new double[ldg, n]; double[] w = new double[n]; double[] work = new double[lwork]; double[,] x = new double[ldx, n]; sr.Reset(); for (i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { g[i - 1, j] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } // Read in the vector W sr.Reset(); for (int j = 0; j < n; j++) { w[j] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // Use the defaults for ERRTOL, MAXITS and MAXIT errtol = 0.00e0; maxits = 0; maxit = 0; // Calculate nearest correlation matrix ifail = 0; G02.g02ab(g, n, opt, alpha, w, errtol, maxits, maxit, x, out iter, out feval, out nrmgrd, out ifail); // Display results ifail = 0; X04.x04ca("General", "", n, n, x, "Nearest Correlation Matrix X", out ifail); Console.WriteLine(""); Console.WriteLine(" {0}{1,11}", "Number of Newton steps taken:", iter); Console.WriteLine(" {0}{1,9}", "Number of function evaluations:", feval); Console.WriteLine(""); Console.WriteLine(" {0}{1,37:f3}", "ALPHA: ", alpha); ifail = 0; // The NAG name equivalent of dsyev is F08.f08fa; String jobz = "N"; String uplo = "U"; F08.f08fa(jobz, uplo, n, x, eig, out ifail); Console.WriteLine(""); double[,] eig_prime = new double[1, n]; for (i = 0; i < n; i++) { eig_prime[0, i] = eig[i]; } X04.x04ca("General", "", 1, n, eig_prime, "Eigenvalues of X", out ifail); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }