You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ss...@apache.org on 2013/04/01 11:18:13 UTC
svn commit: r1463115 [2/2] - in /mahout/trunk: ./
core/src/main/java/org/apache/mahout/cf/taste/hadoop/
core/src/main/java/org/apache/mahout/cf/taste/hadoop/item/
core/src/main/java/org/apache/mahout/cf/taste/impl/recommender/
core/src/main/java/org/ap...
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/Sorting.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/Sorting.java?rev=1463115&r1=1463114&r2=1463115&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/Sorting.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/Sorting.java Mon Apr 1 09:18:12 2013
@@ -17,6 +17,7 @@
package org.apache.mahout.math;
+import java.io.Serializable;
import java.util.Comparator;
import com.google.common.base.Preconditions;
@@ -34,387 +35,7 @@ public final class Sorting {
private static final int SIMPLE_LENGTH = 7;
static final int SMALL = 7;
- private Sorting() {
- /* empty */
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array. Searching in an unsorted array has an undefined
- * result. It's also undefined which element is found if there are multiple
- * occurrences of the same element.
- *
- * @param array
- * the sorted {@code byte} array to search.
- * @param value
- * the {@code byte} element to find.
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @return the non-negative index of the element, or a negative index which is
- * {@code -index - 1} where the element would be inserted.
- */
- public static int binarySearchFromTo(byte[] array, byte value, int from, int to) {
- int mid = -1;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if (value > array[mid]) {
- from = mid + 1;
- } else if (value == array[mid]) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- if (mid < 0) {
- return -1;
- }
-
- return -mid - (value < array[mid] ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array. Searching in an unsorted array has an undefined
- * result. It's also undefined which element is found if there are multiple
- * occurrences of the same element.
- *
- * @param array
- * the sorted {@code char} array to search.
- * @param value
- * the {@code char} element to find.
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @return the non-negative index of the element, or a negative index which is
- * {@code -index - 1} where the element would be inserted.
- */
- public static int binarySearchFromTo(char[] array, char value, int from, int to) {
- int mid = -1;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if (value > array[mid]) {
- from = mid + 1;
- } else if (value == array[mid]) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- if (mid < 0) {
- return -1;
- }
- return -mid - (value < array[mid] ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array. Searching in an unsorted array has an undefined
- * result. It's also undefined which element is found if there are multiple
- * occurrences of the same element.
- *
- * @param array
- * the sorted {@code double} array to search.
- * @param value
- * the {@code double} element to find.
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @return the non-negative index of the element, or a negative index which is
- * {@code -index - 1} where the element would be inserted.
- */
- public static int binarySearchFromTo(double[] array, double value, int from, int to) {
- long longBits = Double.doubleToLongBits(value);
- int mid = -1;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if (lessThan(array[mid], value)) {
- from = mid + 1;
- } else if (longBits == Double.doubleToLongBits(array[mid])) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- if (mid < 0) {
- return -1;
- }
- return -mid - (lessThan(value, array[mid]) ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array. Searching in an unsorted array has an undefined
- * result. It's also undefined which element is found if there are multiple
- * occurrences of the same element.
- *
- * @param array
- * the sorted {@code float} array to search.
- * @param value
- * the {@code float} element to find.
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @return the non-negative index of the element, or a negative index which is
- * {@code -index - 1} where the element would be inserted.
- */
- public static int binarySearchFromTo(float[] array, float value, int from, int to) {
- int intBits = Float.floatToIntBits(value);
- int mid = -1;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if (lessThan(array[mid], value)) {
- from = mid + 1;
- } else if (intBits == Float.floatToIntBits(array[mid])) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- if (mid < 0) {
- return -1;
- }
- return -mid - (lessThan(value, array[mid]) ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array. Searching in an unsorted array has an undefined
- * result. It's also undefined which element is found if there are multiple
- * occurrences of the same element.
- *
- * @param array
- * the sorted {@code int} array to search.
- * @param value
- * the {@code int} element to find.
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @return the non-negative index of the element, or a negative index which is
- * {@code -index - 1} where the element would be inserted.
- */
- public static int binarySearchFromTo(int[] array, int value, int from, int to) {
- int mid = -1;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if (value > array[mid]) {
- from = mid + 1;
- } else if (value == array[mid]) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- if (mid < 0) {
- return -1;
- }
- return -mid - (value < array[mid] ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array. Searching in an unsorted array has an undefined
- * result. It's also undefined which element is found if there are multiple
- * occurrences of the same element.
- *
- * @param array
- * the sorted {@code long} array to search.
- * @param value
- * the {@code long} element to find.
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @return the non-negative index of the element, or a negative index which is
- * {@code -index - 1} where the element would be inserted.
- */
- public static int binarySearchFromTo(long[] array, long value, int from, int to) {
- int mid = -1;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if (value > array[mid]) {
- from = mid + 1;
- } else if (value == array[mid]) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- if (mid < 0) {
- return -1;
- }
- return -mid - (value < array[mid] ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array. Searching in an unsorted array has an undefined
- * result. It's also undefined which element is found if there are multiple
- * occurrences of the same element.
- *
- * @param array
- * the sorted {@code Object} array to search.
- * @param object
- * the {@code Object} element to find
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @return the non-negative index of the element, or a negative index which is
- * {@code -index - 1} where the element would be inserted.
- *
- */
- public static <T extends Comparable<T>> int binarySearchFromTo(T[] array, T object, int from, int to) {
- if (array.length == 0) {
- return -1;
- }
-
- int mid = 0;
- int result = 0;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if ((result = array[mid].compareTo(object)) < 0) {
- from = mid + 1;
- } else if (result == 0) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- return -mid - (result >= 0 ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array using the {@code Comparator} to compare elements.
- * Searching in an unsorted array has an undefined result. It's also undefined
- * which element is found if there are multiple occurrences of the same
- * element.
- *
- * @param array
- * the sorted array to search
- * @param object
- * the element to find
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @param comparator
- * the {@code Comparator} used to compare the elements.
- * @return the non-negative index of the element, or a negative index which
- */
- public static <T> int binarySearchFromTo(T[] array, T object, int from, int to, Comparator<? super T> comparator) {
- int mid = 0;
- int result = 0;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if ((result = comparator.compare(array[mid], object)) < 0) {
- from = mid + 1;
- } else if (result == 0) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- return -mid - (result >= 0 ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * ascending sorted array. Searching in an unsorted array has an undefined
- * result. It's also undefined which element is found if there are multiple
- * occurrences of the same element.
- *
- * @param array
- * the sorted {@code short} array to search.
- * @param value
- * the {@code short} element to find.
- * @param from
- * the first index to sort, inclusive.
- * @param to
- * the last index to sort, inclusive.
- * @return the non-negative index of the element, or a negative index which is
- * {@code -index - 1} where the element would be inserted.
- */
- public static int binarySearchFromTo(short[] array, short value, int from, int to) {
- int mid = -1;
- while (from <= to) {
- mid = (from + to) >>> 1;
- if (value > array[mid]) {
- from = mid + 1;
- } else if (value == array[mid]) {
- return mid;
- } else {
- to = mid - 1;
- }
- }
- if (mid < 0) {
- return -1;
- }
- return -mid - (value < array[mid] ? 1 : 2);
- }
-
- private static boolean lessThan(double double1, double double2) {
- // A slightly specialized version of
- // Double.compare(double1, double2) < 0.
-
- // Non-zero and non-NaN checking.
- if (double1 < double2) {
- return true;
- }
- if (double1 > double2) {
- return false;
- }
- if (double1 == double2 && double1 != 0.0) {
- return false;
- }
-
- // NaNs are equal to other NaNs and larger than any other double.
- if (Double.isNaN(double1)) {
- return false;
- }
- if (Double.isNaN(double2)) {
- return true;
- }
-
- // Deal with +0.0 and -0.0.
- long d1 = Double.doubleToRawLongBits(double1);
- long d2 = Double.doubleToRawLongBits(double2);
- return d1 < d2;
- }
-
- private static boolean lessThan(float float1, float float2) {
- // A slightly specialized version of Float.compare(float1, float2) < 0.
-
- // Non-zero and non-NaN checking.
- if (float1 < float2) {
- return true;
- }
- if (float1 > float2) {
- return false;
- }
- if (float1 == float2 && float1 != 0.0f) {
- return false;
- }
-
- // NaNs are equal to other NaNs and larger than any other float
- if (Float.isNaN(float1)) {
- return false;
- }
- if (Float.isNaN(float2)) {
- return true;
- }
-
- // Deal with +0.0 and -0.0
- int f1 = Float.floatToRawIntBits(float1);
- int f2 = Float.floatToRawIntBits(float2);
- return f1 < f2;
- }
+ private Sorting() {}
private static <T> int med3(T[] array, int a, int b, int c, Comparator<T> comp) {
T x = array[a];
@@ -816,15 +437,13 @@ public final class Sorting {
* @throws ArrayIndexOutOfBoundsException
* if {@code start < 0} or {@code end > array.length}.
*/
- public static void quickSort(char[] array, int start, int end,
- CharComparator comp) {
+ public static void quickSort(char[] array, int start, int end, CharComparator comp) {
Preconditions.checkNotNull(array);
checkBounds(array.length, start, end);
quickSort0(start, end, array, comp);
}
- private static void quickSort0(int start, int end, char[] array,
- CharComparator comp) {
+ private static void quickSort0(int start, int end, char[] array, CharComparator comp) {
char temp;
int length = end - start;
if (length < 7) {
@@ -921,15 +540,13 @@ public final class Sorting {
* if {@code start < 0} or {@code end > array.length}.
* @see Double#compareTo(Double)
*/
- public static void quickSort(double[] array, int start, int end,
- DoubleComparator comp) {
+ public static void quickSort(double[] array, int start, int end, DoubleComparator comp) {
Preconditions.checkNotNull(array);
checkBounds(array.length, start, end);
quickSort0(start, end, array, comp);
}
- private static void quickSort0(int start, int end, double[] array,
- DoubleComparator comp) {
+ private static void quickSort0(int start, int end, double[] array, DoubleComparator comp) {
double temp;
int length = end - start;
if (length < 7) {
@@ -1025,15 +642,13 @@ public final class Sorting {
* @throws ArrayIndexOutOfBoundsException
* if {@code start < 0} or {@code end > array.length}.
*/
- public static void quickSort(float[] array, int start, int end,
- FloatComparator comp) {
+ public static void quickSort(float[] array, int start, int end, FloatComparator comp) {
Preconditions.checkNotNull(array);
checkBounds(array.length, start, end);
quickSort0(start, end, array, comp);
}
- private static void quickSort0(int start, int end, float[] array,
- FloatComparator comp) {
+ private static void quickSort0(int start, int end, float[] array, FloatComparator comp) {
float temp;
int length = end - start;
if (length < 7) {
@@ -1129,15 +744,13 @@ public final class Sorting {
* @throws ArrayIndexOutOfBoundsException
* if {@code start < 0} or {@code end > array.length}.
*/
- public static void quickSort(int[] array, int start, int end,
- IntComparator comp) {
+ public static void quickSort(int[] array, int start, int end, IntComparator comp) {
Preconditions.checkNotNull(array);
checkBounds(array.length, start, end);
quickSort0(start, end, array, comp);
}
- private static void quickSort0(int start, int end, int[] array,
- IntComparator comp) {
+ private static void quickSort0(int start, int end, int[] array, IntComparator comp) {
int temp;
int length = end - start;
if (length < 7) {
@@ -1233,15 +846,13 @@ public final class Sorting {
* @throws ArrayIndexOutOfBoundsException
* if {@code start < 0} or {@code end > array.length}.
*/
- public static void quickSort(long[] array, int start, int end,
- LongComparator comp) {
+ public static void quickSort(long[] array, int start, int end, LongComparator comp) {
Preconditions.checkNotNull(array);
checkBounds(array.length, start, end);
quickSort0(start, end, array, comp);
}
- private static void quickSort0(int start, int end, long[] array,
- LongComparator comp) {
+ private static void quickSort0(int start, int end, long[] array, LongComparator comp) {
long temp;
int length = end - start;
if (length < 7) {
@@ -1337,15 +948,14 @@ public final class Sorting {
* @throws ArrayIndexOutOfBoundsException
* if {@code start < 0} or {@code end > array.length}.
*/
- public static <T> void quickSort(T[] array, int start, int end,
- Comparator<T> comp) {
+ public static <T> void quickSort(T[] array, int start, int end, Comparator<T> comp) {
Preconditions.checkNotNull(array);
checkBounds(array.length, start, end);
quickSort0(start, end, array, comp);
}
private static final class ComparableAdaptor<T extends Comparable<? super T>>
- implements Comparator<T> {
+ implements Comparator<T>, Serializable {
@Override
public int compare(T o1, T o2) {
@@ -1362,13 +972,11 @@ public final class Sorting {
* @param start the first index.
* @param end the last index (exclusive).
*/
- public static <T extends Comparable<? super T>> void quickSort(T[] array,
- int start, int end) {
+ public static <T extends Comparable<? super T>> void quickSort(T[] array, int start, int end) {
quickSort(array, start, end, new ComparableAdaptor<T>());
}
- private static <T> void quickSort0(int start, int end, T[] array,
- Comparator<T> comp) {
+ private static <T> void quickSort0(int start, int end, T[] array, Comparator<T> comp) {
T temp;
int length = end - start;
if (length < 7) {
@@ -1462,15 +1070,13 @@ public final class Sorting {
* @throws ArrayIndexOutOfBoundsException
* if {@code start < 0} or {@code end > array.length}.
*/
- public static void quickSort(short[] array, int start, int end,
- ShortComparator comp) {
+ public static void quickSort(short[] array, int start, int end, ShortComparator comp) {
Preconditions.checkNotNull(array);
checkBounds(array.length, start, end);
quickSort0(start, end, array, comp);
}
- private static void quickSort0(int start, int end, short[] array,
- ShortComparator comp) {
+ private static void quickSort0(int start, int end, short[] array, ShortComparator comp) {
short temp;
int length = end - start;
if (length < 7) {
@@ -1560,8 +1166,7 @@ public final class Sorting {
* @param comp comparator object.
*/
@SuppressWarnings("unchecked") // required to make the temp array work, afaict.
- public static <T> void mergeSort(T[] array, int start, int end,
- Comparator<T> comp) {
+ public static <T> void mergeSort(T[] array, int start, int end, Comparator<T> comp) {
checkBounds(array.length, start, end);
int length = end - start;
if (length <= 0) {
@@ -1603,8 +1208,7 @@ public final class Sorting {
* @param c
* - the comparator to determine the order of the array.
*/
- private static <T> void mergeSort(T[] in, T[] out, int start, int end,
- Comparator<T> c) {
+ private static <T> void mergeSort(T[] in, T[] out, int start, int end, Comparator<T> c) {
int len = end - start;
// use insertion sort for small arrays
if (len <= SIMPLE_LENGTH) {
@@ -1686,8 +1290,7 @@ public final class Sorting {
* @param c
* - the comparator used to compare Objects
*/
- private static <T> int find(T[] arr, T val, int bnd, int l, int r,
- Comparator<T> c) {
+ private static <T> int find(T[] arr, T val, int bnd, int l, int r, Comparator<T> c) {
int m = l;
int d = 1;
while (m <= r) {
@@ -1741,8 +1344,7 @@ public final class Sorting {
mergeSort(out, array, start, end, comp);
}
- private static void mergeSort(byte[] in, byte[] out, int start, int end,
- ByteComparator c) {
+ private static void mergeSort(byte[] in, byte[] out, int start, int end, ByteComparator c) {
int len = end - start;
// use insertion sort for small arrays
if (len <= SIMPLE_LENGTH) {
@@ -1804,8 +1406,7 @@ public final class Sorting {
}
}
- private static int find(byte[] arr, byte val, int bnd, int l, int r,
- ByteComparator c) {
+ private static int find(byte[] arr, byte val, int bnd, int l, int r, ByteComparator c) {
int m = l;
int d = 1;
while (m <= r) {
@@ -1859,8 +1460,7 @@ public final class Sorting {
mergeSort(out, array, start, end, comp);
}
- private static void mergeSort(char[] in, char[] out, int start, int end,
- CharComparator c) {
+ private static void mergeSort(char[] in, char[] out, int start, int end, CharComparator c) {
int len = end - start;
// use insertion sort for small arrays
if (len <= SIMPLE_LENGTH) {
@@ -1922,8 +1522,7 @@ public final class Sorting {
}
}
- private static int find(char[] arr, char val, int bnd, int l, int r,
- CharComparator c) {
+ private static int find(char[] arr, char val, int bnd, int l, int r, CharComparator c) {
int m = l;
int d = 1;
while (m <= r) {
@@ -1978,8 +1577,7 @@ public final class Sorting {
* @param end the last index (exclusive).
* @param c the comparator object.
*/
- private static void mergeSort(short[] in, short[] out, int start, int end,
- ShortComparator c) {
+ private static void mergeSort(short[] in, short[] out, int start, int end, ShortComparator c) {
int len = end - start;
// use insertion sort for small arrays
if (len <= SIMPLE_LENGTH) {
@@ -2041,8 +1639,7 @@ public final class Sorting {
}
}
- private static int find(short[] arr, short val, int bnd, int l, int r,
- ShortComparator c) {
+ private static int find(short[] arr, short val, int bnd, int l, int r, ShortComparator c) {
int m = l;
int d = 1;
while (m <= r) {
@@ -2097,8 +1694,7 @@ public final class Sorting {
* @param end the last index (exclusive).
* @param c the comparator object.
*/
- private static void mergeSort(int[] in, int[] out, int start, int end,
- IntComparator c) {
+ private static void mergeSort(int[] in, int[] out, int start, int end, IntComparator c) {
int len = end - start;
// use insertion sort for small arrays
if (len <= SIMPLE_LENGTH) {
@@ -2160,8 +1756,7 @@ public final class Sorting {
}
}
- private static int find(int[] arr, int val, int bnd, int l, int r,
- IntComparator c) {
+ private static int find(int[] arr, int val, int bnd, int l, int r, IntComparator c) {
int m = l;
int d = 1;
while (m <= r) {
@@ -2216,8 +1811,7 @@ public final class Sorting {
mergeSort(out, array, start, end, comp);
}
- private static void mergeSort(long[] in, long[] out, int start, int end,
- LongComparator c) {
+ private static void mergeSort(long[] in, long[] out, int start, int end, LongComparator c) {
int len = end - start;
// use insertion sort for small arrays
if (len <= SIMPLE_LENGTH) {
@@ -2279,8 +1873,7 @@ public final class Sorting {
}
}
- private static int find(long[] arr, long val, int bnd, int l, int r,
- LongComparator c) {
+ private static int find(long[] arr, long val, int bnd, int l, int r, LongComparator c) {
int m = l;
int d = 1;
while (m <= r) {
@@ -2334,8 +1927,7 @@ public final class Sorting {
mergeSort(out, array, start, end, comp);
}
- private static void mergeSort(float[] in, float[] out, int start, int end,
- FloatComparator c) {
+ private static void mergeSort(float[] in, float[] out, int start, int end, FloatComparator c) {
int len = end - start;
// use insertion sort for small arrays
if (len <= SIMPLE_LENGTH) {
@@ -2397,8 +1989,7 @@ public final class Sorting {
}
}
- private static int find(float[] arr, float val, int bnd, int l, int r,
- FloatComparator c) {
+ private static int find(float[] arr, float val, int bnd, int l, int r, FloatComparator c) {
int m = l;
int d = 1;
while (m <= r) {
@@ -2453,8 +2044,7 @@ public final class Sorting {
mergeSort(out, array, start, end, comp);
}
- private static void mergeSort(double[] in, double[] out, int start, int end,
- DoubleComparator c) {
+ private static void mergeSort(double[] in, double[] out, int start, int end, DoubleComparator c) {
int len = end - start;
// use insertion sort for small arrays
if (len <= SIMPLE_LENGTH) {
@@ -2516,8 +2106,7 @@ public final class Sorting {
}
}
- private static int find(double[] arr, double val, int bnd, int l, int r,
- DoubleComparator c) {
+ private static int find(double[] arr, double val, int bnd, int l, int r, DoubleComparator c) {
int m = l;
int d = 1;
while (m <= r) {
@@ -2542,7 +2131,7 @@ public final class Sorting {
}
/**
- * Transforms two consecutive sorted ranges into a single sorted range. The initial ranges are {@code [first,}
+ * Transforms two consecutive sorted ranges into a single sorted range. The initial ranges are {@code [first,}
* middle)</code> and {@code [middle, last)}, and the resulting range is {@code [first, last)}. Elements in
* the first input range will precede equal elements in the second.
*/
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java?rev=1463115&r1=1463114&r2=1463115&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/WeightedVectorComparator.java Mon Apr 1 09:18:12 2013
@@ -17,12 +17,13 @@
package org.apache.mahout.math;
+import java.io.Serializable;
import java.util.Comparator;
/**
* Orders {@link WeightedVector} by {@link WeightedVector#getWeight()}.
*/
-public final class WeightedVectorComparator implements Comparator<WeightedVector> {
+public final class WeightedVectorComparator implements Comparator<WeightedVector>, Serializable {
private static final double DOUBLE_EQUALITY_ERROR = 1.0e-8;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java?rev=1463115&r1=1463114&r2=1463115&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/decomposer/hebbian/HebbianUpdater.java Mon Apr 1 09:18:12 2013
@@ -29,12 +29,8 @@ public class HebbianUpdater implements E
TrainingState currentState) {
double trainingVectorNorm = trainingVector.norm(2);
int numPreviousEigens = currentState.getNumEigensProcessed();
- if (numPreviousEigens > 0) {
- if (currentState.isFirstPass()) {
- updateTrainingProjectionsVector(currentState,
- trainingVector,
- numPreviousEigens - 1);
- }
+ if (numPreviousEigens > 0 && currentState.isFirstPass()) {
+ updateTrainingProjectionsVector(currentState, trainingVector, numPreviousEigens - 1);
}
if (currentState.getActivationDenominatorSquared() == 0 || trainingVectorNorm == 0) {
if (currentState.getActivationDenominatorSquared() == 0) {
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/random/WeightedThing.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/random/WeightedThing.java?rev=1463115&r1=1463114&r2=1463115&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/random/WeightedThing.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/random/WeightedThing.java Mon Apr 1 09:18:12 2013
@@ -17,6 +17,9 @@
package org.apache.mahout.math.random;
+import com.google.common.base.Preconditions;
+import org.apache.mahout.common.RandomUtils;
+
/**
* Handy for creating multinomial distributions of things.
*/
@@ -25,7 +28,7 @@ public final class WeightedThing<T> impl
private final T value;
public WeightedThing(T thing, double weight) {
- this.value = thing;
+ this.value = Preconditions.checkNotNull(thing);
this.weight = weight;
}
@@ -45,4 +48,18 @@ public final class WeightedThing<T> impl
public int compareTo(WeightedThing<T> other) {
return Double.compare(this.weight, other.weight);
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (o instanceof WeightedThing) {
+ WeightedThing<T> other = (WeightedThing<T>) o;
+ return weight == other.weight && value.equals(other.value);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return 31 * RandomUtils.hashDouble(weight) + value.hashCode();
+ }
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/solver/EigenDecomposition.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/solver/EigenDecomposition.java?rev=1463115&r1=1463114&r2=1463115&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/solver/EigenDecomposition.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/solver/EigenDecomposition.java Mon Apr 1 09:18:12 2013
@@ -260,10 +260,7 @@ public class EigenDecomposition {
// otherwise, iterate.
if (m > l) {
- int iter = 0;
do {
- iter++; // (Could check iteration count here.)
-
// Compute implicit shift
double g = d.getQuick(l);
Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/FileBasedSparseBinaryMatrixTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/FileBasedSparseBinaryMatrixTest.java?rev=1463115&r1=1463114&r2=1463115&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/FileBasedSparseBinaryMatrixTest.java (original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/FileBasedSparseBinaryMatrixTest.java Mon Apr 1 09:18:12 2013
@@ -81,7 +81,7 @@ public class FileBasedSparseBinaryMatrix
FileBasedSparseBinaryMatrix.writeMatrix(f, m0);
FileBasedSparseBinaryMatrix m = new FileBasedSparseBinaryMatrix(10, 21);
- m.setData(f, true);
+ m.setData(f);
for (MatrixSlice row : m) {
Vector diff = row.vector().minus(m0.viewRow(row.index()));
Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/decomposer/lanczos/TestLanczosSolver.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/decomposer/lanczos/TestLanczosSolver.java?rev=1463115&r1=1463114&r2=1463115&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/decomposer/lanczos/TestLanczosSolver.java (original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/decomposer/lanczos/TestLanczosSolver.java Mon Apr 1 09:18:12 2013
@@ -71,7 +71,8 @@ public final class TestLanczosSolver ext
initialVector.assign(1.0 / Math.sqrt(numColumns));
int rank = 50;
LanczosState state = new LanczosState(corpus, rank, initialVector);
- long time = timeLanczos(corpus, state, rank, false);
+ LanczosSolver solver = new LanczosSolver();
+ solver.solve(state, rank, false);
assertOrthonormal(state);
for (int i = 0; i < rank/2; i++) {
assertEigen(i, state.getRightSingularVector(i), corpus, ERROR_TOLERANCE, false);
@@ -87,20 +88,10 @@ public final class TestLanczosSolver ext
initialVector.assign(1.0 / Math.sqrt(numCols));
int rank = 30;
LanczosState state = new LanczosState(corpus, rank, initialVector);
- long time = timeLanczos(corpus, state, rank, true);
+ LanczosSolver solver = new LanczosSolver();
+ solver.solve(state, rank, true);
//assertOrthonormal(state);
//assertEigen(state, rank / 2, ERROR_TOLERANCE, true);
}
- public static long timeLanczos(Matrix corpus, LanczosState state, int rank, boolean symmetric) {
- long start = System.currentTimeMillis();
-
- LanczosSolver solver = new LanczosSolver();
- // initialize!
- solver.solve(state, rank, symmetric);
-
- long end = System.currentTimeMillis();
- return end - start;
- }
-
}