// f08aa Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class F08AAE { static string datafile = "ExampleData/f08aae.d"; static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { try { DataReader sr = new DataReader(datafile); double rnorm=0.0; int i, info, j, ldb, m, n, nrhs; // Skip heading in data file sr.Reset(); sr.Reset(); m = int.Parse(sr.Next()); n = int.Parse(sr.Next()); nrhs = 1; double[,] a = new double[m, n]; double[,] b = new double[Math.Max(m,n),nrhs]; Console.WriteLine("f08aa Example Program Results"); Console.WriteLine(""); if (m > 0 && n > 0 && m >= n) { // // Read A and B from data file // sr.Reset(); for (i = 1 ; i <= m ; i++) { for (j = 1 ; j <= n ; j++) { a[i - 1 , j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } sr.Reset(); for (i = 1 ; i <= m ; i++) { b[i - 1,0] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // Solve the least squares problem min( norm2(b - Ax) ) for x // ldb = (int)m; // F08.f08aa("No transpose", m, n, nrhs, a, b, out info); // // Print solution // if (info < 0) { return; } Console.WriteLine(" {0}","Least squares solution"); for (i = 1 ; i <= n ; i++) { Console.Write(" {0, 11:f4}", b[i - 1,0]); } Console.WriteLine(); // // Compute and print estimate of the square root of the residual // sum of squares // // Compute rnorm for (i = Math.Min(m,n); i < Math.Max(m,n); i++) { rnorm += b[i,0]*b[i,0]; } rnorm = Math.Sqrt(rnorm); Console.WriteLine(""); Console.WriteLine(" {0}","Square root of the residual sum of squares"); Console.WriteLine(" {0,8:e2}",rnorm); } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }