R .C interface: Passing multidimensional arrays

arrays, interface, multidimensional-array, r

Solution

No need to give up on `.C` for straight-forward manipulations like this. Remember that a matrix in R is a vector + dimensions. Likewise in C, so pass the matrix and its dimensions, and access elements of the matrix as appropriate offsets into a vector. Something like

void cplus1(double *x, int *dim)
{
    for (int j = 0; j < dim[1]; ++j)
        for (int i = 0; i < dim[0]; ++i)
            x[j * dim[0] + i] += 1;
}

so using `inline` as a nice party trick

library(inline)
sig <- signature(x="numeric", dim="integer")
body <- "
    for (int j = 0; j < dim[1]; ++j)
        for (int i = 0; i < dim[0]; ++i)
            x[j * dim[0] + i] += 1;
"

cfun <- cfunction(sig, body=body, language="C", convention=".C")

plus1 <- function(m) {
    m[] = cfun(as.numeric(m), dim(m))$x
    m
}

Problem

I've written up a function "foo" in C that I want to call from an R program. The function takes in as input a matrix and does some operations on it, (say add 1 to each element). While it is easy to a single vector as ``` .C("foo", n=as.integer(5), x=as.double(rnorm(5))) ``` with foo implemented as ``` void foo(int *nin, double *x) { int n = nin[0]; int i; for (i=0; i<n; i++) x[i] = x[i] * x[i]; } ``` How do I pass in a two dimensional array? If I change the "double *x" to "double **x" it gives a segmentation fault. Any pointers appreciated.

Original source