You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ce...@apache.org on 2012/09/12 06:27:48 UTC

svn commit: r1383760 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear: ArrayFieldVector.java FieldVector.java

Author: celestin
Date: Wed Sep 12 04:27:47 2012
New Revision: 1383760

URL: http://svn.apache.org/viewvc?rev=1383760&view=rev
Log:
MATH-854: fill the "throws" clause of FieldVector, ArrayFieldVector.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldVector.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java?rev=1383760&r1=1383759&r2=1383760&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java Wed Sep 12 04:27:47 2012
@@ -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 
     }
 
     /** {@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 
     }
 
     /** {@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 
     }
 
     /** {@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 
 
     /** {@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 
      * @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 
 
     /** {@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 
      * @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 
     }
 
     /** {@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 
     }
 
     /** {@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);

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldVector.java?rev=1383760&r1=1383759&r2=1383760&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldVector.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldVector.java Wed Sep 12 04:27:47 2012
@@ -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 F
      * 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 F
      * {@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 F
      * @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 F
      * @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 F
      * @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.