Introduction

Passing Data to User-Supplied Functions

In some cases it may be advantageous to pass problem-specific data to a user-supplied function through the IMSL C Stat Library interface.  This ability can be useful if a user-supplied function requires data that is local to the user's calling function, and the user wants to avoid using global data to allow the user-supplied function to access the data.  Functions in IMSL C Stat Library that accept user-supplied functions have an optional argument(s) that will accept an alternative user-supplied function, along with a pointer to the data, that allows user-specified data to be passed to the function.  The example below demonstrates this feature using the IMSL C Stat Library function imsls_f_kolmogorov_one and optional argument IMSLS_FCN_W_DATA.

#include <imsls.h>

#include <stdio.h>

 

float cdf_w_data(float, void *data);

 

int main()

{

   float *statistics=NULL, *diffs = NULL, *x=NULL;

   int nobs = 100, nmiss;

   float usr_data[] = {0.5, .2886751};

 

   imsls_random_seed_set(123457);

   x = imsls_f_random_uniform(nobs, 0);

 

   statistics = imsls_f_kolmogorov_one(NULL, nobs, x,

      IMSLS_N_MISSING, &nmiss,

      IMSLS_DIFFERENCES, &diffs,

      IMSLS_FCN_W_DATA, cdf_w_data, usr_data,

      0);

   printf("D = %8.4f\n", diffs[0]);

   printf("D+ = %8.4f\n", diffs[1]);

   printf("D- = %8.4f\n", diffs[2]);

   printf("Z = %8.4f\n", statistics[0]);

   printf("Prob greater D one sided = %8.4f\n", statistics[1]);

   printf("Prob greater D two sided = %8.4f\n", statistics[2]);

   printf("N missing = %d\n", nmiss);

}

/*

* User function that accepts additional data in a (void*) pointer.

* This (void*) pointer can be cast to any type and dereferenced to

* get at any sort of data-type or structure that is needed.

* For example, to get at the data in this example

*  *((float*)data)   contains the value  0.5

*  *((float*)data+1) contains the value  0.2886751.

*/

float cdf_w_data(float x, void *data)

{

   float mean, std, z;

   mean = *((float*)data);

   std =  *((float*)data+1);

 

   z = (x-mean)/std;

   return(imsls_f_normal_cdf(z));

}


Visual Numerics, Inc.
Visual Numerics - Developers of IMSL and PV-WAVE
http://www.vni.com/
PHONE: 713.784.3131
FAX:713.781.9260