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