using System; using System.Text; using System.Runtime.InteropServices; /* Provides mappings between C# and native code */ namespace NumericalAlgorithmsGroup { class MultipleLinearRegression { // Declaration of the routine G02DAF in the NAG DLL // Points to note: // (i) the character string argument "mean" requires an extra "invisible" // argument "length_mean" which is passed by value and must be set to the // length of the string argument. The same goes for string argument "weight". // These "invisible arguments" are inserted by the Fortran compiler // and are not required when compiling a Fortran program, which is // why they are not documented in the NAG Fortran Library documentation. // (ii) all scalar arguments other than the string length arguments are // passed by reference, as required by Fortran [DllImport("FLDLL214Z_nag.dll")]/* Import from DLL, the C# compiler provides a rudimentry check of the signature */ public static extern void G02DAF(string mean, int length_mean, string weight, int length_weight, ref int n, double[,] x, ref int ldx, ref int m, int[] isx, ref int ip, double[] y, double[] wt, ref double rss, ref int idf, double[] b, double[] se, double[] cov, double[] res, double[] h, double[,] q, ref int ldq, ref int svd, ref int irank, double[] p, ref double tol, double[] work, ref int ifail); public static void Main() { // We solve the example problem presented in the documentation of // routine G02DAF in the NAG Fortran Library Manual. int n = 12; int m = 4; // Notice that array X is initialised in transposed format because // the NAG Fortran library has arrays in column-major order double [,] x = new double [,] { { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 } }; // Notice that array X is transposed to XT transposed because // the NAG Fortran library has arrays in column-major order double [,] xt = new double [4,12]; for (int i=0; i