oct-rand.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2003-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
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   // Return the current seed.
00049   static double seed (void)
00050   {
00051     return instance_ok () ? instance->do_seed () : octave_NaN;
00052   }
00053 
00054   // Set the seed.
00055   static void seed (double s)
00056   {
00057     if (instance_ok ())
00058       instance->do_seed (s);
00059   }
00060 
00061   // Reset the seed.
00062   static void reset (void)
00063   {
00064     if (instance_ok ())
00065       instance->do_reset ();
00066   }
00067 
00068   // Return the current state.
00069   static ColumnVector state (const std::string& d = std::string ())
00070   {
00071     return instance_ok () ? instance->do_state (d) : ColumnVector ();
00072   }
00073 
00074   // Set the current state/
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   // Reset the current state/
00083   static void reset (const std::string& d)
00084   {
00085     if (instance_ok ())
00086       instance->do_reset (d);
00087   }
00088 
00089   // Return the current distribution.
00090   static std::string distribution (void)
00091   {
00092     return instance_ok () ? instance->do_distribution () : std::string ();
00093   }
00094 
00095   // Set the current distribution.  May be either "uniform" (the
00096   // default), "normal", "exponential", "poisson", or "gamma".
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   // Return the next number from the sequence.
00134   static double scalar (double a = 1.0)
00135   {
00136     return instance_ok () ? instance->do_scalar (a) : octave_NaN;
00137   }
00138 
00139   // Return a matrix of numbers from the sequence, filled in column
00140   // major order.
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   // Return an N-dimensional array of numbers from the sequence,
00147   // filled in column major order.
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   // Return an array of numbers from the sequence.
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   // Current distribution of random numbers.
00176   int current_distribution;
00177 
00178   // If TRUE, use old RANLIB generators.  Otherwise, use Mersenne
00179   // Twister generator.
00180   bool use_old_generators;
00181 
00182   // Saved MT states.
00183   std::map<int, ColumnVector> rand_states;
00184 
00185   // Return the current seed.
00186   double do_seed (void);
00187 
00188   // Set the seed.
00189   void do_seed (double s);
00190 
00191   // Reset the seed.
00192   void do_reset ();
00193 
00194   // Return the current state.
00195   ColumnVector do_state (const std::string& d);
00196 
00197   // Set the current state/
00198   void do_state (const ColumnVector &s, const std::string& d);
00199 
00200   // Reset the current state/
00201   void do_reset (const std::string& d);
00202 
00203   // Return the current distribution.
00204   std::string do_distribution (void);
00205 
00206   // Set the current distribution.  May be either "uniform" (the
00207   // default), "normal", "exponential", "poisson", or "gamma".
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   // Return the next number from the sequence.
00221   double do_scalar (double a = 1.);
00222 
00223   // Return a matrix of numbers from the sequence, filled in column
00224   // major order.
00225   Matrix do_matrix (octave_idx_type r, octave_idx_type c, double a = 1.);
00226 
00227   // Return an N-dimensional array of numbers from the sequence,
00228   // filled in column major order.
00229   NDArray do_nd_array (const dim_vector& dims, double a = 1.);
00230 
00231   // Return an array of numbers from the sequence.
00232   Array<double> do_vector (octave_idx_type n, double a = 1.);
00233 
00234   // Some helper functions.
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
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines