The R Project SVN R

Rev

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

Rev 19912 Rev 24343
Line 369... Line 369...
369
    empty = 1;/*- =1: only zero-length arguments, or NA with na.rm=T */
369
    empty = 1;/*- =1: only zero-length arguments, or NA with na.rm=T */
370
 
370
 
371
    iop = PRIMVAL(op);
371
    iop = PRIMVAL(op);
372
    switch(iop) {
372
    switch(iop) {
373
    case 0:/* sum */
373
    case 0:/* sum */
374
    /* we need to find out if _all_ the arguments are integer in advance, 
374
    /* we need to find out if _all_ the arguments are integer in advance,
375
       as we might overflow before we find out */
375
       as we might overflow before we find out */
376
	a = args;
376
	a = args;
377
	int_a = 1;
377
	int_a = 1;
378
	while (a != R_NilValue) {
378
	while (a != R_NilValue) {
379
	    if(!isInteger(CAR(a))) {
379
	    if(!isInteger(CAR(a))) {
Line 381... Line 381...
381
		break;
381
		break;
382
	    }
382
	    }
383
	    a = CDR(a);
383
	    a = CDR(a);
384
	}
384
	}
385
	ans_type = int_a ? INTSXP: REALSXP; /* try to keep if possible.. */
385
	ans_type = int_a ? INTSXP: REALSXP; /* try to keep if possible.. */
386
	zcum.r = zcum.i = 0.; icum = 0; 
386
	zcum.r = zcum.i = 0.; icum = 0;
387
	break;
387
	break;
388
 
388
 
389
    case 2:/* min */
389
    case 2:/* min */
390
	DbgP2("do_summary: min(.. na.rm=%d) ", narm);
390
	DbgP2("do_summary: min(.. na.rm=%d) ", narm);
391
	ans_type = INTSXP;
391
	ans_type = INTSXP;
392
#ifdef IEEE_754
392
#ifdef IEEE_754
393
	zcum.r = R_PosInf;
393
	zcum.r = R_PosInf;
394
#else
394
#else
395
	zcum.r = NA_REAL;
395
	zcum.r = NA_REAL;
396
#endif
396
#endif
397
	icum = INT_MAX; 
397
	icum = INT_MAX;
398
	break;
398
	break;
399
 
399
 
400
    case 3:/* max */
400
    case 3:/* max */
401
	DbgP2("do_summary: max(.. na.rm=%d) ", narm);
401
	DbgP2("do_summary: max(.. na.rm=%d) ", narm);
402
	ans_type = INTSXP;
402
	ans_type = INTSXP;
Line 570... Line 570...
570
 
570
 
571
		break;/* prod() part */
571
		break;/* prod() part */
572
 
572
 
573
	    }/* switch(iop) */
573
	    }/* switch(iop) */
574
 
574
 
