From 1cd5a3229c2fb9b35fa0ced635765bf59497d074 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Thu, 20 Feb 2014 15:56:37 +0000 Subject: [PATCH] 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 --- .../math3/linear/OpenMapRealVector.java | 19 ---------- .../commons/math3/linear/RealVector.java | 20 ---------- .../math3/linear/RealVectorAbstractTest.java | 37 ++++++++----------- .../math3/linear/SparseRealVectorTest.java | 19 ++++++++++ 4 files changed, 35 insertions(+), 60 deletions(-) diff --git a/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java b/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java index e347ba83a..9430ab34b 100644 --- a/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java +++ b/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java @@ -366,25 +366,6 @@ public class OpenMapRealVector extends SparseRealVector iter.advance(); 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; } diff --git a/src/main/java/org/apache/commons/math3/linear/RealVector.java b/src/main/java/org/apache/commons/math3/linear/RealVector.java index d8e5017c8..58e2a5f1d 100644 --- a/src/main/java/org/apache/commons/math3/linear/RealVector.java +++ b/src/main/java/org/apache/commons/math3/linear/RealVector.java @@ -343,17 +343,7 @@ public abstract class RealVector { * @return a vector containing this[i] / v[i] for all i. * @throws DimensionMismatchException if {@code v} is not the same size as * {@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 - * MATH-803. - * 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) throws DimensionMismatchException; @@ -364,17 +354,7 @@ public abstract class RealVector { * @return a vector containing this[i] * v[i] for all i. * @throws DimensionMismatchException if {@code v} is not the same size as * {@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 - * MATH-803. - * 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) throws DimensionMismatchException; diff --git a/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java b/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java index 54a158773..3e7ded00a 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java @@ -55,13 +55,12 @@ import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; public abstract class RealVectorAbstractTest { - private enum BinaryOperation { + protected enum BinaryOperation { ADD, SUB, MUL, DIV } @@ -264,7 +263,6 @@ public abstract class RealVectorAbstractTest { public void testAddToEntry() { final double x = getPreferredEntryValue(); final double[] data1 = {x, 1d, 2d, x, x}; - final double[] data2 = {x, x, 3d, x, 4d, x}; final double[] expected = MathArrays.copyOf(data1); final RealVector actual = create(data1); @@ -459,7 +457,7 @@ public abstract class RealVectorAbstractTest { 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[] data2 = new double[values.length * values.length]; int k = 0; @@ -509,8 +507,11 @@ public abstract class RealVectorAbstractTest { } } for (int i = 0; i < expected.length; i++) { - final String msg = "entry #"+i+", left = "+data1[i]+", right = " + data2[i]; - Assert.assertEquals(msg, expected[i], actual.getEntry(i), 0.0); + boolean isSpecial = Double.isNaN(expected[i]) || Double.isInfinite(expected[i]); + 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 public void testAddSameType() { - doTestEbeBinaryOperation(BinaryOperation.ADD, false); + doTestEbeBinaryOperation(BinaryOperation.ADD, false, false); } @Test public void testAddMixedTypes() { - doTestEbeBinaryOperation(BinaryOperation.ADD, true); + doTestEbeBinaryOperation(BinaryOperation.ADD, true, false); } @Test(expected = DimensionMismatchException.class) @@ -551,12 +552,12 @@ public abstract class RealVectorAbstractTest { @Test public void testSubtractSameType() { - doTestEbeBinaryOperation(BinaryOperation.SUB, false); + doTestEbeBinaryOperation(BinaryOperation.SUB, false, false); } @Test public void testSubtractMixedTypes() { - doTestEbeBinaryOperation(BinaryOperation.SUB, true); + doTestEbeBinaryOperation(BinaryOperation.SUB, true, false); } @Test(expected = DimensionMismatchException.class) @@ -564,37 +565,31 @@ public abstract class RealVectorAbstractTest { doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.SUB); } - @Ignore("ebeMultiply(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test 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 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) public void testEbeMultiplyDimensionMismatch() { doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.MUL); } - @Ignore("ebeDivide(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test 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() { - 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) public void testEbeDivideDimensionMismatch() { doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.DIV); diff --git a/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java b/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java index bd727e369..61d4f3b45 100644 --- a/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java +++ b/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math3.linear; +import org.apache.commons.math3.linear.RealVectorAbstractTest.BinaryOperation; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -99,6 +100,24 @@ public class SparseRealVectorTest extends RealVectorAbstractTest { 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. * The bug must still be fixed in the sparse vector implementation.