Debian Codegraph @

Xpjproject/pjlib/include/pj/math.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #ifndef __PJ_MATH_H__
21 #define __PJ_MATH_H__
22
23 /**
24 * @file math.h
25 * @brief Mathematics and Statistics.
26 */
27
28 #include <pj/string.h>
29 #include <pj/compat/high_precision.h>
30
31 PJ_BEGIN_DECL
32
33 /**
34 * @defgroup pj_math Mathematics and Statistics
35 * @ingroup PJ_MISC
36 * @{
37 *
38 * Provides common mathematics constants and operations, and also standard
39 * statistics calculation (min, max, mean, standard deviation). Statistics
40 * calculation is done in realtime (statistics state is updated on time each
41 * new sample comes).
42 */
43
44 /**
45 * Mathematical constants
46 */
47 /** pi */
48 #define PJ_PI 3.14159265358979323846 /* pi */
49 /** 1/pi */
50 #define PJ_1_PI 0.318309886183790671538 /* 1/pi */
51
52 /**
53 * Mathematical macros
54 */
55 /** Get the absolute value */
56 #define PJ_ABS(x) ((x) > 0 ? (x) : -(x))
57
58 /** Get the maximum of two values */
59 #define PJ_MAX(x, y) ((x) > (y)? (x) : (y))
60
61 /** Get the minimum of two values */
62 #define PJ_MIN(x, y) ((x) < (y)? (x) : (y))
63
64 /**
65 * This structure describes statistics state.
66 */
67 typedef struct pj_math_stat
68 {
69 int n; /**< number of samples */
70 int max; /**< maximum value */
71 int min; /**< minimum value */
72 int last; /**< last value */
73 int mean; /**< mean */
74
75 /* Private members */
76 #if PJ_HAS_FLOATING_POINT
77 float fmean_; /**< mean(floating point) */
78 #else
79 int mean_res_; /**< mean residue */
80 #endif
81 pj_highprec_t m2_; /**< variance * n */
82 } pj_math_stat;
83
84 /**
85 * Calculate integer square root of an integer.
86 *
87 * @param i Integer to be calculated.
88 *
89 * @return Square root result.
90 */
91 PJ_INLINE(unsigned) pj_isqrt(unsigned i)
92 {
93 unsigned res = 1, prev;
94
95 /* Rough guess, calculate half bit of input */
96 prev = i >> 2;
97 while (prev) {
98 prev >>= 2;
99 res <<= 1;
100 }
101
102 /* Babilonian method */
103 do {
104 prev = res;
105 res = (prev + i/prev) >> 1;
106 } while ((prev+res)>>1 != res);
107
108 return res;
109 }
110
111 /**
112 * Initialize statistics state.
113 *
114 * @param stat Statistic state.
115 */
116 PJ_INLINE(void) pj_math_stat_init(pj_math_stat *stat)
117 {
118 pj_bzero(stat, sizeof(pj_math_stat));
119 }
120
121 /**
122 * Update statistics state as a new sample comes.
123 *
124 * @param stat Statistic state.
125 * @param val The new sample data.
126 */
127 PJ_INLINE(void) pj_math_stat_update(pj_math_stat *stat, int val)
128 {
129 #if PJ_HAS_FLOATING_POINT
130 float delta;
131 #else
132 int delta;
133 #endif
134
135 stat->last = val;
136
137 if (stat->n++) {
138 if (stat->min > val)
139 stat->min = val;
140 if (stat->max < val)
141 stat->max = val;
142 } else {
143 stat->min = stat->max = val;
144 }
145
146 #if PJ_HAS_FLOATING_POINT
147 delta = (float)val - stat->fmean_;
148 stat->fmean_ += delta/(float)stat->n;
149
150 /* Return mean value with 'rounding' */
151 stat->mean = (int) (stat->fmean_ + 0.5);
152
153 stat->m2_ += (int)(delta * ((float)val-stat->fmean_));
154 #else
155 delta = val - stat->mean;
156 stat->mean += delta/stat->n;
157 stat->mean_res_ += delta % stat->n;
158 if (stat->mean_res_ >= stat->n) {
159 ++stat->mean;
160 stat->mean_res_ -= stat->n;
161 } else if (stat->mean_res_ <= -stat->n) {
162 --stat->mean;
163 stat->mean_res_ += stat->n;
164 }
165
166 stat->m2_ += delta * (val-stat->mean);
167 #endif
168 }
169
170 /**
171 * Get the standard deviation of specified statistics state.
172 *
173 * @param stat Statistic state.
174 *
175 * @return The standard deviation.
176 */
177 PJ_INLINE(unsigned) pj_math_stat_get_stddev(const pj_math_stat *stat)
178 {
179 if (stat->n == 0) return 0;
180 return (pj_isqrt((unsigned)(stat->m2_/stat->n)));
181 }
182
183 /**
184 * Set the standard deviation of statistics state. This is useful when
185 * the statistic state is operated in 'read-only' mode as a storage of
186 * statistical data.
187 *
188 * @param stat Statistic state.
189 *
190 * @param dev The standard deviation.
191 */
192 PJ_INLINE(void) pj_math_stat_set_stddev(pj_math_stat *stat, unsigned dev)
193 {
194 if (stat->n == 0)
195 stat->n = 1;
196 stat->m2_ = dev*dev*stat->n;
197 }
198
199 /** @} */
200
201 PJ_END_DECL
202
203 #endif /* __PJ_MATH_H__ */