The R Project SVN R

Rev

Rev 82142 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 82142 Rev 87925
Line 1... Line 1...
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 
3
 
4
 *  Copyright (C) 2007-2021  The R Core Team
4
 *  Copyright (C) 2007-2025  The R Core Team
5
 *  Copyright (C) 1994-9 W. N. Venables and B. D. Ripley
5
 *  Copyright (C) 1994-9 W. N. Venables and B. D. Ripley
6
 *
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  the Free Software Foundation; either version 2 of the License, or
Line 42... Line 42...
42
void R_max_col(double *matrix, int *nr, int *nc, int *maxes, int *ties_meth)
42
void R_max_col(double *matrix, int *nr, int *nc, int *maxes, int *ties_meth)
43
{
43
{
44
    int	 c, m;
44
    int	 c, m;
45
    size_t  n_r = *nr; // for indexing like r + c * n_r
45
    size_t  n_r = *nr; // for indexing like r + c * n_r
46
    double a, b, large;
46
    double a, b, large;
47
    Rboolean isna, used_random = FALSE, do_rand = *ties_meth == 1;
47
    bool isna, used_random = false, do_rand = *ties_meth == 1;
48
 
48
 
49
    for (size_t r = 0; r < n_r; r++) {
49
    for (size_t r = 0; r < n_r; r++) {
50
	/* first check row for any NAs and find the largest abs(entry) */
50
	/* first check row for any NAs and find the largest abs(entry) */
51
	large = 0.0;
51
	large = 0.0;
52
	isna = TRUE;
52
	isna = true;
53
	for (c = 0; c < *nc; c++) {
53
	for (c = 0; c < *nc; c++) {
54
	    a = matrix[r + c * n_r];
54
	    a = matrix[r + c * n_r];
55
	    if (ISNAN(a)) { isna = TRUE; break; }
55
	    if (ISNAN(a)) { isna = true; break; }
56
	    else if(isna) isna = FALSE;
56
	    else if(isna) isna = false;
57
	    if (!R_FINITE(a)) continue;
57
	    if (!R_FINITE(a)) continue;
58
	    if (do_rand) large = fmax2(large, fabs(a));
58
	    if (do_rand) large = fmax2(large, fabs(a));
59
	}
59
	}
60
	if (isna) { maxes[r] = NA_INTEGER; continue; }
60
	if (isna) { maxes[r] = NA_INTEGER; continue; }
61
 
61
 
Line 69... Line 69...
69
		if (b > a + tol) { /* tol could be zero */
69
		if (b > a + tol) { /* tol could be zero */
70
		    a = b; m = c;
70
		    a = b; m = c;
71
		    ntie = 1;
71
		    ntie = 1;
72
		} else if (b >= a - tol) { /* b ~= current max. a */
72
		} else if (b >= a - tol) { /* b ~= current max. a */
73
		    ntie++;
73
		    ntie++;
74
		    if (!used_random) { GetRNGstate(); used_random = TRUE; }
74
		    if (!used_random) { GetRNGstate(); used_random = true; }
75
		    if (ntie * unif_rand() < 1.) m = c;
75
		    if (ntie * unif_rand() < 1.) m = c;
76
		}
76
		}
77
	    }
77
	    }
78
	} else {
78
	} else {
79
	    if(*ties_meth == 2) /* return the *first* max if there are ties */
79
	    if(*ties_meth == 2) /* return the *first* max if there are ties */