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