[Rd] Speeding up transpose

Radford Neal radford at cs.toronto.edu
Thu Aug 26 19:41:47 CEST 2010


> I've appended below the new version of the modified part of the
> do_transpose function in src/main/array.c.

A quick correction...  A "break" went missing from the case INTSXP:
section of the code I posted.  Corrected version below.
 
     Radford Neal

----------------------------------------------------------------------

    PROTECT(r = allocVector(TYPEOF(a), len));
    switch (TYPEOF(a)) {
    case LGLSXP:
    case INTSXP:
        for (i = 0, j = 0; i<len; i += 1, j += nrow) {
            if (j>=len) j -= (len-1);
            INTEGER(r)[i] = INTEGER(a)[j];
        }
        break;
    case REALSXP:
        for (i = 0, j = 0; i<len; i += 1, j += nrow) {
            if (j>=len) j -= (len-1);
            REAL(r)[i] = REAL(a)[j];
        }
        break;
    case CPLXSXP:
        for (i = 0, j = 0; i<len; i += 1, j += nrow) {
            if (j>=len) j -= (len-1);
            COMPLEX(r)[i] = COMPLEX(a)[j];
        }
        break;
    case STRSXP:
        for (i = 0, j = 0; i<len; i += 1, j += nrow) {
            if (j>=len) j -= (len-1);
            SET_STRING_ELT(r, i, STRING_ELT(a,j));
        }
        break;
    case VECSXP:
        for (i = 0, j = 0; i<len; i += 1, j += nrow) {
            if (j>=len) j -= (len-1);
            SET_VECTOR_ELT(r, i, VECTOR_ELT(a,j));
        }
        break;
    case RAWSXP:
        for (i = 0, j = 0; i<len; i += 1, j += nrow) {
            if (j>=len) j -= (len-1);
            RAW(r)[i] = RAW(a)[j];
        }
        break;
    default:
        UNPROTECT(1);
        goto not_matrix;
    }



More information about the R-devel mailing list