Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined (octave_rand_h)
00024 #define octave_rand_h 1
00025
00026 #include <map>
00027 #include <string>
00028
00029 #include "dColVector.h"
00030 #include "dMatrix.h"
00031 #include "dNDArray.h"
00032 #include "lo-ieee.h"
00033
00034 class
00035 OCTAVE_API
00036 octave_rand
00037 {
00038 protected:
00039
00040 octave_rand (void);
00041
00042 public:
00043
00044 ~octave_rand (void) { }
00045
00046 static bool instance_ok (void);
00047
00048
00049 static double seed (void)
00050 {
00051 return instance_ok () ? instance->do_seed () : octave_NaN;
00052 }
00053
00054
00055 static void seed (double s)
00056 {
00057 if (instance_ok ())
00058 instance->do_seed (s);
00059 }
00060
00061
00062 static void reset (void)
00063 {
00064 if (instance_ok ())
00065 instance->do_reset ();
00066 }
00067
00068
00069 static ColumnVector state (const std::string& d = std::string ())
00070 {
00071 return instance_ok () ? instance->do_state (d) : ColumnVector ();
00072 }
00073
00074
00075 static void state (const ColumnVector &s,
00076 const std::string& d = std::string ())
00077 {
00078 if (instance_ok ())
00079 instance->do_state (s, d);
00080 }
00081
00082
00083 static void reset (const std::string& d)
00084 {
00085 if (instance_ok ())
00086 instance->do_reset (d);
00087 }
00088
00089
00090 static std::string distribution (void)
00091 {
00092 return instance_ok () ? instance->do_distribution () : std::string ();
00093 }
00094
00095
00096
00097 static void distribution (const std::string& d)
00098 {
00099 if (instance_ok ())
00100 instance->do_distribution (d);
00101 }
00102
00103 static void uniform_distribution (void)
00104 {
00105 if (instance_ok ())
00106 instance->do_uniform_distribution ();
00107 }
00108
00109 static void normal_distribution (void)
00110 {
00111 if (instance_ok ())
00112 instance->do_normal_distribution ();
00113 }
00114
00115 static void exponential_distribution (void)
00116 {
00117 if (instance_ok ())
00118 instance->do_exponential_distribution ();
00119 }
00120
00121 static void poisson_distribution (void)
00122 {
00123 if (instance_ok ())
00124 instance->do_poisson_distribution ();
00125 }
00126
00127 static void gamma_distribution (void)
00128 {
00129 if (instance_ok ())
00130 instance->do_gamma_distribution ();
00131 }
00132
00133
00134 static double scalar (double a = 1.0)
00135 {
00136 return instance_ok () ? instance->do_scalar (a) : octave_NaN;
00137 }
00138
00139
00140
00141 static Matrix matrix (octave_idx_type r, octave_idx_type c, double a = 1.0)
00142 {
00143 return instance_ok () ? instance->do_matrix (r, c, a) : Matrix ();
00144 }
00145
00146
00147
00148 static NDArray nd_array (const dim_vector& dims, double a = 1.0)
00149 {
00150 return instance_ok () ? instance->do_nd_array (dims, a) : NDArray ();
00151 }
00152
00153
00154 static Array<double> vector (octave_idx_type n, double a = 1.0)
00155 {
00156 return instance_ok () ? instance->do_vector (n, a) : Array<double> ();
00157 }
00158
00159 private:
00160
00161 static octave_rand *instance;
00162
00163 static void cleanup_instance (void) { delete instance; instance = 0; }
00164
00165 enum
00166 {
00167 unknown_dist,
00168 uniform_dist,
00169 normal_dist,
00170 expon_dist,
00171 poisson_dist,
00172 gamma_dist
00173 };
00174
00175
00176 int current_distribution;
00177
00178
00179
00180 bool use_old_generators;
00181
00182
00183 std::map<int, ColumnVector> rand_states;
00184
00185
00186 double do_seed (void);
00187
00188
00189 void do_seed (double s);
00190
00191
00192 void do_reset ();
00193
00194
00195 ColumnVector do_state (const std::string& d);
00196
00197
00198 void do_state (const ColumnVector &s, const std::string& d);
00199
00200
00201 void do_reset (const std::string& d);
00202
00203
00204 std::string do_distribution (void);
00205
00206
00207
00208 void do_distribution (const std::string& d);
00209
00210 void do_uniform_distribution (void);
00211
00212 void do_normal_distribution (void);
00213
00214 void do_exponential_distribution (void);
00215
00216 void do_poisson_distribution (void);
00217
00218 void do_gamma_distribution (void);
00219
00220
00221 double do_scalar (double a = 1.);
00222
00223
00224
00225 Matrix do_matrix (octave_idx_type r, octave_idx_type c, double a = 1.);
00226
00227
00228
00229 NDArray do_nd_array (const dim_vector& dims, double a = 1.);
00230
00231
00232 Array<double> do_vector (octave_idx_type n, double a = 1.);
00233
00234
00235
00236 void initialize_ranlib_generators (void);
00237
00238 void initialize_mersenne_twister (void);
00239
00240 ColumnVector get_internal_state (void);
00241
00242 void save_state (void);
00243
00244 int get_dist_id (const std::string& d);
00245
00246 void set_internal_state (const ColumnVector& s);
00247
00248 void switch_to_generator (int dist);
00249
00250 void fill (octave_idx_type len, double *v, double a);
00251 };
00252
00253 #endif