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 ColumnVector state (const std::string& d = std::string ())
00063 {
00064 return instance_ok () ? instance->do_state (d) : ColumnVector ();
00065 }
00066
00067
00068 static void state (const ColumnVector &s,
00069 const std::string& d = std::string ())
00070 {
00071 if (instance_ok ())
00072 instance->do_state (s, d);
00073 }
00074
00075
00076 static std::string distribution (void)
00077 {
00078 return instance_ok () ? instance->do_distribution () : std::string ();
00079 }
00080
00081
00082
00083 static void distribution (const std::string& d)
00084 {
00085 if (instance_ok ())
00086 instance->do_distribution (d);
00087 }
00088
00089 static void uniform_distribution (void)
00090 {
00091 if (instance_ok ())
00092 instance->do_uniform_distribution ();
00093 }
00094
00095 static void normal_distribution (void)
00096 {
00097 if (instance_ok ())
00098 instance->do_normal_distribution ();
00099 }
00100
00101 static void exponential_distribution (void)
00102 {
00103 if (instance_ok ())
00104 instance->do_exponential_distribution ();
00105 }
00106
00107 static void poisson_distribution (void)
00108 {
00109 if (instance_ok ())
00110 instance->do_poisson_distribution ();
00111 }
00112
00113 static void gamma_distribution (void)
00114 {
00115 if (instance_ok ())
00116 instance->do_gamma_distribution ();
00117 }
00118
00119
00120 static double scalar (double a = 1.0)
00121 {
00122 return instance_ok () ? instance->do_scalar (a) : octave_NaN;
00123 }
00124
00125
00126
00127 static Matrix matrix (octave_idx_type r, octave_idx_type c, double a = 1.0)
00128 {
00129 return instance_ok () ? instance->do_matrix (r, c, a) : Matrix ();
00130 }
00131
00132
00133
00134 static NDArray nd_array (const dim_vector& dims, double a = 1.0)
00135 {
00136 return instance_ok () ? instance->do_nd_array (dims, a) : NDArray ();
00137 }
00138
00139
00140 static Array<double> vector (octave_idx_type n, double a = 1.0)
00141 {
00142 return instance_ok () ? instance->do_vector (n, a) : Array<double> ();
00143 }
00144
00145 private:
00146
00147 static octave_rand *instance;
00148
00149 enum
00150 {
00151 unknown_dist,
00152 uniform_dist,
00153 normal_dist,
00154 expon_dist,
00155 poisson_dist,
00156 gamma_dist
00157 };
00158
00159
00160 int current_distribution;
00161
00162
00163
00164 bool use_old_generators;
00165
00166
00167 std::map<int, ColumnVector> rand_states;
00168
00169
00170 double do_seed (void);
00171
00172
00173 void do_seed (double s);
00174
00175
00176 ColumnVector do_state (const std::string& d);
00177
00178
00179 void do_state (const ColumnVector &s, const std::string& d);
00180
00181
00182 std::string do_distribution (void);
00183
00184
00185
00186 void do_distribution (const std::string& d);
00187
00188 void do_uniform_distribution (void);
00189
00190 void do_normal_distribution (void);
00191
00192 void do_exponential_distribution (void);
00193
00194 void do_poisson_distribution (void);
00195
00196 void do_gamma_distribution (void);
00197
00198
00199 double do_scalar (double a = 1.);
00200
00201
00202
00203 Matrix do_matrix (octave_idx_type r, octave_idx_type c, double a = 1.);
00204
00205
00206
00207 NDArray do_nd_array (const dim_vector& dims, double a = 1.);
00208
00209
00210 Array<double> do_vector (octave_idx_type n, double a = 1.);
00211
00212
00213
00214 void initialize_ranlib_generators (void);
00215
00216 void initialize_mersenne_twister (void);
00217
00218 ColumnVector get_internal_state (void);
00219
00220 void save_state (void);
00221
00222 int get_dist_id (const std::string& d);
00223
00224 void set_internal_state (const ColumnVector& s);
00225
00226 void switch_to_generator (int dist);
00227
00228 void fill (octave_idx_type len, double *v, double a);
00229 };
00230
00231 #endif
00232
00233
00234
00235
00236
00237