[R] Using .Call function to do matrix calculation

Deli Wang deli-wang at uiowa.edu
Mon Mar 17 20:42:50 CET 2003


Hello, 

Because of high dimensionality of matrix, I can not use R matrix calculation 
operator to do matrix multiplication.  My interest is the diagonal elements 
of J*J matrix( which can be got by multiplying matrix a(J*K) and b(K*J)),  
where J is too big to allocate enough memory for it.  In order to get those 
diagonals, I tried .Call function to do that. Here is c program for .Call.

#include <R.h>
#include <Rinternals.h>

SEXP var(SEXP a,SEXP b){
 int i,J=nrows(a),j,K=nrows(b),k;

 SEXP varbeta;
 PROTECT(varbeta=allocVector(REALSXP,J));

 for(j=0;j<J;j++){
  for(k=0;k<J;k++){
  if(j==k){
        for(i=0;i<K;i++) REAL(varbeta)[j]+=REAL(a)[j+i*J]*REAL(b)[i+k*K];}
    }
   }

 UNPROTECT(1);

 return (varbeta);
 }

This function did not work at all. The warning messages is the segmentation 
fault.

Could some expert help me out?

Thanks.



More information about the R-help mailing list