diff --git a/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java b/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java index 6d9a310f0..5c4218988 100644 --- a/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java +++ b/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java @@ -506,90 +506,6 @@ public class ArrayRealVectorTest extends RealVectorAbstractTest { Assert.assertEquals(0, new ArrayRealVector(new double[0], false).getDimension()); } - @Test(expected=DimensionMismatchException.class) - public void testCombinePreconditionSameType() { - final double a = 1d; - final double b = 2d; - double[] aux = new double[] { 3d, 4d, 5d }; - final RealVector x = new ArrayRealVector(aux, false); - aux = new double[] { 6d, 7d }; - final RealVector y = new ArrayRealVector(aux, false); - x.combine(a, b, y); - } - - @Test - public void testCombineSameType() { - final Random random = new Random(20110726); - final int dim = 10; - final double a = (2 * random.nextDouble() - 1); - final double b = (2 * random.nextDouble() - 1); - final RealVector x = new ArrayRealVector(dim); - final RealVector y = new ArrayRealVector(dim); - final double[] expected = new double[dim]; - for (int i = 0; i < dim; i++) { - final double xi = 2 * random.nextDouble() - 1; - final double yi = 2 * random.nextDouble() - 1; - x.setEntry(i, xi); - y.setEntry(i, yi); - expected[i] = a * xi + b * yi; - } - final double[] actual = x.combine(a, b, y).toArray(); - for (int i = 0; i < dim; i++) { - final double delta; - if (expected[i] == 0d) { - delta = Math.ulp(1d); - } else { - delta = Math.ulp(expected[i]); - } - Assert.assertEquals("elements [" + i + "] differ", - expected[i], - actual[i], - delta); - } - } - - @Test(expected=DimensionMismatchException.class) - public void testCombinePreconditionMixedType() { - final double a = 1d; - final double b = 2d; - double[] aux = new double[] { 3d, 4d, 5d }; - final RealVector x = new ArrayRealVector(aux, false); - aux = new double[] { 6d, 7d }; - final RealVector y = new OpenMapRealVector(aux); - x.combine(a, b, y); - } - - @Test - public void testCombineMixedTypes() { - final Random random = new Random(20110726); - final int dim = 10; - final double a = (2 * random.nextDouble() - 1); - final double b = (2 * random.nextDouble() - 1); - final RealVector x = new ArrayRealVector(dim); - final RealVector y = new OpenMapRealVector(dim, 0d); - final double[] expected = new double[dim]; - for (int i = 0; i < dim; i++) { - final double xi = 2 * random.nextDouble() - 1; - final double yi = 2 * random.nextDouble() - 1; - x.setEntry(i, xi); - y.setEntry(i, yi); - expected[i] = a * xi + b * yi; - } - final double[] actual = x.combine(a, b, y).toArray(); - for (int i = 0; i < dim; i++) { - final double delta; - if (expected[i] == 0d) { - delta = Math.ulp(1d); - } else { - delta = Math.ulp(expected[i]); - } - Assert.assertEquals("elements [" + i + "] differ", - expected[i], - actual[i], - delta); - } - } - @Test(expected=DimensionMismatchException.class) public void testCombineToSelfPreconditionSameType() { final double a = 1d; 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 b4d3de709..7a7d7629c 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java @@ -17,6 +17,7 @@ package org.apache.commons.math3.linear; import java.util.Arrays; +import java.util.Random; import junit.framework.Assert; @@ -832,6 +833,91 @@ public abstract class RealVectorAbstractTest { v.cosine(w); } + @Test(expected=DimensionMismatchException.class) + public void testCombinePreconditionSameType() { + final double a = 1d; + final double b = 2d; + double[] aux = new double[] { 3d, 4d, 5d }; + final RealVector x = create(aux); + aux = new double[] { 6d, 7d }; + final RealVector y = create(aux); + x.combine(a, b, y); + } + + @Test + public void testCombineSameType() { + final Random random = new Random(20110726); + final int dim = 10; + final double a = (2 * random.nextDouble() - 1); + final double b = (2 * random.nextDouble() - 1); + final double[] dataX = new double[dim]; + final double[] dataY = new double[dim]; + final double[] expected = new double[dim]; + for (int i = 0; i < dim; i++) { + dataX[i] = 2 * random.nextDouble() - 1; + dataY[i] = 2 * random.nextDouble() - 1; + expected[i] = a * dataX[i] + b * dataY[i]; + } + final RealVector x = create(dataX); + final RealVector y = create(dataY); + final double[] actual = x.combine(a, b, y).toArray(); + for (int i = 0; i < dim; i++) { + final double delta; + if (expected[i] == 0d) { + delta = Math.ulp(1d); + } else { + delta = Math.ulp(expected[i]); + } + Assert.assertEquals("elements [" + i + "] differ", + expected[i], + actual[i], + delta); + } + } + + @Test(expected=DimensionMismatchException.class) + public void testCombinePreconditionMixedType() { + final double a = 1d; + final double b = 2d; + double[] aux = new double[] { 3d, 4d, 5d }; + final RealVector x = create(aux); + aux = new double[] { 6d, 7d }; + final RealVector y = create(aux); + x.combine(a, b, y); + } + + @Test + public void testCombineMixedTypes() { + final Random random = new Random(20110726); + final int dim = 10; + final double a = (2 * random.nextDouble() - 1); + final double b = (2 * random.nextDouble() - 1); + final double[] dataX = new double[dim]; + final double[] dataY = new double[dim]; + final double[] expected = new double[dim]; + for (int i = 0; i < dim; i++) { + dataX[i] = 2 * random.nextDouble() - 1; + dataY[i] = 2 * random.nextDouble() - 1; + expected[i] = a * dataX[i] + b * dataY[i]; + } + final RealVector x = create(dataX); + final RealVector y = createAlien(dataY); + + final double[] actual = x.combine(a, b, y).toArray(); + for (int i = 0; i < dim; i++) { + final double delta; + if (expected[i] == 0d) { + delta = Math.ulp(1d); + } else { + delta = Math.ulp(expected[i]); + } + Assert.assertEquals("elements [" + i + "] differ", + expected[i], + actual[i], + delta); + } + } + /* * TESTS OF THE VISITOR PATTERN */ diff --git a/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java b/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java index dd7bba645..35d19a73a 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java @@ -240,48 +240,6 @@ public class RealVectorTest extends RealVectorAbstractTest{ Assert.assertEquals(new ArrayRealVector(d).getNorm(), new ArrayRealVector(c).getNorm(), 0); } - @Test(expected=DimensionMismatchException.class) - public void testCombinePrecondition() { - final double a = 1d; - final double b = 2d; - double[] aux = new double[] { 3d, 4d, 5d }; - final TestVectorImpl x = new TestVectorImpl(aux); - aux = new double[] { 6d, 7d }; - final TestVectorImpl y = new TestVectorImpl(aux); - x.combine(a, b, y); - } - - @Test - public void testCombine() { - final Random random = new Random(20110726); - final int dim = 10; - final double a = (2 * random.nextDouble() - 1); - final double b = (2 * random.nextDouble() - 1); - final RealVector x = new TestVectorImpl(new double[dim]); - final RealVector y = new TestVectorImpl(new double[dim]); - final double[] expected = new double[dim]; - for (int i = 0; i < dim; i++) { - final double xi = 2 * random.nextDouble() - 1; - final double yi = 2 * random.nextDouble() - 1; - x.setEntry(i, xi); - y.setEntry(i, yi); - expected[i] = a * xi + b * yi; - } - final RealVector z = x.combine(a, b, y); - Assert.assertTrue(z != x); - final double[] actual = z.toArray(); - for (int i = 0; i < dim; i++) { - final double delta; - if (expected[i] == 0d) { - delta = Math.ulp(1d); - } else { - delta = Math.ulp(expected[i]); - } - Assert.assertEquals("elements [" + i + "] differ", expected[i], - actual[i], delta); - } - } - @Test(expected=DimensionMismatchException.class) public void testCombineToSelfPrecondition() { final double a = 1d;