OSDN Git Service

first
[psychlops/cpp.git] / psychlops / core / math / psychlops_m_util.h
1 /*
2  *  psychlops_m_util.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2006/01/04 by Kenchi HOSOKAWA
6  *  (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
7  */
8
9 #ifndef HEADER_PSYCHLOPS_MATH_UTIL
10 #define HEADER_PSYCHLOPS_MATH_UTIL
11
12 #include <math.h>
13 #include <algorithm>
14
15
16 namespace Psychlops {
17
18
19         extern const double PI;
20
21
22         /*              Functions               */
23
24         //      Random
25         // in this version random(X) generates pseudrandom number in half-closed interval
26         double random();        // [0,1)
27         double random(double N);        //      [0,N)
28         int random(int N);      //      [0,N)
29         double random(double min, double max);  //      [min,max)
30         void random(double array[], int size);  // [0,1)
31         void random(double array[], int size, double N);        //      [0,N)
32         void random(double array[], int size, double min, double max);  //      [min,max)
33         class Matrix;
34         void random(Matrix &mtx);
35         void random(Matrix &mtx, double factor);
36         void random(Matrix &mtx, double min, double max);
37         void randomize();
38         void randomize(unsigned long seed);
39
40
41         //      shuffle
42         class Math {
43                 public:\r
44                 static const double E;\r
45                 static const double LOG2E;
46                 inline static double abs(double val) { return (val<0 ? -val : val); }
47                 inline static double mod(double lhs, double rhs) { return lhs - floor(lhs/rhs)*rhs; }
48                 static int round(double val);\r
49                 static double log2(double val);\r
50 #ifdef max\r
51 #undef max\r
52 #endif\r
53 #ifdef min\r
54 #undef min\r
55 #endif\r
56                 template <class X, class Y> static double max(X val1, Y val2) { return val1>val2 ? val1 : val2; }\r
57                 template <class X, class Y> static double min(X val1, Y val2) { return val1<val2 ? val1 : val2; }
58
59                 template <class X> static void shuffle(X array, int n);
60                 template <class X> static void shuffle(X* array, int n, X init);
61                 void setscramble(int array[], unsigned int length);  //  obsolete
62 \r
63                 static double gaussian(double x, double sigma);
64                 static double normalDistibution(double x, double mu, double sigma);
65                 static double cumulativeNormalDistibution(double x, double mu, double sigma);
66         };
67
68         //      !! this code is copied from http://ray.sakura.ne.jp/tips/shaffle.html
69         template <class X> void Math::shuffle(X array, int n) {
70                 int a;
71                 for(int i = 1; i < n; i++){
72                         a = random(i + 1);
73                         std::swap(array[i], array[a]);
74                 }
75         }
76         template <class X> void Math::shuffle(X* array, int n, X init) {
77                 for(int i=init; i<n+init; i++) array[i] = i;
78                 shuffle(array, n);
79         }
80
81 }       /*      <- namespace Psycholops         */
82
83 #endif