The R Project SVN R

Rev

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

Rev 31908 Rev 32350
Line 151... Line 151...
151
	case RAWSXP:
151
	case RAWSXP:
152
	    for (i = 0; i < nr; i++)
152
	    for (i = 0; i < nr; i++)
153
		for (j = 0; j < nc; j++)
153
		for (j = 0; j < nc; j++)
154
		    RAW(snr)[i + j * nr] = 0;
154
		    RAW(snr)[i + j * nr] = 0;
155
	    break;
155
	    break;
156
	default: 
156
	default:
157
	    /* don't fill with anything */
157
	    /* don't fill with anything */
158
	    ;
158
	    ;
159
	}
159
	}
160
    }
160
    }
161
    UNPROTECT(1);
161
    UNPROTECT(1);
Line 393... Line 393...
393
	/* Don't trust the BLAS to handle NA/NaNs correctly: PR#4582
393
	/* Don't trust the BLAS to handle NA/NaNs correctly: PR#4582
394
	 * The test is only O(n) here
394
	 * The test is only O(n) here
395
	 */
395
	 */
396
	for (i = 0; i < nrx*ncx; i++)
396
	for (i = 0; i < nrx*ncx; i++)
397
	    if (ISNAN(x[i])) {have_na = TRUE; break;}
397
	    if (ISNAN(x[i])) {have_na = TRUE; break;}
398
	if (!have_na) 
398
	if (!have_na)
399
	    for (i = 0; i < nry*ncy; i++)
399
	    for (i = 0; i < nry*ncy; i++)
400
		if (ISNAN(y[i])) {have_na = TRUE; break;}
400
		if (ISNAN(y[i])) {have_na = TRUE; break;}
401
	if (have_na) {
401
	if (have_na) {
402
	    for (i = 0; i < nrx; i++)
402
	    for (i = 0; i < nrx; i++)
403
		for (k = 0; k < ncy; k++) {
403
		for (k = 0; k < ncy; k++) {
Line 562... Line 562...
562
	    else if (nry == 1) {	/* x as col vector */
562
	    else if (nry == 1) {	/* x as col vector */
563
		nrx = LENGTH(x);
563
		nrx = LENGTH(x);
564
		ncx = 1;
564
		ncx = 1;
565
	    }
565
	    }
566
	}
566
	}
567
	else {
567
	else { /* crossprod */
568
	    if (LENGTH(x) == nry) {	/* x is a row vector */
568
	    if (LENGTH(x) == nry) {	/* x is a row vector */
569
		nrx = LENGTH(x);
569
		nrx = LENGTH(x);
570
		ncx = 1;
570
		ncx = 1;
571
	    }
571
	    }
572
	}
572
	}
Line 597... Line 597...
597
	nrx = INTEGER(xdims)[0];
597
	nrx = INTEGER(xdims)[0];
598
	ncx = INTEGER(xdims)[1];
598
	ncx = INTEGER(xdims)[1];
599
	nry = INTEGER(ydims)[0];
599
	nry = INTEGER(ydims)[0];
600
	ncy = INTEGER(ydims)[1];
600
	ncy = INTEGER(ydims)[1];
601
    }
601
    }
-
 
602
    /* nr[ow](.) and nc[ol](.) are now defined for x and y */
602
 
603
 
603
    if (PRIMVAL(op) == 0) {
604
    if (PRIMVAL(op) == 0) {
604
	if (ncx != nry)
605
	if (ncx != nry)
605
	    errorcall(call, "non-conformable arguments");
606
	    errorcall(call, "non-conformable arguments");
606
    }
607
    }
Line 614... Line 615...
614
    else
615
    else
615
	mode = REALSXP;
616
	mode = REALSXP;
616
    SETCAR(args, coerceVector(CAR(args), mode));
617
    SETCAR(args, coerceVector(CAR(args), mode));
617
    SETCADR(args, coerceVector(CADR(args), mode));
618
    SETCADR(args, coerceVector(CADR(args), mode));
618
 
619
 
