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.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.NullArgumentException;
import org.apache.commons.math3.exception.OutOfRangeException;
@ -526,7 +528,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
}
/** {@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);
for (int i = 0; i < data.length; i++) {
out[i] = data[i].divide(d);
@ -535,7 +541,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
}
/** {@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++) {
data[i] = data[i].divide(d);
}
@ -543,20 +553,28 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
}
/** {@inheritDoc} */
public FieldVector<T> mapInv() {
public FieldVector<T> mapInv() throws MathArithmeticException {
T[] out = buildArray(data.length);
final T one = field.getOne();
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);
}
/** {@inheritDoc} */
public FieldVector<T> mapInvToSelf() {
public FieldVector<T> mapInvToSelf() throws MathArithmeticException {
final T one = field.getOne();
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;
}
@ -595,14 +613,18 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
/** {@inheritDoc} */
public FieldVector<T> ebeDivide(FieldVector<T> v)
throws DimensionMismatchException {
throws DimensionMismatchException, MathArithmeticException {
try {
return ebeDivide((ArrayFieldVector<T>) v);
} catch (ClassCastException cce) {
checkVectorDimensions(v);
T[] out = buildArray(data.length);
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);
}
@ -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}
* @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
* @throws MathArithmeticException if one entry of {@code v} is zero.
*/
public ArrayFieldVector<T> ebeDivide(ArrayFieldVector<T> v)
throws DimensionMismatchException {
throws DimensionMismatchException, MathArithmeticException {
checkVectorDimensions(v.data.length);
T[] out = buildArray(data.length);
for (int i = 0; i < data.length; i++) {
try {
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);
}
@ -673,7 +700,7 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
/** {@inheritDoc} */
public FieldVector<T> projection(FieldVector<T> v)
throws DimensionMismatchException {
throws DimensionMismatchException, MathArithmeticException {
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}
* @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
* @throws MathArithmeticException if {@code v} is the null vector.
*/
public ArrayFieldVector<T> projection(ArrayFieldVector<T> v)
throws DimensionMismatchException {
throws DimensionMismatchException, MathArithmeticException {
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} */
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);
try {
System.arraycopy(data, index, out.data, 0, n);
@ -780,7 +812,7 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
}
/** {@inheritDoc} */
public void setSubVector(int index, FieldVector<T> v) {
public void setSubVector(int index, FieldVector<T> v) throws OutOfRangeException {
try {
try {
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.FieldElement;
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.NullArgumentException;
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.
* @param d value to divide all entries by
* @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.
* <p>The instance <strong>is</strong> changed by this method.</p>
* @param d value to divide all entries by
* @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.
* @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.
* <p>The instance <strong>is</strong> changed by this method.</p>
* @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.
@ -159,7 +170,7 @@ public interface FieldVector<T extends FieldElement<T>> {
* {@code this}
*/
FieldVector<T> ebeMultiply(FieldVector<T> v)
throws DimensionMismatchException;
throws DimensionMismatchException;
/**
* 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}
* @throws DimensionMismatchException if {@code v} is not the same size as
* {@code this}
* @throws MathArithmeticException if one entry of {@code v} is zero.
*/
FieldVector<T> ebeDivide(FieldVector<T> v)
throws DimensionMismatchException;
throws DimensionMismatchException, MathArithmeticException;
/**
* 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}
* @throws DimensionMismatchException if {@code v} is not the same size as
* {@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.
@ -249,7 +263,7 @@ public interface FieldVector<T extends FieldElement<T>> {
* @throws NotPositiveException if the number of elements if not positive.
*/
FieldVector<T> getSubVector(int index, int n)
throws OutOfRangeException, NotPositiveException;
throws OutOfRangeException, NotPositiveException;
/**
* Set a set of consecutive elements.