GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
n_tools.c
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * MODULE: Grass PDE Numerical Library
5 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
6 * soerengebbert <at> gmx <dot> de
7 *
8 * PURPOSE: Array management functions
9 * part of the gpde library
10 *
11 * COPYRIGHT: (C) 2000 by the GRASS Development Team
12 *
13 * This program is free software under the GNU General Public
14 * License (>=v2). Read the file COPYING that comes with GRASS
15 * for details.
16 *
17 *****************************************************************************/
18 
19 #include <math.h>
20 #include <grass/N_pde.h>
21 #include <grass/glocale.h>
22 
23 
24 /*!
25  * \brief Calculate the arithmetic mean of values a and b
26  *
27  * mean = (a+b)/2
28  *
29  * \param a double
30  * \param b double
31  * \return val double
32  * */
33 double N_calc_arith_mean(double a, double b)
34 {
35  double val = 0;
36 
37  val = (a + b) / 2.0;
38 
39  return val;
40 }
41 
42 /*!
43  * \brief Calculate the arithmetic mean of the values in vector a
44  * of size n
45  *
46  * n = [0 ... size[
47  * mean = (a[0] + a[1] + ... + a[n])/size
48  *
49  * \param a double * -- the value vector
50  * \param size int -- the size of the vector a
51  * \return val double
52  * */
53 double N_calc_arith_mean_n(double *a, int size)
54 {
55  double val = 0.0;
56  int i;
57 
58  for (i = 0; i < size; i++)
59  val += a[i];
60 
61  val = (val / (double)size);
62 
63  return val;
64 }
65 
66 
67 /*!
68  * \brief Calculate the geometrical mean of values a and b
69  *
70  * mean = sqrt(a*b)
71  *
72  * \param a double
73  * \param b double
74  * \return val double
75  * */
76 double N_calc_geom_mean(double a, double b)
77 {
78  double val = 0;
79 
80  val = sqrt(a * b);
81 
82  return val;
83 }
84 
85 /*!
86  * \brief Calculate the geometrical mean of the values in vector a
87  * of size n
88  *
89  * n = [0 ... size[
90  * mean = pow((a[0] * a[1] * ... * a[n]), 1.0/size)
91  *
92  * \param a double * -- the value vector
93  * \param size int -- the size of the vector a
94  * \return val double
95  * */
96 double N_calc_geom_mean_n(double *a, int size)
97 {
98  double val = 1;
99  int i;
100 
101  for (i = 0; i < size; i++)
102  val *= a[i];
103 
104  val = (double)pow((long double)val, (long double)1.0 / (long double)size);
105 
106  return val;
107 }
108 
109 
110 /*!
111  * \brief Calculate the harmonical mean of values a and b
112  *
113  * mean = 2*(a*b)/(a + b)
114  *
115  * \param a double
116  * \param b double
117  * \return val double -- if (a + b) == 0, a 0 is returned
118  * */
119 double N_calc_harmonic_mean(double a, double b)
120 {
121  double val = 0.0;
122 
123  if ((a + b) != 0)
124  val = 2.0 * (a * b) / (a + b);
125 
126  return val;
127 }
128 
129 /*!
130  * \brief Calculate the harmonical mean of the values in vector a
131  * of size n
132  *
133  * n = [0 ... size[
134  * mean = 1/(1/size *(1/a[0] + 1/a[1] + ... + 1/a[n]))
135  *
136  * \param a double * -- the value vector
137  * \param size int -- the size of the vector a
138  * \return val double -- if one division with 0 is detected, 0 will be returned
139  * */
140 double N_calc_harmonic_mean_n(double *a, int size)
141 {
142  double val = 0;
143  int i;
144 
145  for (i = 0; i < size; i++)
146  if (a[i] != 0.0)
147  val += 1.0 / a[i];
148  else
149  return 0.0;
150 
151  if (val == 0.0)
152  return 0.0;
153  else
154  val = 1.0 / (1.0 / (double)size * val);
155 
156  return val;
157 }
158 
159 
160 /*!
161  * \brief Calculate the quadratic mean of values a and b
162  *
163  * mean = sqrt((a*a + b*b)/2)
164  *
165  * \param a double
166  * \param b double
167  * \return val double
168  * */
169 double N_calc_quad_mean(double a, double b)
170 {
171  double val = 0.0;
172 
173  val = sqrt((a * a + b * b) / 2.0);
174 
175  return val;
176 }
177 
178 /*!
179  * \brief Calculate the quadratic mean of the values in vector a
180  * of size n
181  *
182  * n = [0 ... size[
183  * mean = sqrt((a[0]*a[0] + a[1]*a[1] + ... + a[n]*a[n])/size)
184  *
185  * \param a double * -- the value vector
186  * \param size int -- the size of the vector a
187  * \return val double
188  * */
189 double N_calc_quad_mean_n(double *a, int size)
190 {
191  double val = 0;
192  int i;
193 
194  for (i = 0; i < size; i++)
195  val += a[i] * a[i];
196 
197  val = sqrt(val / (double)size);
198 
199  return val;
200 }
double N_calc_quad_mean_n(double *a, int size)
Calculate the quadratic mean of the values in vector a of size n.
Definition: n_tools.c:189
double N_calc_arith_mean_n(double *a, int size)
Calculate the arithmetic mean of the values in vector a of size n.
Definition: n_tools.c:53
double N_calc_quad_mean(double a, double b)
Calculate the quadratic mean of values a and b.
Definition: n_tools.c:169
double N_calc_harmonic_mean_n(double *a, int size)
Calculate the harmonical mean of the values in vector a of size n.
Definition: n_tools.c:140
double N_calc_geom_mean(double a, double b)
Calculate the geometrical mean of values a and b.
Definition: n_tools.c:76
double N_calc_harmonic_mean(double a, double b)
Calculate the harmonical mean of values a and b.
Definition: n_tools.c:119
double N_calc_arith_mean(double a, double b)
Calculate the arithmetic mean of values a and b.
Definition: n_tools.c:33
double b
Definition: r_raster.c:39
double N_calc_geom_mean_n(double *a, int size)
Calculate the geometrical mean of the values in vector a of size n.
Definition: n_tools.c:96