using System; using System.Runtime.InteropServices; using System.Text; using NagLibrary; public class NagE01Functions { public static void Main() { nag_declarations.Nag_Spline spline = new nag_declarations.Nag_Spline(); int m = 7; double[] x = { 0.0, 0.2, 0.4, 0.6, 0.75, 0.9, 1.0 }; double[] y = new double[m]; nag_declarations.NagError fail = new nag_declarations.NagError(); fail.message = new byte[512]; Encoding enc = Encoding.ASCII; for (int i = 0; i < x.Length; i++) { y[i] = Math.Exp(x[i]); } /* nag_1d_spline_interpolant (e01bac). * Interpolating function, cubic spline interpolant, one * variable */ nag_declarations.nag_1d_spline_interpolant(m, x, y, ref spline, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_1d_spline_interpolant (e01bac):"); Console.WriteLine(enc.GetString(fail.message)); goto End; } double xarg; double fit = 0.0; double[] lamda = new double[spline.n]; double[] c = new double[spline.n - 4]; // Copy data from spline struct's IntPtrs to local arrays Marshal.Copy(spline.lamda, lamda, 0, spline.n); Marshal.Copy(spline.c, c, 0, spline.n - 4); Console.WriteLine("\nNumber of distinct knots = {0}\n", m - 2); Console.WriteLine("Distinct knots located at \n"); for (int j = 3; j < m + 1; j++) if ((j - 3) % 5 == 4 || j == m) Console.WriteLine("{0, 8:f4}", lamda[j]); else Console.Write("{0, 8:f4} ", lamda[j]); Console.WriteLine("\n\n J B-spline coeff c\n"); for (int j = 0; j < m; ++j) Console.WriteLine(" {0} {1,13:f4}", j + 1, c[j]); Console.WriteLine("\n J Abscissa Ordinate Spline\n"); for (int j = 0; j < m; ++j) { /* nag_1d_spline_evaluate (e02bbc). * Evaluation of fitted cubic spline, function only */ nag_declarations.nag_1d_spline_evaluate(x[j], ref fit, ref spline, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_1d_spline_evaluate (e02bbc):"); Console.WriteLine(enc.GetString(fail.message)); goto End; } Console.WriteLine(" {0} {1,13:f4} {2,13:f4} {3,13:f4}", j + 1, x[j], y[j], fit); if (j < m - 1) { xarg = (x[j] + x[j + 1]) * 0.5; // half way between these elements /* nag_1d_spline_evaluate (e02bbc), see above. */ nag_declarations.nag_1d_spline_evaluate(xarg, ref fit, ref spline, ref fail); if (fail.code != 0) { Console.WriteLine("Error from nag_1d_spline_evaluate (e02bbc):"); Console.WriteLine(enc.GetString(fail.message)); goto End; } Console.WriteLine(" {0,13:f4} {1,13:f4}", xarg, fit); } } End: // Free memory allocated by nag_1d_spline_interpolant (e01bac) if (spline.lamda != IntPtr.Zero) nag_declarations.NAG_FREE(ref spline.lamda); if (spline.c != IntPtr.Zero) nag_declarations.NAG_FREE(ref spline.c); } }