GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
c_sep.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_sep.c
3 
4  \brief Cluster library - Separation
5 
6  (C) 2001-2009 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Original author CERL
12  */
13 
14 #include <math.h>
15 #include <grass/cluster.h>
16 
17 #define FAR ((double)-1.0)
18 
19 /*!
20  \brief ?
21 
22  \param C pointer to Cluster structure
23  \param class1 1st class
24  \param class2 2nd class
25  */
26 double I_cluster_separation(struct Cluster *C, int class1, int class2)
27 {
28  int band;
29  double q;
30  double d;
31  double var;
32  double a1, a2;
33  double n1, n2;
34  double m1, m2;
35  double s1, s2;
36 
37  if (C->count[class1] < 2)
38  return FAR;
39  if (C->count[class2] < 2)
40  return FAR;
41  n1 = (double)C->count[class1];
42  n2 = (double)C->count[class2];
43 
44  d = 0.0;
45  a1 = a2 = 0.0;
46  for (band = 0; band < C->nbands; band++) {
47  s1 = C->sum[band][class1];
48  s2 = C->sum[band][class2];
49  m1 = s1 / n1;
50  m2 = s2 / n2;
51  q = m1 - m2;
52  q = q * q;
53  d += q;
54 
55  var = C->sum2[band][class1] - (s1 * m1);
56  var /= n1 - 1;
57  if (var)
58  a1 += q / var;
59 
60  var = C->sum2[band][class2] - (s2 * m2);
61  var /= n2 - 1;
62  if (var)
63  a2 += q / var;
64  }
65  if (d == 0.0)
66  return d;
67 
68  if (a1 < 0 || a2 < 0)
69  return FAR;
70  if (a1)
71  a1 = sqrt(6 * d / a1);
72  if (a2)
73  a2 = sqrt(6 * d / a2);
74  q = a1 + a2;
75  if (q == 0.0)
76  return FAR;
77 
78  return (sqrt(d) / q);
79 }
#define FAR
Definition: c_sep.c:17
double I_cluster_separation(struct Cluster *C, int class1, int class2)
?
Definition: c_sep.c:26
float var(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance.
Definition: cluster.h:7
int * count
Definition: cluster.h:18
double ** sum2
Definition: cluster.h:22
double ** sum
Definition: cluster.h:20
int nbands
Definition: cluster.h:8