I am interested in precisely understanding C code for R packages. But I have come across code such as RANDIN
, RANDOUT
, UNIF
, EPS
etc (are they macros?) for which I don't know where to find the implementations/definitions.
Where can I find the code (and explanations if any) for these capitalised 'expressions'?
An example is the VR_onlineSOM
function inside the 'class' R package. This function has the following code written in C:
void
VR_onlineSOM(double *data, double *codes, double *nhbrdist,
double *alpha, double *radii,
Sint *pn, Sint *pp, Sint *pncodes, Sint *rlen)
{
int n = *pn, p = *pp, ncodes = *pncodes;
int i, j, k, nearest = 0 /* -Wall */, nind;
double dm, dist, tmp;
unsigned int cd; /* avoid spurious warning from gcc pre-4.3.0 */
RANDIN;
for (k = 0; k < *rlen; k++) {
/* pick a random data point */
i = (int)(n * UNIF);
/* find the nearest code 'near' */
nind = 0; dm = DOUBLE_XMAX;
for (cd = 0; cd < ncodes; cd++) {
dist = 0.0;
for (j = 0; j < p; j++) {
tmp = data[i + j*n] - codes[cd + j*ncodes];
dist += tmp * tmp;
}
if (dist <= dm * (1 + EPS)) {
if (dist < dm * (1 - EPS)) {
nind = 0;
nearest = cd;
} else {
if(++nind * UNIF < 1.0) nearest = cd;
}
dm = dist;
}
/* update all codes within radii[k] of 'nearest' */
for (cd = 0; cd < ncodes; cd++) {
if(nhbrdist[cd + ncodes*nearest] > radii[k]) continue;
for(j = 0; j < p; j++)
codes[cd + j*ncodes] += alpha[k] *
(data[i + j*n] - codes[cd + j*ncodes]);
}
}
}
RANDOUT;
}
Yes they are some macros and constantes defined in the begining of the class.c.
#define EPS 1e-4 /* relative test of equality of distances */
#define RANDIN GetRNGstate()
#define RANDOUT PutRNGstate()
#define UNIF unif_rand()