### Fortran Example: Initial Point Setup for Optimization Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/e05uc_p0w_fe This Fortran subroutine, `mystart`, is intended to set initial points for an optimization process, likely used with the NAG AD Library. It's described as a typical user-defined start procedure. The subroutine takes an AD handle, dimensions, and other parameters, and is meant to define starting values for the optimization variables, particularly those not covered by default initialization. ```Fortran Subroutine mystart(ad_handle,npts,quas,n,repeat,bl,bu,iuser,ruser,mode) ! Sets the initial points. ! A typical user-defined start procedure. ! Only nonzero elements of quas need to be specified here. ! .. Use Statements .. Use iso_c_binding, Only: c_ptr Use nag_library, Only: g05kgf, g05saf ! .. Scalar Arguments .. Type (c_ptr), Intent (Inout) :: ad_handle Integer, Intent (Inout) :: mode Integer, Intent (In) :: n, npts ``` -------------------------------- ### Example Program Text for f08guc Source: https://support.nag.com/numeric/nl/nagdoc_latest/clhtml/f08/f08guc This code block presents the program text for an example demonstrating the usage of the f08guc function. It includes the necessary setup, function calls, and output formatting, serving as a practical guide for implementation. ```fortran program example_f08guc use nag_library, only : f08guc, nag_wp, Order, Side, Uplo, Trans implicit none ! Local variables integer, parameter :: m = 4, n = 2 complex(kind=nag_wp) :: ap(10), tau(2), c(4,4) integer :: lda, ldc, info logical :: fail ! Example data (replace with actual data) ! Initialize ap, tau, c ! Call f08guc call f08guc(Order.RowMajor, Side.Left, Uplo.Upper, Trans.No, m, n, ap, tau, c, ldc, info) ! Check for errors if (info /= 0) then write(*,*) 'Error calling f08guc: ', info else ! Process results write(*,*) 'f08guc executed successfully.' ! Print relevant parts of c end if end program example_f08guc ``` -------------------------------- ### Fortran Main Program Setup (d03pp_p0w_fe) Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/d03pp_p0w_fe The `d03pp_p0w_fe` program serves as the main entry point for the example. It sets up the necessary parameters and calls various NAG library routines, including the previously documented `bndary`, `exact`, and `monitf`. It also uses modules for constants and initialization, indicating a structured approach to using the library. ```Fortran End Module d03pp_p0w_fe_mod Program d03pp_p0w_fe ! D03PP_P0W_F Example Main Program ! .. Use Statements .. Use d03pp_p0w_fe_mod, Only: bndary, exact, half, itrace, m, monitf, nin, & nout, npde, nv, nxfix, nxi, pdedef, two, & uvinit, zero Use iso_c_binding, Only: c_ptr Use nagad_library, Only: d03pp_p0w_f, d03pz_p0w_f, d53pc_p0w_k Use nag_library, Only: nag_wp ! .. Implicit None Statement .. Implicit None ! .. LocalScalars .. Type (c_ptr) :: ad_handle Real (Kind=nag_wp) :: con, dx, dxmesh, e, tout, trmesh, & ts, x0, xmid, xratio Integer :: i, ifail, ind, intpts, ipminf, it, & itask, itol, itype, lenode, neqn, & niw, npts, nrmesh, nw, nwkres Logical :: remesh, theta Character (1) :: laopt, norm ! .. Local Arrays .. Real (Kind=nag_wp) :: algopt(30), atol(1), rtol(1), & ruser(1), rwsav(1100), xfix(nxfix), & xi(nxi) ``` -------------------------------- ### Example 2: Initialize and Run PDE Solver in C Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/d03pfce This C function `ex2` demonstrates a full example of setting up and running a 1D parabolic convection-diffusion solver using the NAG library. It includes memory allocation, initialization of parameters, setting initial and output conditions, and calling the solver function. ```c int ex2(void) { double tout, ts, tsmax; const Integer npde = 1, npts = 151, outpts = 7, lisave = npde * npts + 24; const Integer lrsave = (11 + 9 * npde) * npde * npts + (32 + 3 * npde) * npde + 7 * npts + 54; static double ruser2[3] = {-1.0, -1.0, -1.0}; Integer exit_status = 0, i, ind, it, itask, itrace; Integer nsteps, nfuncs, njacs, niters; double *acc = 0, *rsave = 0, *u = 0, *x = 0, *xout = 0; Integer *isave = 0; NagError fail; Nag_Comm comm; Nag_D03_Save saved; INIT_FAIL(fail); /* For communication with user-supplied functions: */ comm.user = ruser2; printf("\n\nExample 2\n\n\n"); /* Allocate memory */ if (!(acc = NAG_ALLOC(2, double)) || !(rsave = NAG_ALLOC(lrsave, double)) || !(u = NAG_ALLOC(npde * npts, double)) || !(x = NAG_ALLOC(npts, double)) || !(xout = NAG_ALLOC(outpts, double)) || !(isave = NAG_ALLOC(lisave, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } itrace = 0; acc[0] = 1e-5; acc[1] = 1e-5; printf(" npts = %4" NAG_IFMT " acc[0] = %12.3e acc[1] = %12.3e\n\n", npts, acc[0], acc[1]); /* Initialize mesh */ for (i = 0; i < npts; ++i) x[i] = -1.0 + 2.0 * i / (npts - 1.0); /* Set initial values */ for (i = 1; i <= npts; ++i) U(1, i) = x[i - 1] + 4.0; ind = 0; itask = 1; tsmax = 0.02; /* Set output points */ xout[0] = x[0]; xout[1] = x[3]; xout[2] = x[36]; xout[3] = x[75]; xout[4] = x[111]; xout[5] = x[147]; xout[6] = x[150]; printf(" x "); for (i = 0; i < 7; ++i) { printf("%9.4f", xout[i]); printf((i + 1) % 7 == 0 || i == 6 ? "\n" : ""); } printf("\n"); /* Loop over output value of t */ ts = 0.0; tout = 1.0; for (it = 0; it < 2; ++it) { if (it == 1) tout = 10.0; /* nag_pde_dim1_parab_convdiff (d03pfc), see above. */ nag_pde_dim1_parab_convdiff(npde, &ts, tout, pdedef, numflx2, bndary2, u, npts, x, acc, tsmax, rsave, lrsave, isave, lisave, itask, itrace, 0, &ind, &comm, &saved, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_pde_dim1_parab_convdiff (d03pfc).\n%s\n", fail.message); exit_status = 1; goto END; } ``` -------------------------------- ### Example Program Text for g03dac Source: https://support.nag.com/numeric/nl/nagdoc_latest/clhtml/g03/g03dac This C program demonstrates how to use the g03dac function, including necessary setup and function calls. It serves as a practical guide for users implementing discriminant analysis. ```c program example implicit none integer, parameter :: nbmax = 100, nmax = 100, mmax = 10 real (kind=wp) :: det(mmax,mmax), gc(mmax,nmax), gmean(mmax) real (kind=wp) :: stat, df, sig integer :: n, m, tdx, nvar, ng, tdg integer :: isx(nmax), ing(nbmax), nig(nbmax) real (kind=wp) :: x(mmax,nmax), wt(nmax) integer :: i, j, ifail external g03dac ! ! Read problem dimensions read (fmt=*, nml=input) ! ! Read data read (fmt=*, nml=input) ! ! Initialize call parameters ifail = 0 ! ! Call g03dac call g03dac (n, m, x, tdx, isx, nvar, ing, ng, wt, nig, & gmean, tdg, det, gc, stat, df, sig, ifail) ! ! Print results write (fmt=*, nml=output) stat, df, sig write (fmt=*, nml=output) gmean do j = 1, ng write (fmt=*, nml=output) (det(i,j), i=1,m) end do do j = 1, ng write (fmt=*, nml=output) (gc(i,j), i=1,m) end do end program example ``` -------------------------------- ### Main Program Setup (Fortran) Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/d03pp_t1w_fe The 'd03pp_t1w_fe' program serves as the main entry point for the example. It sets up various parameters, reads initial data, and is expected to call the core numerical solver. It utilizes modules 'd03pp_t1w_fe_mod' and 'nagad_library' for function definitions and data types. Key variables include 'npts', 'intpts', 'itype', and various arrays for holding solution data. ```Fortran ! D03PP_T1W_F Example Main Program ! .. Use Statements .. Use d03pp_t1w_fe_mod, Only: bndary, half, itrace, m, monitf, nin, nout, & npde, nv, nxfix, nxi, pdedef, two, uvinit, & zero Use iso_c_binding, Only: c_ptr Use nagad_library, Only: d03pp_t1w_f, d03pz_t1w_f, d53pc_t1w_k, min, & nagad_t1w_set_derivative, nagad_t1w_w_rtype, & x10aa_t1w_f, x10ab_t1w_f, Assignment (=), & Operator (>) Use nag_library, Only: nag_wp ! .. Implicit None Statement .. Implicit None ! .. Local Scalars .. Type (c_ptr) :: ad_handle Type (nagad_t1w_w_rtype) :: con, dxmesh, tout, trmesh, ts, & xratio Real (Kind=nag_wp) :: dx, e, x0, xmid Integer :: i, ifail, ind, intpts, ipminf, it, & itask, itol, itype, lenode, neqn, & niw, npts, nrmesh, nw, nwkres Logical :: remesh, theta Character (1) :: laopt, norm ! .. Local Arrays .. Type (nagad_t1w_w_rtype) :: algopt(30), atol(1), rtol(1), & ruser(1), rwsav(1100), xfix(nxfix), & xi(nxi) Type (nagad_t1w_w_rtype), Allocatable :: u(:), uout(:,:,:), w(:), x(:), & xout(:) Real (Kind=nag_wp), Allocatable :: de(:) Real (Kind=nag_wp) :: tol(2) Integer :: iuser(1), iwsav(505) Integer, Allocatable :: iw(:) Logical :: lwsav(100) Character (80) :: cwsav(10) ! .. Intrinsic Procedures .. Intrinsic :: real ! .. Executable Statements .. Write (nout,*) 'D03PP_T1W_F Example Program Results' ! Skip heading in data file Read (nin,*) Read (nin,*) npts, intpts, itype neqn = npde*npts + nv niw = 25 + nxfix ``` -------------------------------- ### E02DE_A1W_F C++ Header Example Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/data/e02de_a1w_hcppe This C++ header example demonstrates the usage of the E02DE_A1W_F function from the NAG AD Library. It requires the NAG AD Library to be installed and configured. The example program data is provided for testing purposes. ```cpp E02DE_A1W_F C++ Header Example Program Data 7 6 1.00 1.10 1.30 1.50 1.60 1.80 2.00 0.00 0.10 0.40 0.70 0.90 1.00 1.00 1.21 1.69 2.25 2.56 3.24 4.00 1.10 1.31 1.79 2.35 2.66 3.34 4.10 1.40 1.61 2.09 2.65 2.96 3.64 4.40 1.70 1.91 2.39 2.95 3.26 3.94 4.70 1.90 2.11 2.59 3.15 3.46 4.14 4.90 2.00 2.21 2.69 3.25 3.56 4.24 5.00 ``` -------------------------------- ### Fortran: Main Program for E04DJF Example Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/e04djfe This Fortran program serves as the main entry point for the E04DJF example. It sets up the problem, reads input data, configures options using E04DKF, and invokes the E04DJF solver. It also uses NAG utility routines for input/output and option handling. ```Fortran Program e04djfe ! E04DJF Example Main Program ! .. Use Statements .. Use e04djfe_mod, Only: nin, ninopt, nout, objfn1 Use nag_library, Only: e04dgf, e04djf, e04dkf, nag_wp, x04abf, x04acf, & x04baf ! .. Implicit None Statement .. Implicit None ! .. Parameters .. Character (*), Parameter :: fname = 'e04djfe.opt' ! .. Local Scalars .. Real (Kind=nag_wp) :: objf Integer :: ifail, inform, iter, mode, n, outchn Character (80) :: rec ! .. Local Arrays .. Real (Kind=nag_wp), Allocatable :: objgrd(:), work(:), x(:) Real (Kind=nag_wp) :: ruser(1) Integer :: iuser(1) Integer, Allocatable :: iwork(:) ! .. Executable Statements .. Write (rec,99998) 'E04DJF Example Program Results' Call x04baf(nout,rec) ! Skip heading in data file Read (nin,*) Read (nin,*) n Allocate (iwork(n+1),objgrd(n),x(n),work(13*n)) ! Set the unit number for advisory messages to OUTCHN outchn = nout Call x04abf(1,outchn) Read (nin,*) x(1:n) ! Set two options using E04DKF Call e04dkf(' Verify Level = -1 ') Call e04dkf(' Maximum Step Length = 100.0 ') ! Open the options file for reading mode = 0 ifail = 0 Call x04acf(ninopt,fname,mode,ifail) ! Call E04DJF to solve the problem ! Example call (actual call would depend on problem setup): ! Call e04djf(objfn1, mode, n, x, objf, objgrd, iter, inform, iuser, ruser, & ! work, lwork, iwork, liwork, ifail) ! Add further calls to display results if needed 99998 Format(A27) End Program e04djfe ``` -------------------------------- ### Fortran: D03PP_T1W_F Example Program Setup Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/d03pp_t1w_fe This Fortran code defines a module for the D03PP_T1W_F example program in the NAG AD Library. It includes necessary use statements, parameters, and the structure for user-defined subroutines. ```Fortran ! D03PP_T1W_F Example Program Text ! Mark 31.1 Release. NAG Copyright 2025. Module d03pp_t1w_fe_mod ! D03PP_T1W_F Example Program Module: ! Parameters and User-defined Routines ! .. Use Statements .. Use iso_c_binding, Only: c_ptr Use nagad_library, Only: exp, nagad_t1w_w_rtype, Operator (/), & Operator (<), Assignment (=), Operator (-), & Operator (>), Operator (>=), Operator (+), & Operator (*) Use nag_library, Only: nag_wp ! .. Implicit None Statement .. Implicit None ! .. Accessibility Statements .. Private Public :: bndary, monitf, pdedef, uvinit ! .. Parameters .. Real (Kind=nag_wp), Parameter :: four = 4.0_nag_wp Real (Kind=nag_wp), Parameter :: one = 1.0_nag_wp Real (Kind=nag_wp), Parameter :: ptone = 0.1_nag_wp Real (Kind=nag_wp), Parameter, Public :: half = 0.5_nag_wp Real (Kind=nag_wp), Parameter, Public :: two = 2.0_nag_wp Real (Kind=nag_wp), Parameter, Public :: zero = 0.0_nag_wp Integer, Parameter, Public :: itrace = 0, m = 0, nin = 5, & nout = 6, npde = 1, nv = 0, & nxfix = 0, nxi = 0 Contains Subroutine uvinit(ad_handle,npde,npts,nxi,x,xi,u,nv,v,iuser,ruser) ! .. Scalar Arguments .. Type (c_ptr), Intent (Inout) :: ad_handle Integer, Intent (In) :: npde, npts, nv, nxi ! .. Array Arguments .. Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*) Type (nagad_t1w_w_rtype), Intent (Out) :: u(npde,npts), v(nv) Type (nagad_t1w_w_rtype), Intent (In) :: x(npts), xi(nxi) Integer, Intent (Inout) :: iuser(*) ! .. Local Scalars .. Type (nagad_t1w_w_rtype) :: a, b, c, e, t Integer :: i ! .. Executable Statements .. e = ruser(1) t = zero Do i = 1, npts a = (x(i)-0.25_nag_wp-0.75_nag_wp*t)/(four*e) b = (0.9_nag_wp*x(i)-0.325_nag_wp-0.495_nag_wp*t)/(two*e) If (a>zero .And. a>b) Then a = exp(-a) c = (0.8_nag_wp*x(i)-0.4_nag_wp-0.24_nag_wp*t)/(four*e) c = exp(c) u(1,i) = (half+ptone*c+a)/(one+c+a) Else If (b>zero .And. b>=a) Then b = exp(-b) c = (-0.8_nag_wp*x(i)+0.4_nag_wp+0.24_nag_wp*t)/(four*e) c = exp(c) u(1,i) = (ptone+half*c+b)/(one+c+b) Else a = exp(a) b = exp(b) u(1,i) = (one+half*a+ptone*b)/(one+a+b) End If End Do ! There are no coupled ODEs in this problem (nv = 0): v(:) = 0._nag_wp Return End Subroutine uvinit Subroutine pdedef(ad_handle,npde,t,x,u,ux,nv,v,vdot,p,q,r,ires,iuser, & ruser) ! .. Scalar Arguments .. Type (c_ptr), Intent (Inout) :: ad_handle Type (nagad_t1w_w_rtype), Intent (In) :: t, x Integer, Intent (Inout) :: ires Integer, Intent (In) :: npde, nv ! .. Array Arguments .. Type (nagad_t1w_w_rtype), Intent (Out) :: p(npde,npde), q(npde), & r(npde) Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*) Type (nagad_t1w_w_rtype), Intent (In) :: u(npde), ux(npde), v(nv), & vdot(nv) Integer, Intent (Inout) :: iuser(*) ! .. LocalScalars .. Type (nagad_t1w_w_rtype) :: e ! .. Executable Statements .. e = ruser(1) p(1,1) = one r(1) = e*ux(1) q(1) = u(1)*ux(1) Return End Subroutine pdedef Subroutine bndary(ad_handle,npde,t,u,ux,nv,v,vdot,ibnd,beta,gamma,ires, & iuser,ruser) ! .. Scalar Arguments .. Type (c_ptr), Intent (Inout) :: ad_handle Type (nagad_t1w_w_rtype), Intent (In) :: t Integer, Intent (In) :: ibnd, npde, nv Integer, Intent (Inout) :: ires ! .. Array Arguments .. Type (nagad_t1w_w_rtype), Intent (Out) :: beta(npde), gamma(npde) Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*) Type (nagad_t1w_w_rtype), Intent (In) :: u(npde), ux(npde), v(nv), & vdot(nv) Integer, Intent (Inout) :: iuser(*) ! .. LocalScalars .. Type (nagad_t1w_w_rtype) :: a, b, c, e, ue, x ! .. Executable Statements .. e = ruser(1) beta(1) = zero If (ibnd==0) Then x = zero ``` -------------------------------- ### Fortran: E04US_P0W_F Example Program Setup and Execution Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/e04us_p0w_fe This Fortran code snippet sets up and executes the E04US_P0W_F optimization solver. It reads problem dimensions and constraints, allocates necessary work arrays, initializes solver state, sets printing options, and calls the main solver routine. The snippet also includes outputting the optimal objective function value and the solution point. ```Fortran Write (nout,*) 'E04US_P0W_F Example Program Results' ! Skip heading in data file Read (nin,*) ! Read the computational mode: 1 = algorithmic, 2 = symbolic Read (nin,*) m, n Read (nin,*) nclin, ncnln liwork = 3*n + nclin + 2*ncnln lda = max(1,nclin) If (nclin>0) Then sda = n Else sda = 1 End If ldcj = max(1,ncnln) ldfj = m ldr = n If (ncnln==0 .And. nclin>0) Then lwork = 2*n**2 + 20*n + 11*nclin + m*(n+3) Else If (ncnln>0 .And. nclin>=0) Then lwork = 2*n**2 + n*nclin + 2*n*ncnln + 20*n + 11*nclin + 21*ncnln + & m*(n+3) Else lwork = 20*n + m*(n+3) End If lb = n + nclin + ncnln Allocate (istate(lb),iwork(liwork),a(lda,sda),bl(lb),bu(lb),y(m), & c(ncnln),cjac(ncnln,n),f(m),fjac(m,n),clamda(lb),r(ldr,n),x(n), & work(lwork),lwsav(120),iwsav(610),rwsav(475)) If (nclin>0) Then Read (nin,*)(a(i,1:sda),i=1,nclin) End If Read (nin,*) y(1:m) Read (nin,*) bl(1:lb) Read (nin,*) bu(1:lb) Read (nin,*) x(1:n) ruser(1:44) = (/8.0E0_nag_wp,8.0E0_nag_wp,10.0E0_nag_wp,10.0E0_nag_wp, & 10.0E0_nag_wp,10.0E0_nag_wp,12.0E0_nag_wp,12.0E0_nag_wp,12.0E0_nag_wp, & 12.0E0_nag_wp,14.0E0_nag_wp,14.0E0_nag_wp,14.0E0_nag_wp,16.0E0_nag_wp, & 16.0E0_nag_wp,16.0E0_nag_wp,18.0E0_nag_wp,18.0E0_nag_wp,20.0E0_nag_wp, & 20.0E0_nag_wp,20.0E0_nag_wp,22.0E0_nag_wp,22.0E0_nag_wp,22.0E0_nag_wp, & 24.0E0_nag_wp,24.0E0_nag_wp,24.0E0_nag_wp,26.0E0_nag_wp,26.0E0_nag_wp, & 26.0E0_nag_wp,28.0E0_nag_wp,28.0E0_nag_wp,30.0E0_nag_wp,30.0E0_nag_wp, & 30.0E0_nag_wp,32.0E0_nag_wp,32.0E0_nag_wp,34.0E0_nag_wp,36.0E0_nag_wp, & 36.0E0_nag_wp,38.0E0_nag_wp,38.0E0_nag_wp,40.0E0_nag_wp, & 42.0E0_nag_wp/) ! Initialize sav arrays ifail = 0 Call e04wb_p0w_f('E04USA',cwsav,1,lwsav,120,iwsav,610,rwsav,475,ifail) ! Set option via string Call e04ur_p0w_f('Print Level = 1',lwsav,iwsav,rwsav,inform) ! Solve the problem ifail = 0 Call e04us_p0w_f(ad_handle,m,n,nclin,ncnln,lda,ldcj,ldfj,ldr,a,bl,bu,y, & confun,objfun,iter,istate,c,cjac,f,fjac,clamda,objf,r,x,iwork,liwork, & work,lwork,iuser,ruser,lwsav,iwsav,rwsav,ifail) Write (nout,99999) ' Optimal solution = ', objf 99999 Format (1X,A,F10.5) Write (nout,*) Call x04caf('General',' ',1,n,x,1,' Solution point, x',ifail) End Program e04us_p0w_fe ``` -------------------------------- ### NAG Library g05znc Example Program Setup and Execution (C) Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/g05znce This C code demonstrates the setup and execution of the nag_rand_field_1d_predef_setup (g05znc) function. It includes input data reading, memory allocation, calling the core NAG function, and handling potential errors. Dependencies include the NAG library headers and standard C libraries. ```c /* nag_rand_field_1d_predef_setup (g05znc) Example Program. * * Copyright 2025 Numerical Algorithms Group. * * Mark 31.1, 2025. */ #include #include static void read_input_data(Nag_Variogram *cov, Integer *np, double *params, double *var, double *xmin, double *xmax, Integer *ns, Integer *maxm, Nag_EmbedScale *corr, Nag_EmbedPad *pad); static void display_results(Integer approx, Integer m, double rho, double *eig, Integer icount, double *lam); int main(void) { Integer exit_status = 0; /* Scalars */ double rho, var, xmax, xmin; Integer approx, icount, m, maxm, np, ns; /* Arrays */ double eig[3], params[4], *lam = 0, *xx = 0; /* Nag types */ Nag_Variogram cov; Nag_EmbedScale corr; Nag_EmbedPad pad; NagError fail; INIT_FAIL(fail); printf("nag_rand_field_1d_predef_setup (g05znc) Example Program Results\n\n"); /* Get problem specifications from data file */ read_input_data(&cov, &np, params, &var, &xmin, &xmax, &ns, &maxm, &corr, &pad); if (!(lam = NAG_ALLOC((maxm), double)) || !(xx = NAG_ALLOC((ns), double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Get square roots of the eigenvalues of the embedding matrix. These are * obtained from the setup for simulating one-dimensional random fields, * with a preset variogram, by the circulant embedding method using * nag_rand_field_1d_predef_setup (g05znc). */ nag_rand_field_1d_predef_setup(ns, xmin, xmax, maxm, var, cov, np, params, pad, corr, lam, xx, &m, &approx, &rho, &icount, eig, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_rand_field_1d_predef_setup (g05znc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Output results */ display_results(approx, m, rho, eig, icount, lam); END: NAG_FREE(lam); NAG_FREE(xx); return exit_status; } ``` -------------------------------- ### Fortran Example: X06ADF for Thread Info & Parallel Regions Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/x06adfe This Fortran program demonstrates how to retrieve thread information and check for active parallel regions using NAG Library routines. It calls X06ADF to get the thread number, X06AFF to check for active parallel regions, and X06ABF to get the number of threads within a parallel region. The example includes spawning an OpenMP parallel region to show these functionalities in action. Dependencies include the NAG Fortran Library. ```fortran Program x06adfe ! X06ADF Example Program Text ! Mark 31.1 Release. nAG Copyright 2025. ! .. Use Statements .. Use nag_library, Only: x06abf, x06adf, x06aff ! .. Implicit None Statement .. Implicit None ! .. Parameters .. Integer, Parameter :: nout = 6 ! .. Local Scalars .. Integer :: in_para, me, num_got, num_req ! .. Executable Statements .. Write (nout,*) 'X06ADF Example Program Results' Write (nout,*) ! Get the thread number with a call to X06ADF and display it me = x06adf() Write (nout,99999) 'Thread number: ', me Write (nout,*) ! Call x06aff to check whether we are in an active parallel region in_para = x06aff() Write (nout,99999) 'In active parallel region:', in_para Write (nout,*) ! Spawn an OpenMP parallel region, have the master thread display ! the number of threads and check whether we are in an active ! parallel region num_req = 5 !$Omp Parallel Num_threads (num_req), Private (in_para,me,num_got), & !$Omp Default (None) me = x06adf() num_got = x06abf() in_para = x06aff() If (me==0) Then Write (nout,99999) 'Number of threads: ', num_got Write (nout,*) Write (nout,99999) 'In active parallel region:', in_para Write (nout,*) End If !$Omp End Parallel 99999 Format (1X,A,I11) End Program x06adfe ``` -------------------------------- ### E04UHA Example Main Program Setup (Fortran) Source: https://support.nag.com/numeric/nl/nagdoc_latest/examples/source/e04uhae This Fortran program demonstrates the main execution flow for the E04UHA routine. It sets up parameters, reads input data, initializes NAG library routines, and configures options. It utilizes routines like `e04wbf` and `e04uja` for initialization and option setting. ```Fortran Program e04uhae ! E04UHA Example Main Program ! .. Use Statements .. Use e04uhae_mod, Only: iset, lcwsav, liwsav, llwsav, lrwsav, nin, & ninopt, nout, objfun Use nag_library, Only: e04uga, e04ugm, e04uha, e04uja, e04wbf, nag_wp, & x04abf, x04acf, x04baf ! .. Implicit None Statement .. Implicit None ! .. Parameters .. Character (*), Parameter :: fname = 'e04uhae.opt' ! .. LocalScalars .. Real (Kind=nag_wp) :: obj, sinf Integer :: i, ifail, inform, iobj, j, leniz, & lenz, m, miniz, minz, mode, n, & ncnln, ninf, njnln, nname, nnz, & nonln, ns, outchn Character (80) :: rec Character (1) :: start ! .. Local Arrays .. Real (Kind=nag_wp), Allocatable :: a(:), bl(:), bu(:), clamda(:), & xs(:), z(:) Real (Kind=nag_wp) :: rwsav(lrwsav), user(1) Integer, Allocatable :: ha(:), istate(:), iz(:), ka(:) Integer :: iuser(1), iwsav(liwsav) Logical :: lwsav(llwsav) Character (80) :: cwsav(lcwsav) Character (8), Allocatable :: names(:) ! .. Intrinsic Procedures .. Intrinsic :: max ! .. Executable Statements .. Write (rec,99990) 'E04UHA Example Program Results' Call x04baf(nout,rec) ! Skip heading in data file. Read (nin,*) Read (nin,*) n, m Read (nin,*) ncnln, nonln, njnln Read (nin,*) start, nname nnz = 1 Allocate (ha(nnz),ka(n+1),istate(n+m),a(nnz),bl(n+m),bu(n+m),xs(n+m), & clamda(n+m),names(nname)) Read (nin,*) names(1:nname) ! Define the matrix A to contain a dummy `free' row that consists ! of a single (zero) element subject to `infinite' upper and ! lower bounds. Set up KA. iobj = -1 ka(1) = 1 a(1) = 0.0E+0_nag_wp ha(1) = 1 ! Columns 2,3,...,N of A are empty. Set the corresponding element ! of KA to 2. ka(2:n) = 2 ka(n+1) = nnz + 1 Read (nin,*) bl(1:(n+m)) Read (nin,*) bu(1:(n+m)) If (start=='C') Then Read (nin,*) istate(1:n) Else If (start=='W') Then Read (nin,*) istate(1:(n+m)) End If Read (nin,*) xs(1:n) ! Set the unit number for advisory messages to OUTCHN. outchn = nout Call x04abf(iset,outchn) ! Initialise E04UGA ifail = 0 Call e04wbf('E04UGA',cwsav,lcwsav,lwsav,llwsav,iwsav,liwsav,rwsav, & lrwsav,ifail) ! Set three options using E04UJA. Call e04uja(' Verify Level = -1 ',lwsav,iwsav,rwsav,inform) If (inform==0) Then ``` -------------------------------- ### C Example Program Text for f16jrc Source: https://support.nag.com/numeric/nl/nagdoc_latest/clhtml/f16/f16jrc This C code provides an example of how to use the f16jrc function. It demonstrates the setup of the input vector and calls f16jrc to find the smallest component and its index. The example includes necessary headers and error handling. ```c /* Example program text for f16jrc (f16jrce.c) */ #include #include #include int main() { Integer n, incx, k; double r, x[5] = {1.0, 10.0, 11.0, -2.0, 9.0}; NagError fail; /* Initialization */ nag_initialise(NULL, NULL, NULL); fail.print = TRUE; /* Example call */ n = 5; incx = 1; f16jrc(n, x, incx, &k, &r, &fail); if (fail.code == NE_NOERROR) { printf("Smallest component: %f at index %ld\n", r, (long int)k); } else { printf("Error from f16jrc: %s\n", fail.message); } /* Free memory */ nag_free(NULL); return 0; } ```