OwnMath.h
Go to the documentation of this file.
00001 // this is a -*- C++ -*- file
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 //
00005 // You received this file as part of MCA2
00006 // Modular Controller Architecture Version 2
00007 //
00008 // Copyright (C) FZI Forschungszentrum Informatik Karlsruhe
00009 //
00010 // This program is free software; you mild_base_driving redistribute it and/or
00011 // modify it under the terms of the GNU General Public License
00012 // as published by the Free Software Foundation; either version 2
00013 // of the License, or (at your option) any later version.
00014 //
00015 // This program is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 //
00024 // -- END LICENSE BLOCK ------------------------------------------------
00025 
00026 //----------------------------------------------------------------------
00041 //----------------------------------------------------------------------
00042 
00043 #ifndef _OwnMath_h_
00044 #define _OwnMath_h_
00045 //----------------------------------------------------------------------
00046 // Includes
00047 //----------------------------------------------------------------------
00048 #include "KernelMath.h"
00049 
00050 #ifdef _SYSTEM_WIN32_
00051 # define M_E  2.7182818284590452354 /* e */
00052 # define M_LOG2E 1.4426950408889634074 /* log_2 e */
00053 # define M_LOG10E 0.43429448190325182765 /* log_10 e */
00054 # define M_LN2  0.69314718055994530942 /* log_e 2 */
00055 # define M_LN10  2.30258509299404568402 /* log_e 10 */
00056 # define M_PI  3.14159265358979323846 /* pi */
00057 # define M_PI_2  1.57079632679489661923 /* pi/2 */
00058 # define M_PI_4  0.78539816339744830962 /* pi/4 */
00059 # define M_1_PI  0.31830988618379067154 /* 1/pi */
00060 # define M_2_PI  0.63661977236758134308 /* 2/pi */
00061 # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
00062 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
00063 # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
00064 #endif
00065 
00066 #include <stdlib.h>
00067 #include <math.h>
00068 
00069 double FastSin(double x);
00070 double FastCos(double x);
00071 
00072 //--------------------------------------------------------------------------------------------------
00073 //                                Mathematical Function
00074 //--------------------------------------------------------------------------------------------------
00075 
00076 
00077 
00080 inline double Mod(double a, double b)
00081 {
00082     double c = fmod(a, b);
00083     return c >= 0 ? c : c + b;
00084 }
00085 
00088 inline double Mod(double a, int b)
00089 {
00090     return Mod(a, double(b));
00091 }
00092 
00095 inline int Mod(int a, int b)
00096 {
00097     int c = a % b;
00098     return c >= 0 ? c : c + b;
00099 }
00100 
00103 inline long int Mod(long int a, long int b)
00104 {
00105     int c = a % b;
00106     return c >= 0 ? c : c + b;
00107 }
00108 
00111 inline int Sgn(double a)
00112 {
00113     return a >= 0 ? 1 : -1;
00114 }
00115 
00118 inline int Sgn(long double a)
00119 {
00120     return a >= 0 ? 1 : -1;
00121 }
00122 
00125 inline int Sgn0(double a)
00126 {
00127     return a > 0 ? 1 : a < 0 ? -1 : 0;
00128 }
00129 
00132 inline int Sgn0(long double a)
00133 {
00134     return a > 0 ? 1 : a < 0 ? -1 : 0;
00135 }
00136 
00139 inline int Sgn(long a)
00140 {
00141     return a >= 0 ? 1 : -1;
00142 }
00143 
00146 inline int Sgn0(long a)
00147 {
00148     return a > 0 ? 1 : a < 0 ? -1 : 0;
00149 }
00150 
00153 inline double Rad2Deg(double a)
00154 {
00155     static double factor = 180 / M_PI;
00156     return a*factor;
00157 }
00158 
00161 inline double Deg2Rad(double a)
00162 {
00163     static double factor = M_PI / 180;
00164     return a*factor;
00165 }
00166 
00168 inline double NormalizeAngleUnsigned(const double angle)
00169 {
00170     return Mod(angle, 2*M_PI);
00171 };
00172 
00174 inline double NormalizeAngleSigned(const double angle)
00175 {
00176     return Mod(angle + M_PI, 2*M_PI) - M_PI;
00177 };
00178 
00179 
00181 //@Zacharias: inherente Annahme angle>-180
00182 inline double NormalizeAngleInDegreeSigned(const double angle)
00183 {
00184     return Mod(angle + 180, 360) - 180;
00185 };
00186 
00188 inline int Pow(int a, int b)
00189 {
00190     int ret;
00191     for (ret = 1; b > 0; --b)
00192         ret *= a;
00193     return ret;
00194 }
00196 inline double Round(double value, int precision = 0)
00197 {
00198     double fact = pow(1e1, precision);
00199     return floor(((value*fact) + 0.5)) / fact;
00200 }
00201 
00203 inline double Ceil(double value, int precision = 0)
00204 {
00205     double fact = pow(1e1, precision);
00206     return ceil(value*fact) / fact;
00207 }
00208 
00210 inline double Floor(double value, int precision = 0)
00211 {
00212     double fact = pow(1e1, precision);
00213     return floor(value*fact) / fact;
00214 }
00215 
00217 inline int* Add(int *a, int*b, int size)
00218 {
00219     int i;
00220     for (i = 0; i < size; i++)
00221         a[i] += b[i];
00222     return a;
00223 }
00224 
00226 inline int* Sum(int *a, int*b, int*c, int size)
00227 {
00228     int i;
00229     for (i = 0; i < size; i++)
00230         c[i] = a[i] + b[i];
00231     return c;
00232 }
00233 
00235 inline int* Div(int* a, int b, int size)
00236 {
00237     int i;
00238     for (i = 0; i < size; i++)
00239         a[i] /= b;
00240     return a;
00241 }
00242 
00244 inline int* Div(int* a, int b, int*c, int size)
00245 {
00246     int i;
00247     for (i = 0; i < size; i++)
00248         c[i] = a[i] / b;
00249     return c;
00250 }
00251 
00253 template<typename T>
00254 inline T Sqr(T a)
00255 {
00256     return a*a;
00257 }
00258 
00260 inline double Sqrt(double a)
00261 {
00262     return sqrt(a);
00263 }
00264 
00269 inline double RandomUniformAbs()
00270 {
00271     return float(rand()) / RAND_MAX;
00272 }
00273 
00278 inline double RandomUniform()
00279 {
00280     return 2*float(rand()) / RAND_MAX - 1;
00281 }
00282 
00287 inline double RandomNormal(double variance)
00288 {
00289     double r1 = (float((rand()) + 1) / (float(RAND_MAX) + 1)); // intervall ]0,1] wegen log(0)
00290     double r2 = float(rand()) / RAND_MAX;    // intervall [0,1]
00291 #ifdef MATH_USE_TABLE_LOOKUP
00292     return variance*sqrt(-2*log(r1))*FastSin(2*M_PI*r2);
00293 #else
00294     return variance*sqrt(-2*log(r1))*sin(2*M_PI*r2);
00295 #endif
00296     // Übrigens ergibt (man beachte den cosinus anstelle des sinus)
00297     // eine weitere Zufallsvariable, die von der ersten unabhängig ist:
00298     // sqrt(-2*variance*log(r1))*cos(2*M_PI*r2);
00299 }
00300 
00301 
00303 
00308 inline int ipow(int base, int exponent)
00309 {
00310     if (base <= 0)
00311         return 0;
00312     if (exponent < 0)
00313         return 0;
00314     else
00315     {
00316         int ret = 1;
00317         for (int i = 1; i <= exponent; i++)
00318             ret *= base;
00319         return ret;
00320     }
00321 }
00322 
00324 
00327 inline int log2upw(int value)
00328 {
00329     int res = 1;
00330     while (value > 2)
00331     {
00332         value = value / 2;
00333         res++;
00334     }
00335     return res;
00336 }
00337 
00339 
00342 inline int log8upw(int value)
00343 {
00344     int res = 1;
00345     while (value > 8)
00346     {
00347         value = value / 8;
00348         res++;
00349     }
00350     return res;
00351 }
00352 
00358 inline void SinCos(const double x, double &sx, double &cx)
00359 {
00360 #if defined _SYSTEM_LINUX_ && ! defined _SYSTEM_DARWIN_
00361     sincos(x, &sx, &cx);
00362 #else
00363     sx = sin(x);
00364     cx = cos(x);
00365 #endif
00366 }
00367 
00368 class tSinCosLookupTable
00369 {
00370 public:
00371     tSinCosLookupTable()
00372     {
00373         int i = 0;
00374         double x = 0;
00375         double increment = 2. * M_PI / double(cTABLE_SIZE);
00376         for (; i < cTABLE_SIZE; ++i, x += increment)
00377         {
00378             m_sin_table[i] = ::sin(x);
00379             m_cos_table[i] = ::cos(x);
00380         }
00381     }
00382 
00383     double Sin(int index)
00384     {
00385         return m_sin_table[index & cTABLE_INDEX_MASK];
00386     }
00387 
00388     double Cos(int index)
00389     {
00390         return m_cos_table[index & cTABLE_INDEX_MASK];
00391     }
00392 
00393     static const int cTABLE_SIZE = 16384;
00394     static const int cTABLE_INDEX_MASK;
00395     static const double cSCALE_FACTOR;
00396 
00397 private:
00398     double m_sin_table[cTABLE_SIZE];
00399     double m_cos_table[cTABLE_SIZE];
00400 };
00401 
00402 extern tSinCosLookupTable sin_cos_lookup_table;
00403 
00404 inline double FastSin(double x)
00405 {
00406     double scaled_x = sin_cos_lookup_table.cSCALE_FACTOR * x;
00407     double table_index = ::floor(scaled_x);
00408     double y1 = sin_cos_lookup_table.Sin(int(table_index));
00409     double y2 = sin_cos_lookup_table.Sin(int(table_index) + 1);
00410     return y1 + (y2 - y1) * (scaled_x - table_index);
00411 }
00412 
00413 inline double FastCos(double x)
00414 {
00415     double scaled_x = sin_cos_lookup_table.cSCALE_FACTOR * x;
00416     double table_index = ::floor(scaled_x);
00417     double y1 = sin_cos_lookup_table.Cos(int(table_index));
00418     double y2 = sin_cos_lookup_table.Cos(int(table_index) + 1);
00419     return y1 + (y2 - y1) * (scaled_x - table_index);
00420 }
00421 
00430 inline bool FloatIsEqual(const double a, const double b, const double epsilon = 1e-10)
00431 {
00432     return fabs(a -b) < epsilon;
00433 }
00434 
00435 
00436 #endif


asr_mild_base_driving
Author(s): Aumann Florian, Borella Jocelyn, Dehmani Souheil, Marek Felix, Meißner Pascal, Reckling Reno
autogenerated on Thu Jun 6 2019 22:02:58