// f08kb Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class F08KBE { static string datafile = "ExampleData/f08kbe.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 eps, serrbd; int i, info, j, m, n; sr.Reset(); sr.Reset(); m = int.Parse(sr.Next()); n = int.Parse(sr.Next()); double[,] a = new double[m, n]; double[,] dummy = new double[1, 1]; double[] rcondu = new double[Math.Min(m,n)]; double[] rcondv = new double[Math.Min(m,n)]; double[] s = new double[Math.Min(m,n)]; double[] uerrbd = new double[Math.Min(m,n)]; double[] verrbd = new double[Math.Min(m,n)]; double[,] vt = new double[n, n]; int ifail; Console.WriteLine("f08kb Example Program Results"); Console.WriteLine(""); // Skip heading in data file if (m > 0 && n > 0) { // // Read the m by n matrix A 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); } } // // Compute the singular values and left and right singular vectors // of A (A = U*S*(V**T), m.ge.n) // F08.f08kb("Overwrite A by U", "Singular vectors (V)", m, n, a, s, dummy, vt, out info); if (info < 0) { return; } // if (info == 0) { // // Print solution // Console.WriteLine(" {0}","Singular values"); for (j = 1 ; j <= n ; j++) { Console.Write(" {0, 10:f4}", s[j - 1]); } Console.WriteLine(); // X04.x04ca("General", "", m, n, a, "Left singular vectors (first n columns of U)", out ifail); Console.WriteLine(""); X04.x04ca("General", "", n, n, vt, "Right singular vectors by row (V**T)", out ifail); // // Get the machine precision, eps and compute the approximate // error bound for the computed singular values. Note that for // the 2-norm, s[0] = norm(a) // eps = X02.x02aj(); serrbd = eps * s[0]; // // Call f08fl to estimate reciprocal condition // numbers for the singular vectors // F08.f08fl("Left", m, n, s, rcondu, out info); F08.f08fl("Right", m, n, s, rcondv, out info); // // Compute the error estimates for the singular vectors // for (i = 1 ; i <= n ; i++) { uerrbd[i - 1] = serrbd / rcondu[i - 1]; verrbd[i - 1] = serrbd / rcondv[i - 1]; } // // Print the approximate error bounds for the singular values // and vectors // Console.WriteLine(""); Console.WriteLine(" {0}","Error estimate for the singular values"); Console.WriteLine(" {0,11:e1}",serrbd); Console.WriteLine(""); Console.WriteLine(" {0}","Error estimates for the left singular vectors"); for (i = 1 ; i <= n ; i++) { Console.Write(" {0, 10:e1}", uerrbd[i - 1]); } Console.WriteLine(); Console.WriteLine(""); Console.WriteLine(" {0}","Error estimates for the right singular vectors"); for (i = 1 ; i <= n ; i++) { Console.Write(" {0, 10:e1}", verrbd[i - 1]); } Console.WriteLine(); } else { Console.WriteLine(" {0}{1,4}","Failure in DGESVD. INFO =",info); } } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }