using System; using System.Text; using System.Runtime.InteropServices;/* Provides mappings between C# and native code */ namespace NumericalAlgorithmsGroup { class LUSolve { /* F07ABF Example Program Text */ /* Mark 21 Release. NAG Copyright 2004. */ [DllImport("FLDLL214Z_nag.dll")]/* Import from DLL, the C# compiler provides a rudimentry check of the signature */ public static extern void DGESVX(string fact, int fact_len, string trans, int trans_len, ref int n, ref int nrhs, double [,]a, ref int lda, double [,]af, ref int ldaf, int []ipiv, StringBuilder equed, int len_equed, double []r, double [] c, double [,] b, ref int ldb, double [,] x, ref int ldx, ref double rcond, double [] ferr, double [] berr, double [] work, int [] iwork, ref int info); [DllImport("FLDLL214Z_nag.dll")] public static extern void X04CAF(string matrix, int matrix_len, string diag, int diag_len, ref int m, ref int n, double [,] a, ref int lda, string title, int title_len, ref int ifail); public static void Main() { /* Scalars */ double rcond=0.0; int exit_status, i, ifail, info=0, j, n, nrhs; /* Character args */ /*string equed="X";*/ StringBuilder equed = new StringBuilder("X"); string trans; Console.WriteLine("F07ABF Example Program Results\n"); n = 4; nrhs = 2; double [,] a = new double [4,4] { {1.80, 2.88, 2.05, -0.89}, {525.00, -295.00, -95.00, -380.00}, {1.58, -2.69, -2.90, -1.04}, {-1.11, -0.66, -0.59, 0.80} }; double [,] at =new double [n,n]; double [,] af = new double [n, n]; double [,] b = new double [4,2]{ {9.52, 18.47}, {2435.00, 225.00}, {0.77, -13.28}, {-6.22, -6.21} }; double [,] bt = new double [nrhs,n]; double [] berr = new double [nrhs]; double [] c = new double [n]; double [] ferr = new double [nrhs]; double [] r = new double [n]; double [] work = new double [4*n]; double [,] x = new double [n,nrhs]; int [] ipiv = new int [n]; int [] iwork = new int [n]; /* Transpose A */ for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { at[j,i] = a[i,j]; } } /* Transpose b */ for (i = 0; i < n; ++i) { for (j = 0; j < nrhs; ++j) { bt[j,i] = b[i,j]; } } /* Solve the equations AX = B for X */ DGESVX("Equilibration", 13, "No transpose", 12, ref n, ref nrhs, at, ref n, af, ref n, ipiv, equed, 1, r, c, bt, ref n, x, ref n, ref rcond, ferr, berr, work, iwork, ref info); if (info == 0 || info == n + 1) { /* Print solution, error bounds, condition number, the form */ /* of equilibration and the pivot growth factor */ ifail = 0; /* x is returned from Fortran and so is 'transposed' already */ X04CAF("General", 7, " ", 1, ref n, ref nrhs, x, ref n, "Solution(s)", 11, ref ifail); Console.WriteLine("\nBackward errors (machine-dependent"); for (j = 1; j <= nrhs; ++j) { Console.WriteLine("{0, 11:e1}{1}", berr[j - 1], j%7 == 0 || j == nrhs ?"\n":""); } Console.WriteLine(); Console.WriteLine("Estimated forward error bounds (machine-dependent)"); for (j = 1; j <= nrhs; ++j) { Console.WriteLine("{0, 11:e1}{1}", ferr[j - 1], j%7 == 0 || j == nrhs ?"\n":""); } Console.WriteLine("equed = {0}", equed.ToString()); if (equed.ToString() == "N") { Console.WriteLine("A has not been equilibrated"); } else if (equed.ToString() == "R") { Console.WriteLine("A has been row scaled as diag(R)*A"); } else if (equed.ToString() == "C") { Console.WriteLine("A has been column scaled as A*diag(C)"); } else if (equed.ToString() == "B") { Console.WriteLine("A has been row and column scaled as diag(R)*A*diag(C)"); } Console.WriteLine("Reciprocal condition number estimate of scaled matrix"); Console.WriteLine("{0, 11:e1}", rcond); Console.WriteLine("Estimate of reciprocal pivot growth factor"); Console.WriteLine("{0, 11:e1}", work[0]); if (info == n + 1) { Console.WriteLine("The matrix A is singular to working precision"); } } else { Console.WriteLine("The ( {0}) element of the factor U is zero", info); } return; } } }