3 #include <grass/glocale.h>
4 #include <grass/arraystats.h>
14 finfo =
class_stdev(data, nrec, *nbreaks, classbreaks);
16 finfo =
class_quant(data, nrec, *nbreaks, classbreaks);
21 G_fatal_error(_(
"Discont algorithm currently not available because of bugs"));
26 G_fatal_error(_(
"%s: Error in classification algorithm"), algo);
38 max = data[count - 1];
40 step = (max -
min) / (nbreaks + 1);
42 for (i = 0; i < nbreaks; i++)
43 classbreaks[i] = min + (step * (i + 1));
57 nbclass = nbreaks + 1;
59 if (nbclass % 2 == 1) {
64 if (((stats.mean + stats.stdev * scale / 2) +
65 (stats.stdev * scale * (nbclass / 2 - 1)) > stats.max) ||
66 ((stats.mean - stats.stdev * scale / 2) -
67 (stats.stdev * scale * (nbclass / 2 - 1)) < stats.min))
74 for (i = 0; i < nbreaks / 2; i++)
76 (stats.mean - stats.stdev * scale / 2) -
77 stats.stdev * scale * (nbreaks / 2 - (i + 1));
79 for (i = i; i < nbreaks; i++)
81 (stats.mean + stats.stdev * scale / 2) +
82 stats.stdev * scale * (i - nbreaks / 2);
90 if (((stats.mean) + (stats.stdev * scale * (nbclass / 2 - 1)) >
92 ((stats.mean) - (stats.stdev * scale * (nbclass / 2 - 1)) <
100 for (i = 0; i <= nbreaks / 2; i++)
102 stats.mean - stats.stdev * scale * (nbreaks / 2 - i);
104 for (i = i; i < nbreaks; i++)
106 stats.mean + stats.stdev * scale * (i - nbreaks / 2);
116 step = count / (nbreaks + 1);
118 for (i = 0; i < nbreaks; i++)
119 classbreaks[i] = data[step * (i + 1)];
129 struct GASTATS stats;
132 nbclass = *nbreaks + 1;
134 lequi = G_malloc(*nbreaks *
sizeof(
double));
144 else if (nbclass == 3) {
148 else if (nbclass == 4) {
153 else if (nbclass == 5) {
159 else if (nbclass == 6) {
166 else if (nbclass == 7) {
174 else if (nbclass == 8) {
183 else if (nbclass == 9) {
193 else if (nbclass == 10) {
206 (
"Equiprobable classbreaks currently limited to 10 classes");
213 for (i = 0; i < *nbreaks; i++) {
214 if ((lequi[i] * stats.stdev + stats.mean) >= stats.min &&
215 (lequi[i] * stats.stdev) + stats.mean <= stats.max) {
220 if (j < (*nbreaks)) {
221 G_warning(_(
"There are classbreaks outside the range min-max. Number of classes reduced to %i, but using probabilities for %i classes."),
222 j + 1, *nbreaks + 1);
223 G_realloc(classbreaks, j *
sizeof(
double));
224 for (i = 0; i < j; i++)
229 for (i = 0; i < *nbreaks; i++) {
230 if ((lequi[i] * stats.stdev + stats.mean) >= stats.min &&
231 (lequi[i] * stats.stdev) + stats.mean <= stats.max) {
232 classbreaks[j] = lequi[i] * stats.stdev + stats.mean;
248 double *no, *zz, *nz, *xn, *co;
251 double min = 0,
max = 0, rangemax = 0;
253 double rangemin = 0, xlim = 0;
254 double dmax = 0.0, d2 = 0.0, dd = 0.0, p = 0.0;
255 int nf = 0, nmax = 0;
258 double den = 0, d = 0;
261 int nff = 0, jj = 0, no1 = 0, no2 = 0;
262 double f = 0, xt1 = 0, xt2 = 0, chi2 = 1000.0, xnj_1 = 0, xj_1 = 0;
268 nbclass = nbreaks + 1;
270 num = G_malloc((nbclass + 1) *
sizeof(
int));
271 no = G_malloc((nbclass + 1) *
sizeof(
double));
272 zz = G_malloc((nbclass + 1) *
sizeof(
double));
273 nz = G_malloc(3 *
sizeof(
double));
274 xn = G_malloc((n + 1) *
sizeof(
double));
275 co = G_malloc((nbclass + 1) *
sizeof(
double));
278 x = G_malloc((n + 1) *
sizeof(
double));
283 max = data[count - 1];
284 for (i = 1; i <=
n; i++)
290 for (i = 2; i <=
n; i++) {
291 if (x[i] != x[i - 1] && x[i] - x[i - 1] < rangemin)
292 rangemin = x[i] - x[i - 1];
298 for (i = 1; i <=
n; i++) {
299 x[i] = (x[i] -
min) / rangemax;
300 xn[i] = i / (double)n;
302 xlim = rangemin / rangemax;
303 rangemin = rangemin / 2.0;
307 abc = G_malloc(3 *
sizeof(
double));
310 for (i = 1; i <= nbclass; i++) {
317 for (j = 1; j <= i; j++) {
321 eqdrt(x, xn, nd, nf, abc);
322 den = sqrt(pow(abc[1], 2) + 1.0);
325 for (k = nd; k <= nf; k++) {
327 d = fabs((-1.0 * abc[1] * x[k]) + xn[k] - abc[0]) / den;
329 d = fabs(x[k] - abc[2]);
331 if (x[k] - x[nd] < xlim)
333 if (x[nf] - x[k] < xlim)
341 if (x[nf] != x[nd]) {
343 co[j] = (xn[nf] - xn[nd]) / (x[nf] - x[nd]);
345 co[j] = (xn[nf]) / (x[nf]);
351 for (j = 1; j <= i; j++) {
353 zz[j] = x[num[j]] * rangemax +
min;
356 if (co[j] > co[j + 1]) {
357 zz[j] = zz[j] + rangemin;
360 zz[j] = zz[j] - rangemin;
365 for (j = 1; j <=
im; j++) {
367 no[ji] -= no[ji - 1];
375 for (j = 1; j <= i; j++) {
377 if (num[jj - 1] < nmax) {
382 num[jj] = num[jj - 1];
393 xnj_1 = xn[num[jj - 1]];
394 xj_1 = x[num[jj - 1]];
396 no1 = (xn[num[jj]] - xnj_1) * n;
397 no2 = (xn[num[jj + 1]] - xn[num[jj]]) * n;
398 f = (xn[num[jj + 1]] - xnj_1) / (x[num[jj + 1]] - xj_1);
400 xt1 = (x[num[jj]] - xj_1) * f;
401 xt2 = (x[num[jj + 1]] - x[num[jj]]) * f;
403 xt2 = rangemin / 2.0 / rangemax * f;
406 else if (xt1 * xt2 == 0) {
407 xt1 = rangemin / 2.0 / rangemax * f;
412 if (chi2 > pow((
double)((no1 - no2) - (xt1 - xt2)), 2) / (xt1 + xt2))
413 chi2 = pow((
double)((no1 - no2) - (xt1 - xt2)), 2) / (xt1 + xt2);
418 for (j = 0; j <= (i - 1); j++)
419 classbreaks[j] = zz[j + 1];
425 double *classbreaks,
int *frequencies)
431 max = data[count - 1];
434 for (j = 0; j < nbreaks; j++) {
435 while (data[i] <= classbreaks[j]) {
442 for (i = i; i <
count; i++) {
443 frequencies[nbreaks]++;
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
double class_apply_algorithm(char *algo, double *data, int nrec, int *nbreaks, double *classbreaks)
void G_free(void *buf)
Free allocated memory.
void eqdrt(double vectx[], double vecty[], int i1, int i2, double *vabc)
int class_interval(double *data, int count, int nbreaks, double *classbreaks)
void basic_stats(double *data, int count, struct GASTATS *stats)
int class_equiprob(double *data, int count, int *nbreaks, double *classbreaks)
double class_discont(double *data, int count, int nbreaks, double *classbreaks)
double class_stdev(double *data, int count, int nbreaks, double *classbreaks)
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int class_quant(double *data, int count, int nbreaks, double *classbreaks)
int class_frequencies(double *data, int count, int nbreaks, double *classbreaks, int *frequencies)