[Lemon-commits] alpar: r3323 - in lemon/trunk: lemon test

Lemon SVN svn at lemon.cs.elte.hu
Mon Oct 1 20:57:22 CEST 2007


Author: alpar
Date: Mon Oct  1 20:57:21 2007
New Revision: 3323

Added:
   lemon/trunk/test/random_test.cc
Modified:
   lemon/trunk/lemon/random.h
   lemon/trunk/test/Makefile.am

Log:
- Gamma distributon random variable.
- Test file for random.h


Modified: lemon/trunk/lemon/random.h
==============================================================================
--- lemon/trunk/lemon/random.h	(original)
+++ lemon/trunk/lemon/random.h	Mon Oct  1 20:57:21 2007
@@ -748,6 +748,44 @@
       return -std::log(real<double>())/lambda;
     }
 
+    double gamma(int k) 
+    {
+      double s = 0;
+      for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
+      return s;
+    }
+    
+    /// Gamma distribution with given shape and scale parameter
+
+    /// This function generates a gamma distribution random number.
+    /// 
+    ///\param k shape parameter (<tt>k>0</tt>)
+    ///\param theta scale parameter
+    ///
+    double gamma(double k,double theta=1.0)
+    {
+      double xi,nu;
+      const double delta = k-std::floor(k);
+      const double v0=M_E/(M_E-delta);
+      do {
+	double V0=1.0-real<double>();
+	double V1=1.0-real<double>();
+	double V2=1.0-real<double>();
+	if(V2<=v0) 
+	  {
+	    xi=std::pow(V1,1.0/delta);
+	    nu=V0*std::pow(xi,delta-1.0);
+	  }
+	else 
+	  {
+	    xi=1.0-std::log(V1);
+	    nu=V0*std::exp(-xi);
+	  }
+      } while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
+      return theta*(xi-gamma(int(std::floor(k))));
+    }
+    
+      
     ///@}
     
     ///\name Two dimensional distributions

Modified: lemon/trunk/test/Makefile.am
==============================================================================
--- lemon/trunk/test/Makefile.am	(original)
+++ lemon/trunk/test/Makefile.am	Mon Oct  1 20:57:21 2007
@@ -36,6 +36,7 @@
 	test/preflow_test \
 	test/radix_sort_test \
 	test/refptr_test \
+	test/random_test \
 	test/simann_test \
 	test/suurballe_test \
 	test/test_tools_fail \
@@ -84,6 +85,7 @@
 test_preflow_test_SOURCES = test/preflow_test.cc
 test_radix_sort_test_SOURCES = test/radix_sort_test.cc
 test_refptr_test_SOURCES = test/refptr_test.cc
+test_random_test_SOURCES = test/random_test.cc
 test_simann_test_SOURCES = test/simann_test.cc
 test_suurballe_test_SOURCES = test/suurballe_test.cc
 test_test_tools_fail_SOURCES = test/test_tools_fail.cc

Added: lemon/trunk/test/random_test.cc
==============================================================================
--- (empty file)
+++ lemon/trunk/test/random_test.cc	Mon Oct  1 20:57:21 2007
@@ -0,0 +1,36 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2007
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#include <lemon/random.h>
+#include "test_tools.h"
+
+///\file \brief Test cases for random.h
+///
+///\todo To be extended
+///
+
+int main()
+{
+  double a=rnd();
+  check(a<1.0&&a>0.0,"This should be in [0,1)");
+  a=rnd.gauss();
+  a=rnd.gamma(3.45,0);
+  a=rnd.gamma(4);
+  //Does gamma work with integer k?
+  a=rnd.gamma(4.0,0);
+}



More information about the Lemon-commits mailing list