Scaled complex complement of error function, exp(-z^{2})erfc(-iz) - nag_complex_erfc (s15ddc) routine |
#include <nag.h> #include <nags.h> Complex nag_complex_erfc(Complex z, NagError *fail);The function takes two arguments. The first one is of type Complex and is the argument z of the function. The second argument to s15ddc is the error handling argument fail which is of type NagError. See the NAG C Library Manual for more information about the NagError type. To keep things simple we are not going to try to pass the contents of the NagError structure back to Octave. Instead, we will return only integer value of fail.code.
Here is the source code of our C++ function nag_cmplx_erfc.cc:
#undef Complex #define Complex OctComplex #include <octave/oct.h> #undef Complex #define Complex NagComplex #define MatrixType NagMatrixType #include <nag.h> #include <nags.h> DEFUN_DLD (nag_cmplx_erfc, args, , "Calls nag_complex_erfc (s15ddc), which computes values \n\ of the function w(z)=e^(-z^2)erfc-iz, for complex z.\n") { // Variable to store function output values octave_value_list retval; // Retrieve input arguments from args OctComplex z = args(0).complex_value(); // Local variables NagError fail; INIT_FAIL(fail); OctComplex w; NagComplex z_nag, w_nag; // Copy the input OctComplex variable into NagComplex one z_nag.re = z.real(); z_nag.im = z.imag(); // Call NAG routine like in C w_nag = nag_complex_erfc(z_nag,&fail); // Copy the output NagComplex variable into OctComplex one w.real() = w_nag.re; w.imag() = w_nag.im; // Assign output arguments to retval retval(0) = w; retval(1) = fail.code; return retval; }
Points to note about this code:
To compile the C++ function into oct-files, we use the mkoctfile script supplied with Octave:
% mkoctfile nag_cmplx_erfc.cc -L/opt/NAG/cll6a08dg/lib -lnagc_nag -I/opt/NAG/cll6a08dg/includewhere:
octave:1> z=complex(-3.01,0.75) z = -3.01000 + 0.75000i octave:2> [w,ifail]=nag_cmplx_erfc(z) w = 0.052156 - 0.183776i ifail = 0
(If you get an error message saying that a library cannot be located, see the tip given in Example 1).