field, T[] d) {
+ this.field = field;
+ data = d.clone();
+ }
+
+ /**
* Create a new ArrayFieldVector using the input array as the underlying
* data array.
* If an array is built specially in order to be embedded in a
* ArrayFieldVector and not used directly, the copyArray
may be
* set to false
+ *
+ * This constructor need a non-empty {@code d} array to retrieve
+ * the field from its first element. This implies it cannot build
+ * 0 length vectors. To build vectors from any size, one should
+ * use the {@link #ArrayFieldVector(Field, FieldElement[], boolean)} constructor.
+ *
* @param d data for new vector
* @param copyArray if true, the input array will be copied, otherwise
* it will be referenced
* @throws IllegalArgumentException if d
is empty
* @throws NullPointerException if d
is null
* @see #ArrayFieldVector(FieldElement[])
+ * @see #ArrayFieldVector(Field, FieldElement[], boolean)
*/
public ArrayFieldVector(T[] d, boolean copyArray)
throws NullPointerException, IllegalArgumentException {
- try {
- field = d[0].getField();
- data = copyArray ? d.clone() : d;
- } catch (ArrayIndexOutOfBoundsException e) {
+ if (d.length == 0) {
throw MathRuntimeException.createIllegalArgumentException(
- LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
+ LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
}
+ field = d[0].getField();
+ data = copyArray ? d.clone() : d;
+ }
+
+ /**
+ * Create a new ArrayFieldVector using the input array as the underlying
+ * data array.
+ * If an array is built specially in order to be embedded in a
+ * ArrayFieldVector and not used directly, the copyArray
may be
+ * set to false
+ * @param field field to which the elements belong
+ * @param d data for new vector
+ * @param copyArray if true, the input array will be copied, otherwise
+ * it will be referenced
+ * @see #ArrayFieldVector(FieldElement[], boolean)
+ */
+ public ArrayFieldVector(Field field, T[] d, boolean copyArray) {
+ this.field = field;
+ data = copyArray ? d.clone() : d;
}
/**
@@ -204,9 +246,16 @@ public class ArrayFieldVector
+ * This constructor need at least one non-empty array to retrieve
+ * the field from its first element. This implies it cannot build
+ * 0 length vectors. To build vectors from any size, one should
+ * use the {@link #ArrayFieldVector(Field, FieldElement[], FieldElement[])} constructor.
+ *
* @param v1 first vector (will be put in front of the new vector)
* @param v2 second vector (will be put at back of the new vector)
* @exception IllegalArgumentException if both vectors are empty
+ * @see #ArrayFieldVector(Field, FieldElement[], FieldElement[])
*/
public ArrayFieldVector(T[] v1, T[] v2) {
try {
@@ -220,6 +269,24 @@ public class ArrayFieldVector field, T[] v1, T[] v2) {
+ if (v1.length + v2.length == 0) {
+ throw MathRuntimeException.createIllegalArgumentException(
+ LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
+ }
+ data = buildArray(v1.length + v2.length);
+ System.arraycopy(v1, 0, data, 0, v1.length);
+ System.arraycopy(v2, 0, data, v1.length, v2.length);
+ this.field = data[0].getField();
+ }
+
/** Build an array of elements.
* @param length size of the array to build
* @return a new array
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java?rev=1003994&r1=1003993&r2=1003994&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java Sun Oct 3 16:39:43 2010
@@ -91,18 +91,9 @@ public class ArrayRealVector extends Abs
* @param d data for new vector
* @param copyArray if true, the input array will be copied, otherwise
* it will be referenced
- * @throws IllegalArgumentException if d
is empty
- * @throws NullPointerException if d
is null
* @see #ArrayRealVector(double[])
*/
- public ArrayRealVector(double[] d, boolean copyArray)
- throws NullPointerException, IllegalArgumentException {
- if (d == null) {
- throw new NullPointerException();
- }
- if (d.length == 0) {
- throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
- }
+ public ArrayRealVector(double[] d, boolean copyArray) {
data = copyArray ? d.clone() : d;
}
Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1003994&r1=1003993&r2=1003994&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Oct 3 16:39:43 2010
@@ -82,6 +82,9 @@ The type attribute can be add,u
+
+ Fixed an error preventing zero length vectors to be built by some constructors
+
Fixed an error preventing ODE solvers to be restarted after they have been stopped by a discrete event
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java?rev=1003994&r1=1003993&r2=1003994&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java Sun Oct 3 16:39:43 2010
@@ -590,6 +590,35 @@ public class ArrayFieldVectorTest extend
assertEquals(v,TestUtils.serializeAndRecover(v));
}
+ public void testZeroVectors() {
+
+ // when the field is not specified, array cannot be empty
+ try {
+ new ArrayFieldVector(new Fraction[0]);
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException ex) {
+ // expected behavior
+ }
+ try {
+ new ArrayFieldVector(new Fraction[0], true);
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException ex) {
+ // expected behavior
+ }
+ try {
+ new ArrayFieldVector(new Fraction[0], false);
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException ex) {
+ // expected behavior
+ }
+
+ // when the field is specified, array can be empty
+ assertEquals(0, new ArrayFieldVector(FractionField.getInstance(), new Fraction[0]).getDimension());
+ assertEquals(0, new ArrayFieldVector(FractionField.getInstance(), new Fraction[0], true).getDimension());
+ assertEquals(0, new ArrayFieldVector(FractionField.getInstance(), new Fraction[0], false).getDimension());
+
+ }
+
/** verifies that two vectors are equals */
protected void checkArray(String msg, Fraction[] m, Fraction[] n) {
if (m.length != n.length) {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java?rev=1003994&r1=1003993&r2=1003994&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java Sun Oct 3 16:39:43 2010
@@ -24,6 +24,8 @@ import junit.framework.TestCase;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.fraction.Fraction;
+import org.apache.commons.math.fraction.FractionField;
import org.apache.commons.math.util.FastMath;
/**
@@ -607,20 +609,6 @@ public class ArrayRealVectorTest extends
assertEquals("testData is 9.0 ", 9.0, v14.getEntry(2));
assertEquals("testData is 1.0 ", 1.0, v14.getEntry(3));
- try {
- new ArrayRealVector((double[]) null, false);
- fail("expected exception");
- } catch (NullPointerException npe) {
- // expected
- }
-
- try {
- new ArrayRealVector(new double[0], false);
- fail("expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
-
}
public void testDataInOut() {
@@ -1280,6 +1268,11 @@ public class ArrayRealVectorTest extends
assertEquals(v,TestUtils.serializeAndRecover(v));
}
+ public void testZeroVectors() {
+ assertEquals(0, new ArrayRealVector(new double[0]).getDimension());
+ assertEquals(0, new ArrayRealVector(new double[0], true).getDimension());
+ assertEquals(0, new ArrayRealVector(new double[0], false).getDimension());
+ }
public void testMinMax() {
ArrayRealVector v1 = new ArrayRealVector(new double[] { 0, -6, 4, 12, 7 });