619
    if (PRIMVAL(op) == 0) {		       	/* op == 0 : matprod() */
620
    if (PRIMVAL(op) == 0) {			/* op == 0 : matprod() */
620
 
621
 
621
	PROTECT(ans = allocMatrix(mode, nrx, ncy));
622
	PROTECT(ans = allocMatrix(mode, nrx, ncy));
622
	if (mode == CPLXSXP)
623
	if (mode == CPLXSXP)
623
	    cmatprod(COMPLEX(CAR(args)), nrx, ncx,
624
	    cmatprod(COMPLEX(CAR(args)), nrx, ncx,
624
		     COMPLEX(CADR(args)), nry, ncy, COMPLEX(ans));
625
		     COMPLEX(CADR(args)), nry, ncy, COMPLEX(ans));
Line 628... Line 629...
628
 
629
 
629
	PROTECT(xdims = getAttrib(CAR(args), R_DimNamesSymbol));
630
	PROTECT(xdims = getAttrib(CAR(args), R_DimNamesSymbol));
630
	PROTECT(ydims = getAttrib(CADR(args), R_DimNamesSymbol));
631
	PROTECT(ydims = getAttrib(CADR(args), R_DimNamesSymbol));
631
 
632
 
632
	if (xdims != R_NilValue || ydims != R_NilValue) {
633
	if (xdims != R_NilValue || ydims != R_NilValue) {
-
 
634
	    SEXP dimnames, dimnamesnames, dnx=R_NilValue, dny=R_NilValue;
-
 
635
 
633
	    SEXP dimnames, dimnamesnames, dn;
636
	    /* allocate dimnames and dimnamesnames */
-
 
637
 
634
	    PROTECT(dimnames = allocVector(VECSXP, 2));
638
	    PROTECT(dimnames = allocVector(VECSXP, 2));
635
	    PROTECT(dimnamesnames = allocVector(STRSXP, 2));
639
	    PROTECT(dimnamesnames = allocVector(STRSXP, 2));
636
	    if (xdims != R_NilValue) {
640
	    if (xdims != R_NilValue) {
637
		if (ldx == 2 || ncx ==1) {
641
		if (ldx == 2 || ncx == 1) {
638
		    dn = getAttrib(xdims, R_NamesSymbol);
-
 
639
		    SET_VECTOR_ELT(dimnames, 0, VECTOR_ELT(xdims, 0));
642
		    SET_VECTOR_ELT(dimnames, 0, VECTOR_ELT(xdims, 0));
640
		    if(!isNull(dn))
-
 
641
			SET_STRING_ELT(dimnamesnames, 0, STRING_ELT(dn, 0));
-
 
642
		}
-
 
643
	    }
-
 
644
	    if (ydims != R_NilValue) {
-
 
645
		if (ldy == 2 ){
-
 
646
		    dn = getAttrib(ydims, R_NamesSymbol);
643
		    dnx = getAttrib(xdims, R_NamesSymbol);
647
		    SET_VECTOR_ELT(dimnames, 1, VECTOR_ELT(ydims, 1));
-
 
648
		    if(!isNull(dn))
-
 
649
			SET_STRING_ELT(dimnamesnames, 1, STRING_ELT(dn, 1));
-
 
650
		} else if (nry == 1) {
-
 
651
		    dn = getAttrib(ydims, R_NamesSymbol);
-
 
652
		    SET_VECTOR_ELT(dimnames, 1, VECTOR_ELT(ydims, 0));
-
 
653
		    if(!isNull(dn))
644
		    if(!isNull(dnx))
654
			SET_STRING_ELT(dimnamesnames, 1, STRING_ELT(dn, 0));
645
			SET_STRING_ELT(dimnamesnames, 0, STRING_ELT(dnx, 0));
655
		}
646
		}
656
	    }
647
	    }
-
 
648
 
-
 
649
#define YDIMS_ET_CETERA
-
 
650
	    if (ydims != R_NilValue) {						\
-
 
651
		if (ldy == 2) {							\
-
 
652
		    SET_VECTOR_ELT(dimnames, 1, VECTOR_ELT(ydims, 1));		\
-
 
653
		    dny = getAttrib(ydims, R_NamesSymbol);			\
-
 
654
		    if(!isNull(dny))						\
-
 
655
			SET_STRING_ELT(dimnamesnames, 1, STRING_ELT(dny, 1));	\
-
 
656
		} else if (nry == 1) {						\
-
 
657
		    SET_VECTOR_ELT(dimnames, 1, VECTOR_ELT(ydims, 0));		\
-
 
658
		    dny = getAttrib(ydims, R_NamesSymbol);			\
-
 
659
		    if(!isNull(dny))						\
-
 
660
			SET_STRING_ELT(dimnamesnames, 1, STRING_ELT(dny, 0));	\
-
 
661
		}								\
-
 
662
	    }									\
-
 
663
										\
-
 
664
	    /* We sometimes attach a dimnames attribute				\
-
 
665
	     * whose elements are all NULL ...					\
-
 
666
	     * This is ugly but causes no real damage.				\
-
 
667
	     * Now (2.1.0 ff), we don't anymore: */				\
-
 
668
	    if (VECTOR_ELT(dimnames,0) != R_NilValue ||				\
-
 
669
		VECTOR_ELT(dimnames,1) != R_NilValue) {				\
-
 
670
		if (dnx != R_NilValue || dny != R_NilValue)			\
657
	    setAttrib(dimnames, R_NamesSymbol, dimnamesnames);
671
		    setAttrib(dimnames, R_NamesSymbol, dimnamesnames);		\
658
	    setAttrib(ans, R_DimNamesSymbol, dimnames);
672
		setAttrib(ans, R_DimNamesSymbol, dimnames);			\
-
 
673
	    }									\
659
	    UNPROTECT(2);
674
	    UNPROTECT(2)
-
 
675
 
-
 
676
	    YDIMS_ET_CETERA;
660
	}
677
	}
661
    }
678
    }
