The R Project SVN R-packages

Rev

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

Rev 1389 Rev 1463
Line 525... Line 525...
525
    if (info) error(_("dgeMatrix_Schur: dgees returned code %d"), info);
525
    if (info) error(_("dgeMatrix_Schur: dgees returned code %d"), info);
526
    Free(work);
526
    Free(work);
527
    UNPROTECT(1);
527
    UNPROTECT(1);
528
    return val;
528
    return val;
529
}
529
}
-
 
530
 
-
 
531
SEXP dgeMatrix_colsums(SEXP x, SEXP naRmP, SEXP cols, SEXP mean)
-
 
532
{
-
 
533
    int keepNA = !asLogical(naRmP);
-
 
534
    int doMean = asLogical(mean);
-
 
535
    int useCols = asLogical(cols);
-
 
536
    int *dims = INTEGER(GET_SLOT(x, Matrix_DimSym));
-
 
537
    int cnt = 0, i, j, n = dims[0], p = dims[1];
-
 
538
    SEXP ans = PROTECT(allocVector(REALSXP, (useCols) ? p : n));
-
 
539
    double *xx = REAL(GET_SLOT(x, Matrix_xSym)), *rx, sum;
-
 
540
    
-
 
541
    if (useCols) {
-
 
542
	cnt = n;
-
 
543
	for (j = 0; j < p; j++) {
-
 
544
	    rx = xx + n*j;
-
 
545
	    if (keepNA)
-
 
546
		for (sum = 0., i = 0; i < n; i++) sum += *rx++;
-
 
547
	    else {
-
 
548
		for (cnt = 0, sum = 0., i = 0; i < n; i++, rx++)
-
 
549
		    if (!ISNAN(*rx)) {cnt++; sum += *rx;}
-
 
550
	    }
-
 
551
	    if (doMean) {
-
 
552
		if (cnt > 0) sum /= cnt; else sum = NA_REAL;
-
 
553
	    }
-
 
554
	    REAL(ans)[j] = sum;
-
 
555
	}
-
 
556
    } else {
-
 
557
	double *rans = REAL(ans), *ra = rans, *rx = xx, *Cnt = NULL, *c;
-
 
558
	cnt = p;
-
 
559
	if (!keepNA && doMean) Cnt = Calloc(n, double);
-
 
560
	for (ra = rans, i = 0; i < n; i++) *ra++ = 0.0;
-
 
561
	for (j = 0; j < p; j++) {
-
 
562
	    ra = rans;
-
 
563
	    if (keepNA)
-
 
564
		for (i = 0; i < n; i++) *ra++ += *rx++;
-
 
565
	    else
-
 
566
		for (c = Cnt, i = 0; i < n; i++, ra++, rx++, c++)
-
 
567
		    if (!ISNAN(*rx)) {
-
 
568
			*ra += *rx;
-
 
569
			if (doMean) (*c)++;
-
 
570
		    }
-
 
571
	}
-
 
572
	if (doMean) {
-
 
573
	    if (keepNA)
-
 
574
		for (ra = rans, i = 0; i < n; i++)
-
 
575
		    *ra++ /= p;
-
 
576
	    else {
-
 
577
		for (ra = rans, c = Cnt, i = 0; i < n; i++, c++)
-
 
578
		    if (*c > 0) *ra++ /= *c; else *ra++ = NA_REAL;
-
 
579
		Free(Cnt);
-
 
580
	    }
-
 
581
	}
-
 
582
    }
-
 
583
 
-
 
584
    UNPROTECT(1);
-
 
585
    return ans;
-
 
586
}