http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/Functions.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/Functions.java b/core/src/main/java/org/apache/mahout/math/function/Functions.java deleted file mode 100644 index 61bd5f3..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/Functions.java +++ /dev/null @@ -1,1730 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* -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.function; - - -import com.google.common.base.Preconditions; -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 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 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(org.apache.mahout.math.function.DoubleDoubleFunction ,double)} and {@link - * #bindArg2(org.apache.mahout.math.function.DoubleDoubleFunction ,double)}. The order of arguments can - * be swapped so that the first argument becomes the - * second and vice-versa. See method {@link #swapArgs(org.apache.mahout.math.function.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(org.apache.mahout.math.function.DoubleFunction , org.apache.mahout.math.function.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(org.apache.mahout.math.function.DoubleFunction , org.apache.mahout.math.function.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(org.apache.mahout.math.function.DoubleDoubleFunction , org.apache.mahout.math.function.DoubleFunction , - * org.apache.mahout.math.function.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 functions. Try this <table> <tr><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></tr></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" 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" 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" align="center"> <td nowrap 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" 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> - */ -public final 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 NEGATE = 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 {@code a < 0 ? -1 : a > 0 ? 1 : 0}. - */ - 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> 1 / (1 + exp(-a) </tt> */ - public static final DoubleFunction SIGMOID = new DoubleFunction() { - @Override - public double apply(double a) { - return 1.0 / (1.0 + Math.exp(-a)); - } - }; - - /** Function that returns <tt> a * (1-a) </tt> */ - public static final DoubleFunction SIGMOIDGRADIENT = new DoubleFunction() { - @Override - public double apply(double a) { - return a * (1.0 - 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>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; - } - - /** - * x / 0 = infinity or undefined depending on x - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * 0 / y = 0 unless y = 0 - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * x / 0 = infinity or undefined depending on x - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * x / y != y / x - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return false; - } - - /** - * x / (y / z) = x * z / y - * (x / y) / z = x / (y * z) - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - - }; - - /** 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; - } - - /** - * x = y iff y = x - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - }; - - /** - * 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 IEEE_REMAINDER = 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 IS_EQUAL = new DoubleDoubleProcedure() { -// -// @Override -// public boolean apply(double a, double b) { -// return a == b; -// } -// }; -// -// /** -// * Function that returns {@code a < b}. -// */ -// public static final DoubleDoubleProcedure IS_LESS = new DoubleDoubleProcedure() { -// -// @Override -// public boolean apply(double a, double b) { -// return a < b; -// } -// }; -// -// /** -// * Function that returns {@code a > b}. -// */ -// public static final DoubleDoubleProcedure IS_GREATER = 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); - } - - /** - * max(x, 0) = x or 0 depending on the sign of x - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * max(0, y) = y or 0 depending on the sign of y - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * max(x, 0) = x or 0 depending on the sign of x - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * max(x, max(y, z)) = max(max(x, y), z) - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return true; - } - - /** - * max(x, y) = max(y, x) - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - }; - - public static final DoubleDoubleFunction MAX_ABS = new DoubleDoubleFunction() { - - @Override - public double apply(double a, double b) { - return Math.max(Math.abs(a), Math.abs(b)); - } - - /** - * max(|x|, 0) = |x| - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return true; - } - - /** - * max(0, |y|) = |y| - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * max(|x|, 0) = |x| - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * max(|x|, max(|y|, |z|)) = max(max(|x|, |y|), |z|) - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return true; - } - - /** - * max(|x|, |y|) = max(|y\, |x\) - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - }; - - /** 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); - } - - /** - * min(x, 0) = x or 0 depending on the sign of x - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * min(0, y) = y or 0 depending on the sign of y - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * min(x, 0) = x or 0 depending on the sign of x - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * min(x, min(y, z)) = min(min(x, y), z) - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return true; - } - - /** - * min(x, y) = min(y, x) - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - }; - - /** Function that returns <tt>a - b</tt>. */ - public static final DoubleDoubleFunction MINUS = plusMult(-1); - - public static final DoubleDoubleFunction MINUS_SQUARED = new DoubleDoubleFunction() { - @Override - public double apply(double x, double y) { - return (x - y) * (x - y); - } - - /** - * (x - 0)^2 = x^2 != x - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * (0 - y)^2 != 0 - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * (x - 0)^2 != x - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * (x - y)^2 = (y - x)^2 - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - - /** - * (x - (y - z)^2)^2 != ((x - y)^2 - z)^2 - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - - /** 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 TimesFunction(); - - /** Function that returns <tt>a + b</tt>. */ - public static final DoubleDoubleFunction PLUS = plusMult(1); - - /** Function that returns <tt>Math.abs(a) + Math.abs(b)</tt>. */ - public static final DoubleDoubleFunction PLUS_ABS = new DoubleDoubleFunction() { - - @Override - public double apply(double a, double b) { - return Math.abs(a) + Math.abs(b); - } - - /** - * abs(x) + abs(0) = abs(x) != x - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * abs(0) + abs(y) = abs(y) != 0 unless y = 0 - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * abs(x) + abs(0) = abs(x) != 0 unless x = 0 - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * abs(x) + abs(abs(y) + abs(z)) = abs(x) + abs(y) + abs(z) - * abs(abs(x) + abs(y)) + abs(z) = abs(x) + abs(y) + abs(z) - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return true; - } - - /** - * abs(x) + abs(y) = abs(y) + abs(x) - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - }; - - public static final DoubleDoubleFunction MINUS_ABS = new DoubleDoubleFunction() { - @Override - public double apply(double x, double y) { - return Math.abs(x - y); - } - - /** - * |x - 0| = |x| - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * |0 - y| = |y| - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * |x - 0| = |x| - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * |x - y| = |y - x| - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - - /** - * |x - |y - z|| != ||x - y| - z| (|5 - |4 - 3|| = 1; ||5 - 4| - 3| = |1 - 3| = 2) - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - - /** 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); - } - - /** - * x^0 = 1 for any x unless x = 0 (undefined) - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * 0^y = 0 for any y unless y = 0 (undefined, but Math.pow(0, 0) = 1) - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * x^0 = 1 for any x (even x = 0) - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * x^y != y^x (2^3 != 3^2) - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return false; - } - - /** - * x^(y^z) != (x^y)^z ((2^3)^4 = 8^4 = 2^12 != 2^(3^4) = 2^81) - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - - public static final DoubleDoubleFunction SECOND = new DoubleDoubleFunction() { - @Override - public double apply(double x, double y) { - return y; - } - - /** - * f(x, 0) = x for any x - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * f(0, y) = y for any y - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * f(x, 0) = 0 for any x - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return true; - } - - /** - * f(x, y) = x != y = f(y, x) for any x, y unless x = y - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return false; - } - - /** - * f(x, f(y, z)) = f(x, z) = z - * f(f(x, y), z) = z - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return true; - } - }; - - /** - * This function is specifically designed to be used when assigning a vector to one that is all zeros (created - * by like()). It enables iteration only through the nonzeros of the right hand side by declaring isLikeRightPlus - * to be true. This is NOT generally true for SECOND (hence the other function above). - */ - public static final DoubleDoubleFunction SECOND_LEFT_ZERO = new DoubleDoubleFunction() { - @Override - public double apply(double x, double y) { - Preconditions.checkArgument(x == 0, "This special version of SECOND needs x == 0"); - return y; - } - - /** - * f(x, 0) = 0 for any x; we're only assigning to left hand sides that are strictly 0 - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return true; - } - - /** - * f(0, y) = y for any y - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * f(x, 0) = 0 for any x - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return true; - } - - /** - * f(x, y) = x != y = f(y, x) for any x, y unless x = y - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return false; - } - - /** - * f(x, f(y, z)) = f(x, z) = z - * f(f(x, y), z) = z - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return true; - } - }; - public static final DoubleDoubleFunction MULT_SQUARE_LEFT = new DoubleDoubleFunction() { - @Override - public double apply(double x, double y) { - return x * x * y; - } - - /** - * x * x * 0 = 0 - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * 0 * 0 * y = 0 - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return true; - } - - /** - * x * x * 0 = 0 - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return true; - } - - /** - * x * x * y != y * y * x - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return false; - } - - /** - * x * x * y * y * z != x * x * y * x * x * y * z - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - - public static final DoubleDoubleFunction MULT_RIGHT_PLUS1 = new DoubleDoubleFunction() { - - /** - * Apply the function to the arguments and return the result - * - * @param x a double for the first argument - * @param y a double for the second argument - * @return the result of applying the function - */ - @Override - public double apply(double x, double y) { - return x * (y + 1); - } - - /** - * x * 1 = x - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return true; - } - - /** - * 0 * y = 0 - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return true; - } - - /** - * x * 1 = x != 0 - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * x * (y + 1) != y * (x + 1) - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return false; - } - - /** - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - - public static DoubleDoubleFunction reweigh(final double wx, final double wy) { - final double tw = wx + wy; - return new DoubleDoubleFunction() { - @Override - public double apply(double x, double y) { - return (wx * x + wy * y) / tw; - } - - /** - * f(x, 0) = wx * x / tw = x iff wx = tw (practically, impossible, as tw = wx + wy and wy > 0) - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return wx == tw; - } - - /** - * f(0, y) = wy * y / tw = 0 iff y = 0 - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * f(x, 0) = wx * x / tw = 0 iff x = 0 - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * wx * x + wy * y = wx * y + wy * x iff wx = wy - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return wx == wy; - } - - /** - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - } - - private Functions() { - } - - /** - * Constructs a function that returns {@code (from<=a && a<=to) ? 1 : 0}. - * <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)); - } - - /** - * fx(c, 0) = f(g(x), h(0)) = f(g(x), 0) = g(x) = x if h(0) = 0 and f isLikeRightPlus and g(x) = x - * Impossible to check whether g(x) = x for any x, so we return false. - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * fc(0, y) = f(g(0), h(y)) = f(0, h(y)) = 0 if g(0) = 0 and f isLikeLeftMult - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return g.apply(0) == 0 && f.isLikeLeftMult(); - } - - /** - * fc(x, 0) = f(g(x), h(0)) = f(g(x), 0) = 0 if h(0) = 0 and f isLikeRightMult - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return h.apply(0) == 0 && f.isLikeRightMult(); - } - - /** - * fc(x, y) = f(g(x), h(y)) = f(h(y), g(x)) - * fc(y, x) = f(g(y), h(x)) = f(h(x), g(y)) - * Either g(x) = g(y) for any x, y and h(x) = h(y) for any x, y or g = h and f isCommutative. - * Can only check if g = h (reference equality, assuming they're both the same static function in - * this file) and f isCommutative. There are however other scenarios when this might happen that are NOT - * covered by this definition. - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return g.equals(h) && f.isCommutative(); - } - - /** - * fc(x, fc(y, z)) = f(g(x), h(f(g(y), h(z)))) - * fc(fc(x, y), z) = f(g(f(g(x), h(y))), h(z)) - * Impossible to check. - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - } - - /** - * Constructs the function <tt>g( h(a,b) )</tt>. - * - * @param g a unary function. - * @param h a binary function. - * @return the binary 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)); - } - - /** - * g(h(x, 0)) = g(x) = x for any x iff g(x) = x and h isLikeRightPlus - * Impossible to check. - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * g(h(0, y)) = g(0) = 0 for any y iff g(0) = 0 and h isLikeLeftMult - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return !g.isDensifying() && h.isLikeLeftMult(); - } - - /** - * g(h(x, 0)) = g(0) = 0 for any x iff g(0) = 0 and h isLikeRightMult - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return !g.isDensifying() && h.isLikeRightMult(); - } - - /** - * fc(x, y) = g(h(x, y)) = g(h(y, x)) = fc(y, x) iff h isCommutative - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return h.isCommutative(); - } - - /** - * fc(x, fc(y, z)) = g(h(x, g(h(y, z))) - * fc(fc(x, y), z) = g(h(g(h(x, y)), z)) - * Impossible to check. - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - } - - /** - * 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 the function <tt>g( h(a) )</tt>. - * - * @param g a unary function. - * @param h an {@link IntIntFunction} function. - * @return the unary function <tt>g( h(a) )</tt>. - */ - public static IntIntFunction chain(final DoubleFunction g, final IntIntFunction h) { - return new IntIntFunction() { - - @Override - public double apply(int first, int second) { - return g.apply(h.apply(first, second)); - } - }; - } - - - /** - * Constructs a function that returns {@code a < b ? -1 : a > b ? 1 : 0}. <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 notEqual(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 mathIEEEremainder(final double b) { - return new DoubleFunction() { - - @Override - public double apply(double a) { - return Math.IEEEremainder(a, b); - } - }; - } - - /** - * Constructs a function that returns {@code from<=a && a<=to}. <tt>a</tt> - * is a variable, <tt>from</tt> and - * <tt>to</tt> are fixed. - * - * Note that DoubleProcedure is generated code and thus looks like an invalid reference unless you can see - * the generated stuff. - */ -// 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 {@code a < b}. <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>Math.log(a) / Math.log(b)</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); - } - - /** 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) { - if (b == 2) { - return a * a; - } else { - 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)} - * (excluding 0.0 and 1.0). Currently the engine is {@link 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: - * {@code - * precision = 0.01 rounds 0.012 --> 0.01, 0.018 --> 0.02 - * precision = 10 rounds 123 --> 120 , 127 --> 130 - * } - */ - 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); - } - }; - } - - public static DoubleDoubleFunction minusAbsPow(final double exponent) { - return new DoubleDoubleFunction() { - @Override - public double apply(double x, double y) { - return Math.pow(Math.abs(x - y), exponent); - } - - /** - * |x - 0|^p = |x|^p != x unless x > 0 and p = 1 - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * |0 - y|^p = |y|^p - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * |x - 0|^p = |x|^p - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * |x - y|^p = |y - x|^p - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - - /** - * |x - |y - z|^p|^p != ||x - y|^p - z|^p - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return false; - } - }; - } -}
http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/IntFunction.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/IntFunction.java b/core/src/main/java/org/apache/mahout/math/function/IntFunction.java deleted file mode 100644 index b91fe18..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/IntFunction.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* -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.function; - -/** - * Interface that represents a function object: a function that takes a single argument and returns a single value. - */ -public interface IntFunction { - - /** - * Applies a function to an argument. - * - * @param argument argument passed to the function. - * @return the result of the function. - */ - int apply(int argument); -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/IntIntDoubleFunction.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/IntIntDoubleFunction.java b/core/src/main/java/org/apache/mahout/math/function/IntIntDoubleFunction.java deleted file mode 100644 index b08f08b..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/IntIntDoubleFunction.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* -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.function; - -/** - * Interface that represents a function object: a function that takes three arguments. - */ -public interface IntIntDoubleFunction { - - /** - * Applies a function to three arguments. - * - * @param first first argument passed to the function. - * @param second second argument passed to the function. - * @param third third argument passed to the function. - * @return the result of the function. - */ - double apply(int first, int second, double third); -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/IntIntFunction.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/IntIntFunction.java b/core/src/main/java/org/apache/mahout/math/function/IntIntFunction.java deleted file mode 100644 index f08bb28..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/IntIntFunction.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math.function; - -/** - * A function that takes to integer arguments and returns Double. - */ -public interface IntIntFunction { - double apply(int first, int second); -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/Mult.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/Mult.java b/core/src/main/java/org/apache/mahout/math/function/Mult.java deleted file mode 100644 index 9bbc5ec..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/Mult.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* -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.function; - -/** - * 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. - */ - -public final class Mult extends 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; - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/ObjectObjectProcedure.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/ObjectObjectProcedure.java b/core/src/main/java/org/apache/mahout/math/function/ObjectObjectProcedure.java deleted file mode 100644 index 46ad8d0..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/ObjectObjectProcedure.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.mahout.math.function; - -/** - * Interface that represents a procedure object: - * a procedure that takes two arguments and returns a 'continue' flag. - */ -public interface ObjectObjectProcedure<K,V> { - - /** - * Applies a procedure to an argument. Optionally can return a boolean flag to inform the object calling the - * procedure. - * - * <p>Example: forEach() methods often use procedure objects. To signal to a forEach() method whether iteration should - * continue normally or terminate (because for example a matching element has been found), a procedure can return - * <tt>false</tt> to indicate termination and <tt>true</tt> to indicate continuation. - * - * @param key key value passed to the procedure - * @param value value value passed to the procedure. - * @return a flag to inform the object calling the procedure. - */ - boolean apply(K key, V value); -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/ObjectProcedure.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/ObjectProcedure.java b/core/src/main/java/org/apache/mahout/math/function/ObjectProcedure.java deleted file mode 100644 index 8c1b1c8..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/ObjectProcedure.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* -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.function; - -/** - * Interface that represents a procedure object: a procedure that takes a single argument and does not return a value. - */ -public interface ObjectProcedure<T> { - - /** - * Applies a procedure to an argument. Optionally can return a boolean flag to inform the object calling the - * procedure. - * - * <p>Example: forEach() methods often use procedure objects. To signal to a forEach() method whether iteration should - * continue normally or terminate (because for example a matching element has been found), a procedure can return - * <tt>false</tt> to indicate termination and <tt>true</tt> to indicate continuation. - * - * @param element element passed to the procedure. - * @return a flag to inform the object calling the procedure. - */ - boolean apply(T element); -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/PlusMult.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/PlusMult.java b/core/src/main/java/org/apache/mahout/math/function/PlusMult.java deleted file mode 100644 index ff99a70..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/PlusMult.java +++ /dev/null @@ -1,123 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* -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.function; - -import org.apache.mahout.math.jet.math.Constants; - -/** - * 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. - */ - -public final class PlusMult extends DoubleDoubleFunction { - - private double multiplicator; - - public 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 minusMult(double constant) { - return new PlusMult(-constant); - } - - /** <tt>a + b*constant</tt>. */ - public static PlusMult plusMult(double constant) { - return new PlusMult(constant); - } - - public double getMultiplicator() { - return multiplicator; - } - - /** - * x + 0 * c = x - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return true; - } - - /** - * 0 + y * c = y * c != 0 - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return false; - } - - /** - * x + 0 * c = x != 0 - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return false; - } - - /** - * x + y * c = y + x * c iff c = 1 - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return Math.abs(multiplicator - 1.0) < Constants.EPSILON; - } - - /** - * f(x, f(y, z)) = x + c * (y + c * z) = x + c * y + c^2 * z - * f(f(x, y), z) = (x + c * y) + c * z = x + c * y + c * z - * true only for c = 0 or c = 1 - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return Math.abs(multiplicator - 0.0) < Constants.EPSILON - || Math.abs(multiplicator - 1.0) < Constants.EPSILON; - } - - public void setMultiplicator(double multiplicator) { - this.multiplicator = multiplicator; - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java b/core/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java deleted file mode 100644 index 5eebea0..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math.function; - -public final class SquareRootFunction extends DoubleFunction { - - @Override - public double apply(double arg1) { - return Math.sqrt(arg1); - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/TimesFunction.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/TimesFunction.java b/core/src/main/java/org/apache/mahout/math/function/TimesFunction.java deleted file mode 100644 index e4e27b4..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/TimesFunction.java +++ /dev/null @@ -1,77 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.mahout.math.function; - -public final class TimesFunction extends DoubleDoubleFunction { - - /** - * Computes the product of two numbers. - * - * @param x first argument - * @param y second argument - * @return the product - */ - @Override - public double apply(double x, double y) { - return x * y; - } - - /** - * x * 0 = y only if y = 0 - * @return true iff f(x, 0) = x for any x - */ - @Override - public boolean isLikeRightPlus() { - return false; - } - - /** - * 0 * y = 0 for any y - * @return true iff f(0, y) = 0 for any y - */ - @Override - public boolean isLikeLeftMult() { - return true; - } - - /** - * x * 0 = 0 for any x - * @return true iff f(x, 0) = 0 for any x - */ - @Override - public boolean isLikeRightMult() { - return true; - } - - /** - * x * y = y * x for any x, y - * @return true iff f(x, y) = f(y, x) for any x, y - */ - @Override - public boolean isCommutative() { - return true; - } - - /** - * x * (y * z) = (x * y) * z for any x, y, z - * @return true iff f(x, f(y, z)) = f(f(x, y), z) for any x, y, z - */ - @Override - public boolean isAssociative() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/VectorFunction.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/VectorFunction.java b/core/src/main/java/org/apache/mahout/math/function/VectorFunction.java deleted file mode 100644 index 3b5af77..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/VectorFunction.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math.function; - -import org.apache.mahout.math.Vector; - -/** - * Defines a function of a vector that returns a double. - */ -public interface VectorFunction { - double apply(Vector f); -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/function/package-info.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/function/package-info.java b/core/src/main/java/org/apache/mahout/math/function/package-info.java deleted file mode 100644 index 47ceace..0000000 --- a/core/src/main/java/org/apache/mahout/math/function/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Core interfaces for functions, comparisons and procedures on objects and primitive data types. - */ -package org.apache.mahout.math.function; http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/jet/math/Arithmetic.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/jet/math/Arithmetic.java b/core/src/main/java/org/apache/mahout/math/jet/math/Arithmetic.java deleted file mode 100644 index 83d512b..0000000 --- a/core/src/main/java/org/apache/mahout/math/jet/math/Arithmetic.java +++ /dev/null @@ -1,328 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/* -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; - -/** - * Arithmetic functions. - */ -public final class Arithmetic { - - // for method logFactorial(...) - // log(k!) for k = 0, ..., 29 - private static final double[] LOG_FACTORIAL_TABLE = { - 0.00000000000000000, 0.00000000000000000, 0.69314718055994531, - 1.79175946922805500, 3.17805383034794562, 4.78749174278204599, - 6.57925121201010100, 8.52516136106541430, 10.60460290274525023, - 12.80182748008146961, 15.10441257307551530, 17.50230784587388584, - 19.98721449566188615, 22.55216385312342289, 25.19122118273868150, - 27.89927138384089157, 30.67186010608067280, 33.50507345013688888, - 36.39544520803305358, 39.33988418719949404, 42.33561646075348503, - 45.38013889847690803, 48.47118135183522388, 51.60667556776437357, - 54.78472939811231919, 58.00360522298051994, 61.26170176100200198, - 64.55753862700633106, 67.88974313718153498, 71.25703896716800901 - }; - - // k! for k = 0, ..., 20 - private static final long[] FACTORIAL_TABLE = { - 1L, - 1L, - 2L, - 6L, - 24L, - 120L, - 720L, - 5040L, - 40320L, - 362880L, - 3628800L, - 39916800L, - 479001600L, - 6227020800L, - 87178291200L, - 1307674368000L, - 20922789888000L, - 355687428096000L, - 6402373705728000L, - 121645100408832000L, - 2432902008176640000L - }; - - // k! for k = 21, ..., 170 - private static final double[] LARGE_FACTORIAL_TABLE = { - 5.109094217170944E19, - 1.1240007277776077E21, - 2.585201673888498E22, - 6.204484017332394E23, - 1.5511210043330984E25, - 4.032914611266057E26, - 1.0888869450418352E28, - 3.048883446117138E29, - 8.841761993739701E30, - 2.652528598121911E32, - 8.222838654177924E33, - 2.6313083693369355E35, - 8.68331761881189E36, - 2.952327990396041E38, - 1.0333147966386144E40, - 3.719933267899013E41, - 1.3763753091226346E43, - 5.23022617466601E44, - 2.0397882081197447E46, - 8.15915283247898E47, - 3.34525266131638E49, - 1.4050061177528801E51, - 6.041526306337384E52, - 2.6582715747884495E54, - 1.196222208654802E56, - 5.502622159812089E57, - 2.5862324151116827E59, - 1.2413915592536068E61, - 6.082818640342679E62, - 3.0414093201713376E64, - 1.5511187532873816E66, - 8.06581751709439E67, - 4.274883284060024E69, - 2.308436973392413E71, - 1.2696403353658264E73, - 7.109985878048632E74, - 4.052691950487723E76, - 2.350561331282879E78, - 1.386831185456898E80, - 8.32098711274139E81, - 5.075802138772246E83, - 3.146997326038794E85, - 1.9826083154044396E87, - 1.2688693218588414E89, - 8.247650592082472E90, - 5.443449390774432E92, - 3.6471110918188705E94, - 2.48003554243683E96, - 1.7112245242814127E98, - 1.1978571669969892E100, - 8.504785885678624E101, - 6.123445837688612E103, - 4.470115461512686E105, - 3.307885441519387E107, - 2.4809140811395404E109, - 1.8854947016660506E111, - 1.451830920282859E113, - 1.1324281178206295E115, - 8.94618213078298E116, - 7.15694570462638E118, - 5.797126020747369E120, - 4.7536433370128435E122, - 3.94552396972066E124, - 3.314240134565354E126, - 2.8171041143805494E128, - 2.4227095383672744E130, - 2.107757298379527E132, - 1.854826422573984E134, - 1.6507955160908465E136, - 1.4857159644817605E138, - 1.3520015276784033E140, - 1.2438414054641305E142, - 1.156772507081641E144, - 1.0873661566567426E146, - 1.0329978488239061E148, - 9.916779348709491E149, - 9.619275968248216E151, - 9.426890448883248E153, - 9.332621544394415E155, - 9.332621544394418E157, - 9.42594775983836E159, - 9.614466715035125E161, - 9.902900716486178E163, - 1.0299016745145631E166, - 1.0813967582402912E168, - 1.1462805637347086E170, - 1.2265202031961373E172, - 1.324641819451829E174, - 1.4438595832024942E176, - 1.5882455415227423E178, - 1.7629525510902457E180, - 1.974506857221075E182, - 2.2311927486598138E184, - 2.543559733472186E186, - 2.925093693493014E188, - 3.393108684451899E190, - 3.96993716080872E192, - 4.6845258497542896E194, - 5.574585761207606E196, - 6.689502913449135E198, - 8.094298525273444E200, - 9.875044200833601E202, - 1.2146304367025332E205, - 1.506141741511141E207, - 1.882677176888926E209, - 2.3721732428800483E211, - 3.0126600184576624E213, - 3.856204823625808E215, - 4.974504222477287E217, - 6.466855489220473E219, - 8.471580690878813E221, - 1.1182486511960037E224, - 1.4872707060906847E226, - 1.99294274616152E228, - 2.690472707318049E230, - 3.6590428819525483E232, - 5.0128887482749884E234, - 6.917786472619482E236, - 9.615723196941089E238, - 1.3462012475717523E241, - 1.8981437590761713E243, - 2.6953641378881633E245, - 3.8543707171800694E247, - 5.550293832739308E249, - 8.047926057471989E251, - 1.1749972043909107E254, - 1.72724589045464E256, - 2.5563239178728637E258, - 3.8089226376305687E260, - 5.7133839564458575E262, - 8.627209774233244E264, - 1.3113358856834527E267, - 2.0063439050956838E269, - 3.0897696138473515E271, - 4.789142901463393E273, - 7.471062926282892E275, - 1.1729568794264134E278, - 1.8532718694937346E280, - 2.946702272495036E282, - 4.714723635992061E284, - 7.590705053947223E286, - 1.2296942187394494E289, - 2.0044015765453032E291, - 3.287218585534299E293, - 5.423910666131583E295, - 9.003691705778434E297, - 1.5036165148649983E300, - 2.5260757449731988E302, - 4.2690680090047056E304, - 7.257415615308004E306 - }; - - private Arithmetic() { - } - - /** - * Efficiently returns the binomial coefficient, often also referred to as "n over k" or "n choose k". The binomial - * coefficient is defined as <ul> - * <li><tt>k<0</tt>: <tt>0</tt>.</li> - * <li><tt>k==0 || k==n</tt>: <tt>1</tt>.</li> - * <li><tt>k==1 || k==n-1</tt>: <tt>n</tt>.</li> - * <li>else: <tt>(n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )</tt>.</li> - * </ul> - * - * @return the binomial coefficient. - */ - public static double binomial(long n, long k) { - if (k < 0) { - return 0; - } - if (k == 0 || k == n) { - return 1; - } - if (k == 1 || k == n - 1) { - return n; - } - - // try quick version and see whether we get numeric overflows. - // factorial(..) is O(1); requires no loop; only a table lookup. - if (n > k) { - int max = FACTORIAL_TABLE.length + LARGE_FACTORIAL_TABLE.length; - if (n < max) { // if (n! < inf && k! < inf) - double nFactorial = factorial((int) n); - double kFactorial = factorial((int) k); - double nMinusKFactorial = factorial((int) (n - k)); - double nk = nMinusKFactorial * kFactorial; - if (nk != Double.POSITIVE_INFINITY) { // no numeric overflow? - // now this is completely safe and accurate - return nFactorial / nk; - } - } - if (k > n / 2) { - k = n - k; - } // quicker - } - - // binomial(n,k) = (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ) - long a = n - k + 1; - long b = 1; - double binomial = 1; - for (long i = k; i-- > 0;) { - binomial *= (double) a++ / b++; - } - return binomial; - } - - /** - * Instantly returns the factorial <tt>k!</tt>. - * - * @param k must hold <tt>k >= 0</tt>. - */ - private static double factorial(int k) { - if (k < 0) { - throw new IllegalArgumentException(); - } - - int length1 = FACTORIAL_TABLE.length; - if (k < length1) { - return FACTORIAL_TABLE[k]; - } - - int length2 = LARGE_FACTORIAL_TABLE.length; - if (k < length1 + length2) { - return LARGE_FACTORIAL_TABLE[k - length1]; - } else { - return Double.POSITIVE_INFINITY; - } - } - - /** - * Returns <tt>log(k!)</tt>. Tries to avoid overflows. For {@code k<30} simply - * looks up a table in <tt>O(1)</tt>. For {@code k>=30} uses stirlings - * approximation. - * - * @param k must hold <tt>k >= 0</tt>. - */ - public static double logFactorial(int k) { - if (k >= 30) { - - double r = 1.0 / k; - double rr = r * r; - double c7 = -5.95238095238095238e-04; - double c5 = 7.93650793650793651e-04; - double c3 = -2.77777777777777778e-03; - double c1 = 8.33333333333333333e-02; - double c0 = 9.18938533204672742e-01; - return (k + 0.5) * Math.log(k) - k + c0 + r * (c1 + rr * (c3 + rr * (c5 + rr * c7))); - } else { - return LOG_FACTORIAL_TABLE[k]; - } - } - -} http://git-wip-us.apache.org/repos/asf/mahout/blob/49ad8cb4/core/src/main/java/org/apache/mahout/math/jet/math/Constants.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mahout/math/jet/math/Constants.java b/core/src/main/java/org/apache/mahout/math/jet/math/Constants.java deleted file mode 100644 index b99340d..0000000 --- a/core/src/main/java/org/apache/mahout/math/jet/math/Constants.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* -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; - -/** - * Defines some useful constants. - */ -public final class Constants { - - public static final double MACHEP = 1.11022302462515654042E-16; - public static final double MAXLOG = 7.09782712893383996732E2; - public static final double MINLOG = -7.451332191019412076235E2; - public static final double MAXGAM = 171.624376956302725; - public static final double SQTPI = 2.50662827463100050242E0; - public static final double LOGPI = 1.14472988584940017414; - - public static final double BIG = 4.503599627370496e15; - public static final double BIG_INVERSE = 2.22044604925031308085e-16; - - public static final double EPSILON = 1.0E-6; - - private Constants() { - } -}
