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

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1383760 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastien Brisard 2012-09-12 04:27:47 +00:00
parent b30425df87
commit f0921e844d
2 changed files with 68 additions and 22 deletions

View File

@ -22,6 +22,8 @@ import java.util.Arrays;
import org.apache.commons.math3.Field; import org.apache.commons.math3.Field;
import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.FieldElement;
import org.apache.commons.math3.exception.MathArithmeticException;
import org.apache.commons.math3.exception.NotPositiveException;
import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.ZeroException;
import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.OutOfRangeException;
@ -526,7 +528,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapDivide(T d) { public FieldVector<T> mapDivide(T d)
throws NullArgumentException, MathArithmeticException {
if (d == null) {
throw new NullArgumentException();
}
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++) {
out[i] = data[i].divide(d); out[i] = data[i].divide(d);
@ -535,7 +541,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapDivideToSelf(T d) { public FieldVector<T> mapDivideToSelf(T d)
throws NullArgumentException, MathArithmeticException {
if (d == null) {
throw new NullArgumentException();
}
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
data[i] = data[i].divide(d); data[i] = data[i].divide(d);
} }
@ -543,20 +553,28 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapInv() { public FieldVector<T> mapInv() throws MathArithmeticException {
T[] out = buildArray(data.length); T[] out = buildArray(data.length);
final T one = field.getOne(); final T one = field.getOne();
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
out[i] = one.divide(data[i]); try {
out[i] = one.divide(data[i]);
} catch (final MathArithmeticException e) {
throw new MathArithmeticException(LocalizedFormats.ENTRY, i);
}
} }
return new ArrayFieldVector<T>(field, out, false); return new ArrayFieldVector<T>(field, out, false);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> mapInvToSelf() { public FieldVector<T> mapInvToSelf() throws MathArithmeticException {
final T one = field.getOne(); final T one = field.getOne();
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
data[i] = one.divide(data[i]); try {
data[i] = one.divide(data[i]);
} catch (final MathArithmeticException e) {
throw new MathArithmeticException(LocalizedFormats.ENTRY, i);
}
} }
return this; return this;
} }
@ -595,14 +613,18 @@ 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 { throws DimensionMismatchException, MathArithmeticException {
try { try {
return ebeDivide((ArrayFieldVector<T>) v); return ebeDivide((ArrayFieldVector<T>) v);
} catch (ClassCastException cce) { } catch (ClassCastException cce) {
checkVectorDimensions(v); checkVectorDimensions(v);
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++) {
out[i] = data[i].divide(v.getEntry(i)); try {
out[i] = data[i].divide(v.getEntry(i));
} catch (final MathArithmeticException e) {
throw new MathArithmeticException(LocalizedFormats.ENTRY, i);
}
} }
return new ArrayFieldVector<T>(field, out, false); return new ArrayFieldVector<T>(field, out, false);
} }
@ -614,13 +636,18 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @return a vector containing {@code this[i] / v[i]} for all {@code i} * @return a vector containing {@code this[i] / v[i]} for all {@code i}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this} * {@code this}
* @throws MathArithmeticException if one entry of {@code v} is zero.
*/ */
public ArrayFieldVector<T> ebeDivide(ArrayFieldVector<T> v) public ArrayFieldVector<T> ebeDivide(ArrayFieldVector<T> v)
throws DimensionMismatchException { throws DimensionMismatchException, MathArithmeticException {
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++) {
try {
out[i] = data[i].divide(v.data[i]); out[i] = data[i].divide(v.data[i]);
} catch (final MathArithmeticException e) {
throw new MathArithmeticException(LocalizedFormats.ENTRY, i);
}
} }
return new ArrayFieldVector<T>(field, out, false); return new ArrayFieldVector<T>(field, out, false);
} }
@ -673,7 +700,7 @@ 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 { throws DimensionMismatchException, MathArithmeticException {
return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
} }
@ -682,9 +709,10 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
* @return projection of {@code this} onto {@code v} * @return projection of {@code this} onto {@code v}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this} * {@code this}
* @throws MathArithmeticException if {@code v} is the null vector.
*/ */
public ArrayFieldVector<T> projection(ArrayFieldVector<T> v) public ArrayFieldVector<T> projection(ArrayFieldVector<T> v)
throws DimensionMismatchException { throws DimensionMismatchException, MathArithmeticException {
return (ArrayFieldVector<T>) v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); return (ArrayFieldVector<T>) v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
} }
@ -759,7 +787,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public FieldVector<T> getSubVector(int index, int n) { public FieldVector<T> getSubVector(int index, int n)
throws OutOfRangeException, NotPositiveException {
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
ArrayFieldVector<T> out = new ArrayFieldVector<T>(field, n); ArrayFieldVector<T> out = new ArrayFieldVector<T>(field, n);
try { try {
System.arraycopy(data, index, out.data, 0, n); System.arraycopy(data, index, out.data, 0, n);
@ -780,7 +812,7 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public void setSubVector(int index, FieldVector<T> v) { public void setSubVector(int index, FieldVector<T> v) throws OutOfRangeException {
try { try {
try { try {
set(index, (ArrayFieldVector<T>) v); set(index, (ArrayFieldVector<T>) v);

View File

@ -19,7 +19,9 @@ package org.apache.commons.math3.linear;
import org.apache.commons.math3.Field; import org.apache.commons.math3.Field;
import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.FieldElement;
import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.MathArithmeticException;
import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotPositiveException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.OutOfRangeException;
/** /**
@ -127,29 +129,38 @@ public interface FieldVector<T extends FieldElement<T>> {
* Map a division operation to each entry. * Map a division operation to each entry.
* @param d value to divide all entries by * @param d value to divide all entries by
* @return {@code this / d} * @return {@code this / d}
* @throws NullArgumentException if {@code d} is {@code null}.
* @throws MathArithmeticException if {@code d} is zero.
*/ */
FieldVector<T> mapDivide(T d); FieldVector<T> mapDivide(T d)
throws NullArgumentException, MathArithmeticException;
/** /**
* Map a division operation to each entry. * Map a division operation to each entry.
* <p>The instance <strong>is</strong> changed by this method.</p> * <p>The instance <strong>is</strong> changed by this method.</p>
* @param d value to divide all entries by * @param d value to divide all entries by
* @return for convenience, return {@code this} * @return for convenience, return {@code this}
* @throws NullArgumentException if {@code d} is {@code null}.
* @throws MathArithmeticException if {@code d} is zero.
*/ */
FieldVector<T> mapDivideToSelf(T d); FieldVector<T> mapDivideToSelf(T d)
throws NullArgumentException, MathArithmeticException;
/** /**
* Map the 1/x function to each entry. * Map the 1/x function to each entry.
* @return a vector containing the result of applying the function to each entry * @return a vector containing the result of applying the function to each
* entry.
* @throws MathArithmeticException if one of the entries is zero.
*/ */
FieldVector<T> mapInv(); FieldVector<T> mapInv() throws MathArithmeticException;
/** /**
* Map the 1/x function to each entry. * Map the 1/x function to each entry.
* <p>The instance <strong>is</strong> changed by this method.</p> * <p>The instance <strong>is</strong> changed by this method.</p>
* @return for convenience, return {@code this} * @return for convenience, return {@code this}
* @throws MathArithmeticException if one of the entries is zero.
*/ */
FieldVector<T> mapInvToSelf(); FieldVector<T> mapInvToSelf() throws MathArithmeticException;
/** /**
* Element-by-element multiplication. * Element-by-element multiplication.
@ -159,7 +170,7 @@ public interface FieldVector<T extends FieldElement<T>> {
* {@code this} * {@code this}
*/ */
FieldVector<T> ebeMultiply(FieldVector<T> v) FieldVector<T> ebeMultiply(FieldVector<T> v)
throws DimensionMismatchException; throws DimensionMismatchException;
/** /**
* Element-by-element division. * Element-by-element division.
@ -167,9 +178,10 @@ public interface FieldVector<T extends FieldElement<T>> {
* @return a vector containing {@code this[i] / v[i]} for all {@code i} * @return a vector containing {@code this[i] / v[i]} for all {@code i}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this} * {@code this}
* @throws MathArithmeticException if one entry of {@code v} is zero.
*/ */
FieldVector<T> ebeDivide(FieldVector<T> v) FieldVector<T> ebeDivide(FieldVector<T> v)
throws DimensionMismatchException; throws DimensionMismatchException, MathArithmeticException;
/** /**
* Returns vector entries as a T array. * Returns vector entries as a T array.
@ -191,8 +203,10 @@ public interface FieldVector<T extends FieldElement<T>> {
* @return projection of {@code this} onto {@code v} * @return projection of {@code this} onto {@code v}
* @throws DimensionMismatchException if {@code v} is not the same size as * @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this} * {@code this}
* @throws MathArithmeticException if {@code v} is the null vector.
*/ */
FieldVector<T> projection(FieldVector<T> v) throws DimensionMismatchException; FieldVector<T> projection(FieldVector<T> v)
throws DimensionMismatchException, MathArithmeticException;
/** /**
* Compute the outer product. * Compute the outer product.
@ -249,7 +263,7 @@ public interface FieldVector<T extends FieldElement<T>> {
* @throws NotPositiveException if the number of elements if not positive. * @throws NotPositiveException if the number of elements if not positive.
*/ */
FieldVector<T> getSubVector(int index, int n) FieldVector<T> getSubVector(int index, int n)
throws OutOfRangeException, NotPositiveException; throws OutOfRangeException, NotPositiveException;
/** /**
* Set a set of consecutive elements. * Set a set of consecutive elements.