662
 
679
 
663
    else {					/* op == 1: crossprod() */
680
    else {					/* op == 1: crossprod() */
664
 
681
 
Line 690... Line 707...
690
	    /* allocate dimnames and dimnamesnames */
707
	    /* allocate dimnames and dimnamesnames */
691
 
708
 
692
	    PROTECT(dimnames = allocVector(VECSXP, 2));
709
	    PROTECT(dimnames = allocVector(VECSXP, 2));
693
	    PROTECT(dimnamesnames = allocVector(STRSXP, 2));
710
	    PROTECT(dimnamesnames = allocVector(STRSXP, 2));
694
 
711
 
695
            /* There was a bug here.  The second element of a */
-
 
696
            /* dimnames list was being accessed for a 1-d array. */
-
 
697
            /* I have just excluded the use of dimnames in this */
-
 
698
            /* case. - ihaka Sep 30, 2003. */
-
 
699
 
-
 
700
	    if (xdims != R_NilValue) {
712
	    if (xdims != R_NilValue) {
701
                if (ldx == 2) {
-
 
702
		    dnx = getAttrib(xdims, R_NamesSymbol);
713
		if (ldx == 2) {/* not nrx==1 : .. fixed, ihaka 2003-09-30 */
703
		    SET_VECTOR_ELT(dimnames, 0, VECTOR_ELT(xdims, 1));
714
		    SET_VECTOR_ELT(dimnames, 0, VECTOR_ELT(xdims, 1));
-
 
715
		    dnx = getAttrib(xdims, R_NamesSymbol);
704
		    if(!isNull(dnx))
716
		    if(!isNull(dnx))
705
			SET_STRING_ELT(dimnamesnames, 0, STRING_ELT(dnx, 1));
717
			SET_STRING_ELT(dimnamesnames, 0, STRING_ELT(dnx, 1));
706
		}
718
		}
707
	    }
719
	    }
708
 
720
 
709
	    if (ydims != R_NilValue) {
-
 
710
                if (ldy == 2) {
-
 
711
		    dny = getAttrib(ydims, R_NamesSymbol);
-
 
712
		    SET_VECTOR_ELT(dimnames, 1, VECTOR_ELT(ydims, 1));
-
 
713
		    if(!isNull(dny))
-
 
714
			SET_STRING_ELT(dimnamesnames, 1, STRING_ELT(dny, 1));
-
 
715
		}
-
 
716
	    }
-
 
717
 
-
 
718
            /* We sometimes attach a dimnames attribute */
-
 
719
            /* whose elements are all NULL ... */
-
 
720
            /* Thus is ugly but causes no real damage. */
-
 
721
	    
-
 
722
	    if (!isNull(dnx) || !isNull(dny))
-
 
723
		setAttrib(dimnames, R_NamesSymbol, dimnamesnames);
-
 
724
	    setAttrib(ans, R_DimNamesSymbol, dimnames);
-
 
725
	    UNPROTECT(2);
721
	    YDIMS_ET_CETERA;
726
	}
722
	}
