// g02ae Example Program Text // C# version, NAG Copyright 2012 using System; using NagLibrary; using System.Globalization; using System.Runtime.InteropServices;/* Provides mappings between C# and native code */ namespace NagDotNetExamples { public class G02AEE { static string datafile = "ExampleData/g02aee.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); double one = 1.0; double zero = 0.0; // int nin = 5, // nout = 6; double errtol, nrmpgd; int feval = 0, i = 0, ifail = 0, iter = 0, k = 0, lda = 0, ldg = 0, ldx = 0, maxit = 0, n = 0; Console.WriteLine(" {0}", "g02ae Example Program Results"); Console.WriteLine(""); // Skip heading in data file sr.Reset(); // Read in the problem size sr.Reset(); n = int.Parse(sr.Next()); lda = (int)n; ldg = (int)n; ldx = (int)n; double[,] a = new double[lda, n]; double[,] g = new double[ldg, n]; double[,] x = new double[ldx, n]; // allocate // Read in the matrix G 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); } } // Use the defaults for ERRTOL and MAXIT errtol = (double)zero; maxit = 0; // Set k value k = 2; // Calculate the nearest factor loading matrix ifail = 0; G02.g02ae(g, n, k, errtol, maxit, x, out iter, out feval, out nrmpgd, out ifail); // Display results ifail = 0; X04.x04ca("General", "", n, k, x, "Factor Loading Matrix X", out ifail); Console.WriteLine(""); Console.WriteLine(" {0}{1,11}", "Number of steps taken:", iter); Console.WriteLine(" {0}{1,9}", "Number of function evaluations:", feval); // Generate Nearest k factor correlation matrix // The NAG name equivalent of dgemm is f06ya; F06.f06ya("N", "T", n, n, k, one, x, x, zero, a, out ifail); for (i = 1; i <= n; i++) { a[i - 1, i - 1] = one; } Console.WriteLine(""); ifail = 0; X04.x04ca("General", "", n, n, a, "Nearest Correlation Matrix", out ifail); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }