Index: ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java =================================================================== --- ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java (revision 1504451) +++ ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java (working copy) @@ -362,14 +362,24 @@ * @see de.jungblut.math.DoubleVector#slice(int, int) */ @Override - public DoubleVector slice(int offset, int length) { - DoubleVector nv = new DenseDoubleVector(length - offset); - int index = 0; - for (int i = offset; i < length; i++) { - nv.set(index++, vector[i]); + public DoubleVector slice(int start, int end) { + Preconditions.checkArgument(start >= 0 && start <= end + && end < vector.length, "The given from and to is invalid"); + + return sliceUnsafe(start, end); + } + + /** + * {@inheritDoc} + */ + @Override + public DoubleVector sliceUnsafe(int start, int end) { + DoubleVector newVec = new DenseDoubleVector(end - start + 1); + for (int i = start, j = 0; i <= end; ++i, ++j) { + newVec.set(j, vector[i]); } - return nv; + return newVec; } /* Index: ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java =================================================================== --- ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java (revision 1504451) +++ ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java (working copy) @@ -251,17 +251,29 @@ public DoubleVector slice(int length); /** - * Slices this vector from index offset with the given length. So you end at - * the upper bound of (offset+length). + * Validates the input and then slices this vector from start to end, both are + * INCLUSIVE. For example vec = [0, 1, 2, 3, 4, 5], vec.slice(2, 5) = [2, 3, + * 4, 5]. * * @param offset must be > 0 and smaller than the dimension of the vector * @param length must be > 0 and smaller than the dimension of the vector. * This must be greater than the offset. * @return a new vector that is only (length) long. */ - public DoubleVector slice(int offset, int length); + public DoubleVector slice(int start, int end); /** + * Slices this vector from start to end, both are INCLUSIVE. For example vec = + * [0, 1, 2, 3, 4, 5], vec.slice(2, 5) = [2, 3, 4, 5]. + * + * @param offset must be > 0 and smaller than the dimension of the vector + * @param length must be > 0 and smaller than the dimension of the vector. + * This must be greater than the offset. + * @return a new vector that is only (length) long. + */ + public DoubleVector sliceUnsafe(int start, int end); + + /** * @return the maximum element value in this vector. */ public double max(); Index: ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java =================================================================== --- ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java (revision 1504451) +++ ml/src/test/java/org/apache/hama/ml/math/TestDenseDoubleVector.java (working copy) @@ -20,9 +20,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; /** * Testcase for {@link DenseDoubleVector} @@ -155,4 +153,33 @@ DoubleVector vec2 = new DenseDoubleVector(arr2); vec1.add(vec2); } + + @Test + public void testSliceNormal() { + double[] arr1 = new double[] {2, 3, 4, 5, 6}; + double[] arr2 = new double[] {4, 5, 6}; + DoubleVector vec = new DenseDoubleVector(arr1); + assertArrayEquals(arr2, vec.slice(2, 4).toArray(), 0.000001); + } + + @Test(expected = IllegalArgumentException.class) + public void testSliceAbnormal() { + double[] arr1 = new double[] {2, 3, 4, 5, 6}; + DoubleVector vec = new DenseDoubleVector(arr1); + vec.slice(2, 5); + } + + @Test(expected = IllegalArgumentException.class) + public void testSliceAbnormalEndTooLarge() { + double[] arr1 = new double[] {2, 3, 4, 5, 6}; + DoubleVector vec = new DenseDoubleVector(arr1); + vec.slice(2, 5); + } + + @Test(expected = IllegalArgumentException.class) + public void testSliceAbnormalStartLargerThanEnd() { + double[] arr1 = new double[] {2, 3, 4, 5, 6}; + DoubleVector vec = new DenseDoubleVector(arr1); + vec.slice(4, 3); + } }