The R Project SVN R

Rev

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

Rev 70000 Rev 87859
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
 *  Copyright (C) 2004   The R Core Team.
3
 *  Copyright (C) 2004-2025   The R Core Team.
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
Line 23... Line 23...
23
		  int *pmaxiter, int *nc, double *wss)
23
		  int *pmaxiter, int *nc, double *wss)
24
{
24
{
25
    int n = *pn, k = *pk, p = *pp, maxiter = *pmaxiter;
25
    int n = *pn, k = *pk, p = *pp, maxiter = *pmaxiter;
26
    int iter, i, j, c, it, inew = 0;
26
    int iter, i, j, c, it, inew = 0;
27
    double best, dd, tmp;
27
    double best, dd, tmp;
28
    Rboolean updated;
28
    bool updated;
29
 
29
 
30
    for(i = 0; i < n; i++) cl[i] = -1;
30
    for(i = 0; i < n; i++) cl[i] = -1;
31
    for(iter = 0; iter < maxiter; iter++) {
31
    for(iter = 0; iter < maxiter; iter++) {
32
	updated = FALSE;
32
	updated = false;
33
	for(i = 0; i < n; i++) {
33
	for(i = 0; i < n; i++) {
34
	    /* find nearest centre for each point */
34
	    /* find nearest centre for each point */
35
	    best = R_PosInf;
35
	    best = R_PosInf;
36
	    for(j = 0; j < k; j++) {
36
	    for(j = 0; j < k; j++) {
37
		dd = 0.0;
37
		dd = 0.0;
Line 43... Line 43...
43
		    best = dd;
43
		    best = dd;
44
		    inew = j+1;
44
		    inew = j+1;
45
		}
45
		}
46
	    }
46
	    }
47
	    if(cl[i] != inew) {
47
	    if(cl[i] != inew) {
48
		updated = TRUE;
48
		updated = true;
49
		cl[i] = inew;
49
		cl[i] = inew;
50
	    }
50
	    }
51
	}
51
	}
52
	if(!updated) break;
52
	if(!updated) break;
53
	/* update each centre */
53
	/* update each centre */
Line 75... Line 75...
75
		     int *cl, int *pmaxiter, int *nc, double *wss)
75
		     int *cl, int *pmaxiter, int *nc, double *wss)
76
{
76
{
77
    int n = *pn, k = *pk, p = *pp, maxiter = *pmaxiter;
77
    int n = *pn, k = *pk, p = *pp, maxiter = *pmaxiter;
78
    int iter, i, j, c, it, inew = 0, iold;
78
    int iter, i, j, c, it, inew = 0, iold;
79
    double best, dd, tmp;
79
    double best, dd, tmp;
80
    Rboolean updated;
80
    bool updated;
81
 
81
 
82
    /* first assign each point to the nearest cluster centre */
82
    /* first assign each point to the nearest cluster centre */
83
    for(i = 0; i < n; i++) {
83
    for(i = 0; i < n; i++) {
84
	best = R_PosInf;
84
	best = R_PosInf;
85
	for(j = 0; j < k; j++) {
85
	for(j = 0; j < k; j++) {
Line 103... Line 103...
103
	for(c = 0; c < p; c++) cen[it+c*k] += x[i+c*n];
103
	for(c = 0; c < p; c++) cen[it+c*k] += x[i+c*n];
104
    }
104
    }
105
    for(j = 0; j < k*p; j++) cen[j] /= nc[j % k];
105
    for(j = 0; j < k*p; j++) cen[j] /= nc[j % k];
106
 
106
 
107
    for(iter = 0; iter < maxiter; iter++) {
107
    for(iter = 0; iter < maxiter; iter++) {
108
	updated = FALSE;
108
	updated = false;
109
	for(i = 0; i < n; i++) {
109
	for(i = 0; i < n; i++) {
110
	    best = R_PosInf;
110
	    best = R_PosInf;
111
	    for(j = 0; j < k; j++) {
111
	    for(j = 0; j < k; j++) {
112
		dd = 0.0;
112
		dd = 0.0;
113
		for(c = 0; c < p; c++) {
113
		for(c = 0; c < p; c++) {
Line 118... Line 118...
118
		    best = dd;
118
		    best = dd;
119
		    inew = j;
119
		    inew = j;
120
		}
120
		}
121
	    }
121
	    }
122
	    if((iold = cl[i] - 1) != inew) {
122
	    if((iold = cl[i] - 1) != inew) {
123
		updated = TRUE;
123
		updated = true;
124
		cl[i] = inew + 1;
124
		cl[i] = inew + 1;
125
		nc[iold]--; nc[inew]++;
125
		nc[iold]--; nc[inew]++;
126
		/* update old and new cluster centres */
126
		/* update old and new cluster centres */
127
		for(c = 0; c < p; c++) {
127
		for(c = 0; c < p; c++) {
128
		    cen[iold+k*c] += (cen[iold+k*c] - x[i+n*c])/nc[iold];
128
		    cen[iold+k*c] += (cen[iold+k*c] - x[i+n*c])/nc[iold];