OwnMath.h
Go to the documentation of this file.
1 // this is a -*- C++ -*- file
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 //
5 // You received this file as part of MCA2
6 // Modular Controller Architecture Version 2
7 //
8 // Copyright (C) FZI Forschungszentrum Informatik Karlsruhe
9 //
10 // This program is free software; you mild_base_driving redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2
13 // of the License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 //
24 // -- END LICENSE BLOCK ------------------------------------------------
25 
26 //----------------------------------------------------------------------
41 //----------------------------------------------------------------------
42 
43 #ifndef _OwnMath_h_
44 #define _OwnMath_h_
45 //----------------------------------------------------------------------
46 // Includes
47 //----------------------------------------------------------------------
48 #include "KernelMath.h"
49 
50 #ifdef _SYSTEM_WIN32_
51 # define M_E 2.7182818284590452354 /* e */
52 # define M_LOG2E 1.4426950408889634074 /* log_2 e */
53 # define M_LOG10E 0.43429448190325182765 /* log_10 e */
54 # define M_LN2 0.69314718055994530942 /* log_e 2 */
55 # define M_LN10 2.30258509299404568402 /* log_e 10 */
56 # define M_PI 3.14159265358979323846 /* pi */
57 # define M_PI_2 1.57079632679489661923 /* pi/2 */
58 # define M_PI_4 0.78539816339744830962 /* pi/4 */
59 # define M_1_PI 0.31830988618379067154 /* 1/pi */
60 # define M_2_PI 0.63661977236758134308 /* 2/pi */
61 # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
62 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
63 # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
64 #endif
65 
66 #include <stdlib.h>
67 #include <math.h>
68 
69 double FastSin(double x);
70 double FastCos(double x);
71 
72 //--------------------------------------------------------------------------------------------------
73 // Mathematical Function
74 //--------------------------------------------------------------------------------------------------
75 
76 
77 
80 inline double Mod(double a, double b)
81 {
82  double c = fmod(a, b);
83  return c >= 0 ? c : c + b;
84 }
85 
88 inline double Mod(double a, int b)
89 {
90  return Mod(a, double(b));
91 }
92 
95 inline int Mod(int a, int b)
96 {
97  int c = a % b;
98  return c >= 0 ? c : c + b;
99 }
100 
103 inline long int Mod(long int a, long int b)
104 {
105  int c = a % b;
106  return c >= 0 ? c : c + b;
107 }
108 
111 inline int Sgn(double a)
112 {
113  return a >= 0 ? 1 : -1;
114 }
115 
118 inline int Sgn(long double a)
119 {
120  return a >= 0 ? 1 : -1;
121 }
122 
125 inline int Sgn0(double a)
126 {
127  return a > 0 ? 1 : a < 0 ? -1 : 0;
128 }
129 
132 inline int Sgn0(long double a)
133 {
134  return a > 0 ? 1 : a < 0 ? -1 : 0;
135 }
136 
139 inline int Sgn(long a)
140 {
141  return a >= 0 ? 1 : -1;
142 }
143 
146 inline int Sgn0(long a)
147 {
148  return a > 0 ? 1 : a < 0 ? -1 : 0;
149 }
150 
153 inline double Rad2Deg(double a)
154 {
155  static double factor = 180 / M_PI;
156  return a*factor;
157 }
158 
161 inline double Deg2Rad(double a)
162 {
163  static double factor = M_PI / 180;
164  return a*factor;
165 }
166 
168 inline double NormalizeAngleUnsigned(const double angle)
169 {
170  return Mod(angle, 2*M_PI);
171 };
172 
174 inline double NormalizeAngleSigned(const double angle)
175 {
176  return Mod(angle + M_PI, 2*M_PI) - M_PI;
177 };
178 
179 
181 //@Zacharias: inherente Annahme angle>-180
182 inline double NormalizeAngleInDegreeSigned(const double angle)
183 {
184  return Mod(angle + 180, 360) - 180;
185 };
186 
188 inline int Pow(int a, int b)
189 {
190  int ret;
191  for (ret = 1; b > 0; --b)
192  ret *= a;
193  return ret;
194 }
196 inline double Round(double value, int precision = 0)
197 {
198  double fact = pow(1e1, precision);
199  return floor(((value*fact) + 0.5)) / fact;
200 }
201 
203 inline double Ceil(double value, int precision = 0)
204 {
205  double fact = pow(1e1, precision);
206  return ceil(value*fact) / fact;
207 }
208 
210 inline double Floor(double value, int precision = 0)
211 {
212  double fact = pow(1e1, precision);
213  return floor(value*fact) / fact;
214 }
215 
217 inline int* Add(int *a, int*b, int size)
218 {
219  int i;
220  for (i = 0; i < size; i++)
221  a[i] += b[i];
222  return a;
223 }
224 
226 inline int* Sum(int *a, int*b, int*c, int size)
227 {
228  int i;
229  for (i = 0; i < size; i++)
230  c[i] = a[i] + b[i];
231  return c;
232 }
233 
235 inline int* Div(int* a, int b, int size)
236 {
237  int i;
238  for (i = 0; i < size; i++)
239  a[i] /= b;
240  return a;
241 }
242 
244 inline int* Div(int* a, int b, int*c, int size)
245 {
246  int i;
247  for (i = 0; i < size; i++)
248  c[i] = a[i] / b;
249  return c;
250 }
251 
253 template<typename T>
254 inline T Sqr(T a)
255 {
256  return a*a;
257 }
258 
260 inline double Sqrt(double a)
261 {
262  return sqrt(a);
263 }
264 
269 inline double RandomUniformAbs()
270 {
271  return float(rand()) / RAND_MAX;
272 }
273 
278 inline double RandomUniform()
279 {
280  return 2*float(rand()) / RAND_MAX - 1;
281 }
282 
287 inline double RandomNormal(double variance)
288 {
289  double r1 = (float((rand()) + 1) / (float(RAND_MAX) + 1)); // intervall ]0,1] wegen log(0)
290  double r2 = float(rand()) / RAND_MAX; // intervall [0,1]
291 #ifdef MATH_USE_TABLE_LOOKUP
292  return variance*sqrt(-2*log(r1))*FastSin(2*M_PI*r2);
293 #else
294  return variance*sqrt(-2*log(r1))*sin(2*M_PI*r2);
295 #endif
296  // Übrigens ergibt (man beachte den cosinus anstelle des sinus)
297  // eine weitere Zufallsvariable, die von der ersten unabhängig ist:
298  // sqrt(-2*variance*log(r1))*cos(2*M_PI*r2);
299 }
300 
301 
303 
308 inline int ipow(int base, int exponent)
309 {
310  if (base <= 0)
311  return 0;
312  if (exponent < 0)
313  return 0;
314  else
315  {
316  int ret = 1;
317  for (int i = 1; i <= exponent; i++)
318  ret *= base;
319  return ret;
320  }
321 }
322 
324 
327 inline int log2upw(int value)
328 {
329  int res = 1;
330  while (value > 2)
331  {
332  value = value / 2;
333  res++;
334  }
335  return res;
336 }
337 
339 
342 inline int log8upw(int value)
343 {
344  int res = 1;
345  while (value > 8)
346  {
347  value = value / 8;
348  res++;
349  }
350  return res;
351 }
352 
358 inline void SinCos(const double x, double &sx, double &cx)
359 {
360 #if defined _SYSTEM_LINUX_ && ! defined _SYSTEM_DARWIN_
361  sincos(x, &sx, &cx);
362 #else
363  sx = sin(x);
364  cx = cos(x);
365 #endif
366 }
367 
369 {
370 public:
372  {
373  int i = 0;
374  double x = 0;
375  double increment = 2. * M_PI / double(cTABLE_SIZE);
376  for (; i < cTABLE_SIZE; ++i, x += increment)
377  {
378  m_sin_table[i] = ::sin(x);
379  m_cos_table[i] = ::cos(x);
380  }
381  }
382 
383  double Sin(int index)
384  {
385  return m_sin_table[index & cTABLE_INDEX_MASK];
386  }
387 
388  double Cos(int index)
389  {
390  return m_cos_table[index & cTABLE_INDEX_MASK];
391  }
392 
393  static const int cTABLE_SIZE = 16384;
394  static const int cTABLE_INDEX_MASK;
395  static const double cSCALE_FACTOR;
396 
397 private:
400 };
401 
403 
404 inline double FastSin(double x)
405 {
406  double scaled_x = sin_cos_lookup_table.cSCALE_FACTOR * x;
407  double table_index = ::floor(scaled_x);
408  double y1 = sin_cos_lookup_table.Sin(int(table_index));
409  double y2 = sin_cos_lookup_table.Sin(int(table_index) + 1);
410  return y1 + (y2 - y1) * (scaled_x - table_index);
411 }
412 
413 inline double FastCos(double x)
414 {
415  double scaled_x = sin_cos_lookup_table.cSCALE_FACTOR * x;
416  double table_index = ::floor(scaled_x);
417  double y1 = sin_cos_lookup_table.Cos(int(table_index));
418  double y2 = sin_cos_lookup_table.Cos(int(table_index) + 1);
419  return y1 + (y2 - y1) * (scaled_x - table_index);
420 }
421 
430 inline bool FloatIsEqual(const double a, const double b, const double epsilon = 1e-10)
431 {
432  return fabs(a -b) < epsilon;
433 }
434 
435 
436 #endif
double epsilon
double FastCos(double x)
Definition: OwnMath.h:413
void SinCos(const double x, double &sx, double &cx)
Definition: OwnMath.h:358
double Ceil(double value, int precision=0)
Definition: OwnMath.h:203
int * Sum(int *a, int *b, int *c, int size)
Definition: OwnMath.h:226
double FastSin(double x)
Definition: OwnMath.h:404
tSinCosLookupTable sin_cos_lookup_table
Definition: OwnMath.cpp:39
double RandomUniformAbs()
Definition: OwnMath.h:269
int ipow(int base, int exponent)
An integer pow-function.
Definition: OwnMath.h:308
double Round(double value, int precision=0)
Definition: OwnMath.h:196
double NormalizeAngleUnsigned(const double angle)
Definition: OwnMath.h:168
int log8upw(int value)
calculates log_8, rounded upward
Definition: OwnMath.h:342
double Cos(int index)
Definition: OwnMath.h:388
Mathematical functions.
bool FloatIsEqual(const double a, const double b, const double epsilon=1e-10)
Definition: OwnMath.h:430
double m_cos_table[cTABLE_SIZE]
Definition: OwnMath.h:399
int Pow(int a, int b)
Definition: OwnMath.h:188
int * Div(int *a, int b, int size)
Definition: OwnMath.h:235
static const int cTABLE_SIZE
Definition: OwnMath.h:393
TFSIMD_FORCE_INLINE const tfScalar & x() const
double Sqrt(double a)
Definition: OwnMath.h:260
int * Add(int *a, int *b, int size)
Definition: OwnMath.h:217
double NormalizeAngleSigned(const double angle)
Definition: OwnMath.h:174
double Sin(int index)
Definition: OwnMath.h:383
int Sgn(double a)
Definition: OwnMath.h:111
static const int cTABLE_INDEX_MASK
Definition: OwnMath.h:394
double Mod(double a, double b)
Definition: OwnMath.h:80
static const double cSCALE_FACTOR
Definition: OwnMath.h:395
double NormalizeAngleInDegreeSigned(const double angle)
Definition: OwnMath.h:182
double Deg2Rad(double a)
Definition: OwnMath.h:161
double RandomUniform()
Definition: OwnMath.h:278
int log2upw(int value)
calculates log_2, rounded upward
Definition: OwnMath.h:327
double Floor(double value, int precision=0)
Definition: OwnMath.h:210
double m_sin_table[cTABLE_SIZE]
Definition: OwnMath.h:398
int Sgn0(double a)
Definition: OwnMath.h:125
double RandomNormal(double variance)
Definition: OwnMath.h:287
double Rad2Deg(double a)
Definition: OwnMath.h:153
T Sqr(T a)
Definition: OwnMath.h:254


asr_mild_base_driving
Author(s): Aumann Florian, Borella Jocelyn, Dehmani Souheil, Marek Felix, Meißner Pascal, Reckling Reno
autogenerated on Mon Jun 10 2019 12:43:40