using System; using System.Runtime.InteropServices; /* Provides mappings between C# and native code */ namespace NumericalAlgorithmsGroup { class MultipleLinearRegression { // Declaration of the routine G02EAF in the NAG DLL [DllImport("FLDLL214Z_nag.DLL")]/* Import from DLL, the C# compiler provides a rudimentry check of the signature */ public static extern void G02EAF(string mean, int length_mean, string weight, int length_weight, ref int n, ref int m, double[,] x, ref int ldx, byte[,] name, int length_name, int[] isx, double[] y, double[] wt, ref int nmod, byte[,,] model, int length_model, ref int ldm, double[] rss, int[] nterms, int[] mrank, double[] wk, ref int ifail); // Convenience routine copies a string into a byte array private static void setName(byte[,] name, int i, string sname) { for (int j = 0; j < sname.Length; j++) name[i,j] = (byte)sname[j]; } public static void Main() { // We solve the example problem presented in the documentation of // routine G02EAF in the NAG Fortran Library Manual. int n = 20; int m = 6; string mean = "M"; // Mean(M) or Zero(Z) string weight = "U"; // Unweighted(U) or Weighted(W) double[] wt = new double[n]; for (int i = 0; i < n; i++) wt[i] = 1; int[] isx = new int[n]; for (int i = 0; i < n; i++) isx[i] = 1; isx[0] = 0; // Notice that the 2D input array X is stored in transposed form // because the NAG Fortran Library expects 2D arrays to be stored // by column rather than by row. double[,] x = new double[,] { {0.0, 1125.0, 232.0, 7160.0, 85.9, 8905.0}, {7.0, 920.0, 268.0, 8804.0, 86.5, 7388.0}, {15.0, 835.0, 271.0, 8108.0, 85.2, 5348.0}, {22.0, 1000.0, 237.0, 6370.0, 83.8, 8056.0}, {29.0, 1150.0, 192.0, 6441.0, 82.1, 6960.0}, {37.0, 990.0, 202.0, 5154.0, 79.2, 5690.0}, {44.0, 840.0, 184.0, 5896.0, 81.2, 6932.0}, {58.0, 650.0, 200.0, 5336.0, 80.6, 5400.0}, {65.0, 640.0, 180.0, 5041.0, 78.4, 3177.0}, {72.0, 583.0, 165.0, 5012.0, 79.3, 4461.0}, {80.0, 570.0, 151.0, 4825.0, 78.7, 3901.0}, {86.0, 570.0, 171.0, 4391.0, 78.0, 5002.0}, {93.0, 510.0, 243.0, 4320.0, 72.3, 4665.0}, {100.0, 555.0, 147.0, 3709.0, 74.9, 4642.0}, {107.0, 460.0, 286.0, 3969.0, 74.4, 4840.0}, {122.0, 275.0, 198.0, 3558.0, 72.5, 4479.0}, {129.0, 510.0, 196.0, 4361.0, 57.7, 4200.0}, {151.0, 165.0, 210.0, 3301.0, 71.8, 3410.0}, {171.0, 244.0, 327.0, 2964.0, 72.5, 3360.0}, {220.0, 79.0, 334.0, 2777.0, 71.9, 2599.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[6,20]; for (int i=0; i