GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
basic.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <grass/arraystats.h>
3 
4 
5 /*provides basic univar stats */
6 void AS_basic_stats(double *data, int count, struct GASTATS *stats)
7 {
8  int i = 1;
9  double sum = 0, sumsq = 0, sumabs = 0;
10  double dev = 0, dev2 = 0;
11 
12  stats->count = count;
13  stats->min = data[0];
14  stats->max = data[count - 1];
15 
16  for (i = 0; i < count; i++) {
17  sum += data[i];
18  sumabs += fabs(data[i]);
19  sumsq += data[i] * data[i];
20  }
21  stats->sum = sum;
22  stats->sumabs = sumabs;
23  stats->sumsq = sumsq;
24 
25  stats->mean = stats->sum / stats->count;
26  stats->meanabs = stats->sumabs / stats->count;
27  for (i = 0; i < count; i++) {
28  dev2 = dev2 + (data[i] - stats->mean) * (data[i] - stats->mean);
29  dev = dev + (data[i] - stats->mean);
30  }
31 
32 
33  stats->var = (dev2 - (dev * dev / stats->count)) / stats->count;
34  stats->stdev = sqrt(stats->var);
35 
36  return;
37 }
38 
39 
40 void AS_eqdrt(double vectx[], double vecty[], int i1, int i2, double *vabc)
41 {
42  double bn = 0, bd = 0, x1 = 0, y1 = 0;
43 
44  vabc[0] = 0;
45  vabc[1] = 0;
46  vabc[2] = 0;
47  if (i1 == 0) {
48  x1 = 0;
49  y1 = 0;
50  }
51  else {
52  x1 = vectx[i1];
53  y1 = vecty[i1];
54  }
55  bn = y1 - vecty[i2];
56  bd = x1 - vectx[i2];
57  if (bd != 0) {
58  vabc[1] = bn / bd;
59  vabc[0] = y1 - vabc[1] * x1;
60  return;
61  }
62  if (bn != 0)
63  vabc[2] = x1;
64  else
65  G_debug(3, "Points are equal\n");
66  return;
67 }
double mean
Definition: arraystats.h:19
double stdev
Definition: arraystats.h:22
int count
double count
Definition: arraystats.h:13
double sumabs
Definition: arraystats.h:18
double sum
Definition: arraystats.h:16
double max
Definition: arraystats.h:15
void AS_basic_stats(double *data, int count, struct GASTATS *stats)
Definition: basic.c:6
double min
Definition: arraystats.h:14
double var
Definition: arraystats.h:21
double meanabs
Definition: arraystats.h:20
double sumsq
Definition: arraystats.h:17
void AS_eqdrt(double vectx[], double vecty[], int i1, int i2, double *vabc)
Definition: basic.c:40
int G_debug(int, const char *,...) __attribute__((format(printf