namespace NAG { using System; using System.Runtime.InteropServices;/* Provides mappings between C# and native code */ class E04vh { public delegate void NAG_E04VH_USRFUN (ref int status, ref int n, IntPtr x_ptr, ref int needf, ref int nf, IntPtr f_ptr, ref int needg, ref int leng, IntPtr g_ptr, IntPtr cuser_ptr, int cuser_element_len, IntPtr iuser_ptr, IntPtr ruser_ptr); [DllImport("FLDLL214Z_nag.dll")] public static extern void E04VHF(ref int start, ref int nf, ref int n, ref int nxname, ref int nfname, ref double objadd, ref int objrow, string prob, int prob_len, NAG_E04VH_USRFUN usrfun, int [] iafun, int [] javar, double [] a, ref int lena, ref int nea, int [] igfun, int [] jgvar, ref int leng, ref int neg, double [] xlow, double [] xupp, byte [,]xnames, int xnames_element_len, double [] flow, double [] fupp, byte [,]fnames, int fnames_element_len, double [] x, int [] xstate, double [] xmul, double [] f, int [] fstate, double [] fmul, ref int ns, ref int ninf, ref int sinf, byte [,] cw, int cw_element_len, ref int lencw, int [] iw, ref int leniw, double [] rw, ref int lenrw, byte [,] cuser, int cuser_element_len, int [] iuser, double [] ruser, ref int ifail); [DllImport("FLDLL214Z_nag.dll")] public static extern void E04VGF(byte [,] cw, int cw_element_len, ref int lencw, int [] iw, ref int leniw, double [] rw, ref int lenrw, ref int ifail); [DllImport("FLDLL214Z_nag.dll")] public static extern void E04VMF(string optionstring, int len_optionstring, ref int ivalue, byte [,] cw, int cw_element_len, int [] iw, double [] rw, 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() { // Create delegate object NAG_E04VH_USRFUN userfun = new NAG_E04VH_USRFUN(usrfun); int n = 4; int nf = 6; int nea = 8; int neg = 8; int lena = nea; int leng = neg; int objrow = 6; int start = 0; int lencw = 600; int leniw = 600; int lenrw = 600; int char_array_elem_len = 8; byte [,] cuser=new byte[1, char_array_elem_len]; int [] iuser = new int[1]; double [] ruser = new double[1]; byte [,] cw = new byte[lencw, char_array_elem_len]; int [] iw = new int[leniw]; double [] rw = new double[lenrw]; int nxname = n; string [] xnames = {"X1", "X2","X3","X4"}; byte [,] xnames_byte = new byte[nxname, char_array_elem_len]; for (int i=0; i 0) { // The nonlinear components of f_i(x) need to be assigned, // for i = 1 to NF f[0] = 1000.00e+0 * Math.Sin( -x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin( -x[1] - 0.250e+0); f[1] = 1000.00e+0 * Math.Sin(x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin(x[0] - x[1] - 0.250e+0); f[2] = 1000.00e+0 * Math.Sin(x[1] - x[0] - 0.250e+0) + 1000.00e+0 * Math.Sin(x[1] - 0.250e+0); // N.B. in this example there is no need to assign for the wholly // linear components f_4(x) and f_5(x). f[5] = 1.00e-6 * Math.Pow(x[2] ,3) + 2.00e-6 * Math.Pow(x[3] ,3) / 3.00e+0; } // if (needg > 0) { // The derivatives of the function f_i(x) need to be assigned. // G(k) should be set to partial derivative df_i(x)/dx_j where // i = IGFUN(k) and j = IGVAR(k), for k = 1 to LENG. g[0] = -1000.00e+0 * Math.Cos( -x[0] - 0.250e+0); g[1] = -1000.00e+0 * Math.Cos( -x[1] - 0.250e+0); g[2] = 1000.00e+0 * Math.Cos(x[0] - 0.250e+0) + 1000.00e+0 * Math.Cos(x[0] - x[1] - 0.250e+0); g[3] = -1000.00e+0 * Math.Cos(x[0] - x[1] - 0.250e+0); g[4] = -1000.00e+0 * Math.Cos(x[1] - x[0] - 0.250e+0); g[5] = 1000.00e+0 * Math.Cos(x[1] - x[0] - 0.250e+0) + 1000.00e+0 * Math.Cos(x[1] - 0.250e+0); g[6] = 3.00e-6 * (x[2])*(x[2]); g[7] = 2.00e-6 * (x[3])*(x[3]); } Marshal.Copy(g, 0, g_ptr, leng); Marshal.Copy(f, 0, f_ptr, nf); } // End of usrfun } }