LIBRCSC Docs
Documentation for HELIOS's BASE LIBRCSC library for RoboCup 2D Simulation League.
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
math_util.h
Go to the documentation of this file.
1// -*-c++-*-
2
8/*
9 *Copyright:
10
11 Copyright (C) Hidehisa AKIYAMA
12
13 This code is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27 *EndCopyright:
28 */
29
31
32#ifndef RCSC_MATH_UTIL_H
33#define RCSC_MATH_UTIL_H
34
35#include <algorithm>
36#include <cmath>
37
38namespace rcsc {
39
41constexpr double EPS = 1.0e-10;
42
43/*-------------------------------------------------------------------*/
51template < typename T >
52const T &
53bound( const T & low, const T & x, const T & high )
54{
55 return std::min( std::max( low, x ), high );
56}
57
58/*-------------------------------------------------------------------*/
66template < typename T >
67const T &
68min_max( const T & low, const T & x, const T & high )
69{
70 return std::min( std::max( low, x ), high );
71}
72
73/*-------------------------------------------------------------------*/
78template < typename T >
79T
80square( const T & x )
81{
82 return x * x;
83}
84
85/*-------------------------------------------------------------------*/
92inline
93double
94sign( const double & x )
95{
96 return x > 0.0 ? 1.0 : -1.0;
97}
98
99/*-------------------------------------------------------------------*/
106inline
107double
108round( const double & value,
109 const double & prec )
110{
111 return rint( value / prec ) * prec;
112}
113
114/*-------------------------------------------------------------------*/
124inline
125int
126quadratic_formula( const double & a,
127 const double & b,
128 const double & c,
129 double * sol1,
130 double * sol2 )
131{
132 double d = b * b - 4.0 * a * c;
133
134 // ignore small noise
135 if ( std::fabs( d ) < 0.001 )
136 {
137 if ( sol1 ) *sol1 = -b / ( 2.0 * a );
138 return 1;
139 }
140
141 if ( d < 0.0 )
142 {
143 return 0;
144 }
145
146 d = std::sqrt( d );
147 if ( sol1 ) *sol1 = (-b + d) / (2.0 * a);
148 if ( sol2 ) *sol2 = (-b - d) / (2.0 * a);
149 return 2;
150}
151
152/*-------------------------------------------------------------------*/
163inline
164double
165calc_sum_geom_series( const double & first_term,
166 const double & r,
167 const int len )
168{
169 // sum = f + fr + fr^2 + ... + fr^(n-1)
170 // sum * r = fr + fr^2 + ... + fr^(n-1) + fr^n
171 // sum * ( r - 1 ) = fr^n - f
172 // sum = f * ( r^n - 1.0 ) / ( r - 1 )
173 return first_term * ( ( std::pow( r, len ) - 1.0 ) / ( r - 1.0 ) );
174}
175
176
177/*-------------------------------------------------------------------*/
187inline
188double
189calc_sum_inf_geom_series( const double & first_term,
190 const double & r )
191{
192 if ( r < 0.0 || 1.0 <= r )
193 {
194 return 0.0;
195 }
196
197 // limit(n->inf, 0<r<1) sum = f * ( 1 - r^n ) / ( 1 - r )
198 return first_term / ( 1.0 - r );
199}
200
201/*-------------------------------------------------------------------*/
211inline
212double
213calc_first_term_geom_series( const double & sum,
214 const double & r,
215 const int len )
216{
217 // sum = f * ( 1 - r^n ) / ( 1 - r )
218 // f = sum * ( 1 - r ) / ( 1 - r^n )
219 return sum * ( 1.0 - r ) / ( 1.0 - std::pow( r, len ) );
220}
221
222/*-------------------------------------------------------------------*/
233inline
234double
236 const double & r )
237{
238 // limit(n->inf, 0<r<1) f = sum * ( 1 - r ) / ( 1 - r^n )
239 return sum * ( 1.0 - r );
240}
241
242/*-------------------------------------------------------------------*/
254inline
255double
256calc_first_term_geom_series_last( const double & last_term,
257 const double & sum,
258 const double & r )
259{
260 if ( std::fabs( last_term ) < 0.001 )
261 {
262 return sum * ( 1.0 - r );
263 }
264
265 // l + (l * 1/r) + ... + (l * 1/r^(n-1)) = sum
266 // (l * 1/r) + ... + (l * 1/r^(n-1)) + (l * 1/r^n) = sum * (1/r)
267 // l*(1/r^n) - l = sum * (1/r - 1)
268 // (1/r^n) = sum * (1/r - 1) / l + 1
269 double inverse = 1.0 / r;
270 double tmp = 1.0 + sum * (inverse - 1.0) / last_term;
271 if ( tmp < 0.001 )
272 {
273 return last_term;
274 }
275
276 //double len = std::log( tmp ) / std::log( inverse );
277 //return last_term * std::pow( inverse, len );
278 return last_term * std::pow( inverse, std::log( tmp ) / std::log( inverse ) );
279}
280
281/*-------------------------------------------------------------------*/
291inline
292double
293calc_length_geom_series( const double & first_term,
294 const double & sum,
295 const double & r )
296{
297 if ( first_term <= EPS
298 || sum < 0.0
299 || r <= EPS )
300 {
301 // cannot take the zero first term
302 // cannot take the negative sum
303 // cannot take the negative ratio
304 return -1.0;
305 }
306
307 if ( sum <= EPS )
308 {
309 // already there
310 return 0.0;
311 }
312
313 // f + fr + fr^2 + ... + fr^(n-1) = sum
314 // fr + fr^2 + ... + fr^(n-1) + fr^n = sum * r
315 // fr^n - f = sum * ( r - 1 )
316 // r^n = 1 + sum * ( r - 1 ) / f
317
318 double tmp = 1.0 + sum * ( r - 1.0 ) / first_term;
319 if ( tmp <= EPS )
320 {
321 return -1.0;
322 }
323 return std::log( tmp ) / std::log( r );
324}
325
326} // end namespace
327
328#endif
double calc_sum_inf_geom_series(const double &first_term, const double &r)
the sum of a infinity geometric series
Definition: math_util.h:189
double calc_length_geom_series(const double &first_term, const double &sum, const double &r)
caluculate the length of a geometric series
Definition: math_util.h:293
double calc_first_term_inf_geom_series(const double &sum, const double &r)
caluculate the first term value of a infinity geometric series.
Definition: math_util.h:235
double calc_first_term_geom_series(const double &sum, const double &r, const int len)
caluculate the first term value of a geometric series.
Definition: math_util.h:213
double round(const double &value, const double &prec)
round a floating point number with specified precision
Definition: math_util.h:108
constexpr double EPS
epsilon value
Definition: math_util.h:41
const T & bound(const T &low, const T &x, const T &high)
bound value within the range [low, high]
Definition: math_util.h:53
const T & min_max(const T &low, const T &x, const T &high)
bound value within the range [low, high] (same as rcsc::bound())
Definition: math_util.h:68
double sign(const double &x)
get a sign value
Definition: math_util.h:94
double calc_first_term_geom_series_last(const double &last_term, const double &sum, const double &r)
caluculate the first term value of a geometric series.
Definition: math_util.h:256
T square(const T &x)
calculate squared value.
Definition: math_util.h:80
double calc_sum_geom_series(const double &first_term, const double &r, const int len)
calculate the sum of a geometric series
Definition: math_util.h:165
int quadratic_formula(const double &a, const double &b, const double &c, double *sol1, double *sol2)
Quadratic Formulation (ax^2 + bx + c = 0)
Definition: math_util.h:126