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 98aca4013..fb213d489 100644 --- a/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java +++ b/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java @@ -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> implements FieldVector< } /** {@inheritDoc} */ - public FieldVector mapDivide(T d) { + public FieldVector 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> implements FieldVector< } /** {@inheritDoc} */ - public FieldVector mapDivideToSelf(T d) { + public FieldVector 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> implements FieldVector< } /** {@inheritDoc} */ - public FieldVector mapInv() { + public FieldVector 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(field, out, false); } /** {@inheritDoc} */ - public FieldVector mapInvToSelf() { + public FieldVector 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> implements FieldVector< /** {@inheritDoc} */ public FieldVector ebeDivide(FieldVector v) - throws DimensionMismatchException { + throws DimensionMismatchException, MathArithmeticException { try { return ebeDivide((ArrayFieldVector) 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(field, out, false); } @@ -614,13 +636,18 @@ public class ArrayFieldVector> 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 ebeDivide(ArrayFieldVector 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(field, out, false); } @@ -673,7 +700,7 @@ public class ArrayFieldVector> implements FieldVector< /** {@inheritDoc} */ public FieldVector projection(FieldVector v) - throws DimensionMismatchException { + throws DimensionMismatchException, MathArithmeticException { return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); } @@ -682,9 +709,10 @@ public class ArrayFieldVector> 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 projection(ArrayFieldVector v) - throws DimensionMismatchException { + throws DimensionMismatchException, MathArithmeticException { return (ArrayFieldVector) v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); } @@ -759,7 +787,11 @@ public class ArrayFieldVector> implements FieldVector< } /** {@inheritDoc} */ - public FieldVector getSubVector(int index, int n) { + public FieldVector getSubVector(int index, int n) + throws OutOfRangeException, NotPositiveException { + if (n < 0) { + throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); + } ArrayFieldVector out = new ArrayFieldVector(field, n); try { System.arraycopy(data, index, out.data, 0, n); @@ -780,7 +812,7 @@ public class ArrayFieldVector> implements FieldVector< } /** {@inheritDoc} */ - public void setSubVector(int index, FieldVector v) { + public void setSubVector(int index, FieldVector v) throws OutOfRangeException { try { try { set(index, (ArrayFieldVector) v); diff --git a/src/main/java/org/apache/commons/math3/linear/FieldVector.java b/src/main/java/org/apache/commons/math3/linear/FieldVector.java index 5d10f197c..befabcbef 100644 --- a/src/main/java/org/apache/commons/math3/linear/FieldVector.java +++ b/src/main/java/org/apache/commons/math3/linear/FieldVector.java @@ -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> { * 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 mapDivide(T d); + FieldVector mapDivide(T d) + throws NullArgumentException, MathArithmeticException; /** * Map a division operation to each entry. *

The instance is changed by this method.

* @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 mapDivideToSelf(T d); + FieldVector 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 mapInv(); + FieldVector mapInv() throws MathArithmeticException; /** * Map the 1/x function to each entry. *

The instance is changed by this method.

* @return for convenience, return {@code this} + * @throws MathArithmeticException if one of the entries is zero. */ - FieldVector mapInvToSelf(); + FieldVector mapInvToSelf() throws MathArithmeticException; /** * Element-by-element multiplication. @@ -159,7 +170,7 @@ public interface FieldVector> { * {@code this} */ FieldVector ebeMultiply(FieldVector v) - throws DimensionMismatchException; + throws DimensionMismatchException; /** * Element-by-element division. @@ -167,9 +178,10 @@ public interface 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. */ FieldVector ebeDivide(FieldVector v) - throws DimensionMismatchException; + throws DimensionMismatchException, MathArithmeticException; /** * Returns vector entries as a T array. @@ -191,8 +203,10 @@ public interface 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. */ - FieldVector projection(FieldVector v) throws DimensionMismatchException; + FieldVector projection(FieldVector v) + throws DimensionMismatchException, MathArithmeticException; /** * Compute the outer product. @@ -249,7 +263,7 @@ public interface FieldVector> { * @throws NotPositiveException if the number of elements if not positive. */ FieldVector getSubVector(int index, int n) - throws OutOfRangeException, NotPositiveException; + throws OutOfRangeException, NotPositiveException; /** * Set a set of consecutive elements.