The R Project SVN R

Rev

Rev 51267 | Rev 52652 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 51267 Rev 52649
Line 999... Line 999...
999
 */
999
 */
1000
 
1000
 
1001
/* this increments iip and sets j using strides */
1001
/* this increments iip and sets j using strides */
1002
 
1002
 
1003
#define CLICKJ						\
1003
#define CLICKJ						\
1004
    for (itmp=0; itmp<n; itmp++)			\
1004
    for (itmp = 0; itmp < n; itmp++)			\
1005
	if (iip[itmp] == INTEGER(dimsr)[itmp]-1)	\
1005
	if (iip[itmp] == isr[itmp]-1) iip[itmp] = 0;	\
1006
	    iip[itmp] = 0;				\
-
 
1007
	else {						\
1006
	else {						\
1008
	    iip[itmp]++;				\
1007
	    iip[itmp]++;				\
1009
	    break;					\
1008
	    break;					\
1010
	}						\
1009
	}						\
1011
    for (j=0, itmp=0; itmp<n; itmp++)			\
1010
    for (j = 0, itmp = 0; itmp < n; itmp++)	       	\
1012
	j += iip[itmp] * stride[itmp];
1011
	j += iip[itmp] * stride[itmp];
1013
 
1012
 
1014
/* aperm (a, perm, resize = TRUE) */
1013
/* aperm (a, perm, resize = TRUE) */
1015
SEXP attribute_hidden do_aperm(SEXP call, SEXP op, SEXP args, SEXP rho)
1014
SEXP attribute_hidden do_aperm(SEXP call, SEXP op, SEXP args, SEXP rho)
1016
{
1015
{
1017
    SEXP a, perm, resize, r, dimsa, dimsr, dna;
1016
    SEXP a, perm, r, dimsa, dimsr, dna;
1018
    int i, j, n, len, itmp;
1017
    int i, j, n, len, itmp;
1019
    int *pp, *iip, *stride;
-
 
1020
 
1018
 
1021
    checkArity(op, args);
1019
    checkArity(op, args);
1022
 
1020
 
1023
    a = CAR(args);
1021
    a = CAR(args);
1024
    if (!isArray(a))
1022
    if (!isArray(a))
1025
	error(_("invalid first argument, must be an array"));
1023
	error(_("invalid first argument, must be an array"));
1026
 
1024
 
1027
    PROTECT(dimsa = getAttrib(a, R_DimSymbol));
1025
    PROTECT(dimsa = getAttrib(a, R_DimSymbol));
1028
    n = LENGTH(dimsa);
1026
    n = LENGTH(dimsa);
-
 
1027
    int *isa = INTEGER(dimsa);
1029
 
1028
 
1030
    /* check the permutation */
1029
    /* check the permutation */
1031
 
1030
 
1032
    PROTECT(perm = coerceVector(CADR(args), INTSXP));
1031
    int *pp = (int *) R_alloc(n, sizeof(int));
1033
    pp = (int *) R_alloc(n, sizeof(int));
1032
    perm = CADR(args);
1034
    if (length(perm) == 0) {
1033
    if (length(perm) == 0) {
1035
	for (i=0; i<n; i++)
1034
	for (i = 0; i < n; i++) pp[i] = n-1-i;
-
 
1035
    } else if (isString(perm)) {
1036
	    pp[i] = n-1-i;
1036
    } else {
1037
    } else if (length(perm) == n) {
1037
	PROTECT(perm = coerceVector(perm, INTSXP));
1038
	for (i=0; i<n; i++)
1038
	if (length(perm) == n) {
1039
	    pp[i] = INTEGER(perm)[i] - 1; /* no offset! */
1039
	    for (i = 0; i < n; i++) pp[i] = INTEGER(perm)[i] - 1;
1040
    } else
1040
	    UNPROTECT(1);
1041
	error(_("'perm' is of wrong length"));
1041
	} else error(_("'perm' is of wrong length"));
-
 
1042
    }
1042
 
1043
 
1043
    iip = (int *) R_alloc(n, sizeof(int));
1044
    int *iip = (int *) R_alloc(n, sizeof(int));
1044
    for (i = 0; i < n; iip[i++] = 0);
1045
    for (i = 0; i < n; iip[i++] = 0);
1045
    for (i = 0; i < n; i++)
1046
    for (i = 0; i < n; i++)
1046
	if (pp[i] >= 0 && pp[i] < n)
1047
	if (pp[i] >= 0 && pp[i] < n) iip[pp[i]]++;
1047
	    iip[pp[i]]++;
-
 
1048
	else
-
 
1049
	    error(_("value out of range in 'perm'"));
1048
	else error(_("value out of range in 'perm'"));
1050
    for (i = 0; i < n; i++)
1049
    for (i = 0; i < n; i++)
1051
	if (iip[i] == 0)
-
 
1052
	    error(_("invalid '%s' argument"), "perm");
1050
	if (iip[i] == 0) error(_("invalid '%s' argument"), "perm");
1053
 
1051
 
1054
    /* create the stride object and permute */
1052
    /* create the stride object and permute */
1055
 
1053
 
1056
    stride = (int *) R_alloc(n, sizeof(int));
1054
    int *stride = (int *) R_alloc(n, sizeof(int));
1057
 
-
 
1058
    for (iip[0] = 1, i = 1; i<n; i++)
1055
    for (iip[0] = 1, i = 1; i<n; i++) iip[i] = iip[i-1] * isa[i-1];
1059
	iip[i] = iip[i-1] * INTEGER(dimsa)[i-1];
-
 
1060
 
-
 
1061
    for (i = 0; i < n; i++)
1056
    for (i = 0; i < n; i++) stride[i] = iip[pp[i]];
1062
	stride[i] = iip[pp[i]];
-
 
1063
 
1057
 
1064
    /* also need to have the dimensions of r */
1058
    /* also need to have the dimensions of r */
1065
 
1059
 
1066
    PROTECT(dimsr = allocVector(INTSXP,n));
1060
    PROTECT(dimsr = allocVector(INTSXP, n));
1067
    for (i = 0; i < n; i++)
1061
    int *isr = INTEGER(dimsr);
1068
	INTEGER(dimsr)[i] = INTEGER(dimsa)[pp[i]];
1062
    for (i = 0; i < n; i++) isr[i] = isa[pp[i]];
1069
 
1063
 
1070
    /* and away we go! iip will hold the incrementer */
1064
    /* and away we go! iip will hold the incrementer */
1071
 
1065
 
1072
    len = LENGTH(a);
1066
    len = LENGTH(a);
1073
    len = length(a);
1067
    len = length(a);
1074
    PROTECT(r = allocVector(TYPEOF(a), len));
1068
    PROTECT(r = allocVector(TYPEOF(a), len));
1075
 
1069
 
1076
    for (i=0; i<n; iip[i++] = 0);
1070
    for (i = 0; i < n; iip[i++] = 0);
1077
 
1071
 
1078
    switch (TYPEOF(a)) {
1072
    switch (TYPEOF(a)) {
1079
 
1073
 
1080
    case INTSXP:
1074
    case INTSXP:
1081
	for (j=0, i=0; i<len; i++) {
1075
	for (j=0, i=0; i<len; i++) {
Line 1130... Line 1124...
1130
    default:
1124
    default:
1131
	UNIMPLEMENTED_TYPE("aperm", a);
1125
	UNIMPLEMENTED_TYPE("aperm", a);
1132
    }
1126
    }
1133
 
1127
 
1134
    /* handle the resize */
1128
    /* handle the resize */
1135
    PROTECT(resize = coerceVector(CADDR(args), LGLSXP));
1129
    int resize = asLogical(CADDR(args));
1136
    if (LOGICAL(resize)[0])
1130
    if (resize == NA_LOGICAL) error(_("'resize' must be TRUE or FALSE"));
1137
	setAttrib(r, R_DimSymbol, dimsr);
-
 
1138
    else
-
 
1139
	setAttrib(r, R_DimSymbol, dimsa);
1131
    setAttrib(r, R_DimSymbol, resize ? dimsr : dimsa);
1140
 
1132
 
1141
    /* and handle the dimnames */
1133
    /* and handle the dimnames, if any */
1142
 
-
 
-
 
1134
    if (resize) {
1143
    PROTECT(dna = getAttrib(a, R_DimNamesSymbol));
1135
	PROTECT(dna = getAttrib(a, R_DimNamesSymbol));
1144
 
-
 
1145
    if (LOGICAL(resize)[0] && dna != R_NilValue) {
1136
	if (dna != R_NilValue) {
1146
 
-
 
1147
	SEXP dnna, dnr, dnnr;
1137
	    SEXP dnna, dnr, dnnr;
1148
 
1138
 
-
 
1139
	    PROTECT(dnr  = allocVector(VECSXP, n));
1149
	PROTECT(dnna = getAttrib(dna, R_NamesSymbol));
1140
	    PROTECT(dnna = getAttrib(dna, R_NamesSymbol));
1150
	PROTECT(dnnr = allocVector(STRSXP,n));
1141
	    if (dnna != R_NilValue) {
1151
	PROTECT(dnr  = allocVector(VECSXP,n));
1142
		PROTECT(dnnr = allocVector(STRSXP, n));
1152
 
-
 
1153
	for (i=0; i<n; i++) {
1143
		for (i = 0; i < n; i++) {
1154
	    SET_VECTOR_ELT(dnr, i, VECTOR_ELT(dna, pp[i]));
1144
		    SET_VECTOR_ELT(dnr, i, VECTOR_ELT(dna, pp[i]));
-
 
1145
		    SET_STRING_ELT(dnnr, i, STRING_ELT(dnna, pp[i]));
-
 
1146
		}
-
 
1147
		setAttrib(dnr, R_NamesSymbol, dnnr);
-
 
1148
		UNPROTECT(1);
-
 
1149
	    } else {
1155
	    if (dnna != R_NilValue)
1150
		for (i = 0; i < n; i++)
1156
		SET_STRING_ELT(dnnr, i, STRING_ELT(dnna, pp[i]));
1151
		    SET_VECTOR_ELT(dnr, i, VECTOR_ELT(dna, pp[i]));
-
 
1152
	    }
-
 
1153
	    setAttrib(r, R_DimNamesSymbol, dnr);
-
 
1154
	    UNPROTECT(2);
1157
	}
1155
	}
1158
 
-
 
1159
	if (dnna != R_NilValue)
-
 
1160
	    setAttrib(dnr, R_NamesSymbol, dnnr);
-
 
1161
	setAttrib(r, R_DimNamesSymbol, dnr);
-
 
1162
	UNPROTECT(3); /* dnna, dnr, dnnr */
1156
	UNPROTECT(1);
1163
    }
1157
    }
1164
 
1158
 
1165
    UNPROTECT(6); /* dimsa, perm, r, dimsr, resize, dna */
1159
    UNPROTECT(3); /* dimsa, r, dimsr */
1166
    return r;
1160
    return r;
1167
}
1161
}
1168
 
1162
 
1169
/* colSums(x, n, p, na.rm) and friends */
1163
/* colSums(x, n, p, na.rm) and friends */
1170
SEXP attribute_hidden do_colsum(SEXP call, SEXP op, SEXP args, SEXP rho)
1164
SEXP attribute_hidden do_colsum(SEXP call, SEXP op, SEXP args, SEXP rho)