using System; using System.Runtime.InteropServices; /* Provides mappings between C# and native code */ namespace NumericalAlgorithmsGroup { class Quadrature { static int kount; /* Delegate object, note that the scalar parameter is 'ref' */ public delegate double NAG_D01AJ_FUN (ref double output); [DllImport("FLDLL214Z_nag.dll")] /* Import from DLL, the C# compiler provides a rudimentry check of the signature */ public static extern void D01AJF(NAG_D01AJ_FUN fun, ref double a, ref double b, ref double epsabs, ref double epsrel, ref double result, ref double abserr, double [] w, ref int lw, int [] iw, ref int liw, ref int ifail); public static void Main() { double epsrel = 1.0e-4; double epsabs = 0.0; int lw = 2000; double [] w = new double[lw]; int liw = lw/4; int [] iw = new int [liw]; int ifail = 1; double abserr = 0.0; double result=0; /* Create a delegate object */ NAG_D01AJ_FUN FUNC = new NAG_D01AJ_FUN(func); double a = 0.0; double b = 2.0*Math.PI; D01AJF(FUNC, ref a, ref b, ref epsabs, ref epsrel, ref result, ref abserr, w, ref lw, iw, ref liw, ref ifail); if (ifail != 0) { // throw exception } else { Console.WriteLine("Approximation to the integral = {0, 9:f5}", result); Console.WriteLine("Estimate of the absolute error = {0, 9:e2}", abserr); Console.WriteLine("Number of function evaluations ={0}", kount); Console.WriteLine("Number of subintervals used = {0}", iw[0]); } } public static double func(ref double x) { ++kount; double pi = Math.PI; double retval; retval = (x*Math.Sin(x*30.0)/Math.Sqrt(1.0-x*x/(pi*pi*4.0))); return retval; } } }