[Rd] AS_NUMERIC and as.numeric - Could someone explain?

From: Daniel Hoppe <daniel.hoppe_at_univie.ac.at>
Date: Thu 01 Jul 2004 - 01:40:21 EST


Dear List,

I stepped into a strange effect which I can't explain to myself (probably due to lack of knowledge on R internals).

I have four vectors a,b,c and z of size 10000 each. With these vectors I call

.Call("hyp2f1forrey", a, b, b, z, PACKAGE = "hyp2f1")

to access

SEXP hyp2f1forrey(SEXP a, SEXP b, SEXP c, SEXP x) {

    int i,n;
    double *xa, *xb, *xc, *xx, *xresr, *xresi;     SEXP resr, resi;

    n = LENGTH(a);

    PROTECT(a = AS_NUMERIC(a));
    PROTECT(b = AS_NUMERIC(b));
    PROTECT(c = AS_NUMERIC(c));
    PROTECT(x = AS_NUMERIC(x));
    PROTECT(resr = NEW_NUMERIC(n));
    PROTECT(resi = NEW_NUMERIC(n));

    xa = NUMERIC_POINTER(a);
    xb = NUMERIC_POINTER(b);

    xc = NUMERIC_POINTER(c);
    xx = NUMERIC_POINTER(x);
    xresr = NUMERIC_POINTER(resr);
    xresi = NUMERIC_POINTER(resi);

    for (i = 0; i < n; i++)

        F77_CALL(hyp)(&xx[i], &xa[i], &xb[i], &xc[i], &xresr[i], &xresi[i]);

    UNPROTECT(6);
    return(resr);
}

I read the documentation in the way that I could either do the call from above or

.Call("hyp2f1forrey", as.numeric(a), as.numeric(b), as.numeric(b),
as.numeric(z), PACKAGE = "hyp2f1")

and remove the

    PROTECT(a = AS_NUMERIC(a));
    PROTECT(b = AS_NUMERIC(b));
    PROTECT(c = AS_NUMERIC(c));
    PROTECT(x = AS_NUMERIC(x));

in the C-Code. What happens now is that in my resulting vector of length 10000 some values are incorrect depending on the exact way of calling the C-Function.

.Call("hyp2f1forrey", a, b, b, z, PACKAGE = "hyp2f1")[11] -->
incorrect result

.Call("hyp2f1forrey", a[11], b[11], b[11]), z[11]), PACKAGE =
"hyp2f1") --> works

.Call("hyp2f1forrey", as.numeric(a), as.numeric(b), as.numeric(c),
as.numeric(z), PACKAGE = "hyp2f1")[11] -- works

.Call("hyp2f1forrey", a[1:10000], b[1:10000], b[1:10000]),
z[1:10000]), PACKAGE = "hyp2f1") --> works too!

Could someone explain to me where the difference in these calls comes from?

Thanks in advance,

Daniel



R-devel@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-devel Received on Thu Jul 01 01:43:35 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 08:59:06 EST