MATH-795: in RealVectorAbstractTest, factored out unit tests for
double dotProduct(RealVector) As first reported by Bill Barker, these tests fail with the current default implementation (in RealVector) as well as the sparse implementation (in OpenMapRealVector). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1356576 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8071ded100
commit
075a88a074
|
@ -473,10 +473,8 @@ public class ArrayRealVectorTest extends RealVectorAbstractTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@Test
|
@Test
|
||||||
public void testDataInOut() {
|
public void testDataInOut() {
|
||||||
super.testDataInOut();
|
|
||||||
|
|
||||||
ArrayRealVector v1 = new ArrayRealVector(vec1);
|
ArrayRealVector v1 = new ArrayRealVector(vec1);
|
||||||
|
|
||||||
|
|
|
@ -1158,49 +1158,93 @@ public abstract class RealVectorAbstractTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDataInOut() {
|
public void testCopy() {
|
||||||
final RealVector v1 = create(vec1);
|
final RealVector v = create(values);
|
||||||
final RealVector v2 = create(vec2);
|
final RealVector w = v.copy();
|
||||||
final RealVector v4 = create(vec4);
|
Assert.assertNotSame(v, w);
|
||||||
final RealVector v2_t = createAlien(vec2);
|
TestUtils.assertEquals("", values, w, 0d);
|
||||||
|
}
|
||||||
|
|
||||||
final RealVector v_set1 = v1.copy();
|
private void doTestDotProductRegularValues(final boolean mixed) {
|
||||||
v_set1.setEntry(1, 11.0);
|
final double x = getPreferredEntryValue();
|
||||||
Assert.assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1), 0);
|
final double[] data1 = {
|
||||||
try {
|
x, 1d, x, x, 2d, x, x, x, 3d, x, x, x, x
|
||||||
v_set1.setEntry(3, 11.0);
|
};
|
||||||
Assert.fail("OutOfRangeException expected");
|
final double[] data2 = {
|
||||||
} catch (OutOfRangeException ex) {
|
5d, -6d, 7d, x, x, -8d, -9d, 10d, 11d, x, 12d, 13d, -15d
|
||||||
// expected behavior
|
};
|
||||||
|
double expected = 0d;
|
||||||
|
for (int i = 0; i < data1.length; i++){
|
||||||
|
expected += data1[i] * data2[i];
|
||||||
}
|
}
|
||||||
|
final RealVector v1 = create(data1);
|
||||||
final RealVector v_set2 = v4.copy();
|
final RealVector v2;
|
||||||
v_set2.setSubVector(3, v1);
|
if (mixed) {
|
||||||
Assert.assertEquals("testData is 1.0 ", 1.0, v_set2.getEntry(3), 0);
|
v2 = createAlien(data2);
|
||||||
Assert.assertEquals("testData is 7.0 ", 7.0, v_set2.getEntry(6), 0);
|
} else {
|
||||||
try {
|
v2 = create(data2);
|
||||||
v_set2.setSubVector(7, v1);
|
|
||||||
Assert.fail("OutOfRangeException expected");
|
|
||||||
} catch (OutOfRangeException ex) {
|
|
||||||
// expected behavior
|
|
||||||
}
|
}
|
||||||
|
final double actual = v1.dotProduct(v2);
|
||||||
|
Assert.assertEquals("", expected, actual, 0d);
|
||||||
|
}
|
||||||
|
|
||||||
final RealVector v_set4 = v4.copy();
|
private void doTestDotProductSpecialValues(final boolean mixed) {
|
||||||
v_set4.setSubVector(3, v2_t);
|
for (int i = 0; i < values.length; i++) {
|
||||||
Assert.assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3), 0);
|
final double[] data1 = {
|
||||||
Assert.assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6), 0);
|
values[i]
|
||||||
try {
|
};
|
||||||
v_set4.setSubVector(7, v2_t);
|
final RealVector v1 = create(data1);
|
||||||
Assert.fail("OutOfRangeException expected");
|
for (int j = 0; j < values.length; j++) {
|
||||||
} catch (OutOfRangeException ex) {
|
final double[] data2 = {
|
||||||
// expected behavior
|
values[j]
|
||||||
|
};
|
||||||
|
final RealVector v2;
|
||||||
|
if (mixed) {
|
||||||
|
v2 = createAlien(data2);
|
||||||
|
} else {
|
||||||
|
v2 = create(data2);
|
||||||
|
}
|
||||||
|
final double expected = data1[0] * data2[0];
|
||||||
|
final double actual = v1.dotProduct(v2);
|
||||||
|
Assert.assertEquals(data1[0] + " * " + data2[0], expected,
|
||||||
|
actual, 0d);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final RealVector vout10 = v1.copy();
|
private void doTestDotProductDimensionMismatch(final boolean mixed) {
|
||||||
final RealVector vout10_2 = v1.copy();
|
final double[] data1 = new double[10];
|
||||||
Assert.assertEquals(vout10, vout10_2);
|
final double[] data2 = new double[data1.length + 1];
|
||||||
vout10_2.setEntry(0, 1.1);
|
final RealVector v1 = create(data1);
|
||||||
Assert.assertNotSame(vout10, vout10_2);
|
final RealVector v2;
|
||||||
|
if (mixed) {
|
||||||
|
v2 = createAlien(data2);
|
||||||
|
} else {
|
||||||
|
v2 = create(data2);
|
||||||
|
}
|
||||||
|
v1.dotProduct(v2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDotProductSameType() {
|
||||||
|
doTestDotProductRegularValues(false);
|
||||||
|
doTestDotProductSpecialValues(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=DimensionMismatchException.class)
|
||||||
|
public void testDotProductDimensionMismatchSameType() {
|
||||||
|
doTestDotProductDimensionMismatch(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDotProductMixedTypes() {
|
||||||
|
doTestDotProductRegularValues(true);
|
||||||
|
doTestDotProductSpecialValues(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=DimensionMismatchException.class)
|
||||||
|
public void testDotProductDimensionMismatchMixedTypes() {
|
||||||
|
doTestDotProductDimensionMismatch(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1212,14 +1256,6 @@ public abstract class RealVectorAbstractTest {
|
||||||
|
|
||||||
final RealVector v2_t = createAlien(vec2);
|
final RealVector v2_t = createAlien(vec2);
|
||||||
|
|
||||||
// octave dot(v1,v2)
|
|
||||||
double dot = v1.dotProduct(v2);
|
|
||||||
Assert.assertEquals("compare val ", 32d, dot, normTolerance);
|
|
||||||
|
|
||||||
// octave dot(v1,v2_t)
|
|
||||||
double dot_2 = v1.dotProduct(v2_t);
|
|
||||||
Assert.assertEquals("compare val ", 32d, dot_2, normTolerance);
|
|
||||||
|
|
||||||
RealVector v_projection = v1.projection(v2);
|
RealVector v_projection = v1.projection(v2);
|
||||||
double[] result_projection = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
|
double[] result_projection = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
|
||||||
assertClose("compare vect", v_projection.toArray(), result_projection, normTolerance);
|
assertClose("compare vect", v_projection.toArray(), result_projection, normTolerance);
|
||||||
|
|
|
@ -440,21 +440,6 @@ public class RealVectorTest extends RealVectorAbstractTest{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
@Ignore
|
|
||||||
@Override
|
|
||||||
public void testDataInOut() {
|
|
||||||
/*
|
|
||||||
* TODO Some of the tests carried out in testDataInOut() do not pass,
|
|
||||||
* as the methods to be tested are not implemented in TestVectorImpl.
|
|
||||||
* For the time being, testDataInOut() is overriden, while ommitting
|
|
||||||
* the @Test annotation, which effectively skips the test.
|
|
||||||
*
|
|
||||||
* In the future, testDataInOut() should be split in smaller units, and
|
|
||||||
* only those units which do not make sense should be skipped.
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue