Here is a brief introduction to interfacing IMSL with C and Splus. =================================================================== IMSL is a venerable commercial Fortran subroutine library. On many systems you would have to call the Fortran directly from C, which is not too painful and is described in Howard Seltman's computing pages (cf http://www.stat.cmu.edu/~hseltman/c/CTips.html). On our department Linux machines, IMSL is available through a special C subroutine library, and this makes for easier use from C. The IMSL C subroutine library basically consists of wrapper functions that make calls to underlying IMSL Fortran subroutines. The raw Fortran subroutine library for IMSL is not available on our Linux machines. For help in using the IMSL C libraries you can examine acroread /usr/statlocal/vni/CTT3.0/help/cmath.pdf & acroread /usr/statlocal/vni/CTT3.0/help/cstat.pdf & (and if you use these a lot you may want to create aliases such as alias imslmath acroread /usr/statlocal/vni/CTT3.0/help/cmath.pdf & alias imslstat acroread /usr/statlocal/vni/CTT3.0/help/cstat.pdf & and put these aliases in your .cshrc file). Here is a brief demo program to get you started using IMSL C routines from your C programs: ---------------------------------------------------------------------- /* File: Demo.c Author: H. Seltman; adapted by B. Junker Date: 11/02 Purpose: Demonstrate use of IMSL C functions in C; adapted from H. Seltman's demo of IMSL Ftn functions of 6/98 Compile: Simple compile as follows: source /usr/statlocal/vni/CTT2.1/ctt/bin/cttsetup.csh gcc -o demo demo.c $CFLAGS $LINK_CNL -lm -L/usr/statlocal/lib Runstring: Demo [n], where n is number of randoms generated */ #include #include #include #include #include /* Prototypes and constants for C-IMSL math routines */ #include /* Prototypes and constants for C-IMSL stat routines */ /******************************************/ /******************************************/ /* MAIN PROGAM: Demonstrate IMSL */ /******************************************/ /******************************************/ int main(int argc, char **argv) { int n=5; /* number of random numbers desired */ int i; double *rn; /* pointer to vector of random numbers */ int perline=6; /* items per output line */ double df; /* degrees of freedom for chi square */ int size, choose; /* parameters for binomial coefficient function */ double alpha; /* parameter for gamma function */ /********************************/ /* Handle runstring argument: n */ /********************************/ if (argc>=2) { n=atoi(argv[1]); } /*******************************/ /* allocate memory for results */ /*******************************/ if ((rn=malloc(n*sizeof(double)))==NULL) { printf("Can't allocate memory for %d numbers.\n", n); return(1); } /**********************************************/ /* Setup best random number generation option */ /**********************************************/ imsl_random_option(6); /* best generator */ imsl_random_seed_set(0); /* random start */ /***************************/ /* Generate random numbers */ /***************************/ printf("Random chi-square(10)\n"); df=10.0; imsls_d_random_chi_squared (n, df, IMSLS_RETURN_USER, rn, 0); for (i=0; i #include /* only need math, not stat, routines for this example */ float fcn(int n, float x[]); main() { int i, j, ndim = 3; float q, limit, a[3], b[3]; printf(" integral limit \n"); limit = pow(imsl_f_constant("pi",0), 1.5); /* Evaluate the integral */ for (i = 0; i < 6; i++) { for(j =0;j <3; j++) { a[j] = -(i+1)/2.; b[j] = (i+1)/2.; } q = imsl_f_int_fcn_hyper_rect (fcn, ndim, a, b, 0); /* Print the result and the */ /* limiting answer */ printf(" %10.3f %10.3f\n", q, limit); } } float fcn(int n, float x[]) { float s; s = x[0]*x[0] + x[1]*x[1] + x[2]*x[2]; return exp(-s); } ========================================================================== Here is a demo of using the IMSL C routines in Splus: -------------------------------------------------------------------------- /* Program: srand.c Author: B Junker Date: 11/02 Purpose: Demo calling IMSL C subroutines from Splus Compile: source /usr/statlocal/vni/CTT2.1/ctt/bin/cttsetup.csh gcc -c srand.c $CFLAGS Splus LIBRARY srand.so srand.o $CFLAGS $LINK_CNL -lm -L/usr/statlocal/lib */ #include #include #include #include /* Prototypes and constants for C-IMSL math routines */ #include /* Prototypes and constants for C-IMSL stat routines */ void bjrand(int *n, double *rn) { imsl_random_option(6); /* best generator */ imsl_random_seed_set(0); /* random start */ imsls_d_random_uniform (*n, IMSLS_RETURN_USER, rn, 0); } -------------------------------------------------------------------------- This can be compiled for Splus as follows: source /usr/statlocal/vni/CTT2.1/ctt/bin/cttsetup.csh gcc -c srand.c $CFLAGS Splus LIBRARY srand.so srand.o $CFLAGS $LINK_CNL -lm -L/usr/statlocal/lib [It seems to work to use the same commands to compile srand.so for use with R; I have not figured out how to use R CMD SHLIB for this purpose yet!] And then we use it in Splus or R as usual: -------------------------------------------------------------------------- dyn.open("srand.so") # dyn.load("srand.so") for R... bjrand _ function(n) { out _ rep(0,n) z _ .C("bjrand",as.integer(n),as.double(out)) return(z) } invisible(bjrand(1000000)) invisible(runif(1000000)) # runif is a little faster! # Clearer timing: timeit _ function(fcn, nreps = 10, nrand = 1000000) { print(unix("date")) for(i in 1:nreps) fcn(nrand) print(unix("date")) invisible() } # Timings in Splus -- 3 sec (runif) vs. 9 sec (bjrand) > timeit(runif) [1] "Wed Nov 20 10:41:39 EST 2002" [1] "Wed Nov 20 10:41:41 EST 2002" > timeit(bjrand) [1] "Wed Nov 20 10:41:46 EST 2002" [1] "Wed Nov 20 10:41:55 EST 2002" # Timings in R -- 5 sec (runif) vs. 6 sec (bjrand) timeit(runif) Wed Nov 20 10:43:45 EST 2002 Wed Nov 20 10:43:50 EST 2002 > timeit(bjrand) Wed Nov 20 10:44:30 EST 2002 Wed Nov 20 10:44:36 EST 2002 qqplot(runif(1000),bjrand(1000)) # compare "uniformity" of the # two rng's ========================================================================== The Fortran IMSL library is a collection of mathematical and statistical functions written in Fortran and pre-compiled. They are licensed and available to the CMU community and available on Andrew and throughout the Statistics department on HP and SUN computers. By using these routines, you will get well-implemented versions of functions which can perform a wide range of tasks, from generating random numbers to performing regression, but without any coding on your part! The functions are described in books labelled IMSL Stat Library and IMSL Math Library and via the links listed below. They are currently unavailable at CMU for Windows. For Linux, IMSL C libraries are available, but unfortunately these do not closely parallel the Fortran libraries, so constructing cross-platform programs is difficult. Information on using the IMSL Fortran subroutine packages from C is available via Howard Seltman's page http://www.stat.cmu.edu/~hseltman/c/CTips.html However, since the IMSL Fortran library is not available on our Linux workstations, we will not pursue it here. ========================================================================