[R] problem applying .C function in outer product

Jim McLoughlin jimmcloughlin at earthlink.net
Thu Mar 13 00:02:15 CET 2003


Hi folks

I'm relatively new to R, and am having some problems using a c function 
I'm calling.

The function is loaded, and it works for when given specific (scalar) 
arguments, but does not work when passed Vector arguments, and 
therefore fails in the outer product I need.

Here are the details:

the C function, where the last argument is used to return the value I 
need (price):

/*
  * Wrapper for use in R - requires that return value by passed via 
argument
  */
void blackScholesR( double *S, double *K, double *r, double *T, double 
*volatility, double *price)
{
     *price = blackScholes(*S, *K, *r, *T, *volatility);
}

The function definition in R:

blackS <- function(S,K,r,T,vol){
	tempx <- .C("blackScholesR",
	as.double(S),
	as.double(K),
	as.double(r),
	as.double(T),
	as.double(vol),
	as.double(0.0))
	as.double(tempx[6])
}

This function returns the appropriate value for constant vals, example:

 > blackS(52,50,0,0.5,0.43)
[1] 7.213387

but fails if I use vector arguments for S or T.  I ultimately want to 
create an outer product varying both S and T and graph the surface.

For example, given

 > T
  [1] 0.00000000 0.03571429 0.07142857 0.10714286 0.14285714 0.17857143
  [7] 0.21428571 0.25000000 0.28571429 0.32142857 0.35714286 0.39285714
[13] 0.42857143 0.46428571 0.50000000

I get

 > blackS(52,50,0,T,0.43)
[1] 2

2 is the right answer for the first value of T, but it should produce 
an entire vector of values for all possible T.  I assume here is a 
problem with my return value in the R function blackS.  I tried return 
tempx[6] without casting casting to double.  This gives similar 
results, except it seems to be a vector embedded in a list:

blackS(52,50,0,0.5,0.43)
[[1]]
[1] 7.213387

I'm probably making an obvious mistake, but am at an impasse.  Any help 
greatly appreciated

regards

Jim M



More information about the R-help mailing list