You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by gs...@apache.org on 2009/12/18 00:22:41 UTC

svn commit: r891983 [14/47] - in /lucene/mahout/trunk: ./ core/ core/src/main/java/org/apache/mahout/cf/taste/hadoop/item/ core/src/main/java/org/apache/mahout/clustering/ core/src/main/java/org/apache/mahout/clustering/canopy/ core/src/main/java/org/a...

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/DRand.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/DRand.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/DRand.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/DRand.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,100 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.engine;
+
+import java.util.Date;
+/**
+ * Quick medium quality uniform pseudo-random number generator.
+ *
+ * Produces uniformly distributed <tt>int</tt>'s and <tt>long</tt>'s in the closed intervals <tt>[Integer.MIN_VALUE,Integer.MAX_VALUE]</tt> and <tt>[Long.MIN_VALUE,Long.MAX_VALUE]</tt>, respectively, 
+ * as well as <tt>float</tt>'s and <tt>double</tt>'s in the open unit intervals <tt>(0.0f,1.0f)</tt> and <tt>(0.0,1.0)</tt>, respectively.
+ * <p>
+ * The seed can be any integer satisfying <tt>0 &lt; 4*seed+1 &lt; 2<sup>32</sup></tt>.
+ * In other words, there must hold <tt>seed &gt;= 0 && seed &lt; 1073741823</tt>.
+ * <p>
+ * <b>Quality:</b> This generator follows the multiplicative congruential method of the form                    
+ * <dt>
+ * <tt>z(i+1) = a * z(i) (mod m)</tt> with
+ * <tt>a=663608941 (=0X278DDE6DL), m=2<sup>32</sup></tt>.
+ * <dt>
+ * <tt>z(i)</tt> assumes all different values <tt>0 &lt; 4*seed+1 &lt; m</tt> during a full period of 2<sup>30</sup>.
+ *
+ * <p>
+ * <b>Performance:</b> TO_DO
+ * <p>
+ * <b>Implementation:</b> TO_DO
+ * <p>
+ * Note that this implementation is <b>not synchronized</b>.                                  
+ * <p>
+ *
+ * @see MersenneTwister
+ * @see java.util.Random
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class DRand extends RandomEngine {
+
+  private int current;
+  private static final int DEFAULT_SEED = 1;
+
+  /** Constructs and returns a random number generator with a default seed, which is a <b>constant</b>. */
+  public DRand() {
+    this(DEFAULT_SEED);
+  }
+
+  /**
+   * Constructs and returns a random number generator with the given seed.
+   *
+   * @param seed should not be 0, in such a case <tt>DRand.DEFAULT_SEED</tt> is substituted.
+   */
+  public DRand(int seed) {
+    setSeed(seed);
+  }
+
+  /**
+   * Constructs and returns a random number generator seeded with the given date.
+   *
+   * @param d typically <tt>new Date()</tt>
+   */
+  public DRand(Date d) {
+    this((int) d.getTime());
+  }
+
+  /**
+   * Returns a 32 bit uniformly distributed random number in the closed interval <tt>[Integer.MIN_VALUE,Integer.MAX_VALUE]</tt>
+   * (including <tt>Integer.MIN_VALUE</tt> and <tt>Integer.MAX_VALUE</tt>).
+   */
+  @Override
+  public int nextInt() {
+    current *= 0x278DDE6D;     /* z(i+1)=a*z(i) (mod 2**32) */
+    // a == 0x278DDE6D == 663608941
+
+    return current;
+  }
+
+  /**
+   * Sets the receiver's seed. This method resets the receiver's entire internal state. The following condition must
+   * hold: <tt>seed &gt;= 0 && seed &lt; (2<sup>32</sup>-1) / 4</tt>.
+   *
+   * @param seed if the above condition does not hold, a modified seed that meets the condition is silently
+   *             substituted.
+   */
+  protected void setSeed(int seed) {
+    if (seed < 0) {
+      seed = -seed;
+    }
+    int limit = (int) ((Math.pow(2, 32) - 1) / 4); // --> 536870911
+    if (seed >= limit) {
+      seed >>= 3;
+    }
+
+    this.current = 4 * seed + 1;
+  }
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/DRand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,272 @@
+package org.apache.mahout.math.jet.random.engine;
+
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+
+import java.util.Date;
+/**
+ MersenneTwister (MT19937) is one of the strongest uniform pseudo-random number generators known so far; at the same time it is quick.
+ Produces uniformly distributed <tt>int</tt>'s and <tt>long</tt>'s in the closed intervals <tt>[Integer.MIN_VALUE,Integer.MAX_VALUE]</tt> and <tt>[Long.MIN_VALUE,Long.MAX_VALUE]</tt>, respectively,
+ as well as <tt>float</tt>'s and <tt>double</tt>'s in the open unit intervals <tt>(0.0f,1.0f)</tt> and <tt>(0.0,1.0)</tt>, respectively.
+ The seed can be any 32-bit integer except <tt>0</tt>. Shawn J. Cokus commented that perhaps the seed should preferably be odd.
+ <p>
+ <b>Quality:</b> MersenneTwister is designed to pass the k-distribution test. It has an astronomically large period of 2<sup>19937</sup>-1 (=10<sup>6001</sup>) and 623-dimensional equidistribution up to 32-bit accuracy.
+ It passes many stringent statistical tests, including the <A HREF="http://stat.fsu.edu/~geo/diehard.html">diehard</A> test of G. Marsaglia and the load test of P. Hellekalek and S. Wegenkittl.
+ <p>
+ <b>Performance:</b> Its speed is comparable to other modern generators (in particular, as fast as <tt>java.util.Random.nextFloat()</tt>).
+ 2.5 million calls to <tt>raw()</tt> per second (Pentium Pro 200 Mhz, JDK 1.2, NT).
+ Be aware, however, that there is a non-negligible amount of overhead required to initialize the data
+ structures used by a MersenneTwister. Code like
+ <pre>
+ double sum = 0.0;
+ for (int i=0; i<100000; ++i) {
+ RandomElement twister = new MersenneTwister(new Date());
+ sum += twister.raw();
+ }
+ </pre>
+ will be wildly inefficient. Consider using
+ <pre>
+ double sum = 0.0;
+ RandomElement twister = new MersenneTwister(new Date());
+ for (int i=0; i<100000; ++i) {
+ sum += twister.raw();
+ }
+ </pre>
+ instead.  This allows the cost of constructing the MersenneTwister object
+ to be borne only once, rather than once for each iteration in the loop.
+ <p>
+ <b>Implementation:</b> After M. Matsumoto and T. Nishimura,
+ "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator",
+ ACM Transactions on Modeling and Computer Simulation,
+ Vol. 8, No. 1, January 1998, pp 3--30.
+ <dt>More info on <A HREF="http://www.math.keio.ac.jp/~matumoto/eindex.html"> Masumoto's homepage</A>.
+ <dt>More info on <A HREF="http://www.ncsa.uiuc.edu/Apps/CMP/RNG/www-rng.html"> Pseudo-random number generators is on the Web</A>.
+ <dt>Yet <A HREF="http://nhse.npac.syr.edu/random"> some more info</A>.
+ <p>
+ The correctness of this implementation has been verified against the published output sequence
+ <a href="http://www.math.keio.ac.jp/~nisimura/random/real2/mt19937-2.out">mt19937-2.out</a> of the C-implementation
+ <a href="http://www.math.keio.ac.jp/~nisimura/random/real2/mt19937-2.c">mt19937-2.c</a>.
+ (Call <tt>test(1000)</tt> to print the sequence).
+ <dt>
+ Note that this implementation is <b>not synchronized</b>.
+ <p>
+ <b>Details:</b> MersenneTwister is designed with consideration of the flaws of various existing generators in mind.
+ It is an improved version of TT800, a very successful generator.
+ MersenneTwister is based on linear recurrences modulo 2.
+ Such generators are very fast, have extremely long periods, and appear quite robust.
+ MersenneTwister produces 32-bit numbers, and every <tt>k</tt>-dimensional vector of such numbers appears the same number of times as <tt>k</tt> successive values over the
+ period length, for each <tt>k &lt;= 623</tt> (except for the zero vector, which appears one time less).
+ If one looks at only the first <tt>n &lt;= 16</tt> bits of each number, then the property holds for even larger <tt>k</tt>, as shown in the following table (taken from the publication cited above):
+ <div align="center">
+ <table width="75%" border="1" cellspacing="0" cellpadding="0">
+ <tr>
+ <td width="2%"> <div align="center">n</div> </td>
+ <td width="6%"> <div align="center">1</div> </td>
+ <td width="5%"> <div align="center">2</div> </td>
+ <td width="5%"> <div align="center">3</div> </td>
+ <td width="5%"> <div align="center">4</div> </td>
+ <td width="5%"> <div align="center">5</div> </td>
+ <td width="5%"> <div align="center">6</div> </td>
+ <td width="5%"> <div align="center">7</div> </td>
+ <td width="5%"> <div align="center">8</div> </td>
+ <td width="5%"> <div align="center">9</div> </td>
+ <td width="5%"> <div align="center">10</div> </td>
+ <td width="5%"> <div align="center">11</div> </td>
+ <td width="10%"> <div align="center">12 .. 16</div> </td>
+ <td width="10%"> <div align="center">17 .. 32</div> </td>
+ </tr>
+ <tr>
+ <td width="2%"> <div align="center">k</div> </td>
+ <td width="6%"> <div align="center">19937</div> </td>
+ <td width="5%"> <div align="center">9968</div> </td>
+ <td width="5%"> <div align="center">6240</div> </td>
+ <td width="5%"> <div align="center">4984</div> </td>
+ <td width="5%"> <div align="center">3738</div> </td>
+ <td width="5%"> <div align="center">3115</div> </td>
+ <td width="5%"> <div align="center">2493</div> </td>
+ <td width="5%"> <div align="center">2492</div> </td>
+ <td width="5%"> <div align="center">1869</div> </td>
+ <td width="5%"> <div align="center">1869</div> </td>
+ <td width="5%"> <div align="center">1248</div> </td>
+ <td width="10%"> <div align="center">1246</div> </td>
+ <td width="10%"> <div align="center">623</div> </td>
+ </tr>
+ </table>
+ </div>
+ <p>
+ MersenneTwister generates random numbers in batches of 624 numbers at a time, so the caching and pipelining of modern systems is exploited.
+ The generator is implemented to generate the output by using the fastest arithmetic operations only: 32-bit additions and bit operations (no division, no multiplication, no mod).
+ These operations generate sequences of 32 random bits (<tt>int</tt>'s).
+ <tt>long</tt>'s are formed by concatenating two 32 bit <tt>int</tt>'s.
+ <tt>float</tt>'s are formed by dividing the interval <tt>[0.0,1.0]</tt> into 2<sup>32</sup> sub intervals, then randomly choosing one subinterval.
+ <tt>double</tt>'s are formed by dividing the interval <tt>[0.0,1.0]</tt> into 2<sup>64</sup> sub intervals, then randomly choosing one subinterval.
+ <p>
+ @author wolfgang.hoschek@cern.ch
+ @version 1.0, 09/24/99
+ @see java.util.Random
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class MersenneTwister extends RandomEngine {
+
+  private int mti;
+  private int[] mt = new int[N]; /* set initial seeds: N = 624 words */
+
+  /* Period parameters */
+  private static final int N = 624;
+  private static final int M = 397;
+  private static final int MATRIX_A = 0x9908b0df;   /* constant vector a */
+  private static final int UPPER_MASK = 0x80000000; /* most significant w-r bits */
+  private static final int LOWER_MASK = 0x7fffffff; /* least significant r bits */
+
+  /* for tempering */
+  private static final int TEMPERING_MASK_B = 0x9d2c5680;
+  private static final int TEMPERING_MASK_C = 0xefc60000;
+
+  private static final int mag0 = 0x0;
+  private static final int mag1 = MATRIX_A;
+  //private static final int[] mag01=new int[] {0x0, MATRIX_A};
+  /* mag01[x] = x * MATRIX_A  for x=0,1 */
+
+  private static final int DEFAULT_SEED = 4357;
+
+  /**
+   * Constructs and returns a random number generator with a default seed, which is a <b>constant</b>. Thus using this
+   * constructor will yield generators that always produce exactly the same sequence. This method is mainly intended to
+   * ease testing and debugging.
+   */
+  public MersenneTwister() {
+    this(DEFAULT_SEED);
+  }
+
+  /** Constructs and returns a random number generator with the given seed. */
+  public MersenneTwister(int seed) {
+    setSeed(seed);
+  }
+
+  /**
+   * Constructs and returns a random number generator seeded with the given date.
+   *
+   * @param d typically <tt>new Date()</tt>
+   */
+  public MersenneTwister(Date d) {
+    this((int) d.getTime());
+  }
+
+  /**
+   * Returns a copy of the receiver; the copy will produce identical sequences. After this call has returned, the copy
+   * and the receiver have equal but separate state.
+   *
+   * @return a copy of the receiver.
+   */
+  @Override
+  public Object clone() {
+    MersenneTwister clone = (MersenneTwister) super.clone();
+    clone.mt = this.mt.clone();
+    return clone;
+  }
+
+  /** Generates N words at one time. */
+  protected void nextBlock() {
+    /*
+    // ******************** OPTIMIZED **********************
+    // only 5-10% faster ?
+    int y;
+
+    int kk;
+    int[] cache = mt; // cached for speed
+    int kkM;
+    int limit = N-M;
+    for (kk=0,kkM=kk+M; kk<limit; kk++,kkM++) {
+      y = (cache[kk]&UPPER_MASK)|(cache[kk+1]&LOWER_MASK);
+      cache[kk] = cache[kkM] ^ (y >>> 1) ^ ((y & 0x1) == 0 ? mag0 : mag1);
+    }
+    limit = N-1;
+    for (kkM=kk+(M-N); kk<limit; kk++,kkM++) {
+      y = (cache[kk]&UPPER_MASK)|(cache[kk+1]&LOWER_MASK);
+      cache[kk] = cache[kkM] ^ (y >>> 1) ^ ((y & 0x1) == 0 ? mag0 : mag1);
+    }
+    y = (cache[N-1]&UPPER_MASK)|(cache[0]&LOWER_MASK);
+    cache[N-1] = cache[M-1] ^ (y >>> 1) ^ ((y & 0x1) == 0 ? mag0 : mag1);
+
+    this.mt = cache;
+    this.mti = 0;
+    */
+
+
+    // ******************** UNOPTIMIZED **********************
+    int y;
+
+    int kk;
+
+    for (kk = 0; kk < N - M; kk++) {
+      y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
+      mt[kk] = mt[kk + M] ^ (y >>> 1) ^ ((y & 0x1) == 0 ? mag0 : mag1);
+    }
+    for (; kk < N - 1; kk++) {
+      y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
+      mt[kk] = mt[kk + (M - N)] ^ (y >>> 1) ^ ((y & 0x1) == 0 ? mag0 : mag1);
+    }
+    y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
+    mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ ((y & 0x1) == 0 ? mag0 : mag1);
+
+    this.mti = 0;
+
+  }
+
+  /**
+   * Returns a 32 bit uniformly distributed random number in the closed interval <tt>[Integer.MIN_VALUE,Integer.MAX_VALUE]</tt>
+   * (including <tt>Integer.MIN_VALUE</tt> and <tt>Integer.MAX_VALUE</tt>).
+   */
+  @Override
+  public int nextInt() {
+    /* Each single bit including the sign bit will be random */
+    if (mti == N) {
+      nextBlock();
+    } // generate N ints at one time
+
+    int y = mt[mti++];
+    y ^= y >>> 11; // y ^= TEMPERING_SHIFT_U(y );
+    y ^= (y << 7) & TEMPERING_MASK_B; // y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
+    y ^= (y << 15) & TEMPERING_MASK_C; // y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
+    // y &= 0xffffffff; //you may delete this line if word size = 32
+    y ^= y >>> 18; // y ^= TEMPERING_SHIFT_L(y);
+
+    return y;
+  }
+
+  /** Sets the receiver's seed. This method resets the receiver's entire internal state. */
+  protected void setSeed(int seed) {
+    mt[0] = seed;
+    for (int i = 1; i < N; i++) {
+      mt[i] = (1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i);
+      /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
+      /* In the previous versions, MSBs of the seed affect   */
+      /* only MSBs of the array mt[].                        */
+      /* 2002/01/09 modified by Makoto Matsumoto             */
+      mt[i] &= 0xffffffff;
+      /* for >32 bit machines */
+    }
+    //log.info("init done");
+    mti = N;
+
+    /*
+    old version was:
+    for (int i = 0; i < N; i++) {
+      mt[i] = seed & 0xffff0000;
+      seed = 69069 * seed + 1;
+      mt[i] |= (seed & 0xffff0000) >>> 16;
+      seed = 69069 * seed + 1;
+     }
+    //log.info("init done");
+    mti = N;
+    */
+  }
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister64.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister64.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister64.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister64.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,53 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.engine;
+
+import java.util.Date;
+/**
+ * Same as <tt>MersenneTwister</tt> except that method <tt>raw()</tt> returns 64 bit random numbers instead of 32 bit random numbers.
+ *
+ * @see MersenneTwister
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class MersenneTwister64 extends MersenneTwister {
+
+  /** Constructs and returns a random number generator with a default seed, which is a <b>constant</b>. */
+  public MersenneTwister64() {
+    super();
+  }
+
+  /**
+   * Constructs and returns a random number generator with the given seed.
+   *
+   * @param seed should not be 0, in such a case <tt>MersenneTwister64.DEFAULT_SEED</tt> is silently substituted.
+   */
+  public MersenneTwister64(int seed) {
+    super(seed);
+  }
+
+  /**
+   * Constructs and returns a random number generator seeded with the given date.
+   *
+   * @param d typically <tt>new Date()</tt>
+   */
+  public MersenneTwister64(Date d) {
+    super(d);
+  }
+
+  /**
+   * Returns a 64 bit uniformly distributed random number in the open unit interval <code>(0.0,1.0)</code> (excluding
+   * 0.0 and 1.0).
+   */
+  @Override
+  public double raw() {
+    return nextDouble();
+  }
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister64.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,164 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.engine;
+
+import org.apache.mahout.math.PersistentObject;
+
+/**
+ * Abstract base class for uniform pseudo-random number generating engines.
+ * <p>
+ * Most probability distributions are obtained by using a <b>uniform</b> pseudo-random number generation engine 
+ * followed by a transformation to the desired distribution.
+ * Thus, subclasses of this class are at the core of computational statistics, simulations, Monte Carlo methods, etc.
+ * <p>
+ * Subclasses produce uniformly distributed <tt>int</tt>'s and <tt>long</tt>'s in the closed intervals <tt>[Integer.MIN_VALUE,Integer.MAX_VALUE]</tt> and <tt>[Long.MIN_VALUE,Long.MAX_VALUE]</tt>, respectively, 
+ * as well as <tt>float</tt>'s and <tt>double</tt>'s in the open unit intervals <tt>(0.0f,1.0f)</tt> and <tt>(0.0,1.0)</tt>, respectively.
+ * <p>
+ * Subclasses need to override one single method only: <tt>nextInt()</tt>.
+ * All other methods generating different data types or ranges are usually layered upon <tt>nextInt()</tt>.
+ * <tt>long</tt>'s are formed by concatenating two 32 bit <tt>int</tt>'s.
+ * <tt>float</tt>'s are formed by dividing the interval <tt>[0.0f,1.0f]</tt> into 2<sup>32</sup> sub intervals, then randomly choosing one subinterval.
+ * <tt>double</tt>'s are formed by dividing the interval <tt>[0.0,1.0]</tt> into 2<sup>64</sup> sub intervals, then randomly choosing one subinterval.
+ * <p>
+ * Note that this implementation is <b>not synchronized</b>.
+ *
+ * @see MersenneTwister
+ * @see MersenneTwister64
+ * @see java.util.Random
+ */
+
+//public abstract class RandomEngine extends edu.cornell.lassp.houle.RngPack.RandomSeedable implements org.apache.mahout.math.function.DoubleFunction, org.apache.mahout.math.function.IntFunction {
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public abstract class RandomEngine extends PersistentObject
+    implements org.apache.mahout.math.function.DoubleFunction, org.apache.mahout.math.function.IntFunction {
+
+  /** Makes this class non instantiable, but still let's others inherit from it. */
+  protected RandomEngine() {
+  }
+
+  /**
+   * Equivalent to <tt>raw()</tt>. This has the effect that random engines can now be used as function objects,
+   * returning a random number upon function evaluation.
+   */
+  @Override
+  public double apply(double dummy) {
+    return raw();
+  }
+
+  /**
+   * Equivalent to <tt>nextInt()</tt>. This has the effect that random engines can now be used as function objects,
+   * returning a random number upon function evaluation.
+   */
+  @Override
+  public int apply(int dummy) {
+    return nextInt();
+  }
+
+  /**
+   * Constructs and returns a new uniform random number engine seeded with the current time. Currently this is {@link
+   * org.apache.mahout.math.jet.random.engine.MersenneTwister}.
+   */
+  public static RandomEngine makeDefault() {
+    return new MersenneTwister((int) System.currentTimeMillis());
+  }
+
+  /**
+   * Returns a 64 bit uniformly distributed random number in the open unit interval <code>(0.0,1.0)</code> (excluding
+   * 0.0 and 1.0).
+   */
+  public double nextDouble() {
+    double nextDouble;
+
+    do {
+      // -9.223372036854776E18 == (double) Long.MIN_VALUE
+      // 5.421010862427522E-20 == 1 / Math.pow(2,64) == 1 / ((double) Long.MAX_VALUE - (double) Long.MIN_VALUE);
+      nextDouble = ((double) nextLong() - -9.223372036854776E18) * 5.421010862427522E-20;
+    }
+    // catch loss of precision of long --> double conversion
+    while (!(nextDouble > 0.0 && nextDouble < 1.0));
+
+    // --> in (0.0,1.0)
+    return nextDouble;
+
+    /*
+      nextLong == Long.MAX_VALUE         --> 1.0
+      nextLong == Long.MIN_VALUE         --> 0.0
+      nextLong == Long.MAX_VALUE-1       --> 1.0
+      nextLong == Long.MAX_VALUE-100000L --> 0.9999999999999946
+      nextLong == Long.MIN_VALUE+1       --> 0.0
+      nextLong == Long.MIN_VALUE-100000L --> 0.9999999999999946
+      nextLong == 1L                     --> 0.5
+      nextLong == -1L                    --> 0.5
+      nextLong == 2L                     --> 0.5
+      nextLong == -2L                    --> 0.5
+      nextLong == 2L+100000L             --> 0.5000000000000054
+      nextLong == -2L-100000L            --> 0.49999999999999456
+    */
+  }
+
+  /**
+   * Returns a 32 bit uniformly distributed random number in the open unit interval <code>(0.0f,1.0f)</code> (excluding
+   * 0.0f and 1.0f).
+   */
+  public float nextFloat() {
+    // catch loss of precision of double --> float conversion
+    float nextFloat;
+    do {
+      nextFloat = (float) raw();
+    }
+    while (nextFloat >= 1.0f);
+
+    // --> in (0.0f,1.0f)
+    return nextFloat;
+  }
+
+  /**
+   * Returns a 32 bit uniformly distributed random number in the closed interval <tt>[Integer.MIN_VALUE,Integer.MAX_VALUE]</tt>
+   * (including <tt>Integer.MIN_VALUE</tt> and <tt>Integer.MAX_VALUE</tt>);
+   */
+  public abstract int nextInt();
+
+  /**
+   * Returns a 64 bit uniformly distributed random number in the closed interval <tt>[Long.MIN_VALUE,Long.MAX_VALUE]</tt>
+   * (including <tt>Long.MIN_VALUE</tt> and <tt>Long.MAX_VALUE</tt>).
+   */
+  public long nextLong() {
+    // concatenate two 32-bit strings into one 64-bit string
+    return ((nextInt() & 0xFFFFFFFFL) << 32)
+        | ((nextInt() & 0xFFFFFFFFL));
+  }
+
+  /**
+   * Returns a 32 bit uniformly distributed random number in the open unit interval <code>(0.0,1.0)</code> (excluding
+   * 0.0 and 1.0).
+   */
+  public double raw() {
+    int nextInt;
+    do { // accept anything but zero
+      nextInt = nextInt(); // in [Integer.MIN_VALUE,Integer.MAX_VALUE]-interval
+    } while (nextInt == 0);
+
+    // transform to (0.0,1.0)-interval
+    // 2.3283064365386963E-10 == 1.0 / Math.pow(2,32)
+    return (double) (nextInt & 0xFFFFFFFFL) * 2.3283064365386963E-10;
+
+    /*
+      nextInt == Integer.MAX_VALUE   --> 0.49999999976716936
+      nextInt == Integer.MIN_VALUE   --> 0.5
+      nextInt == Integer.MAX_VALUE-1 --> 0.4999999995343387
+      nextInt == Integer.MIN_VALUE+1 --> 0.5000000002328306
+      nextInt == 1                   --> 2.3283064365386963E-10
+      nextInt == -1                  --> 0.9999999997671694
+      nextInt == 2                   --> 4.6566128730773926E-10
+      nextInt == -2                  --> 0.9999999995343387
+    */
+  }
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomGenerator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomGenerator.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomGenerator.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomGenerator.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,48 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.engine;
+
+/**
+ * Interface for uniform pseudo-random number generators.
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public interface RandomGenerator {
+
+  /**
+   * Returns a 32 bit uniformly distributed random number in the open unit interval <code>(0.0,1.0)</code> (excluding
+   * 0.0 and 1.0).
+   */
+  double raw();
+
+  /**
+   * Returns a 64 bit uniformly distributed random number in the open unit interval <code>(0.0,1.0)</code> (excluding
+   * 0.0 and 1.0).
+   */
+  double nextDouble();
+
+  /**
+   * Returns a 32 bit uniformly distributed random number in the closed interval <tt>[Integer.MIN_VALUE,Integer.MAX_VALUE]</tt>
+   * (including <tt>Integer.MIN_VALUE</tt> and <tt>Integer.MAX_VALUE</tt>);
+   */
+  int nextInt();
+
+  /**
+   * Returns a 64 bit uniformly distributed random number in the closed interval <tt>[Long.MIN_VALUE,Long.MAX_VALUE]</tt>
+   * (including <tt>Long.MIN_VALUE</tt> and <tt>Long.MAX_VALUE</tt>).
+   */
+  long nextLong();
+
+  /**
+   * Returns a 32 bit uniformly distributed random number in the open unit interval <code>(0.0f,1.0f)</code> (excluding
+   * 0.0f and 1.0f).
+   */
+  float nextFloat();
+}
\ No newline at end of file

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedGenerator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedGenerator.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedGenerator.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedGenerator.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,61 @@
+/*
+Copyright 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.engine;
+
+import org.apache.mahout.math.PersistentObject;
+
+/**
+ * Deterministic seed generator for pseudo-random number generators.
+ * The sole purpose of this class is to decorrelate seeds and uniform random number generators.
+ * (If a generator would be used to generate seeds for itself, the result could be correlations.)
+ * <p>
+ * This class has entirelly deterministic behaviour:
+ * Constructing two instances with the same parameters at any two distinct points in time will produce identical seed sequences.
+ * However, it does not (at all) generate uniformly distributed numbers. Do not use it as a uniformly distributed random number generator! 
+ * <p>
+ * Each generated sequence of seeds has a period of 10<sup>9</sup> numbers.
+ * Internally uses {@link RandomSeedTable}.
+ *
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class RandomSeedGenerator extends PersistentObject {
+
+  private int row;
+  private final int column;
+
+  /** Constructs and returns a new seed generator. */
+  public RandomSeedGenerator() {
+    this(0, 0);
+  }
+
+  /**
+   * Constructs and returns a new seed generator; you normally won't need to use this method. <p> The position
+   * <tt>[row,column]</tt> indicates the iteration starting point within a (virtual) seed matrix. The seed matrix is a
+   * n*m matrix with <tt>1 + Integer.MAX_VALUE</tt> (virtual) rows and <tt>RandomSeedTable.COLUMNS</tt> columns.
+   * Successive calls to method <tt>nextSeed()</tt> will cycle over the given column, in ascending order:
+   * <tt>nextSeed()</tt> returns the seed <tt>s[row,column], s[row+1,column], ... s[Integer.MAX_VALUE,column],
+   * s[0,column], s[1,column], ...</tt>
+   *
+   * @param row    should be in <tt>[0,Integer.MAX_VALUE]</tt>.
+   * @param column should be in <tt>[0,RandomSeedTable.COLUMNS - 1]</tt>.
+   */
+  public RandomSeedGenerator(int row, int column) {
+    this.row = row;
+    this.column = column;
+  }
+
+
+  /** Returns the next seed. */
+  public int nextSeed() {
+    return RandomSeedTable.getSeedAtRowColumn(row++, column);
+  }
+
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedTable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedTable.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedTable.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedTable.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,284 @@
+/*
+Copyright 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.engine;
+
+/**
+ * (Seemingly gigantic) table of good seeds for pseudo-random number generators.
+ * <p>
+ * <b>Implementation:</b>
+ * <dt>This is a port of <tt>SeedTable.h</tt> used in <A HREF="http://wwwinfo.cern.ch/asd/lhc++/clhep">CLHEP 1.4.0</A> (C++).
+ * CLHEP's implementation, in turn, is part of <A HREF="http://wwwinfo.cern.ch/asd/geant/geant4.html">GEANT 4</A>, a C++ simulation toolkit for High Energy Physics.
+ * Geant4, in turn,  took the table from the original FORTRAN77 implementation of the HEP CERN Library routine RECUSQ.
+ * Each sequence has a period of 10**9 numbers.
+ *
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class RandomSeedTable {
+
+  /** The number of columns of the matrix (currently COLUMNS = 2). */
+  private static final int COLUMNS = 2;
+
+  // a m*n matrix, just stored as one-dim array
+  // 215 * 2 entries
+  private static final int[] seeds = {
+      9876, 54321,
+      1299961164, 253987020,
+      669708517, 2079157264,
+      190904760, 417696270,
+      1289741558, 1376336092,
+      1803730167, 324952955,
+      489854550, 582847132,
+      1348037628, 1661577989,
+      350557787, 1155446919,
+      591502945, 634133404,
+      1901084678, 862916278,
+      1988640932, 1785523494,
+      1873836227, 508007031,
+      1146416592, 967585720,
+      1837193353, 1522927634,
+      38219936, 921609208,
+      349152748, 112892610,
+      744459040, 1735807920,
+      1983990104, 728277902,
+      309164507, 2126677523,
+      362993787, 1897782044,
+      556776976, 462072869,
+      1584900822, 2019394912,
+      1249892722, 791083656,
+      1686600998, 1983731097,
+      1127381380, 198976625,
+      1999420861, 1810452455,
+      1972906041, 664182577,
+      84636481, 1291886301,
+      1186362995, 954388413,
+      2141621785, 61738584,
+      1969581251, 1557880415,
+      1150606439, 136325185,
+      95187861, 1592224108,
+      940517655, 1629971798,
+      215350428, 922659102,
+      786161212, 1121345074,
+      1450830056, 1922787776,
+      1696578057, 2025150487,
+      1803414346, 1851324780,
+      1017898585, 1452594263,
+      1184497978, 82122239,
+      633338765, 1829684974,
+      430889421, 230039326,
+      492544653, 76320266,
+      389386975, 1314148944,
+      1720322786, 709120323,
+      1868768216, 1992898523,
+      443210610, 811117710,
+      1191938868, 1548484733,
+      616890172, 159787986,
+      935835339, 1231440405,
+      1058009367, 1527613300,
+      1463148129, 1970575097,
+      1795336935, 434768675,
+      274019517, 605098487,
+      483689317, 217146977,
+      2070804364, 340596558,
+      930226308, 1602100969,
+      989324440, 801809442,
+      410606853, 1893139948,
+      1583588576, 1219225407,
+      2102034391, 1394921405,
+      2005037790, 2031006861,
+      1244218766, 923231061,
+      49312790, 775496649,
+      721012176, 321339902,
+      1719909107, 1865748178,
+      1156177430, 1257110891,
+      307561322, 1918244397,
+      906041433, 360476981,
+      1591375755, 268492659,
+      461522398, 227343256,
+      2145930725, 2020665454,
+      1938419274, 1331283701,
+      174405412, 524140103,
+      494343653, 18063908,
+      1025534808, 181709577,
+      2048959776, 1913665637,
+      950636517, 794796256,
+      1828843197, 1335757744,
+      211109723, 983900607,
+      825474095, 1046009991,
+      374915657, 381856628,
+      1241296328, 698149463,
+      1260624655, 1024538273,
+      900676210, 1628865823,
+      697951025, 500570753,
+      1007920268, 1708398558,
+      264596520, 624727803,
+      1977924811, 674673241,
+      1440257718, 271184151,
+      1928778847, 993535203,
+      1307807366, 1801502463,
+      1498732610, 300876954,
+      1617712402, 1574250679,
+      1261800762, 1556667280,
+      949929273, 560721070,
+      1766170474, 1953522912,
+      1849939248, 19435166,
+      887262858, 1219627824,
+      483086133, 603728993,
+      1330541052, 1582596025,
+      1850591475, 723593133,
+      1431775678, 1558439000,
+      922493739, 1356554404,
+      1058517206, 948567762,
+      709067283, 1350890215,
+      1044787723, 2144304941,
+      999707003, 513837520,
+      2140038663, 1850568788,
+      1803100150, 127574047,
+      867445693, 1149173981,
+      408583729, 914837991,
+      1166715497, 602315845,
+      430738528, 1743308384,
+      1388022681, 1760110496,
+      1664028066, 654300326,
+      1767741172, 1338181197,
+      1625723550, 1742482745,
+      464486085, 1507852127,
+      754082421, 1187454014,
+      1315342834, 425995190,
+      960416608, 2004255418,
+      1262630671, 671761697,
+      59809238, 103525918,
+      1205644919, 2107823293,
+      1615183160, 1152411412,
+      1024474681, 2118672937,
+      1703877649, 1235091369,
+      1821417852, 1098463802,
+      1738806466, 1529062843,
+      620780646, 1654833544,
+      1070174101, 795158254,
+      658537995, 1693620426,
+      2055317555, 508053916,
+      1647371686, 1282395762,
+      29067379, 409683067,
+      1763495989, 1917939635,
+      1602690753, 810926582,
+      885787576, 513818500,
+      1853512561, 1195205756,
+      1798585498, 1970460256,
+      1819261032, 1306536501,
+      1133245275, 37901,
+      689459799, 1334389069,
+      1730609912, 1854586207,
+      1556832175, 1228729041,
+      251375753, 683687209,
+      2083946182, 1763106152,
+      2142981854, 1365385561,
+      763711891, 1735754548,
+      1581256466, 173689858,
+      2121337132, 1247108250,
+      1004003636, 891894307,
+      569816524, 358675254,
+      626626425, 116062841,
+      632086003, 861268491,
+      1008211580, 779404957,
+      1134217766, 1766838261,
+      1423829292, 1706666192,
+      942037869, 1549358884,
+      1959429535, 480779114,
+      778311037, 1940360875,
+      1531372185, 2009078158,
+      241935492, 1050047003,
+      272453504, 1870883868,
+      390441332, 1057903098,
+      1230238834, 1548117688,
+      1242956379, 1217296445,
+      515648357, 1675011378,
+      364477932, 355212934,
+      2096008713, 1570161804,
+      1409752526, 214033983,
+      1288158292, 1760636178,
+      407562666, 1265144848,
+      1071056491, 1582316946,
+      1014143949, 911406955,
+      203080461, 809380052,
+      125647866, 1705464126,
+      2015685843, 599230667,
+      1425476020, 668203729,
+      1673735652, 567931803,
+      1714199325, 181737617,
+      1389137652, 678147926,
+      288547803, 435433694,
+      200159281, 654399753,
+      1580828223, 1298308945,
+      1832286107, 169991953,
+      182557704, 1046541065,
+      1688025575, 1248944426,
+      1508287706, 1220577001,
+      36721212, 1377275347,
+      1968679856, 1675229747,
+      279109231, 1835333261,
+      1358617667, 1416978076,
+      740626186, 2103913602,
+      1882655908, 251341858,
+      648016670, 1459615287,
+      780255321, 154906988,
+      857296483, 203375965,
+      1631676846, 681204578,
+      1906971307, 1623728832,
+      1541899600, 1168449797,
+      1267051693, 1020078717,
+      1998673940, 1298394942,
+      1914117058, 1381290704,
+      426068513, 1381618498,
+      139365577, 1598767734,
+      2129910384, 952266588,
+      661788054, 19661356,
+      1104640222, 240506063,
+      356133630, 1676634527,
+      242242374, 1863206182,
+      957935844, 1490681416};
+
+  private RandomSeedTable() {
+  }
+
+  /**
+   * Returns a deterministic seed from a (seemingly gigantic) matrix of predefined seeds.
+   *
+   * @param row    should (but need not) be in [0,Integer.MAX_VALUE].
+   * @param column shoould (but need not) be in [0,SeedTable.COLUMNS-1].
+   * @return the seed at the indicated matrix position.
+   */
+  public static int getSeedAtRowColumn(int row, int column) {
+    // the table is limited; let's snap the unbounded input parameters to the table's actual size.
+    int rows = rows();
+
+    int theRow = Math.abs(row % rows);
+    int theColumn = Math.abs(column % COLUMNS);
+
+    int seed = seeds[theRow * COLUMNS + theColumn];
+
+    // "randomize" the seed (in some ways comparable to hash functions)
+    int cycle = Math.abs(row / rows);
+    int mask = ((cycle & 0x000007ff) << 20); // cycle==0 --> mask = 0
+    seed ^= mask;  // cycle==0 --> seed stays unaffected
+    // now, each sequence has a period of 10**9 numbers.
+
+    return seed;
+  }
+
+  /**
+   * Not yet commented.
+   *
+   * @return int
+   */
+  private static int rows() {
+    return seeds.length / COLUMNS;
+  }
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomSeedTable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/package.html
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/package.html?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/package.html (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/package.html Thu Dec 17 23:22:16 2009
@@ -0,0 +1,8 @@
+<HTML>
+<BODY>
+Engines generating strong uniformly distributed pseudo-random numbers;
+Needed by all JET probability distributions since they rely on uniform random numbers to generate random numbers from
+their own distribution.
+Thus, the classes of this package are at the core of computational statistics, simulations, Monte Carlo methods, etc.
+</BODY>
+</HTML>

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/package.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/package.html
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/package.html?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/package.html (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/package.html Thu Dec 17 23:22:16 2009
@@ -0,0 +1,75 @@
+<HTML>
+<BODY>
+<p>Large variety of probability distributions featuring high performance generation
+  of random numbers, CDF's and PDF's.
+  You can always do a quick and dirty check to test the properties of any given distribution, for example, as follows:
+<table>
+  <td class="PRE">
+<pre>
+// Gamma distribution
+
+// define distribution parameters
+double mean = 5;
+double variance = 1.5;
+double alpha = mean*mean / variance; 
+double lambda = 1 / (variance / mean); 
+
+// for tests and debugging use a random engine with CONSTANT seed --> deterministic and reproducible results
+org.apache.mahout.math.jet.random.engine.RandomEngine engine = new engine.MersenneTwister();
+
+// your favourite distribution goes here
+org.apache.mahout.math.jet.random.AbstractDistribution dist = new Gamma(alpha,lambda,engine);
+
+// collect random numbers and print statistics
+int size = 100000;
+org.apache.mahout.math.list.DoubleArrayList numbers = new DoubleArrayList(size);
+for (int i=0; i < size; i++) numbers.add(dist.nextDouble());
+
+hep.aida.bin.DynamicBin1D bin = new hep.aida.bin.DynamicBin1D();
+bin.addAllOf(numbers);
+log.info(bin);
+
+Will print something like
+
+Size: 100000
+Sum: 499830.30147620925
+SumOfSquares: 2648064.0189520954
+Min: 1.2903021480010035
+Max: 12.632626684290546
+Mean: 4.998303014762093
+RMS: 5.14593433591228
+Variance: 1.497622138362513
+Standard deviation: 1.2237737284165373
+Standard error: 0.0038699123224725817
+Geometric mean: 4.849381516061957
+Product: Infinity
+Harmonic mean: 4.69916104903662
+Sum of inversions: 21280.394299425236
+Skew: 0.49097523334186227
+Kurtosis: 0.3461005384481113
+Sum of powers(3): 1.4822908764628284E7
+Sum of powers(4): 8.741360251658581E7
+Sum of powers(5): 5.41658186456702E8
+Sum of powers(6): 3.5183920126086535E9
+Moment(0,0): 1.0
+Moment(1,0): 4.998303014762093
+Moment(2,0): 26.480640189520955
+Moment(3,0): 148.22908764628284
+Moment(4,0): 874.1360251658581
+Moment(5,0): 5416.58186456702
+Moment(6,0): 35183.92012608654
+Moment(0,mean()): 1.0
+Moment(1,mean()): 3.7017002796346785E-14
+Moment(2,mean()): 1.4976071621409774
+Moment(3,mean()): 0.8998351672510565
+Moment(4,mean()): 7.50487543880015
+Moment(5,mean()): 14.413483695698101
+Moment(6,mean()): 77.72119325586715
+25%, 50%, 75% Quantiles: 4.122365795016783, 4.897730017566362, 5.763097174551738
+quantileInverse(median): 0.500005
+Distinct elements & frequencies not printed (too many).
+</pre>
+  </td>
+</table>
+</BODY>
+</HTML>

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/package.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,566 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.sampling;
+
+import org.apache.mahout.math.PersistentObject;
+import org.apache.mahout.math.Timer;
+import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+/**
+ * Space and time efficiently computes a sorted <i>Simple Random Sample Without Replacement (SRSWOR)</i>, that is, a sorted set of <tt>n</tt> random numbers from an interval of <tt>N</tt> numbers;
+ * Example: Computing <tt>n=3</tt> random numbers from the interval <tt>[1,50]</tt> may yield the sorted random set <tt>(7,13,47)</tt>.
+ * Since we are talking about a set (sampling without replacement), no element will occur more than once.
+ * Each number from the <tt>N</tt> numbers has the same probability to be included in the <tt>n</tt> chosen numbers.
+ *
+ * <p><b>Problem:</b> This class solves problems including the following: <i>
+ * Suppose we have a file containing 10^12 objects.
+ * We would like to take a truly random subset of 10^6 objects and do something with it, 
+ * for example, compute the sum over some instance field, or whatever.
+ * How do we choose the subset? In particular, how do we avoid multiple equal elements? How do we do this quick and without consuming excessive memory? How do we avoid slowly jumping back and forth within the file? </i>
+ *
+ * <p><b>Sorted Simple Random Sample Without Replacement (SRSWOR):</b>
+ * What are the exact semantics of this class? What is a SRSWOR? In which sense exactly is a returned set "random"?
+ * It is random in the sense, that each number from the <tt>N</tt> numbers has the same probability to be included in the <tt>n</tt> chosen numbers.
+ * For those who think in implementations rather than abstract interfaces:
+ * <i>Suppose, we have an empty list.
+ * We pick a random number between 1 and 10^12 and add it to the list only if it was not already picked before, i.e. if it is not already contained in the list.
+ * We then do the same thing again and again until we have eventually collected 10^6 distinct numbers.
+ * Now we sort the set ascending and return it.</i>
+ * <dt>It is exactly in this sense that this class returns "random" sets.
+ * <b>Note, however, that the implementation of this class uses a technique orders of magnitudes better (both in time and space) than the one outlined above.</b> 
+ *
+ * <p><b>Performance:</b> Space requirements are zero. Running time is <tt>O(n)</tt> on average, <tt>O(N)</tt> in the worst case.
+ * <h2 align=center>Performance (200Mhz Pentium Pro, JDK 1.2, NT)</h2>
+ * <center>
+ *   <table border="1">
+ *     <tr> 
+ *       <td align="center" width="20%">n</td>
+ *       <td align="center" width="20%">N</td>
+ *       <td align="center" width="20%">Speed [seconds]</td>
+ *     </tr>
+ *     <tr> 
+ *       <td align="center" width="20%">10<sup>3</sup></td>
+ *       <td align="center" width="20%">1.2*10<sup>3</sup></td>
+ *       <td align="center" width="20">0.0014</td>
+ *     </tr>
+ *     <tr> 
+ *       <td align="center" width="20%">10<sup>3</sup></td>
+ *       <td align="center" width="20%">10<sup>7</sup></td>
+ *       <td align="center" width="20">0.006</td>
+ *     </tr>
+ *     <tr> 
+ *       <td align="center" width="20%">10<sup>5</sup></td>
+ *       <td align="center" width="20%">10<sup>7</sup></td>
+ *       <td align="center" width="20">0.7</td>
+ *     </tr>
+ *     <tr> 
+ *       <td align="center" width="20%">9.0*10<sup>6</sup></td>
+ *       <td align="center" width="20%">10<sup>7</sup></td>
+ *       <td align="center" width="20">8.5</td>
+ *     </tr>
+ *     <tr> 
+ *       <td align="center" width="20%">9.9*10<sup>6</sup></td>
+ *       <td align="center" width="20%">10<sup>7</sup></td>
+ *       <td align="center" width="20">2.0 (samples more than 95%)</td>
+ *     </tr>
+ *     <tr> 
+ *       <td align="center" width="20%">10<sup>4</sup></td>
+ *       <td align="center" width="20%">10<sup>12</sup></td>
+ *       <td align="center" width="20">0.07</td>
+ *     </tr>
+ *     <tr> 
+ *       <td align="center" width="20%">10<sup>7</sup></td>
+ *       <td align="center" width="20%">10<sup>12</sup></td>
+ *       <td align="center" width="20">60</td>
+ *     </tr>
+ *   </table>
+ * </center>
+ *
+ * <p><b>Scalability:</b> This random sampler is designed to be scalable. In iterator style, it is able to compute and deliver sorted random sets stepwise in units called <i>blocks</i>.
+ * Example: Computing <tt>n=9</tt> random numbers from the interval <tt>[1,50]</tt> in 3 blocks may yield the blocks <tt>(7,13,14), (27,37,42), (45,46,49)</tt>.
+ * (The maximum of a block is guaranteed to be less than the minimum of its successor block. Every block is sorted ascending. No element will ever occur twice, both within a block and among blocks.)
+ * A block can be computed and retrieved with method <tt>nextBlock</tt>.
+ * Successive calls to method <tt>nextBlock</tt> will deliver as many random numbers as required.
+ *
+ * <p>Computing and retrieving samples in blocks is useful if you need very many random numbers that cannot be stored in main memory at the same time.
+ * For example, if you want to compute 10^10 such numbers you can do this by computing them in blocks of, say, 500 elements each.
+ * You then need only space to keep one block of 500 elements (i.e. 4 KB).
+ * When you are finished processing the first 500 elements you call <tt>nextBlock</tt> to fill the next 500 elements into the block, process them, and so on.
+ * If you have the time and need, by using such blocks you can compute random sets up to <tt>n=10^19</tt> random numbers.
+ *
+ * <p>If you do not need the block feature, you can also directly call 
+ * the static methods of this class without needing to construct a <tt>RandomSampler</tt> instance first.
+ *
+ * <p><b>Random number generation:</b> By default uses <tt>MersenneTwister</tt>, a very strong random number generator, much better than <tt>java.util.Random</tt>.
+ * You can also use other strong random number generators of Paul Houle's RngPack package.
+ * For example, <tt>Ranecu</tt>, <tt>Ranmar</tt> and <tt>Ranlux</tt> are strong well analyzed research grade pseudo-random number generators with known periods.
+ *
+ * <p><b>Implementation:</b> after J.S. Vitter, An Efficient Algorithm for Sequential Random Sampling,
+ * ACM Transactions on Mathematical Software, Vol 13, 1987.
+ * Paper available <A HREF="http://www.cs.duke.edu/~jsv"> here</A>.
+ *
+ * @see RandomSamplingAssistant
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class RandomSampler extends PersistentObject {
+
+  private static final Logger log = LoggerFactory.getLogger(RandomSampler.class);
+
+  //public class RandomSampler extends Object implements java.io.Serializable {
+  private long n;
+  private long N;
+  private long low;
+  private RandomEngine randomGenerator;
+
+  /**
+   * Constructs a random sampler that computes and delivers sorted random sets in blocks. A set block can be retrieved
+   * with method <tt>nextBlock</tt>. Successive calls to method <tt>nextBlock</tt> will deliver as many random numbers
+   * as required.
+   *
+   * @param n               the total number of elements to choose (must be <tt>n &gt;= 0</tt> and <tt>n &lt;= N</tt>).
+   * @param N               the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
+   * @param low             the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If
+   *                        <tt>low==0</tt>, then random numbers will be drawn from the interval <tt>[0,N-1]</tt>.
+   * @param randomGenerator a random number generator. Set this parameter to <tt>null</tt> to use the default random
+   *                        number generator.
+   */
+  public RandomSampler(long n, long N, long low, RandomEngine randomGenerator) {
+    if (n < 0) {
+      throw new IllegalArgumentException("n must be >= 0");
+    }
+    if (n > N) {
+      throw new IllegalArgumentException("n must by <= N");
+    }
+    this.n = n;
+    this.N = N;
+    this.low = low;
+
+    if (randomGenerator == null) {
+      randomGenerator = org.apache.mahout.math.jet.random.AbstractDistribution.makeDefaultGenerator();
+    }
+    this.randomGenerator = randomGenerator;
+  }
+
+  RandomEngine getRandomGenerator() {
+    return randomGenerator;
+  }
+
+  /** Returns a deep copy of the receiver. */
+  @Override
+  public Object clone() {
+    RandomSampler copy = (RandomSampler) super.clone();
+    copy.randomGenerator = (RandomEngine) this.randomGenerator.clone();
+    return copy;
+  }
+
+  /**
+   * Computes the next <tt>count</tt> random numbers of the sorted random set specified on instance construction and
+   * fills them into <tt>values</tt>, starting at index <tt>fromIndex</tt>.
+   *
+   * <p>Numbers are filled into the specified array starting at index <tt>fromIndex</tt> to the right. The array is
+   * returned sorted ascending in the range filled with numbers.
+   *
+   * @param count     the number of elements to be filled into <tt>values</tt> by this call (must be &gt;= 0).
+   * @param values    the array into which the random numbers are to be filled; must have a length <tt>&gt;=
+   *                  count+fromIndex</tt>.
+   * @param fromIndex the first index within <tt>values</tt> to be filled with numbers (inclusive).
+   */
+  public void nextBlock(int count, long[] values, int fromIndex) {
+    if (count > n) {
+      throw new IllegalArgumentException("Random sample exhausted.");
+    }
+    if (count < 0) {
+      throw new IllegalArgumentException("Negative count.");
+    }
+
+    if (count == 0) {
+      return;
+    } //nothing to do
+
+    sample(n, N, count, low, values, fromIndex, randomGenerator);
+
+    long lastSample = values[fromIndex + count - 1];
+    n -= count;
+    N = N - lastSample - 1 + low;
+    low = lastSample + 1;
+  }
+
+  /**
+   * Efficiently computes a sorted random set of <tt>count</tt> elements from the interval <tt>[low,low+N-1]</tt>. Since
+   * we are talking about a random set, no element will occur more than once.
+   *
+   * <p>Running time is <tt>O(count)</tt>, on average. Space requirements are zero.
+   *
+   * <p>Numbers are filled into the specified array starting at index <tt>fromIndex</tt> to the right. The array is
+   * returned sorted ascending in the range filled with numbers.
+   *
+   * @param n               the total number of elements to choose (must be &gt;= 0).
+   * @param N               the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
+   * @param count           the number of elements to be filled into <tt>values</tt> by this call (must be &gt;= 0 and
+   *                        &lt;=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
+   * @param low             the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If
+   *                        <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
+   * @param values          the array into which the random numbers are to be filled; must have a length <tt>&gt;=
+   *                        count+fromIndex</tt>.
+   * @param fromIndex       the first index within <tt>values</tt> to be filled with numbers (inclusive).
+   * @param randomGenerator a random number generator.
+   */
+  protected static void rejectMethodD(long n, long N, int count, long low, long[] values, int fromIndex,
+                                      RandomEngine randomGenerator) {
+    /*  This algorithm is applicable if a large percentage (90%..100%) of N shall be sampled.
+      In such cases it is more efficient than sampleMethodA() and sampleMethodD().
+        The idea is that it is more efficient to express
+      sample(n,N,count) in terms of reject(N-n,N,count)
+       and then invert the result.
+      For example, sampling 99% turns into sampling 1% plus inversion.
+
+      This algorithm is the same as method sampleMethodD(...) with the exception that sampled elements are rejected, and not sampled elements included in the result set.
+    */
+    n = N - n; // IMPORTANT !!!
+
+    //long threshold;
+    long chosen = -1 + low;
+
+    //long negalphainv =
+    //    -13;  //tuning paramter, determines when to switch from method D to method A. Dependent on programming language, platform, etc.
+
+    double nreal = n;
+    double ninv = 1.0 / nreal;
+    double Nreal = N;
+    double Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+    long qu1 = -n + 1 + N;
+    double qu1real = -nreal + 1.0 + Nreal;
+    //threshold = -negalphainv * n;
+
+    long S;
+    while (n > 1 && count > 0) { //&& threshold<N) {
+      double nmin1inv = 1.0 / (-1.0 + nreal);
+      double negSreal;
+      while (true) {
+        double X;
+        while (true) { // step D2: generate U and X
+          X = Nreal * (-Vprime + 1.0);
+          S = (long) X;
+          if (S < qu1) {
+            break;
+          }
+          Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+        }
+        double U = randomGenerator.raw();
+        negSreal = -S;
+
+        //step D3: Accept?
+        double y1 = Math.exp(Math.log(U * Nreal / qu1real) * nmin1inv);
+        Vprime = y1 * (-X / Nreal + 1.0) * (qu1real / (negSreal + qu1real));
+        if (Vprime <= 1.0) {
+          break;
+        } //break inner loop
+
+        //step D4: Accept?
+        double top = -1.0 + Nreal;
+        long limit;
+        double bottom;
+        if (n - 1 > S) {
+          bottom = -nreal + Nreal;
+          limit = -S + N;
+        } else {
+          bottom = -1.0 + negSreal + Nreal;
+          limit = qu1;
+        }
+        double y2 = 1.0;
+        for (long t = N - 1; t >= limit; t--) {
+          y2 = (y2 * top) / bottom;
+          top--;
+          bottom--;
+        }
+        if (Nreal / (-X + Nreal) >= y1 * Math.exp(Math.log(y2) * nmin1inv)) {
+          // accept !
+          Vprime = Math.exp(Math.log(randomGenerator.raw()) * nmin1inv);
+          break; //break inner loop
+        }
+        Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+      }
+
+      //step D5: reject the (S+1)st record !
+      int iter = count; //int iter = (int) (Math.min(S,count));
+      if (S < iter) {
+        iter = (int) S;
+      }
+
+      count -= iter;
+      while (--iter >= 0) {
+        values[fromIndex++] = ++chosen;
+      }
+      chosen++;
+
+      N -= S + 1;
+      Nreal = negSreal + (-1.0 + Nreal);
+      n--;
+      nreal--;
+      ninv = nmin1inv;
+      qu1 = -S + qu1;
+      qu1real = negSreal + qu1real;
+      //threshold += negalphainv;
+    } //end while
+
+
+    if (count > 0) { //special case n==1
+      //reject the (S+1)st record !
+      S = (long) (N * Vprime);
+
+      int iter = count; //int iter = (int) (Math.min(S,count));
+      if (S < iter) {
+        iter = (int) S;
+      }
+
+      count -= iter;
+      while (--iter >= 0) {
+        values[fromIndex++] = ++chosen;
+      }
+
+      chosen++;
+
+      // fill the rest
+      while (--count >= 0) {
+        values[fromIndex++] = ++chosen;
+      }
+    }
+  }
+
+  /**
+   * Efficiently computes a sorted random set of <tt>count</tt> elements from the interval <tt>[low,low+N-1]</tt>. Since
+   * we are talking about a random set, no element will occur more than once.
+   *
+   * <p>Running time is <tt>O(count)</tt>, on average. Space requirements are zero.
+   *
+   * <p>Numbers are filled into the specified array starting at index <tt>fromIndex</tt> to the right. The array is
+   * returned sorted ascending in the range filled with numbers.
+   *
+   * <p><b>Random number generation:</b> By default uses <tt>MersenneTwister</tt>, a very strong random number
+   * generator, much better than <tt>java.util.Random</tt>. You can also use other strong random number generators of
+   * Paul Houle's RngPack package. For example, <tt>Ranecu</tt>, <tt>Ranmar</tt> and <tt>Ranlux</tt> are strong well
+   * analyzed research grade pseudo-random number generators with known periods.
+   *
+   * @param n               the total number of elements to choose (must be <tt>n &gt;= 0</tt> and <tt>n &lt;= N</tt>).
+   * @param N               the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
+   * @param count           the number of elements to be filled into <tt>values</tt> by this call (must be &gt;= 0 and
+   *                        &lt;=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
+   * @param low             the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If
+   *                        <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
+   * @param values          the array into which the random numbers are to be filled; must have a length <tt>&gt;=
+   *                        count+fromIndex</tt>.
+   * @param fromIndex       the first index within <tt>values</tt> to be filled with numbers (inclusive).
+   * @param randomGenerator a random number generator. Set this parameter to <tt>null</tt> to use the default random
+   *                        number generator.
+   */
+  public static void sample(long n, long N, int count, long low, long[] values, int fromIndex,
+                            RandomEngine randomGenerator) {
+    if (n <= 0 || count <= 0) {
+      return;
+    }
+    if (count > n) {
+      throw new IllegalArgumentException("count must not be greater than n");
+    }
+    if (randomGenerator == null) {
+      randomGenerator = org.apache.mahout.math.jet.random.AbstractDistribution.makeDefaultGenerator();
+    }
+
+    if (count == N) { // rare case treated quickly
+      long val = low;
+      int limit = fromIndex + count;
+      for (int i = fromIndex; i < limit;) {
+        values[i++] = val++;
+      }
+      return;
+    }
+
+    if (n < N * 0.95) { // || Math.min(count,N-n)>maxTmpMemoryAllowed) {
+      sampleMethodD(n, N, count, low, values, fromIndex, randomGenerator);
+    } else { // More than 95% of all numbers shall be sampled.
+      rejectMethodD(n, N, count, low, values, fromIndex, randomGenerator);
+    }
+
+
+  }
+
+  /**
+   * Computes a sorted random set of <tt>count</tt> elements from the interval <tt>[low,low+N-1]</tt>. Since we are
+   * talking about a random set, no element will occur more than once.
+   *
+   * <p>Running time is <tt>O(N)</tt>, on average. Space requirements are zero.
+   *
+   * <p>Numbers are filled into the specified array starting at index <tt>fromIndex</tt> to the right. The array is
+   * returned sorted ascending in the range filled with numbers.
+   *
+   * @param n               the total number of elements to choose (must be &gt;= 0).
+   * @param N               the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
+   * @param count           the number of elements to be filled into <tt>values</tt> by this call (must be &gt;= 0 and
+   *                        &lt;=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
+   * @param low             the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If
+   *                        <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
+   * @param values          the array into which the random numbers are to be filled; must have a length <tt>&gt;=
+   *                        count+fromIndex</tt>.
+   * @param fromIndex       the first index within <tt>values</tt> to be filled with numbers (inclusive).
+   * @param randomGenerator a random number generator.
+   */
+  protected static void sampleMethodA(long n, long N, int count, long low, long[] values, int fromIndex,
+                                      RandomEngine randomGenerator) {
+    long chosen = -1 + low;
+
+    double top = N - n;
+    double Nreal = N;
+    long S;
+    while (n >= 2 && count > 0) {
+      double V = randomGenerator.raw();
+      S = 0;
+      double quot = top / Nreal;
+      while (quot > V) {
+        S++;
+        top--;
+        Nreal--;
+        quot = (quot * top) / Nreal;
+      }
+      chosen += S + 1;
+      values[fromIndex++] = chosen;
+      count--;
+      Nreal--;
+      n--;
+    }
+
+    if (count > 0) {
+      // special case n==1
+      S = (long) (Math.round(Nreal) * randomGenerator.raw());
+      chosen += S + 1;
+      values[fromIndex] = chosen;
+    }
+  }
+
+  /**
+   * Efficiently computes a sorted random set of <tt>count</tt> elements from the interval <tt>[low,low+N-1]</tt>. Since
+   * we are talking about a random set, no element will occur more than once.
+   *
+   * <p>Running time is <tt>O(count)</tt>, on average. Space requirements are zero.
+   *
+   * <p>Numbers are filled into the specified array starting at index <tt>fromIndex</tt> to the right. The array is
+   * returned sorted ascending in the range filled with numbers.
+   *
+   * @param n               the total number of elements to choose (must be &gt;= 0).
+   * @param N               the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
+   * @param count           the number of elements to be filled into <tt>values</tt> by this call (must be &gt;= 0 and
+   *                        &lt;=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
+   * @param low             the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If
+   *                        <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
+   * @param values          the array into which the random numbers are to be filled; must have a length <tt>&gt;=
+   *                        count+fromIndex</tt>.
+   * @param fromIndex       the first index within <tt>values</tt> to be filled with numbers (inclusive).
+   * @param randomGenerator a random number generator.
+   */
+  protected static void sampleMethodD(long n, long N, int count, long low, long[] values, int fromIndex,
+                                      RandomEngine randomGenerator) {
+    long chosen = -1 + low;
+
+    double nreal = n;
+    double ninv = 1.0 / nreal;
+    double Nreal = N;
+    double Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+    long qu1 = -n + 1 + N;
+    double qu1real = -nreal + 1.0 + Nreal;
+    long negalphainv =
+        -13;  //tuning paramter, determines when to switch from method D to method A. Dependent on programming language, platform, etc.
+    long threshold = -negalphainv * n;
+
+    long S;
+    while (n > 1 && count > 0 && threshold < N) {
+      double nmin1inv = 1.0 / (-1.0 + nreal);
+      double negSreal;
+      while (true) {
+        double X;
+        while (true) { // step D2: generate U and X
+          X = Nreal * (-Vprime + 1.0);
+          S = (long) X;
+          if (S < qu1) {
+            break;
+          }
+          Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+        }
+        double U = randomGenerator.raw();
+        negSreal = -S;
+
+        //step D3: Accept?
+        double y1 = Math.exp(Math.log(U * Nreal / qu1real) * nmin1inv);
+        Vprime = y1 * (-X / Nreal + 1.0) * (qu1real / (negSreal + qu1real));
+        if (Vprime <= 1.0) {
+          break;
+        } //break inner loop
+
+        //step D4: Accept?
+        double top = -1.0 + Nreal;
+        long limit;
+        double bottom;
+        if (n - 1 > S) {
+          bottom = -nreal + Nreal;
+          limit = -S + N;
+        } else {
+          bottom = -1.0 + negSreal + Nreal;
+          limit = qu1;
+        }
+        double y2 = 1.0;
+        for (long t = N - 1; t >= limit; t--) {
+          y2 = (y2 * top) / bottom;
+          top--;
+          bottom--;
+        }
+        if (Nreal / (-X + Nreal) >= y1 * Math.exp(Math.log(y2) * nmin1inv)) {
+          // accept !
+          Vprime = Math.exp(Math.log(randomGenerator.raw()) * nmin1inv);
+          break; //break inner loop
+        }
+        Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+      }
+
+      //step D5: select the (S+1)st record !
+      chosen += S + 1;
+      values[fromIndex++] = chosen;
+      /*
+      // invert
+      for (int iter=0; iter<S && count > 0; iter++) {
+        values[fromIndex++] = ++chosen;
+        count--;
+      }
+      chosen++;
+      */
+      count--;
+
+      N -= S + 1;
+      Nreal = negSreal + (-1.0 + Nreal);
+      n--;
+      nreal--;
+      ninv = nmin1inv;
+      qu1 = -S + qu1;
+      qu1real = negSreal + qu1real;
+      threshold += negalphainv;
+    } //end while
+
+
+    if (count > 0) {
+      if (n > 1) { //faster to use method A to finish the sampling
+        sampleMethodA(n, N, count, chosen + 1, values, fromIndex, randomGenerator);
+      } else {
+        //special case n==1
+        S = (long) (N * Vprime);
+        chosen += S + 1;
+        values[fromIndex++] = chosen;
+      }
+    }
+  }
+
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,108 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.sampling;
+
+import org.apache.mahout.math.PersistentObject;
+import org.apache.mahout.math.jet.random.engine.RandomEngine;
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class RandomSamplingAssistant extends PersistentObject {
+  //public class RandomSamplingAssistant extends Object implements java.io.Serializable {
+  private RandomSampler sampler;
+  private final long[] buffer;
+  private int bufferPosition;
+
+  private long skip;
+  private long n;
+
+  private static final int MAX_BUFFER_SIZE = 200;
+
+  /**
+   * Constructs a random sampler that samples <tt>n</tt> random elements from an input sequence of <tt>N</tt> elements.
+   *
+   * @param n               the total number of elements to choose (must be &gt;= 0).
+   * @param N               number of elements to choose from (must be &gt;= n).
+   * @param randomGenerator a random number generator. Set this parameter to <tt>null</tt> to use the default random
+   *                        number generator.
+   */
+  public RandomSamplingAssistant(long n, long N, RandomEngine randomGenerator) {
+    this.n = n;
+    this.sampler = new RandomSampler(n, N, 0, randomGenerator);
+    this.buffer = new long[(int) Math.min(n, MAX_BUFFER_SIZE)];
+    if (n > 0) {
+      this.buffer[0] = -1;
+    } // start with the right offset
+
+    fetchNextBlock();
+  }
+
+  /** Returns a deep copy of the receiver. */
+  @Override
+  public Object clone() {
+    RandomSamplingAssistant copy = (RandomSamplingAssistant) super.clone();
+    copy.sampler = (RandomSampler) this.sampler.clone();
+    return copy;
+  }
+
+  /** Not yet commented. */
+  protected void fetchNextBlock() {
+    if (n > 0) {
+      long last = buffer[bufferPosition];
+      sampler.nextBlock((int) Math.min(n, MAX_BUFFER_SIZE), buffer, 0);
+      skip = buffer[0] - last - 1;
+      bufferPosition = 0;
+    }
+  }
+
+  /** Returns the used random generator. */
+  public RandomEngine getRandomGenerator() {
+    return this.sampler.getRandomGenerator();
+  }
+
+  /** Just shows how this class can be used; samples n elements from and int[] array. */
+  public static int[] sampleArray(int n, int[] elements) {
+    RandomSamplingAssistant assistant = new RandomSamplingAssistant(n, elements.length, null);
+    int[] sample = new int[n];
+    int j = 0;
+    int length = elements.length;
+    for (int i = 0; i < length; i++) {
+      if (assistant.sampleNextElement()) {
+        sample[j++] = elements[i];
+      }
+    }
+    return sample;
+  }
+
+  /**
+   * Returns whether the next element of the input sequence shall be sampled (picked) or not.
+   *
+   * @return <tt>true</tt> if the next element shall be sampled (picked), <tt>false</tt> otherwise.
+   */
+  public boolean sampleNextElement() {
+    if (n == 0) {
+      return false;
+    } //reject
+    if (skip-- > 0) {
+      return false;
+    } //reject
+
+    //accept
+    n--;
+    if (bufferPosition < buffer.length - 1) {
+      skip = buffer[bufferPosition + 1] - buffer[bufferPosition++];
+      --skip;
+    } else {
+      fetchNextBlock();
+    }
+
+    return true;
+  }
+
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/WeightedRandomSampler.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/WeightedRandomSampler.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/WeightedRandomSampler.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/WeightedRandomSampler.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,143 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.jet.random.sampling;
+
+import org.apache.mahout.math.PersistentObject;
+import org.apache.mahout.math.jet.random.Uniform;
+import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import org.apache.mahout.math.list.IntArrayList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+/**
+ * Conveniently computes a stable subsequence of elements from a given input sequence;
+ * Picks (samples) exactly one random element from successive blocks of <tt>weight</tt> input elements each.
+ * For example, if weight==2 (a block is 2 elements), and the input is 5*2=10 elements long, then picks 5 random elements from the 10 elements such that
+ * one element is randomly picked from the first block, one element from the second block, ..., one element from the last block.
+ * weight == 1.0 --> all elements are picked (sampled). weight == 10.0 --> Picks one random element from successive blocks of 10 elements each. Etc.
+ * The subsequence is guaranteed to be <i>stable</i>, i.e. elements never change position relative to each other.
+ *
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class WeightedRandomSampler extends PersistentObject {
+
+  private static final Logger log = LoggerFactory.getLogger(WeightedRandomSampler.class);
+
+
+  //public class BlockedRandomSampler extends Object implements java.io.Serializable {
+  private int skip;
+  private int nextTriggerPos;
+  private int nextSkip;
+  private int weight;
+  private Uniform generator;
+
+  private static final int UNDEFINED = -1;
+
+  /** Calls <tt>BlockedRandomSampler(1,null)</tt>. */
+  public WeightedRandomSampler() {
+    this(1, null);
+  }
+
+  /**
+   * Chooses exactly one random element from successive blocks of <tt>weight</tt> input elements each. For example, if
+   * weight==2, and the input is 5*2=10 elements long, then chooses 5 random elements from the 10 elements such that one
+   * is chosen from the first block, one from the second, ..., one from the last block. weight == 1.0 --> all elements
+   * are consumed (sampled). 10.0 --> Consumes one random element from successive blocks of 10 elements each. Etc.
+   *
+   * @param weight          the weight.
+   * @param randomGenerator a random number generator. Set this parameter to <tt>null</tt> to use the default random
+   *                        number generator.
+   */
+  public WeightedRandomSampler(int weight, RandomEngine randomGenerator) {
+    if (randomGenerator == null) {
+      randomGenerator = org.apache.mahout.math.jet.random.AbstractDistribution.makeDefaultGenerator();
+    }
+    this.generator = new Uniform(randomGenerator);
+    setWeight(weight);
+  }
+
+  /** Returns a deep copy of the receiver. */
+  @Override
+  public Object clone() {
+    WeightedRandomSampler copy = (WeightedRandomSampler) super.clone();
+    copy.generator = (Uniform) this.generator.clone();
+    return copy;
+  }
+
+  public int getWeight() {
+    return this.weight;
+  }
+
+  /**
+   * Chooses exactly one random element from successive blocks of <tt>weight</tt> input elements each. For example, if
+   * weight==2, and the input is 5*2=10 elements long, then chooses 5 random elements from the 10 elements such that one
+   * is chosen from the first block, one from the second, ..., one from the last block.
+   *
+   * @return <tt>true</tt> if the next element shall be sampled (picked), <tt>false</tt> otherwise.
+   */
+  public boolean sampleNextElement() {
+    if (skip > 0) { //reject
+      skip--;
+      return false;
+    }
+
+    if (nextTriggerPos == UNDEFINED) {
+      if (weight == 1) {
+        nextTriggerPos = 0; // tuned for speed
+      } else {
+        nextTriggerPos = generator.nextIntFromTo(0, weight - 1);
+      }
+
+      nextSkip = weight - 1 - nextTriggerPos;
+    }
+
+    if (nextTriggerPos > 0) { //reject
+      nextTriggerPos--;
+      return false;
+    }
+
+    //accept
+    nextTriggerPos = UNDEFINED;
+    skip = nextSkip;
+
+    return true;
+  }
+
+  /**
+   * Not yet commented.
+   *
+   * @param weight int
+   */
+  public void setWeight(int weight) {
+    if (weight < 1) {
+      throw new IllegalArgumentException("bad weight");
+    }
+    this.weight = weight;
+    this.skip = 0;
+    this.nextTriggerPos = UNDEFINED;
+    this.nextSkip = 0;
+  }
+
+  /** Not yet commented. */
+  public static void test(int weight, int size) {
+    WeightedRandomSampler sampler = new WeightedRandomSampler();
+    sampler.setWeight(weight);
+
+    IntArrayList sample = new IntArrayList();
+    for (int i = 0; i < size; i++) {
+      if (sampler.sampleNextElement()) {
+        sample.add(i);
+      }
+    }
+
+    log.info("Sample = " + sample);
+  }
+
+}

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/WeightedRandomSampler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/package.html
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/package.html?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/package.html (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/package.html Thu Dec 17 23:22:16 2009
@@ -0,0 +1,5 @@
+<HTML>
+<BODY>
+Samples (picks) random subsets of data sequences.
+</BODY>
+</HTML>

Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/package.html
------------------------------------------------------------------------------
    svn:eol-style = native