GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_var.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 
3 void c_var(DCELL * result, DCELL * values, int n, const void *closure)
4 {
5  DCELL sum, ave, sumsq;
6  int count;
7  int i;
8 
9  sum = 0.0;
10  count = 0;
11 
12  for (i = 0; i < n; i++) {
13  if (G_is_d_null_value(&values[i]))
14  continue;
15 
16  sum += values[i];
17  count++;
18  }
19 
20  if (count == 0) {
21  G_set_d_null_value(result, 1);
22  return;
23  }
24 
25  ave = sum / count;
26 
27  sumsq = 0;
28 
29  for (i = 0; i < n; i++) {
30  DCELL d;
31 
32  if (G_is_d_null_value(&values[i]))
33  continue;
34 
35  d = values[i] - ave;
36  sumsq += d * d;
37  }
38 
39  *result = sumsq / count;
40 }
41 
42 void w_var(DCELL * result, DCELL(*values)[2], int n, const void *closure)
43 {
44  DCELL sum, ave, sumsq;
45  int count;
46  int i;
47 
48  sum = 0.0;
49  count = 0;
50 
51  for (i = 0; i < n; i++) {
52  if (G_is_d_null_value(&values[i][0]))
53  continue;
54 
55  sum += values[i][0] * values[i][1];
56  count += values[i][1];
57  }
58 
59  if (count == 0) {
60  G_set_d_null_value(result, 1);
61  return;
62  }
63 
64  ave = sum / count;
65 
66  sumsq = 0;
67 
68  for (i = 0; i < n; i++) {
69  DCELL d;
70 
71  if (G_is_d_null_value(&values[i][0]))
72  continue;
73 
74  d = values[i][0] - ave;
75  sumsq += d * d * values[i][1];
76  }
77 
78  *result = sumsq / count;
79 }
void w_var(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_var.c:42
void G_set_d_null_value(DCELL *dcellVals, int numVals)
Definition: null_val.c:176
void c_var(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_var.c:3
int count
int G_is_d_null_value(const DCELL *dcellVal)
Returns 1 if dcell is NULL, 0 otherwise. This will test if the value dcell is a NaN. Same test as in G_is_f_null_value().
Definition: null_val.c:306
int n
Definition: dataquad.c:291