// This example of calling the NAG Library for .NET by David carlisle, // based on an example form application from the F# sample distribution // carrying the notice: // Copyright (c) Microsoft Corporation 2005-2006. // This sample code is provided "as is" without warranty of any kind. // We disclaim all warranties, either express or implied, including the // warranties of merchantability and fitness for a particular purpose. # light #if INTERACTIVE #I @"c:\Program Files\NAG\DTW02" #r "NagLibrary32.dll" #endif open System open System.Windows.Forms open NagLibrary // first form example // declaration of mye04cb let mye04cb n x tolf tolx func monit maxcal = let ffunc = new E04.E04CB_FUNCT(fun n xc (res:byref) -> res <- func xc) let fmonit = new E04.E04CB_MONIT(monit) let ifl = ref 0 let rf = ref 0.0 E04.e04cb(n,x,rf,tolx,tolf,ffunc,fmonit,maxcal,ifl) (x,!rf) // object function for e04cb example let mutable e04cbf = fun (xc: float[]) -> Math.Exp(xc.[0]) * (4.00e0 * xc.[0] * (xc.[0] + xc.[1]) + 2.00e0 * xc.[1] * (xc.[1] + 1.00e0) + 1.00e0); (* // Update e04cb object function e04cbf <- fun (xc: float[]) -> 1.0;; *) // e04cb example bounds let mutable e04cbx = [|-1.0;1.0|] // second form example // declaration of mye04xa let mye04xa mlevel n epsrf x mode objfun hforw = let objf = ref 0.0 let objgrd :float array = Array.zeroCreate n let hcntrl :float array = Array.zeroCreate n let h :float [,] = Array2D.zeroCreate n n let info : int array = Array.zeroCreate n let mref = ref mode let objfref = ref 0.0 let warnref = ref 0 let ifailref = ref 0 let objfund = E04.E04XA_OBJFUN(fun (a:byref) b c (d:byref) e f -> d <- objfun a b c d e f ) E04.e04xa (mlevel,n,epsrf,x,mref,objfund,hforw,objfref,objgrd,hcntrl,h,warnref,info,ifailref) (!objfref,objgrd,hcntrl,h,!warnref) let e04xaobjfun int n (x: float array) objf objgrd nstate = let a = x.[0] + 10.0*x.[1] let b = x.[2] - x.[3] let c = x.[1] - 2.0*x.[2] let d = x.[0] - x.[3] a*a + 5.0*b*b + c*c*c*c + 10.0*d*d*d*d // form setup let form = new Form() form.Width <- 900 form.Height <- 300 form.Visible <- true form.Text <- "NAG Example Form" // Menu bar, menus let mMain = form.Menu <- new MainMenu() let mFile = form.Menu.MenuItems.Add("Choose &Example") let miRun1 = new MenuItem("Run example &1 : e04cb") let _ = mFile.MenuItems.Add(miRun1) let miRun2 = new MenuItem("Run example &2 : e04xa") let _ = mFile.MenuItems.Add(miRun2) let miRun3 = new MenuItem("Run example &3 : e04xa (error)") let _ = mFile.MenuItems.Add(miRun3) let miQuit = new MenuItem("&Quit") let _ = mFile.MenuItems.Add(miQuit) // RichTextView let textB = new RichTextBox() textB.Dock <- DockStyle.Fill textB.Font <- new System.Drawing.Font("Courier New", 7.0F) form.Controls.Add(textB) // RichTextView let textB2 = new RichTextBox() textB2.Dock <- DockStyle.Left form.Controls.Add(textB2) // e04cb monit function let e04cbmonitor fmin fmax sim n ncall se vr = textB.Text <- ( textB.Text + (sprintf " There have been %d function calls\n" ncall)) // run the e04cb example let runexmpl1 () = textB.BackColor<- Drawing.Color.White textB.Text <- "monitor\n" textB2.Text <- sprintf "The final function value is ...\n" let (interv,vl) = mye04cb 2 e04cbx (Math.Sqrt(X02.x02aj())) (Math.Sqrt(Math.Sqrt(X02.x02aj()))) e04cbf e04cbmonitor 100 textB2.Text <- ( textB2.Text + (sprintf " %f \n" vl)) // end first eample //second example // run the e04xa example // make mode a parameter just so we can force an error to occur // in the 3rd example on the menu. // 10 is not a valid input value for mode. let runexmpl2 (mode) = textB.BackColor<- Drawing.Color.White textB.Text <- "monitor\n" textB2.Text <- sprintf "The final function value is ...\n" // Beta 2 code needed to redirect Console.Out // Now we can use PrintManager properties. // let oldOut = Console.Out // let strWriter = new IO.StringWriter() // Console.SetOut(strWriter); PrintManager.Message <- (fun s -> textB.Text <- textB.Text + s + "\n") PrintManager.Warning <- (fun s -> textB.BackColor<- Drawing.Color.Red textB.Text <- textB.Text + "\nError:\n" + s + "\n") let (objf,z1,z2,z3,z4)= mye04xa (*mlevel*) 10 (*n*) 4 (*epsrf*) 0.0 (*x*) [|3.0;-1.0;0.0;1.0|] (*mode*) mode (*objfun*) e04xaobjfun (*hforw*) [|-1.0;-1.0;-1.0;-1.0|] textB2.Text <- ( textB2.Text + (sprintf " %f \n" objf)) // Console.SetOut(oldOut) // textB.Text <- strWriter.ToString() // textB2.Text <- ( textB2.Text + (sprintf " %f \n" objf)) // callbacks miQuit.Click.Add(fun _ -> form.Close()) miRun1.Click.Add(fun _ -> runexmpl1 ()) miRun2.Click.Add(fun _ -> runexmpl2 (0)) miRun3.Click.Add(fun _ -> runexmpl2 (10)) #if COMPILED // Run the main code. The attribute marks the startup application thread as "Single // Thread Apartment" mode, which is necessary for GUI applications. [] do Application.Run(form) #endif