MATH-795: factored out
- testCombinePreconditionSameType() - testCombineSameType() - testCombinePreconditionMixedType() - testCombineMixedTypes() git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1346523 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
118e94b5e9
commit
5a4f0a8171
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue