Continue un-deprecation of sparse methods.

JIRA: MATH-870
JIRA: MATH-803

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1570246 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2014-02-20 15:56:37 +00:00
parent 4332119866
commit 1cd5a3229c
4 changed files with 35 additions and 60 deletions

View File

@ -366,25 +366,6 @@ public class OpenMapRealVector extends SparseRealVector
iter.advance(); iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key())); res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
} }
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
return res; return res;
} }

View File

@ -343,17 +343,7 @@ public abstract class RealVector {
* @return a vector containing this[i] / v[i] for all i. * @return a vector containing this[i] / v[i] for all i.
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this} vector. * {@code this} vector.
* @deprecated As of version 3.1, this method is deprecated, and will be
* removed in version 4.0. This decision follows the discussion reported in
* <a href="https://issues.apache.org/jira/browse/MATH-803?focusedCommentId=13399150#comment-13399150">MATH-803</a>.
* Uses of this method involving sparse implementations of
* {@link RealVector} might lead to wrong results. Since there is no
* satisfactory correction to this bug, this method is deprecated. Users who
* want to preserve this feature are advised to implement
* {@link RealVectorPreservingVisitor} (possibly ignoring corner cases for
* the sake of efficiency).
*/ */
@Deprecated
public abstract RealVector ebeDivide(RealVector v) public abstract RealVector ebeDivide(RealVector v)
throws DimensionMismatchException; throws DimensionMismatchException;
@ -364,17 +354,7 @@ public abstract class RealVector {
* @return a vector containing this[i] * v[i] for all i. * @return a vector containing this[i] * v[i] for all i.
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this} vector. * {@code this} vector.
* @deprecated As of version 3.1, this method is deprecated, and will be
* removed in version 4.0. This decision follows the discussion reported in
* <a href="https://issues.apache.org/jira/browse/MATH-803?focusedCommentId=13399150#comment-13399150">MATH-803</a>.
* Uses of this method involving sparse implementations of
* {@link RealVector} might lead to wrong results. Since there is no
* satisfactory correction to this bug, this method is deprecated. Users who
* want to preserve this feature are advised to implement
* {@link RealVectorPreservingVisitor} (possibly ignoring corner cases for
* the sake of efficiency).
*/ */
@Deprecated
public abstract RealVector ebeMultiply(RealVector v) public abstract RealVector ebeMultiply(RealVector v)
throws DimensionMismatchException; throws DimensionMismatchException;

View File

@ -55,13 +55,12 @@ import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathArrays;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public abstract class RealVectorAbstractTest { public abstract class RealVectorAbstractTest {
private enum BinaryOperation { protected enum BinaryOperation {
ADD, SUB, MUL, DIV ADD, SUB, MUL, DIV
} }
@ -264,7 +263,6 @@ public abstract class RealVectorAbstractTest {
public void testAddToEntry() { public void testAddToEntry() {
final double x = getPreferredEntryValue(); final double x = getPreferredEntryValue();
final double[] data1 = {x, 1d, 2d, x, x}; final double[] data1 = {x, 1d, 2d, x, x};
final double[] data2 = {x, x, 3d, x, 4d, x};
final double[] expected = MathArrays.copyOf(data1); final double[] expected = MathArrays.copyOf(data1);
final RealVector actual = create(data1); final RealVector actual = create(data1);
@ -459,7 +457,7 @@ public abstract class RealVectorAbstractTest {
Assert.assertFalse(v.isInfinite()); Assert.assertFalse(v.isInfinite());
} }
private void doTestEbeBinaryOperation(final BinaryOperation op, final boolean mixed) { protected void doTestEbeBinaryOperation(final BinaryOperation op, final boolean mixed, boolean ignoreSpecial) {
final double[] data1 = new double[values.length * values.length]; final double[] data1 = new double[values.length * values.length];
final double[] data2 = new double[values.length * values.length]; final double[] data2 = new double[values.length * values.length];
int k = 0; int k = 0;
@ -509,8 +507,11 @@ public abstract class RealVectorAbstractTest {
} }
} }
for (int i = 0; i < expected.length; i++) { for (int i = 0; i < expected.length; i++) {
final String msg = "entry #"+i+", left = "+data1[i]+", right = " + data2[i]; boolean isSpecial = Double.isNaN(expected[i]) || Double.isInfinite(expected[i]);
Assert.assertEquals(msg, expected[i], actual.getEntry(i), 0.0); if (!(isSpecial && ignoreSpecial)) {
final String msg = "entry #"+i+", left = "+data1[i]+", right = " + data2[i];
Assert.assertEquals(msg, expected[i], actual.getEntry(i), 0.0);
}
} }
} }
@ -536,12 +537,12 @@ public abstract class RealVectorAbstractTest {
@Test @Test
public void testAddSameType() { public void testAddSameType() {
doTestEbeBinaryOperation(BinaryOperation.ADD, false); doTestEbeBinaryOperation(BinaryOperation.ADD, false, false);
} }
@Test @Test
public void testAddMixedTypes() { public void testAddMixedTypes() {
doTestEbeBinaryOperation(BinaryOperation.ADD, true); doTestEbeBinaryOperation(BinaryOperation.ADD, true, false);
} }
@Test(expected = DimensionMismatchException.class) @Test(expected = DimensionMismatchException.class)
@ -551,12 +552,12 @@ public abstract class RealVectorAbstractTest {
@Test @Test
public void testSubtractSameType() { public void testSubtractSameType() {
doTestEbeBinaryOperation(BinaryOperation.SUB, false); doTestEbeBinaryOperation(BinaryOperation.SUB, false, false);
} }
@Test @Test
public void testSubtractMixedTypes() { public void testSubtractMixedTypes() {
doTestEbeBinaryOperation(BinaryOperation.SUB, true); doTestEbeBinaryOperation(BinaryOperation.SUB, true, false);
} }
@Test(expected = DimensionMismatchException.class) @Test(expected = DimensionMismatchException.class)
@ -564,37 +565,31 @@ public abstract class RealVectorAbstractTest {
doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.SUB); doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.SUB);
} }
@Ignore("ebeMultiply(RealVector) is known to be faulty (MATH-803) and is deprecated.")
@Test @Test
public void testEbeMultiplySameType() { public void testEbeMultiplySameType() {
doTestEbeBinaryOperation(BinaryOperation.MUL, false); doTestEbeBinaryOperation(BinaryOperation.MUL, false, false);
} }
@Ignore("ebeMultiply(RealVector) is known to be faulty (MATH-803) and is deprecated.")
@Test @Test
public void testEbeMultiplyMixedTypes() { public void testEbeMultiplyMixedTypes() {
doTestEbeBinaryOperation(BinaryOperation.MUL, true); doTestEbeBinaryOperation(BinaryOperation.MUL, true, false);
} }
@Ignore("ebeMultiply(RealVector) is known to be faulty (MATH-803) and is deprecated.")
@Test(expected = DimensionMismatchException.class) @Test(expected = DimensionMismatchException.class)
public void testEbeMultiplyDimensionMismatch() { public void testEbeMultiplyDimensionMismatch() {
doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.MUL); doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.MUL);
} }
@Ignore("ebeDivide(RealVector) is known to be faulty (MATH-803) and is deprecated.")
@Test @Test
public void testEbeDivideSameType() { public void testEbeDivideSameType() {
doTestEbeBinaryOperation(BinaryOperation.DIV, false); doTestEbeBinaryOperation(BinaryOperation.DIV, false, false);
} }
@Ignore("ebeDivide(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test
@Test
public void testEbeDivideMixedTypes() { public void testEbeDivideMixedTypes() {
doTestEbeBinaryOperation(BinaryOperation.DIV, true); doTestEbeBinaryOperation(BinaryOperation.DIV, true, false);
} }
@Ignore("ebeDivide(RealVector) is known to be faulty (MATH-803) and is deprecated.")
@Test(expected = DimensionMismatchException.class) @Test(expected = DimensionMismatchException.class)
public void testEbeDivideDimensionMismatch() { public void testEbeDivideDimensionMismatch() {
doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.DIV); doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.DIV);

View File

@ -16,6 +16,7 @@
*/ */
package org.apache.commons.math3.linear; package org.apache.commons.math3.linear;
import org.apache.commons.math3.linear.RealVectorAbstractTest.BinaryOperation;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -99,6 +100,24 @@ public class SparseRealVectorTest extends RealVectorAbstractTest {
u.ebeDivide(v1); u.ebeDivide(v1);
} }
@Test
@Override
public void testEbeMultiplyMixedTypes() {
doTestEbeBinaryOperation(BinaryOperation.MUL, true, true);
}
@Test
@Override
public void testEbeMultiplySameType() {
doTestEbeBinaryOperation(BinaryOperation.MUL, false, true);
}
@Test
@Override
public void testEbeDivideSameType() {
doTestEbeBinaryOperation(BinaryOperation.DIV, false, true);
}
/** /**
* XXX This test is disabled because it currently fails. * XXX This test is disabled because it currently fails.
* The bug must still be fixed in the sparse vector implementation. * The bug must still be fixed in the sparse vector implementation.