/* A simplified version of normcdf that uses the NAG C library
 * Written to demonstrate how to compile MATLAB mex files that use the NAG C Library
 * Only returns a normcdf where mu=0 and sigma=1
 * October 2011 Michael Croucher
 * www.walkingrandomly.com
 */

#include <math.h>
#include "mex.h"
#include "nag.h"
#include "nags.h"
#include <omp.h>

void do_calculation(double in[],double out[],int num_elements)
{
    int i,tid;
    
    
#pragma omp parallel for shared(in,out,num_elements) private(i,tid)
    for(i=0; i<num_elements; i++){
          out[i] = nag_cumul_normal(in[i]);
         } 
}


void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    double *in,*out;
    int rows,cols,num_elements;
    
    if(nrhs>1)
    {
        mexErrMsgIdAndTxt("NAG_NORMCDF:BadArgs","This simplified version of normcdf only takes 1 input argument");
    } 
    
    /*Get pointers to input matrix*/
    in = mxGetPr(prhs[0]); 
    /*Get rows and columns of input matrix*/
    rows = mxGetM(prhs[0]); 
    cols = mxGetN(prhs[0]);
    num_elements = rows*cols;
    
    /* Create output matrix */
    plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL); 
    /* Assign pointer to the output */ 
    out = mxGetPr(plhs[0]);
    
    do_calculation(in,out,num_elements);
    
}

