You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by jm...@apache.org on 2010/01/28 07:11:47 UTC
svn commit: r903965 [1/2] - in /lucene/mahout/trunk:
core/src/main/java/org/apache/mahout/clustering/dirichlet/
core/src/main/java/org/apache/mahout/clustering/dirichlet/models/
core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/ core/src/main...
Author: jmannix
Date: Thu Jan 28 06:11:42 2010
New Revision: 903965
URL: http://svn.apache.org/viewvc?rev=903965&view=rev
Log:
Sorry about the montser commit, but I was on a roll.
Fixes MAHOUT-269, MAHOUT-268, MAHOUT-267, MAHOUT-263, MAHOUT-209, MAHOUT-265, and MAHOUT-208. MAHOUT-211 was actually not fixed yet either, but now is.
Also includes
* some UnaryFunction / DoubleFunction and BinaryFunction / DoubleDoubleFunction refactor-merging,
* performance tuning in the world of vectors: AbstracVector.assign(BinaryFunction f, double d) can be sparse-optimized in the case where f(0,d) = 0, much like assign(UnaryFunction f), when f(0) == 0.
* SquaredEuclideanDistanceMeasure (and hence EuclideanDistanceMeasure) were both broken and wrong (see discussion on mahout-dev). Fixed now. Tests passed before, and still do though. There'll be another checkin on that subject shortly.
* tests for much of the above.
mvn clean install works fine, but I haven't run all the drivers.
Added:
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixSlice.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorIterable.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/BinaryFunction.java (contents, props changed)
- copied, changed from r903542, lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/BinaryFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Functions.java (contents, props changed)
- copied, changed from r903924, lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Functions.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Mult.java (contents, props changed)
- copied, changed from r903924, lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Mult.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/PlusMult.java (contents, props changed)
- copied, changed from r903924, lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/PlusMult.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java (contents, props changed)
- copied, changed from r903542, lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SquareRootFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/TimesFunction.java (contents, props changed)
- copied, changed from r903542, lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/TimesFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/UnaryFunction.java
Removed:
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/BinaryFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/NegateFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/PlusFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/PlusWithScaleFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SquareRootFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/TimesFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/UnaryFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Functions.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Mult.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/PlusMult.java
Modified:
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletClusterer.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletMapper.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/AsymmetricSampledNormalModel.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/NormalModel.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/SoftCluster.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/lda/LDAInference.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/meanshift/MeanShiftCanopy.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/common/distance/SquaredEuclideanDistanceMeasure.java
lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/dirichlet/DisplayDirichlet.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Matrix.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianSolver.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/lanczos/LanczosSolver.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleDoubleFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleFunction.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/IntFunctions.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/DoubleFactory1D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/DoubleFactory2D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/DoubleFactory3D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/DoubleMatrix1D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/DoubleMatrix2D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/DoubleMatrix3D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/doublealgo/Sorting.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/doublealgo/Statistic.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/doublealgo/Transform.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseDoubleMatrix1D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseDoubleMatrix2D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCDoubleMatrix2D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCMDoubleMatrix2D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SparseDoubleMatrix2D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/TridiagonalDoubleMatrix2D.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/linalg/Algebra.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/linalg/Blas.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/linalg/CholeskyDecomposition.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/linalg/LUDecompositionQuick.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/linalg/Property.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/linalg/QRDecomposition.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/linalg/SeqBlas.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/linalg/SmpBlas.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/MatrixTest.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestDenseVector.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestMatrixView.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestSparseVector.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java
lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/vectors/arff/Driver.java
lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/vectors/RandomVectorIterable.java
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletClusterer.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletClusterer.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletClusterer.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletClusterer.java Thu Jan 28 06:11:42 2010
@@ -20,7 +20,7 @@
import org.apache.mahout.clustering.dirichlet.models.Model;
import org.apache.mahout.clustering.dirichlet.models.ModelDistribution;
import org.apache.mahout.math.DenseVector;
-import org.apache.mahout.math.TimesFunction;
+import org.apache.mahout.math.function.TimesFunction;
import org.apache.mahout.math.Vector;
import java.util.ArrayList;
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletMapper.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletMapper.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletMapper.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/DirichletMapper.java Thu Jan 28 06:11:42 2010
@@ -30,7 +30,7 @@
import org.apache.hadoop.mapred.OutputLogFilter;
import org.apache.hadoop.mapred.Reporter;
import org.apache.mahout.math.DenseVector;
-import org.apache.mahout.math.TimesFunction;
+import org.apache.mahout.math.function.TimesFunction;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/AsymmetricSampledNormalModel.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/AsymmetricSampledNormalModel.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/AsymmetricSampledNormalModel.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/AsymmetricSampledNormalModel.java Thu Jan 28 06:11:42 2010
@@ -17,7 +17,7 @@
package org.apache.mahout.clustering.dirichlet.models;
-import org.apache.mahout.math.SquareRootFunction;
+import org.apache.mahout.math.function.SquareRootFunction;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/NormalModel.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/NormalModel.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/NormalModel.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/NormalModel.java Thu Jan 28 06:11:42 2010
@@ -17,7 +17,7 @@
package org.apache.mahout.clustering.dirichlet.models;
-import org.apache.mahout.math.SquareRootFunction;
+import org.apache.mahout.math.function.SquareRootFunction;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/SoftCluster.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/SoftCluster.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/SoftCluster.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/SoftCluster.java Thu Jan 28 06:11:42 2010
@@ -20,7 +20,7 @@
import org.apache.hadoop.io.Writable;
import org.apache.mahout.math.AbstractVector;
import org.apache.mahout.math.RandomAccessSparseVector;
-import org.apache.mahout.math.SquareRootFunction;
+import org.apache.mahout.math.function.SquareRootFunction;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/kmeans/Cluster.java Thu Jan 28 06:11:42 2010
@@ -18,7 +18,7 @@
import org.apache.mahout.clustering.ClusterBase;
import org.apache.mahout.math.AbstractVector;
-import org.apache.mahout.math.SquareRootFunction;
+import org.apache.mahout.math.function.SquareRootFunction;
import org.apache.mahout.math.Vector;
import org.apache.mahout.common.distance.DistanceMeasure;
import org.apache.mahout.math.VectorWritable;
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/lda/LDAInference.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/lda/LDAInference.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/lda/LDAInference.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/lda/LDAInference.java Thu Jan 28 06:11:42 2010
@@ -22,7 +22,7 @@
import java.util.Map;
import org.apache.commons.math.special.Gamma;
-import org.apache.mahout.math.BinaryFunction;
+import org.apache.mahout.math.function.BinaryFunction;
import org.apache.mahout.math.DenseMatrix;
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Matrix;
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/meanshift/MeanShiftCanopy.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/meanshift/MeanShiftCanopy.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/meanshift/MeanShiftCanopy.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/meanshift/MeanShiftCanopy.java Thu Jan 28 06:11:42 2010
@@ -24,7 +24,7 @@
import org.apache.mahout.math.CardinalityException;
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.JsonVectorAdapter;
-import org.apache.mahout.math.PlusFunction;
+import static org.apache.mahout.math.function.Functions.*;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
@@ -127,7 +127,7 @@
public Vector computeBoundCentroid() {
Vector result = new DenseVector(getCenter().size());
for (Vector v : boundPoints) {
- result.assign(v, new PlusFunction());
+ result.assign(v, plus);
}
return result.divide(boundPoints.size());
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/common/distance/SquaredEuclideanDistanceMeasure.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/common/distance/SquaredEuclideanDistanceMeasure.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/common/distance/SquaredEuclideanDistanceMeasure.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/common/distance/SquaredEuclideanDistanceMeasure.java Thu Jan 28 06:11:42 2010
@@ -61,6 +61,6 @@
if (centroid.size() != v.size()) {
throw new CardinalityException();
}
- return centroidLengthSquare + v.getDistanceSquared(centroid);
+ return centroidLengthSquare + v.getLengthSquared() - 2 * v.dot(centroid);
}
}
Modified: lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/dirichlet/DisplayDirichlet.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/dirichlet/DisplayDirichlet.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/dirichlet/DisplayDirichlet.java (original)
+++ lucene/mahout/trunk/examples/src/main/java/org/apache/mahout/clustering/dirichlet/DisplayDirichlet.java Thu Jan 28 06:11:42 2010
@@ -35,7 +35,7 @@
import org.apache.mahout.clustering.dirichlet.models.Model;
import org.apache.mahout.clustering.dirichlet.models.ModelDistribution;
import org.apache.mahout.math.DenseVector;
-import org.apache.mahout.math.TimesFunction;
+import org.apache.mahout.math.function.TimesFunction;
import org.apache.mahout.math.Vector;
import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.VectorWritable;
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java Thu Jan 28 06:11:42 2010
@@ -20,6 +20,9 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
+import org.apache.mahout.math.function.BinaryFunction;
+import org.apache.mahout.math.function.PlusMult;
+import org.apache.mahout.math.function.UnaryFunction;
import java.lang.reflect.Type;
import java.util.HashMap;
@@ -34,6 +37,51 @@
protected Map<String, Integer> rowLabelBindings;
@Override
+ public Iterator<MatrixSlice> iterator() {
+ return iterateAll();
+ }
+
+ @Override
+ public Iterator<MatrixSlice> iterateAll() {
+ return new Iterator<MatrixSlice>() {
+ int slice = 0;
+
+ @Override
+ public boolean hasNext() {
+ return slice < numSlices();
+ }
+
+ @Override
+ public MatrixSlice next() {
+ int i = slice++;
+ return new MatrixSlice(slice(i), i);
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("remove() not supported for Matrix iterator");
+ }
+ };
+ }
+
+ /**
+ * Abstracted out for iterating over either rows or columns (default is rows).
+ * @param index the row or column number to grab as a vector (shallowly)
+ * @return the row or column vector at that index.
+ */
+ protected Vector slice(int index) {
+ return getRow(index);
+ }
+
+ /**
+ * Abstracted out for the iterator
+ * @return numRows() for row-based iterator, numColumns() for column-based.
+ */
+ public int numSlices() {
+ return numRows();
+ }
+
+ @Override
public double get(String rowLabel, String columnLabel) throws IndexException,
UnboundLabelException {
if (columnLabelBindings == null || rowLabelBindings == null) {
@@ -431,7 +479,7 @@
Vector xi = getRow(i);
double d = xi.dot(v);
if(d != 0)
- w.assign(xi, new PlusWithScaleFunction(d));
+ w.assign(xi, new PlusMult(d));
}
return w;
@@ -575,7 +623,7 @@
}
/**
- * TODO: currently I don't know of an efficient way to get this value correctly.
+ * TODO: currently I don't know of an efficient way to getVector this value correctly.
*
* @return the number of nonzero entries
*/
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java Thu Jan 28 06:11:42 2010
@@ -20,6 +20,9 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
+import org.apache.mahout.math.function.BinaryFunction;
+import org.apache.mahout.math.function.UnaryFunction;
+
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
@@ -53,6 +56,22 @@
this.size = size;
}
+ public double aggregate(BinaryFunction aggregator, UnaryFunction map) {
+ double result = 0;
+ for(int i=0; i<size(); i++) {
+ result = aggregator.apply(result, map.apply(getQuick(i)) );
+ }
+ return result;
+ }
+
+ public double aggregate(Vector other, BinaryFunction aggregator, BinaryFunction combiner) {
+ double result = 0;
+ for(int i=0; i<size(); i++) {
+ result = aggregator.apply(result, combiner.apply(getQuick(i), other.getQuick(i)));
+ }
+ return result;
+ }
+
/**
* Subclasses must override to return an appropriately sparse or dense result
*
@@ -156,11 +175,21 @@
}
// we can special case certain powers
if (Double.isInfinite(power)) {
- return maxValue();
+ double val = 0.0;
+ Iterator<Element> iter = this.iterateNonZero();
+ while (iter.hasNext()) {
+ val = Math.max(val, Math.abs(iter.next().get()));
+ }
+ return val;
} else if (power == 2.0) {
return Math.sqrt(dot(this));
} else if (power == 1.0) {
- return zSum();
+ double val = 0.0;
+ Iterator<Element> iter = this.iterateNonZero();
+ while (iter.hasNext()) {
+ val += Math.abs(iter.next().get());
+ }
+ return val;
} else if (power == 0.0) {
// this is the number of non-zero elements
double val = 0.0;
@@ -190,19 +219,32 @@
@Override
public double getDistanceSquared(Vector v) {
- double d = 0;
- Iterator<Element> it = iterateNonZero();
+ // if this and v has a cached lengthSquared, dot product is quickest way to compute this.
+ if(lengthSquared >= 0 && v instanceof AbstractVector && ((AbstractVector)v).lengthSquared >= 0) {
+ return lengthSquared + v.getLengthSquared() - 2 * this.dot(v);
+ }
+ Vector randomlyAccessed;
+ Iterator<Element> it;
Element e;
+ double d = 0;
+ if(lengthSquared >= 0 ) {
+ it = v.iterateNonZero();
+ randomlyAccessed = this;
+ d += lengthSquared;
+ } else { // TODO: could be further optimized, figure out which one is smaller, etc
+ it = iterateNonZero();
+ randomlyAccessed = v;
+ d += v.getLengthSquared();
+ }
while(it.hasNext() && (e = it.next()) != null) {
- double diff = e.get() - v.getQuick(e.index());
- d += (diff * diff);
+ d += e.get() * (e.get() - 2 * randomlyAccessed.getQuick(e.index()));
}
return d;
}
@Override
public double maxValue() {
- double result = Double.MIN_VALUE;
+ double result = Double.NEGATIVE_INFINITY;
for (int i = 0; i < size(); i++) {
result = Math.max(result, getQuick(i));
}
@@ -212,7 +254,7 @@
@Override
public int maxValueIndex() {
int result = -1;
- double max = Double.MIN_VALUE;
+ double max = Double.NEGATIVE_INFINITY;
for (int i = 0; i < size(); i++) {
double tmp = getQuick(i);
if (tmp > max) {
@@ -342,16 +384,30 @@
@Override
public Vector assign(BinaryFunction f, double y) {
- for (int i = 0; i < size(); i++) {
- setQuick(i, f.apply(getQuick(i), y));
+ Iterator<Element> it;
+ Element e;
+ if(f.apply(0, y) == 0) {
+ it = iterateNonZero();
+ } else {
+ it = iterateAll();
+ }
+ while(it.hasNext() && (e = it.next()) != null) {
+ e.set(f.apply(e.get(), y));
}
return this;
}
@Override
public Vector assign(UnaryFunction function) {
- for (int i = 0; i < size(); i++) {
- setQuick(i, function.apply(getQuick(i)));
+ Iterator<Element> it;
+ Element e;
+ if(function.apply(0) == 0) {
+ it = iterateNonZero();
+ } else {
+ it = iterateAll();
+ }
+ while(it.hasNext() && (e = it.next()) != null) {
+ e.set(function.apply(e.get()));
}
return this;
}
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java Thu Jan 28 06:11:42 2010
@@ -17,6 +17,9 @@
package org.apache.mahout.math;
+import org.apache.mahout.math.function.BinaryFunction;
+import org.apache.mahout.math.function.PlusMult;
+
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
@@ -25,7 +28,6 @@
public class DenseVector extends AbstractVector {
protected double[] values;
- protected double lengthSquared = -1.0;
/** For serialization purposes only */
public DenseVector() {
@@ -121,7 +123,7 @@
throw new CardinalityException();
}
// is there some other way to know if function.apply(0, x) = x for all x?
- if(function instanceof PlusFunction || function instanceof PlusWithScaleFunction) {
+ if(function instanceof PlusMult) {
Iterator<Vector.Element> it = other.iterateNonZero();
Vector.Element e;
while(it.hasNext() && (e = it.next()) != null) {
@@ -132,6 +134,7 @@
values[i] = function.apply(values[i], other.getQuick(i));
}
}
+ lengthSquared = -1;
return this;
}
@@ -316,16 +319,6 @@
}
@Override
- public double getDistanceSquared(Vector v) {
- double result = 0.0;
- for (int i = 0; i < values.length; i++) {
- double delta = values[i] - v.getQuick(i);
- result += delta * delta;
- }
- return result;
- }
-
- @Override
public void addTo(Vector v) {
if (v.size() != size()) {
throw new CardinalityException();
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Matrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Matrix.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Matrix.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Matrix.java Thu Jan 28 06:11:42 2010
@@ -17,10 +17,13 @@
package org.apache.mahout.math;
+import org.apache.mahout.math.function.BinaryFunction;
+import org.apache.mahout.math.function.UnaryFunction;
+
import java.util.Map;
/** The basic interface including numerous convenience functions */
-public interface Matrix extends Cloneable {
+public interface Matrix extends Cloneable, VectorIterable {
/** @return a formatted String suitable for output */
String asFormatString();
@@ -96,12 +99,6 @@
*/
int[] size();
- /** Helper method to return the cardinality of the row dimension */
- int numRows();
-
- /** Helper method to return the cardinality of the column dimension */
- int numCols();
-
/**
* Return a copy of the recipient
*
@@ -248,26 +245,6 @@
Matrix times(Matrix x);
/**
- * Return a new vector with cardinality equal to getNumRows() of this matrix which is the matrix product of the
- * recipient and the argument
- *
- * @param v a vector with cardinality equal to getNumCols() of the recipient
- * @return a new vector (typically a DenseVector)
- * @throws CardinalityException if this.getNumRows() != v.size()
- */
- Vector times(Vector v);
-
- /**
- * Convenience method for producing this.transpose().times(this.times(v)), which can be implemented with only one pass
- * over the matrix, without making the transpose() call (which can be expensive if the matrix is sparse)
- *
- * @param v a vector with cardinality equal to getNumCols() of the recipient
- * @return a new vector (typically a DenseVector) with cardinality equal to that of the argument.
- * @throws CardinalityException if this.getNumCols() != v.size()
- */
- Vector timesSquared(Vector v);
-
- /**
* Return a new matrix that is the transpose of the receiver
*
* @return the transpose
@@ -377,9 +354,9 @@
*/
// void getNonZeros(IntArrayList jx, DoubleArrayList values);
// void foreachNonZero(IntDoubleFunction f);
- // double aggregate(DoubleDoubleFunction aggregator, DoubleFunction map);
- // double aggregate(Matrix other, DoubleDoubleFunction aggregator,
- // DoubleDoubleFunction map);
- // NewMatrix assign(Matrix y, DoubleDoubleFunction function, IntArrayList
+ // double aggregate(BinaryFunction aggregator, UnaryFunction map);
+ // double aggregate(Matrix other, BinaryFunction aggregator,
+ // BinaryFunction map);
+ // NewMatrix assign(Matrix y, BinaryFunction function, IntArrayList
// nonZeroIndexes);
}
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixSlice.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixSlice.java?rev=903965&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixSlice.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixSlice.java Thu Jan 28 06:11:42 2010
@@ -0,0 +1,15 @@
+package org.apache.mahout.math;
+
+
+public class MatrixSlice {
+ private Vector v;
+ private int index;
+ public MatrixSlice(Vector v, int index) {
+ this.v = v;
+ this.index = index;
+ }
+
+ public Vector vector() { return v; }
+ public int index() { return index; }
+}
+
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java Thu Jan 28 06:11:42 2010
@@ -23,6 +23,7 @@
import org.apache.mahout.math.function.IntDoubleProcedure;
import org.apache.mahout.math.list.IntArrayList;
import org.apache.mahout.math.map.OpenIntDoubleHashMap;
+import org.apache.mahout.math.set.OpenIntHashSet;
/** Implements vector that only stores non-zero doubles */
@@ -96,6 +97,7 @@
@Override
public void setQuick(int index, double value) {
+ lengthSquared = -1.0;
values.put(index, value);
}
@@ -254,36 +256,58 @@
@Override
public void set(double value) {
+ lengthSquared = -1.0;
values.put(ind, value);
}
}
-
- private class DistanceSquared implements IntDoubleProcedure {
+
+ private static class DistanceSquared implements IntDoubleProcedure {
final Vector v;
+ final OpenIntHashSet skipSet;
public double result = 0.0;
- DistanceSquared(Vector v) {
+ DistanceSquared(Vector v, OpenIntHashSet skipSet) {
this.v = v;
+ this.skipSet = skipSet;
}
@Override
public boolean apply(int key, double value) {
+ if(skipSet.contains(key)) {
+ // returning true is ok, yes? It is ignored.
+ return true;
+ }
+ skipSet.add(key);
double centroidValue = v.get(key);
double delta = value - centroidValue;
- result += (delta * delta) - (centroidValue * centroidValue);
+ result += (delta * delta);// - (centroidValue * centroidValue);
return true;
}
}
+ // TODO is this more optimal than the version in AbstractVector? Should be checked.
@Override
public double getDistanceSquared(Vector v) {
- //TODO: Check sizes?
-
- DistanceSquared distanceSquared = new DistanceSquared(v);
+ if(v instanceof DenseVector) {
+ // quicker to just use the DenseVector version
+ return v.getDistanceSquared(this);
+ }
+ if (v.size() != size()) {
+ throw new CardinalityException();
+ }
+ OpenIntHashSet used = new OpenIntHashSet();
+ DistanceSquared distanceSquared = new DistanceSquared(v, used);
values.forEachPair(distanceSquared);
- return distanceSquared.result;
+ Iterator<Vector.Element> it = v.iterateNonZero();
+ Vector.Element e;
+ DistanceSquared otherHalf = new DistanceSquared(this, used);
+ while(it.hasNext() && (e = it.next()) != null) {
+ otherHalf.apply(e.index(), e.get());
+ }
+ return distanceSquared.result + otherHalf.result;
}
+
private class AddToVector implements IntDoubleProcedure {
final Vector v;
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java Thu Jan 28 06:11:42 2010
@@ -92,103 +92,6 @@
return values.getNumMappings();
}
- private static class DistanceSquarer implements Iterator<Element> {
- final Iterator<Element> it1;
- final Iterator<Element> it2;
- Element e1 = null;
- Element e2 = null;
- boolean firstIteration = true;
- Iterator<Element> notExhausted = null;
- double v = 0;
-
- DistanceSquarer(Iterator<Element> it1, Iterator<Element> it2) {
- this.it1 = it1;
- this.it2 = it2;
- }
-
- @Override
- public boolean hasNext() {
- return firstIteration || it1.hasNext() || it2.hasNext();
- }
-
- @Override
- public Element next() {
- if(firstIteration) {
- e1 = it1.next();
- e2 = it2.next();
- firstIteration = false;
- }
- Iterator<Element> it;
- Element e = null;
- if(notExhausted != null) {
- if(notExhausted.hasNext()) {
- e = notExhausted.next();
- v += e.get() * e.get();
- }
- } else if(e1.index() < e2.index()) {
- v += e1.get() * e1.get();
- e = e1;
- if(it1.hasNext()) {
- e1 = it1.next();
- } else {
- notExhausted = it2;
- }
- } else if(e1.index() > e2.index()) {
- v += e2.get() * e2.get();
- e = e2;
- if(it2.hasNext()) {
- e2 = it2.next();
- } else {
- notExhausted = it1;
- }
- } else {
- double d = e1.get() - e2.get();
- if(it1.hasNext()) {
- e1 = it1.next();
- e = e1;
- } else if(it2.hasNext()) {
- e2 = it2.next();
- e = e2;
- } else {
- e = null;
- }
- v += d*d;
- }
- return e;
- }
-
- @Override
- public void remove() {
- // ignore
- }
-
- public double distanceSquared() {
- return v;
- }
- }
-
- @Override
- public double getDistanceSquared(Vector v) {
- if(v instanceof SequentialAccessSparseVector) {
- Iterator<Element> it = iterateNonZero();
- Iterator<Element> vIt = v.iterateNonZero();
- if(!it.hasNext()) {
- return v.dot(v);
- }
- if(!vIt.hasNext()) {
- return this.dot(this);
- }
- DistanceSquarer d = new DistanceSquarer(it, vIt);
- while(d.hasNext()) {
- d.next();
- }
- return d.distanceSquared();
- }
- else {
- return super.getDistanceSquared(v);
- }
- }
-
@Override
public SequentialAccessSparseVector like() {
int numValues = 256;
@@ -209,12 +112,12 @@
@Override
public java.util.Iterator<Element> iterateNonZero() {
- return new IntDoublePairIterator(values);
+ return new IntDoublePairIterator(this);
}
@Override
public Iterator<Element> iterateAll() {
- return new IntDoublePairIterator(values, size());
+ return new IntDoublePairIterator(this, size());
}
/**
@@ -262,12 +165,12 @@
private final AbstractElement element;
private final int maxOffset;
- IntDoublePairIterator(OrderedIntDoubleMapping mapping) {
- element = new SparseElement(offset, mapping);
- maxOffset = mapping.getNumMappings();
+ IntDoublePairIterator(SequentialAccessSparseVector v) {
+ element = new SparseElement(offset, v);
+ maxOffset = v.values.getNumMappings();
}
- IntDoublePairIterator(OrderedIntDoubleMapping mapping, int cardinality) {
- element = new DenseElement(offset, mapping);
+ IntDoublePairIterator(SequentialAccessSparseVector v, int cardinality) {
+ element = new DenseElement(offset, v);
maxOffset = cardinality;
}
@Override
@@ -292,7 +195,7 @@
@Override
public Element getElement(int index) {
- return new DenseElement(index, values);
+ return new DenseElement(index, this);
}
@@ -302,19 +205,20 @@
final int[] indices;
final double[] values;
- AbstractElement(int ind, OrderedIntDoubleMapping m) {
+ AbstractElement(int ind, SequentialAccessSparseVector v) {
offset = ind;
- mapping = m;
- values = m.getValues();
- indices = m.getIndices();
+ mapping = v.values;
+ values = mapping.getValues();
+ indices = mapping.getIndices();
}
}
private static final class DenseElement extends AbstractElement {
int index;
-
- DenseElement(int ind, OrderedIntDoubleMapping mapping) {
- super(ind, mapping);
+ SequentialAccessSparseVector v;
+ DenseElement(int ind, SequentialAccessSparseVector v) {
+ super(ind, v);
+ this.v = v;
index = ind;
}
@@ -334,14 +238,15 @@
@Override
public void set(double value) {
+ v.lengthSquared = -1;
if(value != 0.0) mapping.set(indices[offset], value);
}
}
private static final class SparseElement extends AbstractElement {
- SparseElement(int ind, OrderedIntDoubleMapping mapping) {
- super(ind, mapping);
+ SparseElement(int ind, SequentialAccessSparseVector v) {
+ super(ind, v);
}
@Override
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java Thu Jan 28 06:11:42 2010
@@ -74,6 +74,23 @@
return clone;
}
+ /**
+ * Iterate over columns.
+ * @param index of the column number to grab as a vector (shallowly)
+ * @return the column vector at that index.
+ */
+ protected Vector slice(int index) {
+ return getColumn(index);
+ }
+
+ /**
+ * Abstracted out for the iterator
+ * @return numColumns()
+ */
+ public int numSlices() {
+ return numCols();
+ }
+
@Override
public double getQuick(int row, int column) {
if (columns[column] == null) {
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java Thu Jan 28 06:11:42 2010
@@ -18,6 +18,9 @@
package org.apache.mahout.math;
+import org.apache.mahout.math.function.BinaryFunction;
+import org.apache.mahout.math.function.UnaryFunction;
+
import java.util.Iterator;
import java.util.Map;
@@ -75,7 +78,7 @@
/**
* Apply the function to each element of the receiver
*
- * @param function a DoubleFunction to apply
+ * @param function a UnaryFunction to apply
* @return the modified receiver
*/
Vector assign(UnaryFunction function);
@@ -84,7 +87,7 @@
* Apply the function to each element of the receiver and the corresponding element of the other argument
*
* @param other a Vector containing the second arguments to the function
- * @param function a DoubleDoubleFunction to apply
+ * @param function a BinaryFunction to apply
* @return the modified receiver
* @throws CardinalityException if the cardinalities differ
*/
@@ -379,12 +382,33 @@
*/
// void getNonZeros(IntArrayList jx, DoubleArrayList values);
// void foreachNonZero(IntDoubleFunction f);
- // double aggregate(DoubleDoubleFunction aggregator, DoubleFunction map);
- // double aggregate(Vector other, DoubleDoubleFunction aggregator,
- // DoubleDoubleFunction map);
- // NewVector assign(Vector y, DoubleDoubleFunction function, IntArrayList
+ // BinaryFunction map);
+ // NewVector assign(Vector y, BinaryFunction function, IntArrayList
// nonZeroIndexes);
+ /**
+ * Examples speak louder than words: aggregate(plus, pow(2)) is another way to say
+ * getLengthSquared(), aggregate(max, abs) is norm(Double.POSITIVE_INFINITY). To sum all of the postive values,
+ * aggregate(plus, max(0)).
+ * @param aggregator used to combine the current value of the aggregation with the result of map.apply(nextValue)
+ * @param map a function to apply to each element of the vector in turn before passing to the aggregator
+ * @return the final aggregation
+ */
+ double aggregate(BinaryFunction aggregator, UnaryFunction map);
+
+ /**
+ * <p>Generalized inner product - take two vectors, iterate over them both, using the combiner to combine together
+ * (and possibly map in some way) each pair of values, which are then aggregated with the previous accumulated
+ * value in the combiner.</p>
+ * <p>
+ * Example: dot(other) could be expressed as aggregate(other, Plus, Times), and kernelized inner products (which
+ * are symmetric on the indices) work similarly.
+ * @param other a vector to aggregate in combination with
+ * @param aggregator
+ * @param combiner
+ * @return the final aggregation
+ */
+ double aggregate(Vector other, BinaryFunction aggregator, BinaryFunction combiner);
/** Return the sum of squares of all elements in the vector. Square root of this value is the length of the vector. */
double getLengthSquared();
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorIterable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorIterable.java?rev=903965&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorIterable.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorIterable.java Thu Jan 28 06:11:42 2010
@@ -0,0 +1,36 @@
+package org.apache.mahout.math;
+
+import java.util.Iterator;
+
+
+public interface VectorIterable extends Iterable<MatrixSlice> {
+
+ Iterator<MatrixSlice> iterateAll();
+
+ int numSlices();
+
+ int numRows();
+
+ int numCols();
+
+ /**
+ * Return a new vector with cardinality equal to getNumRows() of this matrix which is the matrix product of the
+ * recipient and the argument
+ *
+ * @param v a vector with cardinality equal to getNumCols() of the recipient
+ * @return a new vector (typically a DenseVector)
+ * @throws CardinalityException if this.getNumRows() != v.size()
+ */
+ Vector times(Vector v);
+
+ /**
+ * Convenience method for producing this.transpose().times(this.times(v)), which can be implemented with only one pass
+ * over the matrix, without making the transpose() call (which can be expensive if the matrix is sparse)
+ *
+ * @param v a vector with cardinality equal to getNumCols() of the recipient
+ * @return a new vector (typically a DenseVector) with cardinality equal to that of the argument.
+ * @throws CardinalityException if this.getNumCols() != v.size()
+ */
+ Vector timesSquared(Vector v);
+
+}
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java Thu Jan 28 06:11:42 2010
@@ -138,7 +138,7 @@
@Override
public void set(double value) {
- el.set(value);
+ decorated.set(value);
}
};
return;
@@ -198,7 +198,7 @@
@Override
public void set(double value) {
- el.set(value);
+ decorated.set(value);
}
};
return;
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianSolver.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianSolver.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianSolver.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianSolver.java Thu Jan 28 06:11:42 2010
@@ -27,9 +27,9 @@
import org.apache.mahout.math.DenseMatrix;
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Matrix;
-import org.apache.mahout.math.PlusWithScaleFunction;
-import org.apache.mahout.math.TimesFunction;
+import org.apache.mahout.math.function.TimesFunction;
import org.apache.mahout.math.Vector;
+import org.apache.mahout.math.function.PlusMult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -263,7 +263,7 @@
*/
for (int i = 0; i < state.getNumEigensProcessed(); i++) {
Vector previousEigen = previousEigens.getRow(i);
- currentPseudoEigen.assign(previousEigen, new PlusWithScaleFunction(-state.getHelperVector().get(i)));
+ currentPseudoEigen.assign(previousEigen, new PlusMult(-state.getHelperVector().get(i)));
state.getHelperVector().set(i, 0);
}
if (debug && currentPseudoEigen.norm(2) > 0) {
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java Thu Jan 28 06:11:42 2010
@@ -18,9 +18,8 @@
package org.apache.mahout.math.decomposer.hebbian;
-import org.apache.mahout.math.PlusFunction;
-import org.apache.mahout.math.PlusWithScaleFunction;
import org.apache.mahout.math.Vector;
+import org.apache.mahout.math.function.PlusMult;
public class HebbianUpdater implements EigenUpdater {
@@ -39,7 +38,7 @@
}
if (currentState.getActivationDenominatorSquared() == 0 || trainingVectorNorm == 0) {
if (currentState.getActivationDenominatorSquared() == 0) {
- pseudoEigen.assign(trainingVector, new PlusFunction());
+ pseudoEigen.assign(trainingVector, new PlusMult(1));
currentState.setHelperVector(currentState.currentTrainingProjection().clone());
double helperNorm = currentState.getHelperVector().norm(2);
currentState.setActivationDenominatorSquared(trainingVectorNorm * trainingVectorNorm - helperNorm * helperNorm);
@@ -53,8 +52,8 @@
currentState.setActivationDenominatorSquared(currentState.getActivationDenominatorSquared() + 2 * activation * currentState.getActivationNumerator()
+ (activation * activation) * (trainingVector.getLengthSquared() - currentState.currentTrainingProjection().getLengthSquared()));
if (numPreviousEigens > 0)
- currentState.getHelperVector().assign(currentState.currentTrainingProjection(), new PlusWithScaleFunction(activation));
- pseudoEigen.assign(trainingVector, new PlusWithScaleFunction(activation));
+ currentState.getHelperVector().assign(currentState.currentTrainingProjection(), new PlusMult(activation));
+ pseudoEigen.assign(trainingVector, new PlusMult(activation));
}
private static void updateTrainingProjectionsVector(TrainingState state,
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/lanczos/LanczosSolver.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/lanczos/LanczosSolver.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/lanczos/LanczosSolver.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/lanczos/LanczosSolver.java Thu Jan 28 06:11:42 2010
@@ -24,9 +24,9 @@
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Matrix;
-import org.apache.mahout.math.PlusWithScaleFunction;
import org.apache.mahout.math.SparseRowMatrix;
-import org.apache.mahout.math.UnaryFunction;
+import org.apache.mahout.math.function.PlusMult;
+import org.apache.mahout.math.function.UnaryFunction;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.matrix.DoubleMatrix1D;
import org.apache.mahout.math.matrix.DoubleMatrix2D;
@@ -109,10 +109,10 @@
Vector nextVector = corpus.timesSquared(currentVector);
log.info("{} passes through the corpus so far...", i);
nextVector.assign(new Scale(1 / scaleFactor));
- nextVector.assign(previousVector, new PlusWithScaleFunction(-beta));
+ nextVector.assign(previousVector, new PlusMult(-beta));
// now orthogonalize
alpha = currentVector.dot(nextVector);
- nextVector.assign(currentVector, new PlusWithScaleFunction(-alpha));
+ nextVector.assign(currentVector, new PlusMult(-alpha));
endTime(TimingSection.ITERATE);
startTime(TimingSection.ORTHOGANLIZE);
orthoganalizeAgainstAllButLast(nextVector, basis);
@@ -152,7 +152,7 @@
DoubleMatrix1D ejCol = eigenVects.viewColumn(basis.numRows() - i - 1);
for (int j = 0; j < ejCol.size(); j++) {
double d = ejCol.getQuick(j);
- realEigen.assign(basis.getRow(j), new PlusWithScaleFunction(d));
+ realEigen.assign(basis.getRow(j), new PlusMult(d));
}
realEigen = realEigen.normalize();
eigenVectors.assignRow(i, realEigen);
@@ -170,7 +170,7 @@
private static void orthoganalizeAgainstAllButLast(Vector nextVector, Matrix basis) {
for (int i = 0; i < basis.numRows() - 1; i++) {
double alpha = nextVector.dot(basis.getRow(i));
- nextVector.assign(basis.getRow(i), new PlusWithScaleFunction(-alpha));
+ nextVector.assign(basis.getRow(i), new PlusMult(-alpha));
}
}
@@ -183,7 +183,7 @@
if (v == null) {
v = new DenseVector(vector.size()).plus(vector);
} else {
- v.assign(vector, new PlusWithScaleFunction(1));
+ v.assign(vector, new PlusMult(1));
}
}
v.assign(new Scale(1.0 / v.norm(2)));
Copied: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/BinaryFunction.java (from r903542, lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/BinaryFunction.java)
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/BinaryFunction.java?p2=lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/BinaryFunction.java&p1=lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/BinaryFunction.java&r1=903542&r2=903965&rev=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/BinaryFunction.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/BinaryFunction.java Thu Jan 28 06:11:42 2010
@@ -15,24 +15,13 @@
* limitations under the License.
*/
-package org.apache.mahout.math;
+package org.apache.mahout.math.function;
/**
* This interface allows the formulation of binary functions to be applied to matrices inside the inner loops of their
* implementations.
*/
-public interface BinaryFunction {
+public interface BinaryFunction extends DoubleDoubleFunction {
- BinaryFunction plus = new PlusFunction();
- BinaryFunction times = new TimesFunction();
-
- /**
- * Apply the function to the arguments and return the result
- *
- * @param arg1 a double for the first argument
- * @param arg2 a double for the second argument
- * @return the result of applying the function
- */
- double apply(double arg1, double arg2);
}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/BinaryFunction.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleDoubleFunction.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleDoubleFunction.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleDoubleFunction.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleDoubleFunction.java Thu Jan 28 06:11:42 2010
@@ -11,20 +11,15 @@
/**
* Interface that represents a function object: a function that takes two arguments and returns a single value.
- *
- * @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 interface DoubleDoubleFunction {
/**
- * Applies a function to two arguments.
+ * Apply the function to the arguments and return the result
*
- * @param x the first argument passed to the function.
- * @param y the second argument passed to the function.
- * @return the result of the function.
+ * @param arg1 a double for the first argument
+ * @param arg2 a double for the second argument
+ * @return the result of applying the function
*/
- double apply(double x, double y);
+ double apply(double arg1, double arg2);
}
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleFunction.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleFunction.java?rev=903965&r1=903964&r2=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleFunction.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleFunction.java Thu Jan 28 06:11:42 2010
@@ -30,15 +30,16 @@
/**
* Interface that represents a function object: a function that takes a single argument and returns a single value.
- *
+ * @see org.apache.mahout.math.map
*/
public interface DoubleFunction {
/**
- * Applies a function to an argument.
+ * Apply the function to the argument and return the result
*
- * @param argument argument passed to the function.
- * @return the result of the function.
+ * @param arg1 double for the argument
+ * @return the result of applying the function
*/
- double apply(double argument);
+ double apply(double arg1);
+
}
Copied: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Functions.java (from r903924, 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/function/Functions.java?p2=lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Functions.java&p1=lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Functions.java&r1=903924&r2=903965&rev=903965&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Functions.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Functions.java Thu Jan 28 06:11:42 2010
@@ -6,12 +6,14 @@
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;
+package org.apache.mahout.math.function;
-import org.apache.mahout.math.function.DoubleDoubleFunction;
+import org.apache.mahout.math.function.BinaryFunction;
+import org.apache.mahout.math.function.UnaryFunction;
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.function.Mult;
+import org.apache.mahout.math.function.PlusMult;
import org.apache.mahout.math.jet.random.engine.MersenneTwister;
import java.util.Date;
@@ -22,8 +24,8 @@
* 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>
+ * {@link org.apache.mahout.math.function.UnaryFunction}, binary functions of type {@link
+ * org.apache.mahout.math.function.BinaryFunction}. 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>.
@@ -33,27 +35,27 @@
* <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>
+ * argument. See methods {@link #bindArg1(org.apache.mahout.math.function.BinaryFunction ,double)} and {@link
+ * #bindArg2(org.apache.mahout.math.function.BinaryFunction ,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.BinaryFunction)}. 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
+ * #chain(org.apache.mahout.math.function.UnaryFunction , org.apache.mahout.math.function.UnaryFunction)}: <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>
+ * #chain(org.apache.mahout.math.function.UnaryFunction , org.apache.mahout.math.function.BinaryFunction)}: <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.BinaryFunction , org.apache.mahout.math.function.UnaryFunction , org.apache.mahout.math.function.UnaryFunction)}: <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() {
+ * new BinaryFunction() {
* 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">
+ * <p> For aliasing see functions. Try this <table> <td class="PRE">
* <pre>
* // should yield 1.4399560356056456 in all cases
* double a = 0.5;
@@ -61,9 +63,9 @@
* 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));
+ * BinaryFunction f = F.chain(F.plus,F.sin,F.chain(F.square,F.cos));
* log.info(f.apply(a,b));
- * DoubleDoubleFunction g = new DoubleDoubleFunction() {
+ * BinaryFunction g = new BinaryFunction() {
* public double apply(double a, double b) { return Math.sin(a) + Math.pow(Math.cos(b),2); }
* };
* log.info(g.apply(a,b));
@@ -88,21 +90,14 @@
* 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() {
+ public static final UnaryFunction abs = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.abs(a);
@@ -110,7 +105,7 @@
};
/** Function that returns <tt>Math.acos(a)</tt>. */
- public static final DoubleFunction acos = new DoubleFunction() {
+ public static final UnaryFunction acos = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.acos(a);
@@ -118,7 +113,7 @@
};
/** Function that returns <tt>Math.asin(a)</tt>. */
- public static final DoubleFunction asin = new DoubleFunction() {
+ public static final UnaryFunction asin = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.asin(a);
@@ -126,7 +121,7 @@
};
/** Function that returns <tt>Math.atan(a)</tt>. */
- public static final DoubleFunction atan = new DoubleFunction() {
+ public static final UnaryFunction atan = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.atan(a);
@@ -134,7 +129,7 @@
};
/** Function that returns <tt>Math.ceil(a)</tt>. */
- public static final DoubleFunction ceil = new DoubleFunction() {
+ public static final UnaryFunction ceil = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.ceil(a);
@@ -142,7 +137,7 @@
};
/** Function that returns <tt>Math.cos(a)</tt>. */
- public static final DoubleFunction cos = new DoubleFunction() {
+ public static final UnaryFunction cos = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.cos(a);
@@ -150,7 +145,7 @@
};
/** Function that returns <tt>Math.exp(a)</tt>. */
- public static final DoubleFunction exp = new DoubleFunction() {
+ public static final UnaryFunction exp = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.exp(a);
@@ -158,7 +153,7 @@
};
/** Function that returns <tt>Math.floor(a)</tt>. */
- public static final DoubleFunction floor = new DoubleFunction() {
+ public static final UnaryFunction floor = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.floor(a);
@@ -166,7 +161,7 @@
};
/** Function that returns its argument. */
- public static final DoubleFunction identity = new DoubleFunction() {
+ public static final UnaryFunction identity = new UnaryFunction() {
@Override
public double apply(double a) {
return a;
@@ -174,7 +169,7 @@
};
/** Function that returns <tt>1.0 / a</tt>. */
- public static final DoubleFunction inv = new DoubleFunction() {
+ public static final UnaryFunction inv = new UnaryFunction() {
@Override
public double apply(double a) {
return 1.0 / a;
@@ -182,7 +177,7 @@
};
/** Function that returns <tt>Math.log(a)</tt>. */
- public static final DoubleFunction logarithm = new DoubleFunction() {
+ public static final UnaryFunction logarithm = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.log(a);
@@ -190,7 +185,7 @@
};
/** Function that returns <tt>Math.log(a) / Math.log(2)</tt>. */
- public static final DoubleFunction log2 = new DoubleFunction() {
+ public static final UnaryFunction log2 = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.log(a) * 1.4426950408889634;
@@ -198,7 +193,7 @@
};
/** Function that returns <tt>-a</tt>. */
- public static final DoubleFunction neg = new DoubleFunction() {
+ public static final UnaryFunction negate = new UnaryFunction() {
@Override
public double apply(double a) {
return -a;
@@ -206,7 +201,7 @@
};
/** Function that returns <tt>Math.rint(a)</tt>. */
- public static final DoubleFunction rint = new DoubleFunction() {
+ public static final UnaryFunction rint = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.rint(a);
@@ -214,7 +209,7 @@
};
/** Function that returns <tt>a < 0 ? -1 : a > 0 ? 1 : 0</tt>. */
- public static final DoubleFunction sign = new DoubleFunction() {
+ public static final UnaryFunction sign = new UnaryFunction() {
@Override
public double apply(double a) {
return a < 0 ? -1 : a > 0 ? 1 : 0;
@@ -222,7 +217,7 @@
};
/** Function that returns <tt>Math.sin(a)</tt>. */
- public static final DoubleFunction sin = new DoubleFunction() {
+ public static final UnaryFunction sin = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.sin(a);
@@ -230,7 +225,7 @@
};
/** Function that returns <tt>Math.sqrt(a)</tt>. */
- public static final DoubleFunction sqrt = new DoubleFunction() {
+ public static final UnaryFunction sqrt = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.sqrt(a);
@@ -238,7 +233,7 @@
};
/** Function that returns <tt>a * a</tt>. */
- public static final DoubleFunction square = new DoubleFunction() {
+ public static final UnaryFunction square = new UnaryFunction() {
@Override
public double apply(double a) {
return a * a;
@@ -246,7 +241,7 @@
};
/** Function that returns <tt>Math.tan(a)</tt>. */
- public static final DoubleFunction tan = new DoubleFunction() {
+ public static final UnaryFunction tan = new UnaryFunction() {
@Override
public double apply(double a) {
return Math.tan(a);
@@ -259,7 +254,7 @@
*****************************/
/** Function that returns <tt>Math.atan2(a,b)</tt>. */
- public static final DoubleDoubleFunction atan2 = new DoubleDoubleFunction() {
+ public static final BinaryFunction atan2 = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return Math.atan2(a, b);
@@ -270,14 +265,14 @@
* Function that returns <tt>com.imsl.math.Sfun.logBeta(a,b)</tt>.
*/
/*
- public static final DoubleDoubleFunction logBeta = new DoubleDoubleFunction() {
+ public static final BinaryFunction logBeta = new BinaryFunction() {
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() {
+ public static final BinaryFunction compare = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return a < b ? -1 : a > b ? 1 : 0;
@@ -285,7 +280,7 @@
};
/** Function that returns <tt>a / b</tt>. */
- public static final DoubleDoubleFunction div = new DoubleDoubleFunction() {
+ public static final BinaryFunction div = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return a / b;
@@ -293,7 +288,7 @@
};
/** Function that returns <tt>a == b ? 1 : 0</tt>. */
- public static final DoubleDoubleFunction equals = new DoubleDoubleFunction() {
+ public static final BinaryFunction equals = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return a == b ? 1 : 0;
@@ -301,7 +296,7 @@
};
/** Function that returns <tt>a > b ? 1 : 0</tt>. */
- public static final DoubleDoubleFunction greater = new DoubleDoubleFunction() {
+ public static final BinaryFunction greater = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return a > b ? 1 : 0;
@@ -309,7 +304,7 @@
};
/** Function that returns <tt>Math.IEEEremainder(a,b)</tt>. */
- public static final DoubleDoubleFunction IEEEremainder = new DoubleDoubleFunction() {
+ public static final BinaryFunction IEEEremainder = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return Math.IEEEremainder(a, b);
@@ -341,7 +336,7 @@
};
/** Function that returns <tt>a < b ? 1 : 0</tt>. */
- public static final DoubleDoubleFunction less = new DoubleDoubleFunction() {
+ public static final BinaryFunction less = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return a < b ? 1 : 0;
@@ -349,7 +344,7 @@
};
/** Function that returns <tt>Math.log(a) / Math.log(b)</tt>. */
- public static final DoubleDoubleFunction lg = new DoubleDoubleFunction() {
+ public static final BinaryFunction lg = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return Math.log(a) / Math.log(b);
@@ -357,7 +352,7 @@
};
/** Function that returns <tt>Math.max(a,b)</tt>. */
- public static final DoubleDoubleFunction max = new DoubleDoubleFunction() {
+ public static final BinaryFunction max = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return Math.max(a, b);
@@ -365,7 +360,7 @@
};
/** Function that returns <tt>Math.min(a,b)</tt>. */
- public static final DoubleDoubleFunction min = new DoubleDoubleFunction() {
+ public static final BinaryFunction min = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return Math.min(a, b);
@@ -373,15 +368,15 @@
};
/** Function that returns <tt>a - b</tt>. */
- public static final DoubleDoubleFunction minus = plusMult(-1);
+ public static final BinaryFunction minus = plusMult(-1);
/*
- new DoubleDoubleFunction() {
+ new BinaryFunction() {
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() {
+ public static final BinaryFunction mod = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return a % b;
@@ -389,7 +384,7 @@
};
/** Function that returns <tt>a * b</tt>. */
- public static final DoubleDoubleFunction mult = new DoubleDoubleFunction() {
+ public static final BinaryFunction mult = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return a * b;
@@ -397,15 +392,15 @@
};
/** Function that returns <tt>a + b</tt>. */
- public static final DoubleDoubleFunction plus = plusMult(1);
+ public static final BinaryFunction plus = plusMult(1);
/*
- new DoubleDoubleFunction() {
+ new BinaryFunction() {
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() {
+ public static final BinaryFunction plusAbs = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return Math.abs(a) + Math.abs(b);
@@ -413,7 +408,7 @@
};
/** Function that returns <tt>Math.pow(a,b)</tt>. */
- public static final DoubleDoubleFunction pow = new DoubleDoubleFunction() {
+ public static final BinaryFunction pow = new BinaryFunction() {
@Override
public double apply(double a, double b) {
return Math.pow(a, b);
@@ -427,8 +422,8 @@
* 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() {
+ public static UnaryFunction between(final double from, final double to) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return (from <= a && a <= to) ? 1 : 0;
@@ -443,8 +438,8 @@
* @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() {
+ public static UnaryFunction bindArg1(final BinaryFunction function, final double c) {
+ return new UnaryFunction() {
@Override
public double apply(double var) {
return function.apply(c, var);
@@ -459,8 +454,8 @@
* @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() {
+ public static UnaryFunction bindArg2(final BinaryFunction function, final double c) {
+ return new UnaryFunction() {
@Override
public double apply(double var) {
return function.apply(var, c);
@@ -476,9 +471,9 @@
* @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() {
+ public static BinaryFunction chain(final BinaryFunction f, final UnaryFunction g,
+ final UnaryFunction h) {
+ return new BinaryFunction() {
@Override
public double apply(double a, double b) {
return f.apply(g.apply(a), h.apply(b));
@@ -493,8 +488,8 @@
* @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() {
+ public static BinaryFunction chain(final UnaryFunction g, final BinaryFunction h) {
+ return new BinaryFunction() {
@Override
public double apply(double a, double b) {
return g.apply(h.apply(a, b));
@@ -509,8 +504,8 @@
* @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() {
+ public static UnaryFunction chain(final UnaryFunction g, final UnaryFunction h) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return g.apply(h.apply(a));
@@ -522,8 +517,8 @@
* 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() {
+ public static UnaryFunction compare(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return a < b ? -1 : a > b ? 1 : 0;
@@ -532,8 +527,8 @@
}
/** Constructs a function that returns the constant <tt>c</tt>. */
- public static DoubleFunction constant(final double c) {
- return new DoubleFunction() {
+ public static UnaryFunction constant(final double c) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return c;
@@ -543,13 +538,13 @@
/** 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) {
+ public static UnaryFunction 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() {
+ public static UnaryFunction equals(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return a == b ? 1 : 0;
@@ -558,8 +553,8 @@
}
/** 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() {
+ public static UnaryFunction greater(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return a > b ? 1 : 0;
@@ -571,8 +566,8 @@
* 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() {
+ public static UnaryFunction IEEEremainder(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return Math.IEEEremainder(a, b);
@@ -624,8 +619,8 @@
}
/** 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() {
+ public static UnaryFunction less(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return a < b ? 1 : 0;
@@ -637,8 +632,8 @@
* 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() {
+ public static UnaryFunction lg(final double b) {
+ return new UnaryFunction() {
private final double logInv = 1 / Math.log(b); // cached for speed
@Override
@@ -649,8 +644,8 @@
}
/** 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() {
+ public static UnaryFunction max(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return Math.max(a, b);
@@ -659,8 +654,8 @@
}
/** 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() {
+ public static UnaryFunction min(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return Math.min(a, b);
@@ -669,7 +664,7 @@
}
/** 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) {
+ public static UnaryFunction minus(double b) {
return plus(-b);
}
@@ -677,13 +672,13 @@
* 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) {
+ public static BinaryFunction 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() {
+ public static UnaryFunction mod(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return a % b;
@@ -692,18 +687,18 @@
}
/** 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) {
+ public static UnaryFunction mult(double b) {
return new Mult(b);
/*
- return new DoubleFunction() {
+ return new UnaryFunction() {
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() {
+ public static UnaryFunction plus(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return a + b;
@@ -715,18 +710,18 @@
* 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) {
+ public static BinaryFunction plusMult(double constant) {
return new PlusMult(constant);
/*
- return new DoubleDoubleFunction() {
+ return new BinaryFunction() {
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() {
+ public static UnaryFunction pow(final double b) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return Math.pow(a, b);
@@ -743,7 +738,7 @@
* interfaces. Thus, if you are not happy with the default, just pass your favourite random generator to function
* evaluating methods.
*/
- public static DoubleFunction random() {
+ public static UnaryFunction random() {
return new MersenneTwister(new Date());
}
@@ -755,8 +750,8 @@
* precision = 10 rounds 123 --> 120 , 127 --> 130
* </pre>
*/
- public static DoubleFunction round(final double precision) {
- return new DoubleFunction() {
+ public static UnaryFunction round(final double precision) {
+ return new UnaryFunction() {
@Override
public double apply(double a) {
return Math.rint(a / precision) * precision;
@@ -771,8 +766,8 @@
* @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() {
+ public static BinaryFunction swapArgs(final BinaryFunction function) {
+ return new BinaryFunction() {
@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/function/Functions.java
------------------------------------------------------------------------------
svn:eol-style = native