575
	} else { /*len(a)=0*/ 
575
	} else { /*len(a)=0*/
576
	    if(TYPEOF(a) == CPLXSXP && (iop == 2 || iop == 3)) goto badmode;
576
	    if(TYPEOF(a) == CPLXSXP && (iop == 2 || iop == 3)) goto badmode;
577
	    if(ans_type < TYPEOF(a) && ans_type != CPLXSXP) {
577
	    if(ans_type < TYPEOF(a) && ans_type != CPLXSXP) {
578
		if(!empty && ans_type == INTSXP)
578
		if(!empty && ans_type == INTSXP)
579
		    zcum.r = Int2Real(icum);
579
		    zcum.r = Int2Real(icum);
580
		ans_type = TYPEOF(a);
580
		ans_type = TYPEOF(a);
Line 588... Line 588...
588
 
588
 
589
    /*-------------------------------------------------------*/
589
    /*-------------------------------------------------------*/
590
    if(empty && (iop == 2 || iop == 3)) {
590
    if(empty && (iop == 2 || iop == 3)) {
591
	if(iop == 2)
591
	if(iop == 2)
592
	    warning("no finite arguments to min; returning Inf");
592
	    warning("no finite arguments to min; returning Inf");
593
	else 
593
	else
594
	    warning("no finite arguments to max; returning -Inf");
594
	    warning("no finite arguments to max; returning -Inf");
595
	ans_type = REALSXP;
595
	ans_type = REALSXP;
596
    }
596
    }
597
 
597
 
598
    ans = allocVector(ans_type, 1);
598
    ans = allocVector(ans_type, 1);
Line 634... Line 634...
634
}
634
}
635

635

636
/* which.min(x) : The index (starting at 1), of the first min(x) in x */
636
/* which.min(x) : The index (starting at 1), of the first min(x) in x */
637
SEXP do_first_min(SEXP call, SEXP op, SEXP args, SEXP rho)
637
SEXP do_first_min(SEXP call, SEXP op, SEXP args, SEXP rho)
638
{
638
{
-
 
639
#define Beg_do_first					\
639
    SEXP sx, ans;
640
    SEXP sx, ans;					\
640
    double s;
641
    double s;						\
641
    int i, n, indx;
642
    int i, n, indx;					\
642
 
643
							\
643
    checkArity(op, args);
644
    checkArity(op, args);				\
644
 
645
							\
645
    PROTECT(sx = coerceVector(CAR(args), REALSXP));
646
    PROTECT(sx = coerceVector(CAR(args), REALSXP));	\
646
    if (!isNumeric(sx))
647
    if (!isNumeric(sx))					\
647
      errorcall(call, "non-numeric argument");
648
      errorcall(call, "non-numeric argument");		\
648
    n = LENGTH(sx);
649
    n = LENGTH(sx);					\
649
    indx = NA_INTEGER;
650
    indx = NA_INTEGER;
-
 
651
 
-
 
652
    Beg_do_first
-
 
653
 
650
    s = R_PosInf;
654
    s = R_PosInf;
651
    for (i = 0; i < n; i++)
655
    for (i = 0; i < n; i++)
652
	if (!ISNAN(REAL(sx)[i]) && REAL(sx)[i] < s) {
656
	if (!ISNAN(REAL(sx)[i]) && REAL(sx)[i] < s) {
653
	    s = REAL(sx)[i]; indx = i;
657
	    s = REAL(sx)[i]; indx = i;
654
	}
658
	}
-
 
659
 
-
 
660
#define End_do_first							\
655
    UNPROTECT(1);
661
    i = (indx != NA_INTEGER);						\
656
    ans = allocVector(INTSXP, (i = (indx != NA_INTEGER)) ? 1 : 0);
662
    ans = allocVector(INTSXP, i ? 1 : 0);				\
-
 
663
    if (i) {								\
657
    if (i) INTEGER(ans)[0] = indx + 1;
664
	INTEGER(ans)[0] = indx + 1;					\
-
 
665
	if (getAttrib(sx, R_NamesSymbol) != R_NilValue) { /* keep name */\
-
 
666
	    SEXP ansnam;						\
-
 
667
	    PROTECT(ansnam = allocVector(STRSXP, 1));			\
-
 
668
	    SET_STRING_ELT(ansnam, 0,					\
-
 
669
			   STRING_ELT(getAttrib(sx, R_NamesSymbol), indx));\
-
 
670
	    setAttrib(ans, R_NamesSymbol, ansnam);			\
-
 
671
	    UNPROTECT(1);						\
-
 
672
	}								\
-
 
673
    }									\
-
 
674
    UNPROTECT(1);							\
658
    return ans;
675
    return ans;
-
 
676
 
-
 
677
    End_do_first
659
}
678
}
660
 
679
 
661
/* which.max(x) : The index (starting at 1), of the first max(x) in x */
680
/* which.max(x) : The index (starting at 1), of the first max(x) in x */
662
SEXP do_first_max(SEXP call, SEXP op, SEXP args, SEXP rho)
681
SEXP do_first_max(SEXP call, SEXP op, SEXP args, SEXP rho)
663
{
682
{
664
    SEXP sx, ans;
-
 
665
    double s;
683
    Beg_do_first
666
    int i, n, indx;
-
 
667
 
684
 
668
    checkArity(op, args);
-
 
669
 
-
 
670
    PROTECT(sx = coerceVector(CAR(args), REALSXP));
-
 
671
    if (!isNumeric(sx))
-
 
672
      errorcall(call, "non-numeric argument");
-
 
673
    n = LENGTH(sx);
-
 
674
    indx = NA_INTEGER;
-
 
675
    s = R_NegInf;
685
    s = R_NegInf;
676
    for (i = 0; i < n; i++)
686
    for (i = 0; i < n; i++)
677
	if (!ISNAN(REAL(sx)[i]) && REAL(sx)[i] > s) {
687
	if (!ISNAN(REAL(sx)[i]) && REAL(sx)[i] > s) {
678
	    s = REAL(sx)[i]; indx = i;
688
	    s = REAL(sx)[i]; indx = i;
679
	}
689
	}
680
    UNPROTECT(1);
-
 
681
    ans = allocVector(INTSXP, (i = (indx != NA_INTEGER)) ? 1 : 0);
-
 
682
    if (i) INTEGER(ans)[0] = indx + 1;
-
 
-
 
690
 
683
    return ans;
691
    End_do_first
684
}
692
}
-
 
693
#undef Beg_do_first
-
 
694
#undef End_do_first
685
 
695
 
686
 
696
 
687
/* complete.cases(.) */
697
/* complete.cases(.) */
688
SEXP do_compcases(SEXP call, SEXP op, SEXP args, SEXP rho)
698
SEXP do_compcases(SEXP call, SEXP op, SEXP args, SEXP rho)
689
{
699
{