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 [11/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/math/Functions.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Functions.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Functions.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Functions.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,783 @@
+/*
+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.math;
+
+import org.apache.mahout.math.function.DoubleDoubleFunction;
+import org.apache.mahout.math.function.DoubleDoubleProcedure;
+import org.apache.mahout.math.function.DoubleFunction;
+import org.apache.mahout.math.function.DoubleProcedure;
+import org.apache.mahout.math.jet.random.engine.MersenneTwister;
+
+import java.util.Date;
+
+/**
+ * Function objects to be passed to generic methods. Contains the functions of {@link java.lang.Math} as function
+ * objects, as well as a few more basic functions. <p>Function objects conveniently allow to express arbitrary functions
+ * in a generic manner. Essentially, a function object is an object that can perform a function on some arguments. It
+ * has a minimal interface: a method <tt>apply</tt> that takes the arguments, computes something and returns some result
+ * value. Function objects are comparable to function pointers in C used for call-backs. <p>Unary functions are of type
+ * {@link org.apache.mahout.math.function.DoubleFunction}, binary functions of type {@link
+ * org.apache.mahout.math.function.DoubleDoubleFunction}. All can be retrieved via <tt>public static final</tt>
+ * variables named after the function. Unary predicates are of type {@link org.apache.mahout.math.function.DoubleProcedure},
+ * binary predicates of type {@link org.apache.mahout.math.function.DoubleDoubleProcedure}. All can be retrieved via
+ * <tt>public static final</tt> variables named <tt>isXXX</tt>.
+ *
+ * <p> Binary functions and predicates also exist as unary functions with the second argument being fixed to a constant.
+ * These are generated and retrieved via factory methods (again with the same name as the function). Example: <ul>
+ * <li><tt>Functions.pow</tt> gives the function <tt>a<sup>b</sup></tt>. <li><tt>Functions.pow.apply(2,3)==8</tt>.
+ * <li><tt>Functions.pow(3)</tt> gives the function <tt>a<sup>3</sup></tt>. <li><tt>Functions.pow(3).apply(2)==8</tt>.
+ * </ul> More general, any binary function can be made an unary functions by fixing either the first or the second
+ * argument. See methods {@link #bindArg1(DoubleDoubleFunction,double)} and {@link
+ * #bindArg2(DoubleDoubleFunction,double)}. The order of arguments can be swapped so that the first argument becomes the
+ * second and vice-versa. See method {@link #swapArgs(DoubleDoubleFunction)}. Example: <ul> <li><tt>Functions.pow</tt>
+ * gives the function <tt>a<sup>b</sup></tt>. <li><tt>Functions.bindArg2(Functions.pow,3)</tt> gives the function
+ * <tt>x<sup>3</sup></tt>. <li><tt>Functions.bindArg1(Functions.pow,3)</tt> gives the function <tt>3<sup>x</sup></tt>.
+ * <li><tt>Functions.swapArgs(Functions.pow)</tt> gives the function <tt>b<sup>a</sup></tt>. </ul> <p> Even more
+ * general, functions can be chained (composed, assembled). Assume we have two unary functions <tt>g</tt> and
+ * <tt>h</tt>. The unary function <tt>g(h(a))</tt> applying both in sequence can be generated via {@link
+ * #chain(DoubleFunction,DoubleFunction)}: <ul> <li><tt>Functions.chain(g,h);</tt> </ul> Assume further we have a binary
+ * function <tt>f</tt>. The binary function <tt>g(f(a,b))</tt> can be generated via {@link
+ * #chain(DoubleFunction,DoubleDoubleFunction)}: <ul> <li><tt>Functions.chain(g,f);</tt> </ul> The binary function
+ * <tt>f(g(a),h(b))</tt> can be generated via {@link #chain(DoubleDoubleFunction,DoubleFunction,DoubleFunction)}: <ul>
+ * <li><tt>Functions.chain(f,g,h);</tt> </ul> Arbitrarily complex functions can be composed from these building blocks.
+ * For example <tt>sin(a) + cos<sup>2</sup>(b)</tt> can be specified as follows: <ul>
+ * <li><tt>chain(plus,sin,chain(square,cos));</tt> </ul> or, of course, as
+ * <pre>
+ * new DoubleDoubleFunction() {
+ * public final double apply(double a, double b) { return Math.sin(a) + Math.pow(Math.cos(b),2); }
+ * }
+ * </pre>
+ * <p> For aliasing see {@link #functions}. Try this <table> <td class="PRE">
+ * <pre>
+ * // should yield 1.4399560356056456 in all cases
+ * double a = 0.5;
+ * double b = 0.2;
+ * double v = Math.sin(a) + Math.pow(Math.cos(b),2);
+ * log.info(v);
+ * Functions F = Functions.functions;
+ * DoubleDoubleFunction f = F.chain(F.plus,F.sin,F.chain(F.square,F.cos));
+ * log.info(f.apply(a,b));
+ * DoubleDoubleFunction g = new DoubleDoubleFunction() {
+ * public double apply(double a, double b) { return Math.sin(a) + Math.pow(Math.cos(b),2); }
+ * };
+ * log.info(g.apply(a,b));
+ * </pre>
+ * </td> </table>
+ *
+ * <p> <H3>Performance</H3>
+ *
+ * Surprise. Using modern non-adaptive JITs such as SunJDK 1.2.2 (java -classic) there seems to be no or only moderate
+ * performance penalty in using function objects in a loop over traditional code in a loop. For complex nested function
+ * objects (e.g. <tt>F.chain(F.abs,F.chain(F.plus,F.sin,F.chain(F.square,F.cos)))</tt>) the penalty is zero, for trivial
+ * functions (e.g. <tt>F.plus</tt>) the penalty is often acceptable. <center> <table border cellpadding="3"
+ * cellspacing="0" align="center"> <tr valign="middle" bgcolor="#33CC66" nowrap align="center"> <td nowrap colspan="7">
+ * <font size="+2">Iteration Performance [million function evaluations per second]</font><br> <font size="-1">Pentium
+ * Pro 200 Mhz, SunJDK 1.2.2, NT, java -classic, </font></td> </tr> <tr valign="middle" bgcolor="#66CCFF" nowrap
+ * align="center"> <td nowrap bgcolor="#FF9966" rowspan="2"> </td> <td bgcolor="#FF9966" colspan="2"> <p> 30000000
+ * iterations</p> </td> <td bgcolor="#FF9966" colspan="2"> 3000000 iterations (10 times less)</td> <td bgcolor="#FF9966"
+ * colspan="2"> </td> </tr> <tr valign="middle" bgcolor="#66CCFF" nowrap align="center"> <td bgcolor="#FF9966">
+ * <tt>F.plus</tt></td> <td bgcolor="#FF9966"><tt>a+b</tt></td> <td bgcolor="#FF9966">
+ * <tt>F.chain(F.abs,F.chain(F.plus,F.sin,F.chain(F.square,F.cos)))</tt></td> <td bgcolor="#FF9966">
+ * <tt>Math.abs(Math.sin(a) + Math.pow(Math.cos(b),2))</tt></td> <td bgcolor="#FF9966"> </td> <td
+ * bgcolor="#FF9966"> </td> </tr> <tr valign="middle" bgcolor="#66CCFF" nowrap align="center"> <td nowrap
+ * bgcolor="#FF9966"> </td> <td nowrap>10.8</td> <td nowrap>29.6</td> <td nowrap>0.43</td> <td nowrap>0.35</td> <td
+ * nowrap> </td> <td nowrap> </td> </tr> </table></center>
+ *
+ * @author wolfgang.hoschek@cern.ch
+ * @version 1.0, 09/24/99
+ * @deprecated until unit tests are in place. Until this time, this class/interface is unsupported.
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public class Functions {
+
+ /*****************************
+ * <H3>Unary functions</H3>
+ *****************************/
+ /** Function that returns <tt>Math.abs(a)</tt>. */
+ public static final DoubleFunction abs = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.abs(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.acos(a)</tt>. */
+ public static final DoubleFunction acos = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.acos(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.asin(a)</tt>. */
+ public static final DoubleFunction asin = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.asin(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.atan(a)</tt>. */
+ public static final DoubleFunction atan = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.atan(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.ceil(a)</tt>. */
+ public static final DoubleFunction ceil = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.ceil(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.cos(a)</tt>. */
+ public static final DoubleFunction cos = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.cos(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.exp(a)</tt>. */
+ public static final DoubleFunction exp = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.exp(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.floor(a)</tt>. */
+ public static final DoubleFunction floor = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.floor(a);
+ }
+ };
+
+ /** Function that returns its argument. */
+ public static final DoubleFunction identity = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a;
+ }
+ };
+
+ /** Function that returns <tt>1.0 / a</tt>. */
+ public static final DoubleFunction inv = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return 1.0 / a;
+ }
+ };
+
+ /** Function that returns <tt>Math.log(a)</tt>. */
+ public static final DoubleFunction logarithm = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.log(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.log(a) / Math.log(2)</tt>. */
+ public static final DoubleFunction log2 = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.log(a) * 1.4426950408889634;
+ }
+ };
+
+ /** Function that returns <tt>-a</tt>. */
+ public static final DoubleFunction neg = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return -a;
+ }
+ };
+
+ /** Function that returns <tt>Math.rint(a)</tt>. */
+ public static final DoubleFunction rint = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.rint(a);
+ }
+ };
+
+ /** Function that returns <tt>a < 0 ? -1 : a > 0 ? 1 : 0</tt>. */
+ public static final DoubleFunction sign = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a < 0 ? -1 : a > 0 ? 1 : 0;
+ }
+ };
+
+ /** Function that returns <tt>Math.sin(a)</tt>. */
+ public static final DoubleFunction sin = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.sin(a);
+ }
+ };
+
+ /** Function that returns <tt>Math.sqrt(a)</tt>. */
+ public static final DoubleFunction sqrt = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.sqrt(a);
+ }
+ };
+
+ /** Function that returns <tt>a * a</tt>. */
+ public static final DoubleFunction square = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a * a;
+ }
+ };
+
+ /** Function that returns <tt>Math.tan(a)</tt>. */
+ public static final DoubleFunction tan = new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.tan(a);
+ }
+ };
+
+
+ /*****************************
+ * <H3>Binary functions</H3>
+ *****************************/
+
+ /** Function that returns <tt>Math.atan2(a,b)</tt>. */
+ public static final DoubleDoubleFunction atan2 = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return Math.atan2(a, b);
+ }
+ };
+
+ /**
+ * Function that returns <tt>com.imsl.math.Sfun.logBeta(a,b)</tt>.
+ */
+ /*
+ public static final DoubleDoubleFunction logBeta = new DoubleDoubleFunction() {
+ public final double apply(double a, double b) { return Sfun.logBeta(a,b); }
+ };
+ */
+
+
+ /** Function that returns <tt>a < b ? -1 : a > b ? 1 : 0</tt>. */
+ public static final DoubleDoubleFunction compare = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return a < b ? -1 : a > b ? 1 : 0;
+ }
+ };
+
+ /** Function that returns <tt>a / b</tt>. */
+ public static final DoubleDoubleFunction div = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return a / b;
+ }
+ };
+
+ /** Function that returns <tt>a == b ? 1 : 0</tt>. */
+ public static final DoubleDoubleFunction equals = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return a == b ? 1 : 0;
+ }
+ };
+
+ /** Function that returns <tt>a > b ? 1 : 0</tt>. */
+ public static final DoubleDoubleFunction greater = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return a > b ? 1 : 0;
+ }
+ };
+
+ /** Function that returns <tt>Math.IEEEremainder(a,b)</tt>. */
+ public static final DoubleDoubleFunction IEEEremainder = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return Math.IEEEremainder(a, b);
+ }
+ };
+
+ /** Function that returns <tt>a == b</tt>. */
+ public static final DoubleDoubleProcedure isEqual = new DoubleDoubleProcedure() {
+ @Override
+ public boolean apply(double a, double b) {
+ return a == b;
+ }
+ };
+
+ /** Function that returns <tt>a < b</tt>. */
+ public static final DoubleDoubleProcedure isLess = new DoubleDoubleProcedure() {
+ @Override
+ public boolean apply(double a, double b) {
+ return a < b;
+ }
+ };
+
+ /** Function that returns <tt>a > b</tt>. */
+ public static final DoubleDoubleProcedure isGreater = new DoubleDoubleProcedure() {
+ @Override
+ public boolean apply(double a, double b) {
+ return a > b;
+ }
+ };
+
+ /** Function that returns <tt>a < b ? 1 : 0</tt>. */
+ public static final DoubleDoubleFunction less = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return a < b ? 1 : 0;
+ }
+ };
+
+ /** Function that returns <tt>Math.log(a) / Math.log(b)</tt>. */
+ public static final DoubleDoubleFunction lg = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return Math.log(a) / Math.log(b);
+ }
+ };
+
+ /** Function that returns <tt>Math.max(a,b)</tt>. */
+ public static final DoubleDoubleFunction max = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return Math.max(a, b);
+ }
+ };
+
+ /** Function that returns <tt>Math.min(a,b)</tt>. */
+ public static final DoubleDoubleFunction min = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return Math.min(a, b);
+ }
+ };
+
+ /** Function that returns <tt>a - b</tt>. */
+ public static final DoubleDoubleFunction minus = plusMult(-1);
+ /*
+ new DoubleDoubleFunction() {
+ public final double apply(double a, double b) { return a - b; }
+ };
+ */
+
+ /** Function that returns <tt>a % b</tt>. */
+ public static final DoubleDoubleFunction mod = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return a % b;
+ }
+ };
+
+ /** Function that returns <tt>a * b</tt>. */
+ public static final DoubleDoubleFunction mult = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return a * b;
+ }
+ };
+
+ /** Function that returns <tt>a + b</tt>. */
+ public static final DoubleDoubleFunction plus = plusMult(1);
+ /*
+ new DoubleDoubleFunction() {
+ public final double apply(double a, double b) { return a + b; }
+ };
+ */
+
+ /** Function that returns <tt>Math.abs(a) + Math.abs(b)</tt>. */
+ public static final DoubleDoubleFunction plusAbs = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return Math.abs(a) + Math.abs(b);
+ }
+ };
+
+ /** Function that returns <tt>Math.pow(a,b)</tt>. */
+ public static final DoubleDoubleFunction pow = new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return Math.pow(a, b);
+ }
+ };
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected Functions() {
+ }
+
+ /**
+ * Constructs a function that returns <tt>(from<=a && a<=to) ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>from</tt> and
+ * <tt>to</tt> are fixed.
+ */
+ public static DoubleFunction between(final double from, final double to) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return (from <= a && a <= to) ? 1 : 0;
+ }
+ };
+ }
+
+ /**
+ * Constructs a unary function from a binary function with the first operand (argument) fixed to the given constant
+ * <tt>c</tt>. The second operand is variable (free).
+ *
+ * @param function a binary function taking operands in the form <tt>function.apply(c,var)</tt>.
+ * @return the unary function <tt>function(c,var)</tt>.
+ */
+ public static DoubleFunction bindArg1(final DoubleDoubleFunction function, final double c) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double var) {
+ return function.apply(c, var);
+ }
+ };
+ }
+
+ /**
+ * Constructs a unary function from a binary function with the second operand (argument) fixed to the given constant
+ * <tt>c</tt>. The first operand is variable (free).
+ *
+ * @param function a binary function taking operands in the form <tt>function.apply(var,c)</tt>.
+ * @return the unary function <tt>function(var,c)</tt>.
+ */
+ public static DoubleFunction bindArg2(final DoubleDoubleFunction function, final double c) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double var) {
+ return function.apply(var, c);
+ }
+ };
+ }
+
+ /**
+ * Constructs the function <tt>f( g(a), h(b) )</tt>.
+ *
+ * @param f a binary function.
+ * @param g a unary function.
+ * @param h a unary function.
+ * @return the binary function <tt>f( g(a), h(b) )</tt>.
+ */
+ public static DoubleDoubleFunction chain(final DoubleDoubleFunction f, final DoubleFunction g,
+ final DoubleFunction h) {
+ return new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return f.apply(g.apply(a), h.apply(b));
+ }
+ };
+ }
+
+ /**
+ * Constructs the function <tt>g( h(a,b) )</tt>.
+ *
+ * @param g a unary function.
+ * @param h a binary function.
+ * @return the unary function <tt>g( h(a,b) )</tt>.
+ */
+ public static DoubleDoubleFunction chain(final DoubleFunction g, final DoubleDoubleFunction h) {
+ return new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return g.apply(h.apply(a, b));
+ }
+ };
+ }
+
+ /**
+ * Constructs the function <tt>g( h(a) )</tt>.
+ *
+ * @param g a unary function.
+ * @param h a unary function.
+ * @return the unary function <tt>g( h(a) )</tt>.
+ */
+ public static DoubleFunction chain(final DoubleFunction g, final DoubleFunction h) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return g.apply(h.apply(a));
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns <tt>a < b ? -1 : a > b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is
+ * fixed.
+ */
+ public static DoubleFunction compare(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a < b ? -1 : a > b ? 1 : 0;
+ }
+ };
+ }
+
+ /** Constructs a function that returns the constant <tt>c</tt>. */
+ public static DoubleFunction constant(final double c) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return c;
+ }
+ };
+ }
+
+
+ /** Constructs a function that returns <tt>a / b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction div(double b) {
+ return mult(1 / b);
+ }
+
+ /** Constructs a function that returns <tt>a == b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction equals(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a == b ? 1 : 0;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a > b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction greater(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a > b ? 1 : 0;
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns <tt>Math.IEEEremainder(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is
+ * fixed.
+ */
+ public static DoubleFunction IEEEremainder(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.IEEEremainder(a, b);
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns <tt>from<=a && a<=to</tt>. <tt>a</tt> is a variable, <tt>from</tt> and
+ * <tt>to</tt> are fixed.
+ */
+ public static DoubleProcedure isBetween(final double from, final double to) {
+ return new DoubleProcedure() {
+ @Override
+ public boolean apply(double a) {
+ return from <= a && a <= to;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a == b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleProcedure isEqual(final double b) {
+ return new DoubleProcedure() {
+ @Override
+ public boolean apply(double a) {
+ return a == b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a > b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleProcedure isGreater(final double b) {
+ return new DoubleProcedure() {
+ @Override
+ public boolean apply(double a) {
+ return a > b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a < b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleProcedure isLess(final double b) {
+ return new DoubleProcedure() {
+ @Override
+ public boolean apply(double a) {
+ return a < b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a < b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction less(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a < b ? 1 : 0;
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns <tt><tt>Math.log(a) / Math.log(b)</tt></tt>. <tt>a</tt> is a variable,
+ * <tt>b</tt> is fixed.
+ */
+ public static DoubleFunction lg(final double b) {
+ return new DoubleFunction() {
+ private final double logInv = 1 / Math.log(b); // cached for speed
+
+ @Override
+ public double apply(double a) {
+ return Math.log(a) * logInv;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>Math.max(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction max(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.max(a, b);
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>Math.min(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction min(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.min(a, b);
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a - b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction minus(double b) {
+ return plus(-b);
+ }
+
+ /**
+ * Constructs a function that returns <tt>a - b*constant</tt>. <tt>a</tt> and <tt>b</tt> are variables,
+ * <tt>constant</tt> is fixed.
+ */
+ public static DoubleDoubleFunction minusMult(double constant) {
+ return plusMult(-constant);
+ }
+
+ /** Constructs a function that returns <tt>a % b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction mod(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a % b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a * b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction mult(double b) {
+ return new Mult(b);
+ /*
+ return new DoubleFunction() {
+ public final double apply(double a) { return a * b; }
+ };
+ */
+ }
+
+ /** Constructs a function that returns <tt>a + b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction plus(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return a + b;
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns <tt>a + b*constant</tt>. <tt>a</tt> and <tt>b</tt> are variables,
+ * <tt>constant</tt> is fixed.
+ */
+ public static DoubleDoubleFunction plusMult(double constant) {
+ return new PlusMult(constant);
+ /*
+ return new DoubleDoubleFunction() {
+ public final double apply(double a, double b) { return a + b*constant; }
+ };
+ */
+ }
+
+ /** Constructs a function that returns <tt>Math.pow(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static DoubleFunction pow(final double b) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.pow(a, b);
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns a new uniform random number in the open unit interval <code>(0.0,1.0)</code>
+ * (excluding 0.0 and 1.0). Currently the engine is {@link org.apache.mahout.math.jet.random.engine.MersenneTwister} and is
+ * seeded with the current time. <p> Note that any random engine derived from {@link
+ * org.apache.mahout.math.jet.random.engine.RandomEngine} and any random distribution derived from {@link
+ * org.apache.mahout.math.jet.random.AbstractDistribution} are function objects, because they implement the proper
+ * interfaces. Thus, if you are not happy with the default, just pass your favourite random generator to function
+ * evaluating methods.
+ */
+ public static DoubleFunction random() {
+ return new MersenneTwister(new Date());
+ }
+
+ /**
+ * Constructs a function that returns the number rounded to the given precision;
+ * <tt>Math.rint(a/precision)*precision</tt>. Examples:
+ * <pre>
+ * precision = 0.01 rounds 0.012 --> 0.01, 0.018 --> 0.02
+ * precision = 10 rounds 123 --> 120 , 127 --> 130
+ * </pre>
+ */
+ public static DoubleFunction round(final double precision) {
+ return new DoubleFunction() {
+ @Override
+ public double apply(double a) {
+ return Math.rint(a / precision) * precision;
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns <tt>function.apply(b,a)</tt>, i.e. applies the function with the first operand
+ * as second operand and the second operand as first operand.
+ *
+ * @param function a function taking operands in the form <tt>function.apply(a,b)</tt>.
+ * @return the binary function <tt>function(b,a)</tt>.
+ */
+ public static DoubleDoubleFunction swapArgs(final DoubleDoubleFunction function) {
+ return new DoubleDoubleFunction() {
+ @Override
+ public double apply(double a, double b) {
+ return function.apply(b, a);
+ }
+ };
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Functions.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/IntFunctions.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/IntFunctions.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/IntFunctions.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/IntFunctions.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,370 @@
+/*
+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.math;
+
+import org.apache.mahout.math.function.IntFunction;
+import org.apache.mahout.math.function.IntIntFunction;
+import org.apache.mahout.math.jet.random.engine.MersenneTwister;
+
+import java.util.Date;
+
+/**
+ Integer Function objects to be passed to generic methods.
+ Same as {@link Functions} except operating on integers.
+ <p>
+ For aliasing see {@link #intFunctions}.
+
+ @author wolfgang.hoschek@cern.ch
+ @version 1.0, 09/24/99
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public class IntFunctions {
+
+ /*****************************
+ * <H3>Unary functions</H3>
+ *****************************/
+ /** Function that returns <tt>Math.abs(a) == (a < 0) ? -a : a</tt>. */
+ public static final IntFunction abs = new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return (a < 0) ? -a : a;
+ }
+ };
+
+ /** Function that returns its argument. */
+ public static final IntFunction identity = new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a;
+ }
+ };
+
+ /** Function that returns <tt>~a</tt>. */
+ public static final IntFunction not = new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return ~a;
+ }
+ };
+
+ /** Function that returns <tt>a < 0 ? -1 : a > 0 ? 1 : 0</tt>. */
+ public static final IntFunction sign = new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a < 0 ? -1 : a > 0 ? 1 : 0;
+ }
+ };
+
+ /** Function that returns <tt>a * a</tt>. */
+ public static final IntFunction square = new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a * a;
+ }
+ };
+
+
+ /*****************************
+ * <H3>Binary functions</H3>
+ *****************************/
+
+ /** Function that returns <tt>a & b</tt>. */
+ public static final IntIntFunction and = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return a & b;
+ }
+ };
+
+ /** Function that returns <tt>a < b ? -1 : a > b ? 1 : 0</tt>. */
+ public static final IntIntFunction compare = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return a < b ? -1 : a > b ? 1 : 0;
+ }
+ };
+
+ /** Function that returns <tt>a / b</tt>. */
+ public static final IntIntFunction div = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return a / b;
+ }
+ };
+
+ /** Function that returns <tt>a == b ? 1 : 0</tt>. */
+ public static final IntIntFunction equals = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return a == b ? 1 : 0;
+ }
+ };
+
+ /** Function that returns <tt>Math.max(a,b)</tt>. */
+ public static final IntIntFunction max = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return (a >= b) ? a : b;
+ }
+ };
+
+ /** Function that returns <tt>Math.min(a,b)</tt>. */
+ public static final IntIntFunction min = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return (a <= b) ? a : b;
+ }
+ };
+
+ /** Function that returns <tt>a - b</tt>. */
+ public static final IntIntFunction minus = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return a - b;
+ }
+ };
+
+ /** Function that returns <tt>a * b</tt>. */
+ public static final IntIntFunction mult = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return a * b;
+ }
+ };
+
+ /** Function that returns <tt>a | b</tt>. */
+ public static final IntIntFunction or = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return a | b;
+ }
+ };
+
+ /** Function that returns <tt>a + b</tt>. */
+ public static final IntIntFunction plus = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return a + b;
+ }
+ };
+
+ /** Function that returns <tt>(int) Math.pow(a,b)</tt>. */
+ public static final IntIntFunction pow = new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return (int) Math.pow(a, b);
+ }
+ };
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected IntFunctions() {
+ }
+
+ /** Constructs a function that returns <tt>a & b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction and(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a & b;
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns <tt>(from<=a && a<=to) ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>from</tt> and
+ * <tt>to</tt> are fixed.
+ */
+ public static IntFunction between(final int from, final int to) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return (from <= a && a <= to) ? 1 : 0;
+ }
+ };
+ }
+
+ /**
+ * Constructs the function <tt>g( h(a) )</tt>.
+ *
+ * @param g a unary function.
+ * @param h a unary function.
+ * @return the unary function <tt>g( h(a) )</tt>.
+ */
+ public static IntFunction chain(final IntFunction g, final IntFunction h) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return g.apply(h.apply(a));
+ }
+ };
+ }
+
+ /**
+ * Constructs the function <tt>g( h(a,b) )</tt>.
+ *
+ * @param g a unary function.
+ * @param h a binary function.
+ * @return the unary function <tt>g( h(a,b) )</tt>.
+ */
+ public static IntIntFunction chain(final IntFunction g, final IntIntFunction h) {
+ return new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return g.apply(h.apply(a, b));
+ }
+ };
+ }
+
+ /**
+ * Constructs the function <tt>f( g(a), h(b) )</tt>.
+ *
+ * @param f a binary function.
+ * @param g a unary function.
+ * @param h a unary function.
+ * @return the binary function <tt>f( g(a), h(b) )</tt>.
+ */
+ public static IntIntFunction chain(final IntIntFunction f, final IntFunction g, final IntFunction h) {
+ return new IntIntFunction() {
+ @Override
+ public int apply(int a, int b) {
+ return f.apply(g.apply(a), h.apply(b));
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that returns <tt>a < b ? -1 : a > b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is
+ * fixed.
+ */
+ public static IntFunction compare(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a < b ? -1 : a > b ? 1 : 0;
+ }
+ };
+ }
+
+ /** Constructs a function that returns the constant <tt>c</tt>. */
+ public static IntFunction constant(final int c) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return c;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a / b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction div(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a / b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a == b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction equals(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a == b ? 1 : 0;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>Math.max(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction max(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return (a >= b) ? a : b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>Math.min(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction min(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return (a <= b) ? a : b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a - b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction minus(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a - b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a * b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction mult(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a * b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a | b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction or(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a | b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>a + b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction plus(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return a + b;
+ }
+ };
+ }
+
+ /** Constructs a function that returns <tt>(int) Math.pow(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
+ public static IntFunction pow(final int b) {
+ return new IntFunction() {
+ @Override
+ public int apply(int a) {
+ return (int) Math.pow(a, b);
+ }
+ };
+ }
+
+ /**
+ * Constructs a function that 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>). Currently the engine is {@link org.apache.mahout.math.jet.random.engine.MersenneTwister}
+ * and is seeded with the current time. <p> Note that any random engine derived from {@link
+ * org.apache.mahout.math.jet.random.engine.RandomEngine} and any random distribution derived from {@link
+ * org.apache.mahout.math.jet.random.AbstractDistribution} are function objects, because they implement the proper
+ * interfaces. Thus, if you are not happy with the default, just pass your favourite random generator to function
+ * evaluating methods.
+ */
+ public static IntFunction random() {
+ return new MersenneTwister(new Date());
+ }
+
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/IntFunctions.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Mult.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Mult.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Mult.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Mult.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,55 @@
+/*
+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.math;
+
+/**
+ * Only for performance tuning of compute intensive linear algebraic computations.
+ * Constructs functions that return one of
+ * <ul>
+ * <li><tt>a * constant</tt>
+ * <li><tt>a / constant</tt>
+ * </ul>
+ * <tt>a</tt> is variable, <tt>constant</tt> is fixed, but for performance reasons publicly accessible.
+ * Intended to be passed to <tt>matrix.assign(function)</tt> methods.
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public final class Mult implements org.apache.mahout.math.function.DoubleFunction {
+
+ private double multiplicator;
+
+ Mult(double multiplicator) {
+ this.multiplicator = multiplicator;
+ }
+
+ /** Returns the result of the function evaluation. */
+ @Override
+ public double apply(double a) {
+ return a * multiplicator;
+ }
+
+ /** <tt>a / constant</tt>. */
+ public static Mult div(double constant) {
+ return mult(1 / constant);
+ }
+
+ /** <tt>a * constant</tt>. */
+ public static Mult mult(double constant) {
+ return new Mult(constant);
+ }
+
+ public double getMultiplicator() {
+ return multiplicator;
+ }
+
+ public void setMultiplicator(double multiplicator) {
+ this.multiplicator = multiplicator;
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Mult.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/NumericalIntegration.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/NumericalIntegration.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/NumericalIntegration.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/NumericalIntegration.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,17 @@
+/*
+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.math;
+
+/** Not yet commented. */
+class NumericalIntegration extends Constants {
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected NumericalIntegration() {
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/NumericalIntegration.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/PlusMult.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/PlusMult.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/PlusMult.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/PlusMult.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,68 @@
+/*
+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.math;
+
+/**
+ * Only for performance tuning of compute intensive linear algebraic computations.
+ * Constructs functions that return one of
+ * <ul>
+ * <li><tt>a + b*constant</tt>
+ * <li><tt>a - b*constant</tt>
+ * <li><tt>a + b/constant</tt>
+ * <li><tt>a - b/constant</tt>
+ * </ul>
+ * <tt>a</tt> and <tt>b</tt> are variables, <tt>constant</tt> is fixed, but for performance reasons publicly accessible.
+ * Intended to be passed to <tt>matrix.assign(otherMatrix,function)</tt> methods.
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public final class PlusMult implements org.apache.mahout.math.function.DoubleDoubleFunction {
+
+ private double multiplicator;
+
+ /** Insert the method's description here. Creation date: (8/10/99 19:12:09) */
+ PlusMult(double multiplicator) {
+ this.multiplicator = multiplicator;
+ }
+
+ /** Returns the result of the function evaluation. */
+ @Override
+ public double apply(double a, double b) {
+ return a + b * multiplicator;
+ }
+
+ /** <tt>a - b/constant</tt>. */
+ public static PlusMult minusDiv(double constant) {
+ return new PlusMult(-1 / constant);
+ }
+
+ /** <tt>a - b*constant</tt>. */
+ public static PlusMult minusMult(double constant) {
+ return new PlusMult(-constant);
+ }
+
+ /** <tt>a + b/constant</tt>. */
+ public static PlusMult plusDiv(double constant) {
+ return new PlusMult(1 / constant);
+ }
+
+ /** <tt>a + b*constant</tt>. */
+ public static PlusMult plusMult(double constant) {
+ return new PlusMult(constant);
+ }
+
+ public double getMultiplicator() {
+ return multiplicator;
+ }
+
+ public void setMultiplicator(double multiplicator) {
+ this.multiplicator = multiplicator;
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/PlusMult.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,85 @@
+/*
+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.math;
+
+/**
+ * Polynomial functions.
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public class Polynomial extends Constants {
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected Polynomial() {
+ }
+
+ /**
+ * Evaluates the given polynomial of degree <tt>N</tt> at <tt>x</tt>, assuming coefficient of N is 1.0. Otherwise same
+ * as <tt>polevl()</tt>.
+ * <pre>
+ * 2 N
+ * y = C + C x + C x +...+ C x
+ * 0 1 2 N
+ *
+ * where C = 1 and hence is omitted from the array.
+ * N
+ *
+ * Coefficients are stored in reverse order:
+ *
+ * coef[0] = C , ..., coef[N-1] = C .
+ * N-1 0
+ *
+ * Calling arguments are otherwise the same as polevl().
+ * </pre>
+ * In the interest of speed, there are no checks for out of bounds arithmetic.
+ *
+ * @param x argument to the polynomial.
+ * @param coef the coefficients of the polynomial.
+ * @param N the degree of the polynomial.
+ */
+ public static double p1evl(double x, double[] coef, int N) throws ArithmeticException {
+
+ double ans = x + coef[0];
+
+ for (int i = 1; i < N; i++) {
+ ans = ans * x + coef[i];
+ }
+
+ return ans;
+ }
+
+ /**
+ * Evaluates the given polynomial of degree <tt>N</tt> at <tt>x</tt>.
+ * <pre>
+ * 2 N
+ * y = C + C x + C x +...+ C x
+ * 0 1 2 N
+ *
+ * Coefficients are stored in reverse order:
+ *
+ * coef[0] = C , ..., coef[N] = C .
+ * N 0
+ * </pre>
+ * In the interest of speed, there are no checks for out of bounds arithmetic.
+ *
+ * @param x argument to the polynomial.
+ * @param coef the coefficients of the polynomial.
+ * @param N the degree of the polynomial.
+ */
+ public static double polevl(double x, double[] coef, int N) throws ArithmeticException {
+ double ans = coef[0];
+
+ for (int i = 1; i <= N; i++) {
+ ans = ans * x + coef[i];
+ }
+
+ return ans;
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/package.html
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/package.html?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/package.html (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/package.html Thu Dec 17 23:22:16 2009
@@ -0,0 +1,7 @@
+<HTML>
+<BODY>
+Tools for basic and advanced mathematics: Arithmetics and Algebra, Polynomials and Chebyshev series, Bessel and Airy
+functions,
+Function Objects for generic function evaluation, etc.
+</BODY>
+</HTML>
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractContinousDistribution.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractContinousDistribution.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractContinousDistribution.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractContinousDistribution.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,23 @@
+/*
+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;
+
+/**
+ * Abstract base class for all continous distributions.
+ *
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public abstract class AbstractContinousDistribution extends AbstractDistribution {
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected AbstractContinousDistribution() {
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractContinousDistribution.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDiscreteDistribution.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDiscreteDistribution.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDiscreteDistribution.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDiscreteDistribution.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,33 @@
+/*
+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;
+
+/**
+ * Abstract base class for all discrete distributions.
+ *
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public abstract class AbstractDiscreteDistribution extends AbstractDistribution {
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected AbstractDiscreteDistribution() {
+ }
+
+ /** Returns a random number from the distribution; returns <tt>(double) nextInt()</tt>. */
+ @Override
+ public double nextDouble() {
+ return (double) nextInt();
+ }
+
+ /** Returns a random number from the distribution. */
+ @Override
+ public abstract int nextInt();
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDiscreteDistribution.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,86 @@
+/*
+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;
+
+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 abstract class AbstractDistribution extends PersistentObject
+ implements org.apache.mahout.math.function.DoubleFunction, org.apache.mahout.math.function.IntFunction {
+
+ protected RandomEngine randomGenerator;
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected AbstractDistribution() {
+ }
+
+ /**
+ * Equivalent to <tt>nextDouble()</tt>. This has the effect that distributions can now be used as function objects,
+ * returning a random number upon function evaluation.
+ */
+ @Override
+ public double apply(double dummy) {
+ return nextDouble();
+ }
+
+ /**
+ * Equivalent to <tt>nextInt()</tt>. This has the effect that distributions can now be used as function objects,
+ * returning a random number upon function evaluation.
+ */
+ @Override
+ public int apply(int dummy) {
+ return nextInt();
+ }
+
+ /**
+ * Returns a deep 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() {
+ AbstractDistribution copy = (AbstractDistribution) super.clone();
+ if (this.randomGenerator != null) {
+ copy.randomGenerator = (RandomEngine) this.randomGenerator.clone();
+ }
+ return copy;
+ }
+
+ /** Returns the used uniform random number generator; */
+ protected RandomEngine getRandomGenerator() {
+ return randomGenerator;
+ }
+
+ /**
+ * Constructs and returns a new uniform random number generation engine seeded with the current time. Currently this
+ * is {@link org.apache.mahout.math.jet.random.engine.MersenneTwister}.
+ */
+ public static RandomEngine makeDefaultGenerator() {
+ return org.apache.mahout.math.jet.random.engine.RandomEngine.makeDefault();
+ }
+
+ /** Returns a random number from the distribution. */
+ public abstract double nextDouble();
+
+ /**
+ * Returns a random number from the distribution; returns <tt>(int) Math.round(nextDouble())</tt>. Override this
+ * method if necessary.
+ */
+ public int nextInt() {
+ return (int) Math.round(nextDouble());
+ }
+
+ /** Sets the uniform random generator internally used. */
+ protected void setRandomGenerator(RandomEngine randomGenerator) {
+ this.randomGenerator = randomGenerator;
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,461 @@
+/*
+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;
+
+import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import org.apache.mahout.math.jet.stat.Probability;
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public class Beta extends AbstractContinousDistribution {
+
+ private double alpha;
+ private double beta;
+
+ private double PDF_CONST; // cache to speed up pdf()
+
+ // cached values shared by bXX
+ private double a_last = 0.0, b_last = 0.0;
+ private double a_, b_, t, fa, fb, p1, p2;
+
+ // cached values for b00
+
+ // chached values for b01
+ private double ml, mu;
+
+ // chached values for b1prs
+ private double p_last = 0.0, q_last = 0.0;
+ private double a;
+ private double b;
+ private double m;
+ private double D;
+ private double Dl;
+ private double x1;
+ private double x2;
+ private double x4;
+ private double x5;
+ private double f1;
+ private double f2;
+ private double f4;
+ private double f5;
+ private double ll, lr, z2, z4, p3, p4;
+
+
+ // The uniform random number generated shared by all <b>static</b> methods.
+ private static final Beta shared = new Beta(10.0, 10.0, makeDefaultGenerator());
+
+ /** Constructs a Beta distribution. */
+ public Beta(double alpha, double beta, RandomEngine randomGenerator) {
+ setRandomGenerator(randomGenerator);
+ setState(alpha, beta);
+ }
+
+ /**
+ *
+ */
+ protected double b00(double a, double b, RandomEngine randomGenerator) {
+
+ if (a != a_last || b != b_last) {
+ a_last = a;
+ b_last = b;
+
+ a_ = a - 1.0;
+ b_ = b - 1.0;
+ double c = (b * b_) / (a * a_);
+ t = (c == 1.0) ? 0.5 : (1.0 - Math.sqrt(c)) / (1.0 - c); // t = t_opt
+ fa = Math.exp(a_ * Math.log(t));
+ fb = Math.exp(b_ * Math.log(1.0 - t)); // f(t) = fa * fb
+
+ p1 = t / a; // 0 < X < t
+ p2 = (1.0 - t) / b + p1; // t < X < 1
+ }
+
+ double X;
+ while (true) {
+ double Z;
+ double V;
+ double U;
+ if ((U = randomGenerator.raw() * p2) <= p1) { // X < t
+ Z = Math.exp(Math.log(U / p1) / a);
+ X = t * Z;
+ // squeeze accept: L(x) = 1 + (1 - b)x
+ if ((V = randomGenerator.raw() * fb) <= 1.0 - b_ * X) {
+ break;
+ }
+ // squeeze reject: U(x) = 1 + ((1 - t)^(b-1) - 1)/t * x
+ if (V <= 1.0 + (fb - 1.0) * Z) {
+ // quotient accept: q(x) = (1 - x)^(b-1) / fb
+ if (Math.log(V) <= b_ * Math.log(1.0 - X)) {
+ break;
+ }
+ }
+ } else { // X > t
+ Z = Math.exp(Math.log((U - p1) / (p2 - p1)) / b);
+ X = 1.0 - (1.0 - t) * Z;
+ // squeeze accept: L(x) = 1 + (1 - a)(1 - x)
+ if ((V = randomGenerator.raw() * fa) <= 1.0 - a_ * (1.0 - X)) {
+ break;
+ }
+ // squeeze reject: U(x) = 1 + (t^(a-1) - 1)/(1 - t) * (1 - x)
+ if (V <= 1.0 + (fa - 1.0) * Z) {
+ // quotient accept: q(x) = x^(a-1) / fa
+ if (Math.log(V) <= a_ * Math.log(X)) {
+ break;
+ }
+ }
+ }
+ }
+ return (X);
+ }
+
+ /**
+ *
+ */
+ protected double b01(double a, double b, RandomEngine randomGenerator) {
+
+ if (a != a_last || b != b_last) {
+ a_last = a;
+ b_last = b;
+
+ a_ = a - 1.0;
+ b_ = b - 1.0;
+ t = a_ / (a - b); // one step Newton * start value t
+ fb = Math.exp((b_ - 1.0) * Math.log(1.0 - t));
+ fa = a - (a + b_) * t;
+ t -= (t - (1.0 - fa) * (1.0 - t) * fb / b) / (1.0 - fa * fb);
+ fa = Math.exp(a_ * Math.log(t));
+ fb = Math.exp(b_ * Math.log(1.0 - t)); // f(t) = fa * fb
+ if (b_ <= 1.0) {
+ ml = (1.0 - fb) / t; // ml = -m1
+ mu = b_ * t; // mu = -m2 * t
+ } else {
+ ml = b_;
+ mu = 1.0 - fb;
+ }
+ p1 = t / a; // 0 < X < t
+ p2 = fb * (1.0 - t) / b + p1; // t < X < 1
+ }
+
+ double X;
+ while (true) {
+ double Z;
+ double V;
+ double U;
+ if ((U = randomGenerator.raw() * p2) <= p1) { // X < t
+ Z = Math.exp(Math.log(U / p1) / a);
+ X = t * Z;
+ // squeeze accept: L(x) = 1 + m1*x, ml = -m1
+ if ((V = randomGenerator.raw()) <= 1.0 - ml * X) {
+ break;
+ }
+ // squeeze reject: U(x) = 1 + m2*x, mu = -m2 * t
+ if (V <= 1.0 - mu * Z) {
+ // quotient accept: q(x) = (1 - x)^(b-1)
+ if (Math.log(V) <= b_ * Math.log(1.0 - X)) {
+ break;
+ }
+ }
+ } else { // X > t
+ Z = Math.exp(Math.log((U - p1) / (p2 - p1)) / b);
+ X = 1.0 - (1.0 - t) * Z;
+ // squeeze accept: L(x) = 1 + (1 - a)(1 - x)
+ if ((V = randomGenerator.raw() * fa) <= 1.0 - a_ * (1.0 - X)) {
+ break;
+ }
+ // squeeze reject: U(x) = 1 + (t^(a-1) - 1)/(1 - t) * (1 - x)
+ if (V <= 1.0 + (fa - 1.0) * Z) {
+ // quotient accept: q(x) = (x)^(a-1) / fa
+ if (Math.log(V) <= a_ * Math.log(X)) {
+ break;
+ }
+ }
+ }
+ }
+ return (X);
+ }
+
+ /**
+ *
+ */
+ protected double b1prs(double p, double q, RandomEngine randomGenerator) {
+
+ if (p != p_last || q != q_last) {
+ p_last = p;
+ q_last = q;
+
+ a = p - 1.0;
+ b = q - 1.0;
+ double s = a + b;
+ m = a / s;
+ if (a > 1.0 || b > 1.0) {
+ D = Math.sqrt(m * (1.0 - m) / (s - 1.0));
+ }
+
+ if (a <= 1.0) {
+ x2 = (Dl = m * 0.5);
+ x1 = z2 = 0.0;
+ f1 = ll = 0.0;
+ } else {
+ x2 = m - D;
+ x1 = x2 - D;
+ z2 = x2 * (1.0 - (1.0 - x2) / (s * D));
+ if (x1 <= 0.0 || (s - 6.0) * x2 - a + 3.0 > 0.0) {
+ x1 = z2;
+ x2 = (x1 + m) * 0.5;
+ Dl = m - x2;
+ } else {
+ Dl = D;
+ }
+ f1 = f(x1, a, b, m);
+ ll = x1 * (1.0 - x1) / (s * (m - x1)); // z1 = x1 - ll
+ }
+ f2 = f(x2, a, b, m);
+
+ if (b <= 1.0) {
+ x4 = 1.0 - (D = (1.0 - m) * 0.5);
+ x5 = z4 = 1.0;
+ f5 = lr = 0.0;
+ } else {
+ x4 = m + D;
+ x5 = x4 + D;
+ z4 = x4 * (1.0 + (1.0 - x4) / (s * D));
+ if (x5 >= 1.0 || (s - 6.0) * x4 - a + 3.0 < 0.0) {
+ x5 = z4;
+ x4 = (m + x5) * 0.5;
+ D = x4 - m;
+ }
+ f5 = f(x5, a, b, m);
+ lr = x5 * (1.0 - x5) / (s * (x5 - m)); // z5 = x5 + lr
+ }
+ f4 = f(x4, a, b, m);
+
+ p1 = f2 * (Dl + Dl); // x1 < X < m
+ p2 = f4 * (D + D) + p1; // m < X < x5
+ p3 = f1 * ll + p2; // X < x1
+ p4 = f5 * lr + p3; // x5 < X
+ }
+
+ while (true) {
+ double Y;
+ double X;
+ double W;
+ double V;
+ double U;
+ if ((U = randomGenerator.raw() * p4) <= p1) {
+ // immediate accept: x2 < X < m, - f(x2) < W < 0
+ if ((W = U / Dl - f2) <= 0.0) {
+ return (m - U / f2);
+ }
+ // immediate accept: x1 < X < x2, 0 < W < f(x1)
+ if (W <= f1) {
+ return (x2 - W / f1 * Dl);
+ }
+ // candidates for acceptance-rejection-test
+ V = Dl * (U = randomGenerator.raw());
+ X = x2 - V;
+ Y = x2 + V;
+ // squeeze accept: L(x) = f(x2) (x - z2) / (x2 - z2)
+ if (W * (x2 - z2) <= f2 * (X - z2)) {
+ return (X);
+ }
+ if ((V = f2 + f2 - W) < 1.0) {
+ // squeeze accept: L(x) = f(x2) + (1 - f(x2))(x - x2)/(m - x2)
+ if (V <= f2 + (1.0 - f2) * U) {
+ return (Y);
+ }
+ // quotient accept: x2 < Y < m, W >= 2f2 - f(Y)
+ if (V <= f(Y, a, b, m)) {
+ return (Y);
+ }
+ }
+ } else if (U <= p2) {
+ U -= p1;
+ // immediate accept: m < X < x4, - f(x4) < W < 0
+ if ((W = U / D - f4) <= 0.0) {
+ return (m + U / f4);
+ }
+ // immediate accept: x4 < X < x5, 0 < W < f(x5)
+ if (W <= f5) {
+ return (x4 + W / f5 * D);
+ }
+ // candidates for acceptance-rejection-test
+ V = D * (U = randomGenerator.raw());
+ X = x4 + V;
+ Y = x4 - V;
+ // squeeze accept: L(x) = f(x4) (z4 - x) / (z4 - x4)
+ if (W * (z4 - x4) <= f4 * (z4 - X)) {
+ return (X);
+ }
+ if ((V = f4 + f4 - W) < 1.0) {
+ // squeeze accept: L(x) = f(x4) + (1 - f(x4))(x4 - x)/(x4 - m)
+ if (V <= f4 + (1.0 - f4) * U) {
+ return (Y);
+ }
+ // quotient accept: m < Y < x4, W >= 2f4 - f(Y)
+ if (V <= f(Y, a, b, m)) {
+ return (Y);
+ }
+ }
+ } else if (U <= p3) { // X < x1
+ Y = Math.log(U = (U - p2) / (p3 - p2));
+ if ((X = x1 + ll * Y) <= 0.0) {
+ continue;
+ } // X > 0!!
+ W = randomGenerator.raw() * U;
+ // squeeze accept: L(x) = f(x1) (x - z1) / (x1 - z1)
+ // z1 = x1 - ll, W <= 1 + (X - x1)/ll
+ if (W <= 1.0 + Y) {
+ return (X);
+ }
+ W *= f1;
+ } else { // x5 < X
+ Y = Math.log(U = (U - p3) / (p4 - p3));
+ if ((X = x5 - lr * Y) >= 1.0) {
+ continue;
+ } // X < 1!!
+ W = randomGenerator.raw() * U;
+ // squeeze accept: L(x) = f(x5) (z5 - x) / (z5 - x5)
+ // z5 = x5 + lr, W <= 1 + (x5 - X)/lr
+ if (W <= 1.0 + Y) {
+ return (X);
+ }
+ W *= f5;
+ }
+ // density accept: f(x) = (x/m)^a ((1 - x)/(1 - m))^b
+ if (Math.log(W) <= a * Math.log(X / m) + b * Math.log((1.0 - X) / (1.0 - m))) {
+ return (X);
+ }
+ }
+ }
+
+ /** Returns the cumulative distribution function. */
+ public double cdf(double x) {
+ return Probability.beta(alpha, beta, x);
+ }
+
+ private static double f(double x, double a, double b, double m) {
+ return Math.exp(a * Math.log(x / m) + b * Math.log((1.0 - x) / (1.0 - m)));
+ }
+
+ /** Returns a random number from the distribution. */
+ @Override
+ public double nextDouble() {
+ return nextDouble(alpha, beta);
+ }
+
+ /** Returns a beta distributed random number; bypasses the internal state. */
+ public double nextDouble(double alpha, double beta) {
+/******************************************************************
+ * *
+ * Beta Distribution - Stratified Rejection/Patchwork Rejection *
+ * *
+ ******************************************************************
+ * For parameters a < 1 , b < 1 and a < 1 < b or b < 1 < a *
+ * the stratified rejection methods b00 and b01 of Sakasegawa are *
+ * used. Both procedures employ suitable two-part power functions *
+ * from which samples can be obtained by inversion. *
+ * If a > 1 , b > 1 (unimodal case) the patchwork rejection *
+ * method b1prs of Zechner/Stadlober is utilized: *
+ * The area below the density function f(x) in its body is *
+ * rearranged by certain point reflections. Within a large center *
+ * interval variates are sampled efficiently by rejection from *
+ * uniform hats. Rectangular immediate acceptance regions speed *
+ * up the generation. The remaining tails are covered by *
+ * exponential functions. *
+ * If (a-1)(b-1) = 0 sampling is done by inversion if either a *
+ * or b are not equal to one. If a = b = 1 a uniform random *
+ * variate is delivered. *
+ * *
+ ******************************************************************
+ * *
+ * FUNCTION : - bsprc samples a random variate from the beta *
+ * distribution with parameters a > 0, b > 0. *
+ * REFERENCES : - H. Sakasegawa (1983): Stratified rejection and *
+ * squeeze method for generating beta random *
+ * numbers, Ann. Inst. Statist. Math. 35 B, *
+ * 291-302. *
+ * - H. Zechner, E. Stadlober (1993): Generating *
+ * beta variates via patchwork rejection, *
+ * Computing 50, 1-18. *
+ * *
+ * SUBPROGRAMS: - drand(seed) ... (0,1)-Uniform generator with *
+ * unsigned long integer *seed. *
+ * - b00(seed,a,b) ... Beta generator for a<1, b<1 *
+ * - b01(seed,a,b) ... Beta generator for a<1<b or *
+ * b<1<a *
+ * - b1prs(seed,a,b) ... Beta generator for a>1, b>1 *
+ * with unsigned long integer *seed, double a, b. *
+ * *
+ ******************************************************************/
+ double a = alpha;
+ double b = beta;
+ if (a > 1.0) {
+ if (b > 1.0) {
+ return (b1prs(a, b, randomGenerator));
+ }
+ if (b < 1.0) {
+ return (1.0 - b01(b, a, randomGenerator));
+ }
+ if (b == 1.0) {
+ return (Math.exp(Math.log(randomGenerator.raw()) / a));
+ }
+ }
+
+ if (a < 1.0) {
+ if (b > 1.0) {
+ return (b01(a, b, randomGenerator));
+ }
+ if (b < 1.0) {
+ return (b00(a, b, randomGenerator));
+ }
+ if (b == 1.0) {
+ return (Math.exp(Math.log(randomGenerator.raw()) / a));
+ }
+ }
+
+ if (a == 1.0) {
+ if (b != 1.0) {
+ return (1.0 - Math.exp(Math.log(randomGenerator.raw()) / b));
+ }
+ if (b == 1.0) {
+ return (randomGenerator.raw());
+ }
+ }
+
+ return 0.0;
+ }
+
+ /** Returns the cumulative distribution function. */
+ public double pdf(double x) {
+ if (x < 0 || x > 1) {
+ return 0.0;
+ }
+ return Math.exp(PDF_CONST) * Math.pow(x, alpha - 1) * Math.pow(1 - x, beta - 1);
+ }
+
+ /** Sets the parameters. */
+ public void setState(double alpha, double beta) {
+ this.alpha = alpha;
+ this.beta = beta;
+ this.PDF_CONST = Fun.logGamma(alpha + beta) - Fun.logGamma(alpha) - Fun.logGamma(beta);
+ }
+
+ /** Returns a random number from the distribution. */
+ public static double staticNextDouble(double alpha, double beta) {
+ synchronized (shared) {
+ return shared.nextDouble(alpha, beta);
+ }
+ }
+
+ /** Returns a String representation of the receiver. */
+ public String toString() {
+ return this.getClass().getName() + '(' + alpha + ',' + beta + ')';
+ }
+
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,304 @@
+/*
+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;
+
+import org.apache.mahout.math.jet.math.Arithmetic;
+import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import org.apache.mahout.math.jet.stat.Probability;
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public class Binomial extends AbstractDiscreteDistribution {
+
+ private int n;
+ private double p;
+
+ // cache vars for method generateBinomial(...)
+ private int n_last = -1, n_prev = -1;
+ private double par, np, p0, q, p_last = -1.0, p_prev = -1.0;
+ private int b, m, nm;
+ private double pq, rc, ss, xm, xl, xr, ll, lr, c, p1, p2, p3, p4, ch;
+
+ // cache vars for method pdf(...)
+ private double log_p, log_q, log_n;
+
+ // The uniform random number generated shared by all <b>static</b> methods.
+ private static final Binomial shared = new Binomial(1, 0.5, makeDefaultGenerator());
+
+ /**
+ * Constructs a binomial distribution. Example: n=1, p=0.5.
+ *
+ * @param n the number of trials (also known as <i>sample size</i>).
+ * @param p the probability of success.
+ * @param randomGenerator a uniform random number generator.
+ * @throws IllegalArgumentException if <tt>n*Math.min(p,1-p) <= 0.0</tt>
+ */
+ public Binomial(int n, double p, RandomEngine randomGenerator) {
+ setRandomGenerator(randomGenerator);
+ setNandP(n, p);
+ }
+
+ /** Returns the cumulative distribution function. */
+ public double cdf(int k) {
+ return Probability.binomial(k, n, p);
+ }
+
+ /** Returns the cumulative distribution function. */
+ private double cdfSlow(int k) {
+ if (k < 0) {
+ throw new IllegalArgumentException();
+ }
+
+ double sum = 0.0;
+ for (int r = 0; r <= k; r++) {
+ sum += pdf(r);
+ }
+
+ return sum;
+ }
+
+ /**
+ * *************************************************************** * Binomial-Distribution - Acceptance
+ * Rejection/Inversion * * ***************************************************************** * Acceptance
+ * Rejection method combined with Inversion for * generating Binomial random numbers with parameters * n
+ * (number of trials) and p (probability of success). * For min(n*p,n*(1-p)) < 10 the Inversion method is
+ * applied: * The random numbers are generated via sequential search, * starting at the lowest index k=0. The
+ * cumulative probabilities * are avoided by using the technique of chop-down. * For min(n*p,n*(1-p)) >=
+ * 10 Acceptance Rejection is used: * The algorithm is based on a hat-function which is uniform in * the centre
+ * region and exponential in the tails. * A triangular immediate acceptance region in the centre speeds
+ * * up the generation of binomial variates. * If candidate k is near the mode, f(k) is
+ * computed recursively * starting at the mode m. * The acceptance test by
+ * Stirling's formula is modified * according to W. Hoermann (1992): The generation of binomial * random
+ * variates, to appear in J. Statist. Comput. Simul. * If p < .5 the algorithm is applied to parameters n, p.
+ * * Otherwise p is replaced by 1-p, and k is replaced by n - k. * * *****************************************************************
+ * * FUNCTION: - samples a random number from the binomial * distribution with parameters n and p and is *
+ * valid for n*min(p,1-p) > 0. * REFERENCE: - V. Kachitvichyanukul, B.W. Schmeiser (1988): * Binomial random
+ * variate generation, * Communications of the ACM 31, 216-222. * SUBPROGRAMS: - StirlingCorrection() * ...
+ * Correction term of the Stirling * approximation for log(k!) * (series in 1/k or table values * for small k) with
+ * long int k * - randomGenerator ... (0,1)-Uniform engine * * ****************************************************************
+ */
+ protected int generateBinomial(int n, double p) {
+
+
+ int i;
+ double f;
+
+ if (n != n_last || p != p_last) { // set-up
+ n_last = n;
+ p_last = p;
+ par = Math.min(p, 1.0 - p);
+ q = 1.0 - par;
+ np = n * par;
+
+ // Check for invalid input values
+
+ if (np <= 0.0) {
+ return -1;
+ }
+
+ double rm = np + par;
+ m = (int) rm; // mode, integer
+ if (np < 10) {
+ p0 = Math.exp(n * Math.log(q)); // Chop-down
+ int bh = (int) (np + 10.0 * Math.sqrt(np * q));
+ b = Math.min(n, bh);
+ } else {
+ rc = (n + 1.0) * (pq = par / q); // recurr. relat.
+ ss = np * q; // variance
+ i = (int) (2.195 * Math.sqrt(ss) - 4.6 * q); // i = p1 - 0.5
+ xm = m + 0.5;
+ xl = (double) (m - i); // limit left
+ xr = (double) (m + i + 1L); // limit right
+ f = (rm - xl) / (rm - xl * par);
+ ll = f * (1.0 + 0.5 * f);
+ f = (xr - rm) / (xr * q);
+ lr = f * (1.0 + 0.5 * f);
+ c = 0.134 + 20.5 / (15.3 + (double) m); // parallelogram
+ // height
+ p1 = i + 0.5;
+ p2 = p1 * (1.0 + c + c); // probabilities
+ p3 = p2 + c / ll; // of regions 1-4
+ p4 = p3 + c / lr;
+ }
+ }
+
+ double U;
+ int K;
+ if (np < 10) { //Inversion Chop-down
+
+ K = 0;
+ double pk = p0;
+ U = randomGenerator.raw();
+ while (U > pk) {
+ ++K;
+ if (K > b) {
+ U = randomGenerator.raw();
+ K = 0;
+ pk = p0;
+ } else {
+ U -= pk;
+ pk = ((n - K + 1) * par * pk) / (K * q);
+ }
+ }
+ return ((p > 0.5) ? (n - K) : K);
+ }
+
+ int DMAX_KM = 20;
+ double C1_6 = 0.16666666666666667;
+ double C5_8 = 0.62500000000000000;
+ double C1_3 = 0.33333333333333333;
+ while (true) {
+ double V = randomGenerator.raw();
+ if ((U = randomGenerator.raw() * p4) <= p1) { // triangular region
+ K = (int) (xm - U + p1 * V);
+ return (p > 0.5) ? (n - K) : K; // immediate accept
+ }
+ double X;
+ if (U <= p2) { // parallelogram
+ X = xl + (U - p1) / c;
+ if ((V = V * c + 1.0 - Math.abs(xm - X) / p1) >= 1.0) {
+ continue;
+ }
+ K = (int) X;
+ } else if (U <= p3) { // left tail
+ if ((X = xl + Math.log(V) / ll) < 0.0) {
+ continue;
+ }
+ K = (int) X;
+ V *= (U - p2) * ll;
+ } else { // right tail
+ if ((K = (int) (xr - Math.log(V) / lr)) > n) {
+ continue;
+ }
+ V *= (U - p3) * lr;
+ }
+
+ // acceptance test : two cases, depending on |K - m|
+ int Km;
+ if ((Km = Math.abs(K - m)) <= DMAX_KM || Km + Km + 2L >= ss) {
+
+ // computation of p(K) via recurrence relationship from the mode
+ f = 1.0; // f(m)
+ if (m < K) {
+ for (i = m; i < K;) {
+ if ((f *= (rc / ++i - pq)) < V) {
+ break;
+ } // multiply f
+ }
+ } else {
+ for (i = K; i < m;) {
+ if ((V *= (rc / ++i - pq)) > f) {
+ break;
+ } // multiply V
+ }
+ }
+ if (V <= f) {
+ break;
+ } // acceptance test
+ } else {
+
+ // lower and upper squeeze tests, based on lower bounds for log p(K)
+ V = Math.log(V);
+ double T = -Km * Km / (ss + ss);
+ double E = (Km / ss) * ((Km * (Km * C1_3 + C5_8) + C1_6) / ss + 0.5);
+ if (V <= T - E) {
+ break;
+ }
+ if (V <= T + E) {
+ if (n != n_prev || par != p_prev) {
+ n_prev = n;
+ p_prev = par;
+
+ nm = n - m + 1;
+ ch = xm * Math.log((m + 1.0) / (pq * nm)) +
+ Arithmetic.stirlingCorrection(m + 1) + Arithmetic.stirlingCorrection(nm);
+ }
+ int nK = n - K + 1;
+
+ // computation of log f(K) via Stirling's formula
+ // final acceptance-rejection test
+ if (V <= ch + (n + 1.0) * Math.log((double) nm / (double) nK) +
+ (K + 0.5) * Math.log(nK * pq / (K + 1.0)) -
+ Arithmetic.stirlingCorrection(K + 1) - Arithmetic.stirlingCorrection(nK)) {
+ break;
+ }
+ }
+ }
+ }
+ return (p > 0.5) ? (n - K) : K;
+ }
+
+ /** Returns a random number from the distribution. */
+ @Override
+ public int nextInt() {
+ return generateBinomial(n, p);
+ }
+
+ /**
+ * Returns a random number from the distribution with the given parameters n and p; bypasses the internal state.
+ *
+ * @param n the number of trials
+ * @param p the probability of success.
+ * @throws IllegalArgumentException if <tt>n*Math.min(p,1-p) <= 0.0</tt>
+ */
+ public int nextInt(int n, double p) {
+ if (n * Math.min(p, 1 - p) <= 0.0) {
+ throw new IllegalArgumentException();
+ }
+ return generateBinomial(n, p);
+ }
+
+ /** Returns the probability distribution function. */
+ public double pdf(int k) {
+ if (k < 0) {
+ throw new IllegalArgumentException();
+ }
+ int r = this.n - k;
+ return Math
+ .exp(this.log_n - Arithmetic.logFactorial(k) - Arithmetic.logFactorial(r) + this.log_p * k + this.log_q * r);
+ }
+
+ /**
+ * Sets the parameters number of trials and the probability of success.
+ *
+ * @param n the number of trials
+ * @param p the probability of success.
+ * @throws IllegalArgumentException if <tt>n*Math.min(p,1-p) <= 0.0</tt>
+ */
+ public void setNandP(int n, double p) {
+ if (n * Math.min(p, 1 - p) <= 0.0) {
+ throw new IllegalArgumentException();
+ }
+ this.n = n;
+ this.p = p;
+
+ this.log_p = Math.log(p);
+ this.log_q = Math.log(1.0 - p);
+ this.log_n = Arithmetic.logFactorial(n);
+ }
+
+ /**
+ * Returns a random number from the distribution with the given parameters n and p.
+ *
+ * @param n the number of trials
+ * @param p the probability of success.
+ * @throws IllegalArgumentException if <tt>n*Math.min(p,1-p) <= 0.0</tt>
+ */
+ public static int staticNextInt(int n, double p) {
+ synchronized (shared) {
+ return shared.nextInt(n, p);
+ }
+ }
+
+ /** Returns a String representation of the receiver. */
+ public String toString() {
+ return this.getClass().getName() + '(' + n + ',' + p + ')';
+ }
+
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java
------------------------------------------------------------------------------
svn:eol-style = native