727
    }
723
    }
728
    UNPROTECT(3);
724
    UNPROTECT(3);
729
    return ans;
725
    return ans;
730
}
726
}
-
 
727
#undef YDIMS_ET_CETERA
-
 
728
 
731
 
729
 
732
SEXP do_transpose(SEXP call, SEXP op, SEXP args, SEXP rho)
730
SEXP do_transpose(SEXP call, SEXP op, SEXP args, SEXP rho)
733
{
731
{
734
    SEXP a, r, dims, dimnames, dimnamesnames=R_NilValue,
732
    SEXP a, r, dims, dimnames, dimnamesnames=R_NilValue,
735
	ndimnamesnames, rnames, cnames;
733
	ndimnamesnames, rnames, cnames;
736
    int i, len = 0, ncol=0, nrow=0;
734
    int i, ldim, len = 0, ncol=0, nrow=0;
737
 
735
 
738
    checkArity(op, args);
736
    checkArity(op, args);
739
    a = CAR(args);
737
    a = CAR(args);
740
 
738
 
741
    if (isVector(a)) {
739
    if (isVector(a)) {
742
	dims = getAttrib(a, R_DimSymbol);
740
	dims = getAttrib(a, R_DimSymbol);
-
 
741
	ldim = length(dims);
743
	rnames = R_NilValue;
742
	rnames = R_NilValue;
744
	cnames = R_NilValue;
743
	cnames = R_NilValue;
745
	switch(length(dims)) {
744
	switch(ldim) {
746
	case 0:
745
	case 0:
747
	    nrow = len = length(a);
746
	    nrow = len = length(a);
748
	    ncol = 1;
747
	    ncol = 1;
749
	    rnames = getAttrib(a, R_NamesSymbol);
748
	    rnames = getAttrib(a, R_NamesSymbol);
750
	    break;
749
	    break;
751
	case 1:
750
	case 1:
752
	    nrow = len = length(a);
751
	    nrow = len = length(a);
753
	    ncol = 1;
752
	    ncol = 1;
754
	    rnames = getAttrib(a, R_DimNamesSymbol);
753
	    dimnames = getAttrib(a, R_DimNamesSymbol);
755
	    if (rnames != R_NilValue)
754
	    if (dimnames != R_NilValue) {
756
		rnames = VECTOR_ELT(rnames, 0);
755
		rnames = VECTOR_ELT(dimnames, 0);
-
 
756
		dimnamesnames = getAttrib(dimnames, R_NamesSymbol);
-
 
757
	    }
757
	    break;
758
	    break;
758
	case 2:
759
	case 2:
759
	    ncol = ncols(a);
760
	    ncol = ncols(a);
760
	    nrow = nrows(a);
761
	    nrow = nrows(a);
761
	    len = length(a);
762
	    len = length(a);
Line 814... Line 815...
814
	SET_VECTOR_ELT(dimnames, 0, cnames);
815
	SET_VECTOR_ELT(dimnames, 0, cnames);
815
	SET_VECTOR_ELT(dimnames, 1, rnames);
816
	SET_VECTOR_ELT(dimnames, 1, rnames);
816
	if(!isNull(dimnamesnames)) {
817
	if(!isNull(dimnamesnames)) {
817
	    PROTECT(ndimnamesnames = allocVector(VECSXP, 2));
818
	    PROTECT(ndimnamesnames = allocVector(VECSXP, 2));
818
	    SET_STRING_ELT(ndimnamesnames, 1, STRING_ELT(dimnamesnames, 0));
819
	    SET_STRING_ELT(ndimnamesnames, 1, STRING_ELT(dimnamesnames, 0));
-
 
820
	    SET_STRING_ELT(ndimnamesnames, 0,
819
	    SET_STRING_ELT(ndimnamesnames, 0, STRING_ELT(dimnamesnames, 1));
821
			   (ldim == 2) ? STRING_ELT(dimnamesnames, 1):
-
 
822
			   R_BlankString);
820
	    setAttrib(dimnames, R_NamesSymbol, ndimnamesnames);
823
	    setAttrib(dimnames, R_NamesSymbol, ndimnamesnames);
821
	    UNPROTECT(1);
824
	    UNPROTECT(1);
822
	}
825
	}
823
	setAttrib(r, R_DimNamesSymbol, dimnames);
826
	setAttrib(r, R_DimNamesSymbol, dimnames);
824
	UNPROTECT(1);
827
	UNPROTECT(1);
Line 1078... Line 1081...
1078
	cnt = p;
1081
	cnt = p;
1079
	PROTECT(ans = allocVector(REALSXP, n));
1082
	PROTECT(ans = allocVector(REALSXP, n));
1080
 
1083
 
1081
	/* reverse summation order to improve cache hits */
1084
	/* reverse summation order to improve cache hits */
1082
	if (type == REALSXP) {
1085
	if (type == REALSXP) {
1083
	    double *rans = REAL(ans), *ra = rans, *cnt = NULL, *c;
1086
	    double *rans = REAL(ans), *ra = rans, *Cnt = NULL, *c;
1084
	    rx = REAL(x);
1087
	    rx = REAL(x);
1085
	    if (!keepNA && OP == 3) cnt = Calloc(n, double);
1088
	    if (!keepNA && OP == 3) Cnt = Calloc(n, double);
1086
	    for (ra = rans, i = 0; i < n; i++) *ra++ = 0.0;
1089
	    for (ra = rans, i = 0; i < n; i++) *ra++ = 0.0;
1087
	    for (j = 0; j < p; j++) {
1090
	    for (j = 0; j < p; j++) {
1088
		ra = rans;
1091
		ra = rans;
1089
		if (keepNA)
1092
		if (keepNA)
1090
		    for (i = 0; i < n; i++) *ra++ += *rx++;
1093
		    for (i = 0; i < n; i++) *ra++ += *rx++;
1091
		else
1094
		else
1092
		    for (c = cnt, i = 0; i < n; i++, ra++, rx++, c++)
1095
		    for (c = Cnt, i = 0; i < n; i++, ra++, rx++, c++)
1093
			if (!ISNAN(*rx)) {
1096
			if (!ISNAN(*rx)) {
1094
			    *ra += *rx;
1097
			    *ra += *rx;
1095
			    if (OP == 3) (*c)++;
1098
			    if (OP == 3) (*c)++;
1096
			}
1099
			}
1097
	    }
1100
	    }
1098
	    if (OP == 3) {
1101
	    if (OP == 3) {
1099
		if (keepNA)
1102
		if (keepNA)
1100
		    for (ra = rans, i = 0; i < n; i++)
1103
		    for (ra = rans, i = 0; i < n; i++)
1101
			*ra++ /= p;
1104
			*ra++ /= p;
1102
		else {
1105
		else {
1103
		    for (ra = rans, c = cnt, i = 0; i < n; i++, c++)
1106
		    for (ra = rans, c = Cnt, i = 0; i < n; i++, c++)
1104
			if (*c > 0) *ra++ /= *c; else *ra++ = NA_REAL;
1107
			if (*c > 0) *ra++ /= *c; else *ra++ = NA_REAL;
1105
		    Free(cnt);
1108
		    Free(Cnt);
1106
		}
1109
		}
1107
	    }
1110
	    }
1108
	    UNPROTECT(1);
1111
	    UNPROTECT(1);
1109
	    return ans;
1112
	    return ans;
1110
	}
1113
	}