diff --git a/src/changes/changes.xml b/src/changes/changes.xml index a0e254985..9a75f2ee1 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -55,6 +55,9 @@ This is a minor release: It combines bug fixes and new features. Changes to existing features were made in a backwards-compatible way such as to allow drop-in replacement of the v3.1[.1] JAR file. "> + + ArrayFieldVector can now be constructed from any FieldVector. + Improved checking of null vector elements. diff --git a/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java b/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java index 5592ba6fa..4c31bf35f 100644 --- a/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java +++ b/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java @@ -269,15 +269,12 @@ public class ArrayFieldVector> implements FieldVector< * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. + * @deprecated as of 3.2, replaced by {@link #ArrayFieldVector(FieldVector, FieldVector)} */ + @Deprecated public ArrayFieldVector(ArrayFieldVector v1, ArrayFieldVector v2) throws NullArgumentException { - MathUtils.checkNotNull(v1); - MathUtils.checkNotNull(v2); - field = v1.getField(); - data = MathArrays.buildArray(field, v1.data.length + v2.data.length); - System.arraycopy(v1.data, 0, data, 0, v1.data.length); - System.arraycopy(v2.data, 0, data, v1.data.length, v2.data.length); + this((FieldVector) v1, (FieldVector) v2); } /** @@ -287,15 +284,35 @@ public class ArrayFieldVector> implements FieldVector< * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. + * @since 3.2 */ - public ArrayFieldVector(ArrayFieldVector v1, T[] v2) + public ArrayFieldVector(FieldVector v1, FieldVector v2) throws NullArgumentException { MathUtils.checkNotNull(v1); MathUtils.checkNotNull(v2); field = v1.getField(); - data = MathArrays.buildArray(field, v1.data.length + v2.length); - System.arraycopy(v1.data, 0, data, 0, v1.data.length); - System.arraycopy(v2, 0, data, v1.data.length, v2.length); + final T[] v1Data = + (v1 instanceof ArrayFieldVector) ? ((ArrayFieldVector) v1).data : v1.toArray(); + final T[] v2Data = + (v2 instanceof ArrayFieldVector) ? ((ArrayFieldVector) v2).data : v2.toArray(); + data = MathArrays.buildArray(field, v1Data.length + v2Data.length); + System.arraycopy(v1Data, 0, data, 0, v1Data.length); + System.arraycopy(v2Data, 0, data, v1Data.length, v2Data.length); + } + + /** + * Construct a vector by appending one vector to another vector. + * + * @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). + * @throws NullArgumentException if {@code v1} or {@code v2} is + * {@code null}. + * @deprecated as of 3.2, replaced by {@link #ArrayFieldVector(FieldVector, FieldElement[]) + */ + @Deprecated + public ArrayFieldVector(ArrayFieldVector v1, T[] v2) + throws NullArgumentException { + this((FieldVector) v1, v2); } /** @@ -306,14 +323,51 @@ public class ArrayFieldVector> implements FieldVector< * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. */ + public ArrayFieldVector(FieldVector v1, T[] v2) + throws NullArgumentException { + MathUtils.checkNotNull(v1); + MathUtils.checkNotNull(v2); + field = v1.getField(); + final T[] v1Data = + (v1 instanceof ArrayFieldVector) ? ((ArrayFieldVector) v1).data : v1.toArray(); + data = MathArrays.buildArray(field, v1Data.length + v2.length); + System.arraycopy(v1Data, 0, data, 0, v1Data.length); + System.arraycopy(v2, 0, data, v1Data.length, v2.length); + } + + /** + * Construct a vector by appending one vector to another vector. + * + * @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). + * @throws NullArgumentException if {@code v1} or {@code v2} is + * {@code null}. + * @deprecated as of 3.2, replaced by {@link #ArrayFieldVector(FieldElement[], FieldVector) + */ + @Deprecated public ArrayFieldVector(T[] v1, ArrayFieldVector v2) throws NullArgumentException { + this(v1, (FieldVector) v2); + } + + /** + * Construct a vector by appending one vector to another vector. + * + * @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). + * @throws NullArgumentException if {@code v1} or {@code v2} is + * {@code null}. + */ + public ArrayFieldVector(T[] v1, FieldVector v2) + throws NullArgumentException { MathUtils.checkNotNull(v1); MathUtils.checkNotNull(v2); field = v2.getField(); - data = MathArrays.buildArray(field, v1.length + v2.data.length); + final T[] v2Data = + (v2 instanceof ArrayFieldVector) ? ((ArrayFieldVector) v2).data : v2.toArray(); + data = MathArrays.buildArray(field, v1.length + v2Data.length); System.arraycopy(v1, 0, data, 0, v1.length); - System.arraycopy(v2.data, 0, data, v1.length, v2.data.length); + System.arraycopy(v2Data, 0, data, v1.length, v2Data.length); } /**