MATH-653
Renamed "AbstracRealVector" to "RealVector" (getting rid of the "interface"). Changed "SparseRealVector" from "interface" to "abstract" class. Modified subclasses ("extends" instead of "implements") and unit test name ("RealVectorTest" instead of "AbstractRealVectorTest") accordingly. Slightly modified "UnmodifiableRealVectorAbstractTest" to exclude methods that are now inherited from "Object". Changed unmodifiable vector view from a static inner class to an anonymous class. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1162511 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c2570afd5f
commit
760078b411
File diff suppressed because it is too large
Load Diff
|
@ -34,7 +34,7 @@ import org.apache.commons.math.util.FastMath;
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public class ArrayRealVector extends AbstractRealVector implements Serializable {
|
public class ArrayRealVector extends RealVector implements Serializable {
|
||||||
/** Serializable version identifier. */
|
/** Serializable version identifier. */
|
||||||
private static final long serialVersionUID = -1097961340710804027L;
|
private static final long serialVersionUID = -1097961340710804027L;
|
||||||
/** Default format. */
|
/** Default format. */
|
||||||
|
|
|
@ -30,8 +30,8 @@ import org.apache.commons.math.util.FastMath;
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public class OpenMapRealVector extends AbstractRealVector
|
public class OpenMapRealVector extends SparseRealVector
|
||||||
implements SparseRealVector, Serializable {
|
implements Serializable {
|
||||||
/** Default Tolerance for having a value considered zero. */
|
/** Default Tolerance for having a value considered zero. */
|
||||||
public static final double DEFAULT_ZERO_TOLERANCE = 1.0e-12;
|
public static final double DEFAULT_ZERO_TOLERANCE = 1.0e-12;
|
||||||
/** Serializable version identifier. */
|
/** Serializable version identifier. */
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -17,11 +17,9 @@
|
||||||
package org.apache.commons.math.linear;
|
package org.apache.commons.math.linear;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Marker interface for RealVectors that require sparse backing storage
|
* Marker class for RealVectors that require sparse backing storage
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface SparseRealVector extends RealVector {
|
public abstract class SparseRealVector extends RealVector {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -52,6 +52,13 @@ The <action> type attribute can be add,update,fix,remove.
|
||||||
If the output is not quite correct, check for invisible trailing spaces!
|
If the output is not quite correct, check for invisible trailing spaces!
|
||||||
-->
|
-->
|
||||||
<release version="3.0" date="TBD" description="TBD">
|
<release version="3.0" date="TBD" description="TBD">
|
||||||
|
<action dev="erans" type="fix" issue="MATH-653">
|
||||||
|
Renamed "AbstractRealVector" to "RealVector". The interface was removed
|
||||||
|
in favour of its unique (abstract) implementation.
|
||||||
|
</action>
|
||||||
|
<action dev="erans" type="add" issue="MATH-646" due-to="Sébastien Brisard">
|
||||||
|
Unmodifiable view of a "RealVector".
|
||||||
|
</action>
|
||||||
<action dev="luc" type="fix" issue="MATH-501" >
|
<action dev="luc" type="fix" issue="MATH-501" >
|
||||||
Refactored integration API for consistency with solvers API. Now the main convergence
|
Refactored integration API for consistency with solvers API. Now the main convergence
|
||||||
parameters are set in the constructor and remain fixed.
|
parameters are set in the constructor and remain fixed.
|
||||||
|
|
|
@ -78,7 +78,8 @@ public class ArrayRealVectorTest {
|
||||||
|
|
||||||
// Testclass to test the RealVector interface
|
// Testclass to test the RealVector interface
|
||||||
// only with enough content to support the test
|
// only with enough content to support the test
|
||||||
public static class RealVectorTestImpl implements RealVector, Serializable {
|
public static class RealVectorTestImpl extends RealVector
|
||||||
|
implements Serializable {
|
||||||
|
|
||||||
/** Serializable version identifier. */
|
/** Serializable version identifier. */
|
||||||
private static final long serialVersionUID = 4715341047369582908L;
|
private static final long serialVersionUID = 4715341047369582908L;
|
||||||
|
|
|
@ -26,13 +26,13 @@ import java.util.Iterator;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Tests for {@link RealVector}.
|
||||||
*/
|
*/
|
||||||
public class AbstractRealVectorTest {
|
public class RealVectorTest {
|
||||||
private double[] vec1 = { 1d, 2d, 3d, 4d, 5d };
|
private double[] vec1 = { 1d, 2d, 3d, 4d, 5d };
|
||||||
private double[] vec2 = { -3d, 0d, 0d, 2d, 1d };
|
private double[] vec2 = { -3d, 0d, 0d, 2d, 1d };
|
||||||
|
|
||||||
private static class TestVectorImpl extends AbstractRealVector {
|
private static class TestVectorImpl extends RealVector {
|
||||||
private double[] values;
|
private double[] values;
|
||||||
|
|
||||||
TestVectorImpl(double[] values) {
|
TestVectorImpl(double[] values) {
|
||||||
|
@ -43,11 +43,10 @@ public class AbstractRealVectorTest {
|
||||||
public double[] getData() { return values; }
|
public double[] getData() { return values; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AbstractRealVector copy() {
|
public RealVector copy() {
|
||||||
return new TestVectorImpl(values.clone());
|
return new TestVectorImpl(values.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
UnsupportedOperationException unsupported() {
|
UnsupportedOperationException unsupported() {
|
||||||
return new UnsupportedOperationException("Test implementation only supports methods necessary for testing");
|
return new UnsupportedOperationException("Test implementation only supports methods necessary for testing");
|
||||||
}
|
}
|
||||||
|
@ -156,6 +155,13 @@ public class AbstractRealVectorTest {
|
||||||
throw unsupported();
|
throw unsupported();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setSubVector(int index, double[] v) {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
public void setSubVector(int index, RealVector v) {
|
||||||
|
throw unsupported();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isNaN() {
|
public boolean isNaN() {
|
||||||
throw unsupported();
|
throw unsupported();
|
||||||
}
|
}
|
|
@ -76,7 +76,7 @@ public class SparseRealVectorTest {
|
||||||
|
|
||||||
// Testclass to test the RealVector interface
|
// Testclass to test the RealVector interface
|
||||||
// only with enough content to support the test
|
// only with enough content to support the test
|
||||||
public static class SparseRealVectorTestImpl extends AbstractRealVector implements Serializable {
|
public static class SparseRealVectorTestImpl extends RealVector implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -6251371752518113791L;
|
private static final long serialVersionUID = -6251371752518113791L;
|
||||||
/** Entries of the vector. */
|
/** Entries of the vector. */
|
||||||
|
@ -106,7 +106,7 @@ public class SparseRealVectorTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AbstractRealVector copy() {
|
public RealVector copy() {
|
||||||
return new SparseRealVectorTestImpl(data);
|
return new SparseRealVectorTestImpl(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,8 @@ package org.apache.commons.math.linear;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.Set;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
@ -31,10 +32,12 @@ import org.apache.commons.math.linear.RealVector.Entry;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an abstract test of the {@link AbstractRealVector.UnmodifiableVector}
|
* This is an abstract test of the {@link
|
||||||
|
* RealVector#unmodifiableRealVector(RealVector) unmodifiable vector}
|
||||||
* implementation. These unmodifiable vectors decorate a (modifiable)
|
* implementation. These unmodifiable vectors decorate a (modifiable)
|
||||||
* {@link RealVector}; therefore, a new implementation of this abstract test
|
* {@link RealVector}; therefore, a new implementation of this abstract
|
||||||
* should be considered for each implementation of {@link RealVector}.
|
* test should be considered for each implementation of
|
||||||
|
* {@link RealVector}.
|
||||||
*
|
*
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*
|
*
|
||||||
|
@ -48,18 +51,22 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
* The list of methods which are excluded from the general test
|
* The list of methods which are excluded from the general test
|
||||||
* {@link #testAllButExcluded()}.
|
* {@link #testAllButExcluded()}.
|
||||||
*/
|
*/
|
||||||
protected static final ArrayList<String> EXCLUDE;
|
protected static final Set<String> EXCLUDE = new HashSet<String>();
|
||||||
/** The random number generator (always initialized with the same seed. */
|
/** The random number generator (always initialized with the same seed. */
|
||||||
protected static final Random RANDOM;
|
protected static final Random RANDOM;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
EXCLUDE = new ArrayList<String>();
|
|
||||||
EXCLUDE.add("getEntry");
|
EXCLUDE.add("getEntry");
|
||||||
EXCLUDE.add("setEntry");
|
EXCLUDE.add("setEntry");
|
||||||
EXCLUDE.add("getSubVector");
|
EXCLUDE.add("getSubVector");
|
||||||
EXCLUDE.add("setSubVector");
|
EXCLUDE.add("setSubVector");
|
||||||
EXCLUDE.add("iterator");
|
EXCLUDE.add("iterator");
|
||||||
EXCLUDE.add("sparseIterator");
|
EXCLUDE.add("sparseIterator");
|
||||||
|
|
||||||
|
// Excluded because they are inherited from "Object".
|
||||||
|
for (Method m : Object.class.getMethods()) {
|
||||||
|
EXCLUDE.add(m.getName());
|
||||||
|
}
|
||||||
RANDOM = new Random(20110813);
|
RANDOM = new Random(20110813);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,7 +241,7 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new random vector of a specified type. This vector is then to
|
* Creates a new random vector of a specified type. This vector is then to
|
||||||
* be wrapped in a {@link AbstractRealVector.UnmodifiableVector}.
|
* be wrapped in an unmodifiable vector.
|
||||||
*
|
*
|
||||||
* @return a new random vector.
|
* @return a new random vector.
|
||||||
*/
|
*/
|
||||||
|
@ -243,11 +250,10 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
/**
|
/**
|
||||||
* Creates a new random object of the specified type.
|
* Creates a new random object of the specified type.
|
||||||
*
|
*
|
||||||
* @param c
|
* @param c Class of the object to be created.
|
||||||
* the class of the object to be created.
|
|
||||||
* @return a new random object.
|
* @return a new random object.
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException if the specified class is not
|
||||||
* if the specified class is not recognized by this method.
|
* recognized by this method.
|
||||||
*/
|
*/
|
||||||
public Object createParameter(final Class<?> c) {
|
public Object createParameter(final Class<?> c) {
|
||||||
if (c == Integer.TYPE) {
|
if (c == Integer.TYPE) {
|
||||||
|
@ -270,8 +276,9 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the general test of most methods in
|
* This is the general test of most methods in the
|
||||||
* {@link AbstractRealVector.UnmodifiableVector}. It works as follows.
|
* {@link RealVector#unmodifiableRealVector(RealVector) unmodifiable vector}.
|
||||||
|
* It works as follows.
|
||||||
* First, an unmodifiable view of a copy of the specified random vector
|
* First, an unmodifiable view of a copy of the specified random vector
|
||||||
* {@code u} is created: this defines {@code v}. Then the <em>same</em>
|
* {@code u} is created: this defines {@code v}. Then the <em>same</em>
|
||||||
* method {@code m} is invoked on {@code u} and {@code v}, with randomly
|
* method {@code m} is invoked on {@code u} and {@code v}, with randomly
|
||||||
|
@ -288,19 +295,21 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
*constructed.
|
*constructed.
|
||||||
* @param args Arguments to be passed to method {@code m}.
|
* @param args Arguments to be passed to method {@code m}.
|
||||||
*/
|
*/
|
||||||
private void callMethod(final Method m, final RealVector u,
|
private void callMethod(final Method m,
|
||||||
final Object... args) throws IllegalAccessException,
|
final RealVector u,
|
||||||
IllegalArgumentException, InvocationTargetException {
|
final Object... args)
|
||||||
|
throws IllegalAccessException,
|
||||||
|
IllegalArgumentException,
|
||||||
|
InvocationTargetException {
|
||||||
final RealVector uu = u.copy();
|
final RealVector uu = u.copy();
|
||||||
final RealVector v = AbstractRealVector
|
final RealVector v = RealVector.unmodifiableRealVector(u.copy());
|
||||||
.unmodifiableRealVector(u.copy());
|
|
||||||
Object exp = m.invoke(u, args);
|
Object exp = m.invoke(u, args);
|
||||||
if (equals(uu, u)) {
|
if (equals(uu, u)) {
|
||||||
Object act = m.invoke(v, args);
|
Object act = m.invoke(v, args);
|
||||||
Assert.assertTrue(m.toGenericString()
|
Assert.assertTrue(m.toGenericString() + ", unmodifiable vector has changed",
|
||||||
+ ", unmodifiable vector has changed", equals(uu, v));
|
equals(uu, v));
|
||||||
Assert.assertTrue(m.toGenericString() + ", wrong result",
|
Assert.assertTrue(m.toGenericString() + ", wrong result",
|
||||||
equals(exp, act));
|
equals(exp, act));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
|
@ -322,8 +331,10 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
* {@link #EXCLUDE}), they must be handled by separate tests.
|
* {@link #EXCLUDE}), they must be handled by separate tests.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testAllButExcluded() throws IllegalAccessException,
|
public void testAllButExcluded()
|
||||||
IllegalArgumentException, InvocationTargetException {
|
throws IllegalAccessException,
|
||||||
|
IllegalArgumentException,
|
||||||
|
InvocationTargetException {
|
||||||
Method[] method = RealVector.class.getMethods();
|
Method[] method = RealVector.class.getMethods();
|
||||||
for (int i = 0; i < method.length; i++) {
|
for (int i = 0; i < method.length; i++) {
|
||||||
Method m = method[i];
|
Method m = method[i];
|
||||||
|
@ -342,7 +353,7 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGetEntry() {
|
public void testGetEntry() {
|
||||||
RealVector u = createVector();
|
RealVector u = createVector();
|
||||||
RealVector v = AbstractRealVector.unmodifiableRealVector(u);
|
RealVector v = RealVector.unmodifiableRealVector(u);
|
||||||
for (int i = 0; i < DIM; i++) {
|
for (int i = 0; i < DIM; i++) {
|
||||||
Assert.assertTrue(equals(u.getEntry(i), v.getEntry(i)));
|
Assert.assertTrue(equals(u.getEntry(i), v.getEntry(i)));
|
||||||
}
|
}
|
||||||
|
@ -351,7 +362,7 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
@Test(expected = MathUnsupportedOperationException.class)
|
@Test(expected = MathUnsupportedOperationException.class)
|
||||||
public void testSetEntry() {
|
public void testSetEntry() {
|
||||||
RealVector u = createVector();
|
RealVector u = createVector();
|
||||||
RealVector v = AbstractRealVector.unmodifiableRealVector(u);
|
RealVector v = RealVector.unmodifiableRealVector(u);
|
||||||
for (int i = 0; i < DIM; i++) {
|
for (int i = 0; i < DIM; i++) {
|
||||||
v.setEntry(i, 0d);
|
v.setEntry(i, 0d);
|
||||||
}
|
}
|
||||||
|
@ -360,7 +371,7 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGetSubVector() {
|
public void testGetSubVector() {
|
||||||
RealVector u = createVector();
|
RealVector u = createVector();
|
||||||
RealVector v = AbstractRealVector.unmodifiableRealVector(u);
|
RealVector v = RealVector.unmodifiableRealVector(u);
|
||||||
for (int i = 0; i < DIM; i++) {
|
for (int i = 0; i < DIM; i++) {
|
||||||
for (int n = 1; n < DIM - i; n++) {
|
for (int n = 1; n < DIM - i; n++) {
|
||||||
RealVector exp = u.getSubVector(i, n);
|
RealVector exp = u.getSubVector(i, n);
|
||||||
|
@ -373,7 +384,7 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
@Test(expected = MathUnsupportedOperationException.class)
|
@Test(expected = MathUnsupportedOperationException.class)
|
||||||
public void testSetSubVector() {
|
public void testSetSubVector() {
|
||||||
RealVector u = createVector();
|
RealVector u = createVector();
|
||||||
RealVector v = AbstractRealVector.unmodifiableRealVector(u);
|
RealVector v = RealVector.unmodifiableRealVector(u);
|
||||||
v.setSubVector(0, new ArrayRealVector());
|
v.setSubVector(0, new ArrayRealVector());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -381,7 +392,7 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
public void testIterator() {
|
public void testIterator() {
|
||||||
RealVector u = createVector();
|
RealVector u = createVector();
|
||||||
Iterator<Entry> i = u.iterator();
|
Iterator<Entry> i = u.iterator();
|
||||||
RealVector v = AbstractRealVector.unmodifiableRealVector(u.copy());
|
RealVector v = RealVector.unmodifiableRealVector(u.copy());
|
||||||
Iterator<Entry> j = v.iterator();
|
Iterator<Entry> j = v.iterator();
|
||||||
boolean flag;
|
boolean flag;
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
|
@ -407,7 +418,7 @@ public abstract class UnmodifiableRealVectorAbstractTest {
|
||||||
public void testSparseIterator() {
|
public void testSparseIterator() {
|
||||||
RealVector u = createVector();
|
RealVector u = createVector();
|
||||||
Iterator<Entry> i = u.sparseIterator();
|
Iterator<Entry> i = u.sparseIterator();
|
||||||
RealVector v = AbstractRealVector.unmodifiableRealVector(u.copy());
|
RealVector v = RealVector.unmodifiableRealVector(u.copy());
|
||||||
Iterator<Entry> j = v.sparseIterator();
|
Iterator<Entry> j = v.sparseIterator();
|
||||||
boolean flag;
|
boolean flag;
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
|
|
Loading…
Reference in New Issue