From radford at cs.toronto.edu  Thu Aug 26 19:41:47 2010
From: radford at cs.toronto.edu (Radford Neal)
Date: Thu, 26 Aug 2010 13:41:47 -0400
Subject: [Rd] Speeding up transpose
In-Reply-To: <20100826025012.GA4179@cs.toronto.edu>
References: <20100826025012.GA4179@cs.toronto.edu>
Message-ID: <20100826174147.GA26101@cs.toronto.edu>

> 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;
    }



[Rd] Speeding up transpose
Radford Neal