MATH-854: fill the "throws" clause of ArrayFieldVector.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1383052 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastien Brisard 2012-09-10 19:05:54 +00:00
parent a70cb1668e
commit 5998035a9f

View File

@ -47,7 +47,7 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
/** /**
* Build a 0-length vector. * Build a 0-length vector.
* Zero-length vectors may be used to initialized construction of vectors * Zero-length vectors may be used to initialize construction of vectors
* by data gathering. We start with zero-length and use either the {@link * by data gathering. We start with zero-length and use either the {@link
* #ArrayFieldVector(ArrayFieldVector, ArrayFieldVector)} constructor * #ArrayFieldVector(ArrayFieldVector, ArrayFieldVector)} constructor
* or one of the {@code append} methods ({@link #add(FieldVector)} or * or one of the {@code append} methods ({@link #add(FieldVector)} or
@ -94,7 +94,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws ZeroException if {@code d} is empty. * @throws ZeroException if {@code d} is empty.
* @see #ArrayFieldVector(Field, FieldElement[]) * @see #ArrayFieldVector(Field, FieldElement[])
*/ */
public ArrayFieldVector(T[] d) { public ArrayFieldVector(T[] d)
throws NullArgumentException, ZeroException {
if (d == null) { if (d == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -114,7 +115,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws NullArgumentException if {@code d} is {@code null}. * @throws NullArgumentException if {@code d} is {@code null}.
* @see #ArrayFieldVector(FieldElement[]) * @see #ArrayFieldVector(FieldElement[])
*/ */
public ArrayFieldVector(Field<T> field, T[] d) { public ArrayFieldVector(Field<T> field, T[] d)
throws NullArgumentException {
if (d == null) { if (d == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -143,7 +145,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @see #ArrayFieldVector(FieldElement[]) * @see #ArrayFieldVector(FieldElement[])
* @see #ArrayFieldVector(Field, FieldElement[], boolean) * @see #ArrayFieldVector(Field, FieldElement[], boolean)
*/ */
public ArrayFieldVector(T[] d, boolean copyArray) { public ArrayFieldVector(T[] d, boolean copyArray)
throws NullArgumentException, ZeroException {
if (d == null) { if (d == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -169,7 +172,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws NullArgumentException if {@code d} is {@code null}. * @throws NullArgumentException if {@code d} is {@code null}.
* @see #ArrayFieldVector(FieldElement[], boolean) * @see #ArrayFieldVector(FieldElement[], boolean)
*/ */
public ArrayFieldVector(Field<T> field, T[] d, boolean copyArray) { public ArrayFieldVector(Field<T> field, T[] d, boolean copyArray)
throws NullArgumentException {
if (d == null) { if (d == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -187,7 +191,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws NumberIsTooLargeException if the size of {@code d} is less * @throws NumberIsTooLargeException if the size of {@code d} is less
* than {@code pos + size}. * than {@code pos + size}.
*/ */
public ArrayFieldVector(T[] d, int pos, int size) { public ArrayFieldVector(T[] d, int pos, int size)
throws NullArgumentException, NumberIsTooLargeException {
if (d == null) { if (d == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -210,7 +215,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws NumberIsTooLargeException if the size of {@code d} is less * @throws NumberIsTooLargeException if the size of {@code d} is less
* than {@code pos + size}. * than {@code pos + size}.
*/ */
public ArrayFieldVector(Field<T> field, T[] d, int pos, int size) { public ArrayFieldVector(Field<T> field, T[] d, int pos, int size)
throws NullArgumentException, NumberIsTooLargeException {
if (d == null) { if (d == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -228,7 +234,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @param v Vector to copy. * @param v Vector to copy.
* @throws NullArgumentException if {@code v} is {@code null}. * @throws NullArgumentException if {@code v} is {@code null}.
*/ */
public ArrayFieldVector(FieldVector<T> v) { public ArrayFieldVector(FieldVector<T> v)
throws NullArgumentException {
if (v == null) { if (v == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -245,7 +252,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @param v Vector to copy. * @param v Vector to copy.
* @throws NullArgumentException if {@code v} is {@code null}. * @throws NullArgumentException if {@code v} is {@code null}.
*/ */
public ArrayFieldVector(ArrayFieldVector<T> v) { public ArrayFieldVector(ArrayFieldVector<T> v)
throws NullArgumentException {
if (v == null) { if (v == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -261,7 +269,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* a shallow copy * a shallow copy
* @throws NullArgumentException if {@code v} is {@code null}. * @throws NullArgumentException if {@code v} is {@code null}.
*/ */
public ArrayFieldVector(ArrayFieldVector<T> v, boolean deep) { public ArrayFieldVector(ArrayFieldVector<T> v, boolean deep)
throws NullArgumentException {
if (v == null) { if (v == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -277,9 +286,9 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws NullArgumentException if {@code v1} or {@code v2} is * @throws NullArgumentException if {@code v1} or {@code v2} is
* {@code null}. * {@code null}.
*/ */
public ArrayFieldVector(ArrayFieldVector<T> v1, ArrayFieldVector<T> v2) { public ArrayFieldVector(ArrayFieldVector<T> v1, ArrayFieldVector<T> v2)
if (v1 == null || throws NullArgumentException {
v2 == null) { if (v1 == null || v2 == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
field = v1.getField(); field = v1.getField();
@ -296,9 +305,9 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws NullArgumentException if {@code v1} or {@code v2} is * @throws NullArgumentException if {@code v1} or {@code v2} is
* {@code null}. * {@code null}.
*/ */
public ArrayFieldVector(ArrayFieldVector<T> v1, T[] v2) { public ArrayFieldVector(ArrayFieldVector<T> v1, T[] v2)
if (v1 == null || throws NullArgumentException {
v2 == null) { if (v1 == null || v2 == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
field = v1.getField(); field = v1.getField();
@ -315,9 +324,9 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws NullArgumentException if {@code v1} or {@code v2} is * @throws NullArgumentException if {@code v1} or {@code v2} is
* {@code null}. * {@code null}.
*/ */
public ArrayFieldVector(T[] v1, ArrayFieldVector<T> v2) { public ArrayFieldVector(T[] v1, ArrayFieldVector<T> v2)
if (v1 == null || throws NullArgumentException {
v2 == null) { if (v1 == null || v2 == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
field = v2.getField(); field = v2.getField();
@ -341,9 +350,9 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws ZeroException if both arrays are empty. * @throws ZeroException if both arrays are empty.
* @see #ArrayFieldVector(Field, FieldElement[], FieldElement[]) * @see #ArrayFieldVector(Field, FieldElement[], FieldElement[])
*/ */
public ArrayFieldVector(T[] v1, T[] v2) { public ArrayFieldVector(T[] v1, T[] v2)
if (v1 == null || throws NullArgumentException, ZeroException {
v2 == null) { if (v1 == null || v2 == null) {
throw new NullArgumentException(); throw new NullArgumentException();
} }
if (v1.length + v2.length == 0) { if (v1.length + v2.length == 0) {
@ -366,7 +375,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @throws ZeroException if both arrays are empty. * @throws ZeroException if both arrays are empty.
* @see #ArrayFieldVector(FieldElement[], FieldElement[]) * @see #ArrayFieldVector(FieldElement[], FieldElement[])
*/ */
public ArrayFieldVector(Field<T> field, T[] v1, T[] v2) { public ArrayFieldVector(Field<T> field, T[] v1, T[] v2)
throws NullArgumentException, ZeroException {
if (v1 == null || v2 == null) {
throw new NullArgumentException();
}
if (v1.length + v2.length == 0) { if (v1.length + v2.length == 0) {
throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT); throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
} }
@ -398,7 +411,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> add(FieldVector<T> v) { public FieldVector<T> add(FieldVector<T> v)
throws DimensionMismatchException {
try { try {
return add((ArrayFieldVector<T>) v); return add((ArrayFieldVector<T>) v);
} catch (ClassCastException cce) { } catch (ClassCastException cce) {
@ -412,12 +426,14 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** /**
* Compute the sum of this and v. * Compute the sum of {@code this} and {@code v}.
* @param v vector to be added * @param v vector to be added
* @return this + v * @return {@code this + v}
* @throws IllegalArgumentException if v is not the same size as this * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
*/ */
public ArrayFieldVector<T> add(ArrayFieldVector<T> v) { public ArrayFieldVector<T> add(ArrayFieldVector<T> v)
throws DimensionMismatchException {
checkVectorDimensions(v.data.length); checkVectorDimensions(v.data.length);
T[] out = buildArray(data.length); T[] out = buildArray(data.length);
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
@ -427,7 +443,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> subtract(FieldVector<T> v) { public FieldVector<T> subtract(FieldVector<T> v)
throws DimensionMismatchException {
try { try {
return subtract((ArrayFieldVector<T>) v); return subtract((ArrayFieldVector<T>) v);
} catch (ClassCastException cce) { } catch (ClassCastException cce) {
@ -441,12 +458,14 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** /**
* Compute this minus v. * Compute {@code this} minus {@code v}.
* @param v vector to be subtracted * @param v vector to be subtracted
* @return this + v * @return {@code this - v}
* @throws IllegalArgumentException if v is not the same size as this * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
*/ */
public ArrayFieldVector<T> subtract(ArrayFieldVector<T> v) { public ArrayFieldVector<T> subtract(ArrayFieldVector<T> v)
throws DimensionMismatchException {
checkVectorDimensions(v.data.length); checkVectorDimensions(v.data.length);
T[] out = buildArray(data.length); T[] out = buildArray(data.length);
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
@ -543,7 +562,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> ebeMultiply(FieldVector<T> v) { public FieldVector<T> ebeMultiply(FieldVector<T> v)
throws DimensionMismatchException {
try { try {
return ebeMultiply((ArrayFieldVector<T>) v); return ebeMultiply((ArrayFieldVector<T>) v);
} catch (ClassCastException cce) { } catch (ClassCastException cce) {
@ -559,10 +579,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
/** /**
* Element-by-element multiplication. * Element-by-element multiplication.
* @param v vector by which instance elements must be multiplied * @param v vector by which instance elements must be multiplied
* @return a vector containing this[i] * v[i] for all i * @return a vector containing {@code this[i] * v[i]} for all {@code i}
* @exception IllegalArgumentException if v is not the same size as this * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
*/ */
public ArrayFieldVector<T> ebeMultiply(ArrayFieldVector<T> v) { public ArrayFieldVector<T> ebeMultiply(ArrayFieldVector<T> v)
throws DimensionMismatchException {
checkVectorDimensions(v.data.length); checkVectorDimensions(v.data.length);
T[] out = buildArray(data.length); T[] out = buildArray(data.length);
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
@ -572,7 +594,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> ebeDivide(FieldVector<T> v) { public FieldVector<T> ebeDivide(FieldVector<T> v)
throws DimensionMismatchException {
try { try {
return ebeDivide((ArrayFieldVector<T>) v); return ebeDivide((ArrayFieldVector<T>) v);
} catch (ClassCastException cce) { } catch (ClassCastException cce) {
@ -588,10 +611,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
/** /**
* Element-by-element division. * Element-by-element division.
* @param v vector by which instance elements must be divided * @param v vector by which instance elements must be divided
* @return a vector containing this[i] / v[i] for all i * @return a vector containing {@code this[i] / v[i]} for all {@code i}
* @throws IllegalArgumentException if v is not the same size as this * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
*/ */
public ArrayFieldVector<T> ebeDivide(ArrayFieldVector<T> v) { public ArrayFieldVector<T> ebeDivide(ArrayFieldVector<T> v)
throws DimensionMismatchException {
checkVectorDimensions(v.data.length); checkVectorDimensions(v.data.length);
T[] out = buildArray(data.length); T[] out = buildArray(data.length);
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
@ -615,7 +640,8 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public T dotProduct(FieldVector<T> v) { public T dotProduct(FieldVector<T> v)
throws DimensionMismatchException {
try { try {
return dotProduct((ArrayFieldVector<T>) v); return dotProduct((ArrayFieldVector<T>) v);
} catch (ClassCastException cce) { } catch (ClassCastException cce) {
@ -631,10 +657,12 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
/** /**
* Compute the dot product. * Compute the dot product.
* @param v vector with which dot product should be computed * @param v vector with which dot product should be computed
* @return the scalar dot product between instance and v * @return the scalar dot product of {@code this} and {@code v}
* @exception IllegalArgumentException if v is not the same size as this * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
*/ */
public T dotProduct(ArrayFieldVector<T> v) { public T dotProduct(ArrayFieldVector<T> v)
throws DimensionMismatchException {
checkVectorDimensions(v.data.length); checkVectorDimensions(v.data.length);
T dot = field.getZero(); T dot = field.getZero();
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
@ -644,16 +672,19 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> projection(FieldVector<T> v) { public FieldVector<T> projection(FieldVector<T> v)
throws DimensionMismatchException {
return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
} }
/** Find the orthogonal projection of this vector onto another vector. /** Find the orthogonal projection of this vector onto another vector.
* @param v vector onto which instance must be projected * @param v vector onto which {@code this} must be projected
* @return projection of the instance onto v * @return projection of {@code this} onto {@code v}
* @throws IllegalArgumentException if v is not the same size as this * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
*/ */
public ArrayFieldVector<T> projection(ArrayFieldVector<T> v) { public ArrayFieldVector<T> projection(ArrayFieldVector<T> v)
throws DimensionMismatchException {
return (ArrayFieldVector<T>) v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); return (ArrayFieldVector<T>) v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
} }
@ -677,8 +708,7 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
/** /**
* Compute the outer product. * Compute the outer product.
* @param v vector with which outer product should be computed * @param v vector with which outer product should be computed
* @return the square matrix outer product between instance and v * @return the matrix outer product between instance and v
* @exception IllegalArgumentException if v is not the same size as this
*/ */
public FieldMatrix<T> outerProduct(ArrayFieldVector<T> v) { public FieldMatrix<T> outerProduct(ArrayFieldVector<T> v) {
final int m = data.length; final int m = data.length;
@ -770,10 +800,9 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* *
* @param index index of first element to be set. * @param index index of first element to be set.
* @param v vector containing the values to set. * @param v vector containing the values to set.
* @throws OutOfRangeException if the index is * @throws OutOfRangeException if the index is invalid.
* inconsistent with vector size
*/ */
public void set(int index, ArrayFieldVector<T> v) { public void set(int index, ArrayFieldVector<T> v) throws OutOfRangeException {
try { try {
System.arraycopy(v.data, 0, data, index, v.data.length); System.arraycopy(v.data, 0, data, index, v.data.length);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -795,10 +824,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
/** /**
* Check if instance and specified vectors have the same dimension. * Check if instance and specified vectors have the same dimension.
* @param v vector to compare instance with * @param v vector to compare instance with
* @exception IllegalArgumentException if the vectors do not * @exception DimensionMismatchException if the vectors do not
* have the same dimension * have the same dimensions
*/ */
protected void checkVectorDimensions(FieldVector<T> v) { protected void checkVectorDimensions(FieldVector<T> v)
throws DimensionMismatchException {
checkVectorDimensions(v.getDimension()); checkVectorDimensions(v.getDimension());
} }
@ -806,10 +836,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* Check if instance dimension is equal to some expected value. * Check if instance dimension is equal to some expected value.
* *
* @param n Expected dimension. * @param n Expected dimension.
* @throws OutOfRangeException if the dimension is * @throws DimensionMismatchException if the dimension is not equal to the
* inconsistent with this vector size. * size of {@code this} vector.
*/ */
protected void checkVectorDimensions(int n) { protected void checkVectorDimensions(int n)
throws DimensionMismatchException {
if (data.length != n) { if (data.length != n) {
throw new DimensionMismatchException(data.length, n); throw new DimensionMismatchException(data.length, n);
} }
@ -870,7 +901,7 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @param index Index to check. * @param index Index to check.
* @exception OutOfRangeException if the index is not valid. * @exception OutOfRangeException if the index is not valid.
*/ */
private void checkIndex(final int index) { private void checkIndex(final int index) throws OutOfRangeException {
if (index < 0 || index >= getDimension()) { if (index < 0 || index >= getDimension()) {
throw new OutOfRangeException(LocalizedFormats.INDEX, throw new OutOfRangeException(LocalizedFormats.INDEX,
index, 0, getDimension() - 1); index, 0, getDimension() - 1);