You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2013/03/11 18:00:42 UTC
svn commit: r1455233 - in /commons/proper/math/trunk/src: changes/
main/java/org/apache/commons/math3/linear/
Author: luc
Date: Mon Mar 11 17:00:41 2013
New Revision: 1455233
URL: http://svn.apache.org/r1455233
Log:
Improved checking of null vector elements.
The suggestions by Sébastien have been added and the second
implementation of FieldVector (SparseFieldVector) has been adapted
accordingly, despite it is deprecated.
JIRA: MATH-861
Modified:
commons/proper/math/trunk/src/changes/changes.xml
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
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SparseFieldVector.java
Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1455233&r1=1455232&r2=1455233&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Mon Mar 11 17:00:41 2013
@@ -55,6 +55,9 @@ This is a minor release: It combines bug
Changes to existing features were made in a backwards-compatible
way such as to allow drop-in replacement of the v3.1[.1] JAR file.
">
+ <action dev="luc" type="fix" issue="MATH-861" due-to="Sébastien Brisard" >
+ Improved checking of null vector elements.
+ </action>
<action dev="luc" type="add" issue="MATH-845" due-to="Sébastien Riou" >
Added utilities for prime numbers.
</action>
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=1455233&r1=1455232&r2=1455233&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 Mon Mar 11 17:00:41 2013
@@ -30,6 +30,7 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.ZeroException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.MathArrays;
+import org.apache.commons.math3.util.MathUtils;
/**
* This class implements the {@link FieldVector} interface with a {@link FieldElement} array.
@@ -97,9 +98,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(T[] d)
throws NullArgumentException, ZeroException {
- if (d == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(d);
try {
field = d[0].getField();
data = d.clone();
@@ -118,9 +117,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(Field<T> field, T[] d)
throws NullArgumentException {
- if (d == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(d);
this.field = field;
data = d.clone();
}
@@ -148,9 +145,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(T[] d, boolean copyArray)
throws NullArgumentException, ZeroException {
- if (d == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(d);
if (d.length == 0) {
throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
}
@@ -175,9 +170,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(Field<T> field, T[] d, boolean copyArray)
throws NullArgumentException {
- if (d == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(d);
this.field = field;
data = copyArray ? d.clone() : d;
}
@@ -194,9 +187,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(T[] d, int pos, int size)
throws NullArgumentException, NumberIsTooLargeException {
- if (d == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(d);
if (d.length < pos + size) {
throw new NumberIsTooLargeException(pos + size, d.length, true);
}
@@ -218,9 +209,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(Field<T> field, T[] d, int pos, int size)
throws NullArgumentException, NumberIsTooLargeException {
- if (d == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(d);
if (d.length < pos + size) {
throw new NumberIsTooLargeException(pos + size, d.length, true);
}
@@ -237,9 +226,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(FieldVector<T> v)
throws NullArgumentException {
- if (v == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(v);
field = v.getField();
data = MathArrays.buildArray(field, v.getDimension());
for (int i = 0; i < data.length; ++i) {
@@ -255,9 +242,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(ArrayFieldVector<T> v)
throws NullArgumentException {
- if (v == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(v);
field = v.getField();
data = v.data.clone();
}
@@ -272,9 +257,7 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(ArrayFieldVector<T> v, boolean deep)
throws NullArgumentException {
- if (v == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(v);
field = v.getField();
data = deep ? v.data.clone() : v.data;
}
@@ -289,9 +272,8 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(ArrayFieldVector<T> v1, ArrayFieldVector<T> v2)
throws NullArgumentException {
- if (v1 == null || v2 == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(v1);
+ MathUtils.checkNotNull(v2);
field = v1.getField();
data = MathArrays.buildArray(field, v1.data.length + v2.data.length);
System.arraycopy(v1.data, 0, data, 0, v1.data.length);
@@ -308,9 +290,8 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(ArrayFieldVector<T> v1, T[] v2)
throws NullArgumentException {
- if (v1 == null || v2 == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(v1);
+ MathUtils.checkNotNull(v2);
field = v1.getField();
data = MathArrays.buildArray(field, v1.data.length + v2.length);
System.arraycopy(v1.data, 0, data, 0, v1.data.length);
@@ -327,9 +308,8 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(T[] v1, ArrayFieldVector<T> v2)
throws NullArgumentException {
- if (v1 == null || v2 == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(v1);
+ MathUtils.checkNotNull(v2);
field = v2.getField();
data = MathArrays.buildArray(field, v1.length + v2.data.length);
System.arraycopy(v1, 0, data, 0, v1.length);
@@ -353,9 +333,8 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(T[] v1, T[] v2)
throws NullArgumentException, ZeroException {
- if (v1 == null || v2 == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(v1);
+ MathUtils.checkNotNull(v2);
if (v1.length + v2.length == 0) {
throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
}
@@ -378,9 +357,8 @@ public class ArrayFieldVector<T extends
*/
public ArrayFieldVector(Field<T> field, T[] v1, T[] v2)
throws NullArgumentException, ZeroException {
- if (v1 == null || v2 == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(v1);
+ MathUtils.checkNotNull(v2);
if (v1.length + v2.length == 0) {
throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
}
@@ -518,9 +496,7 @@ public class ArrayFieldVector<T extends
/** {@inheritDoc} */
public FieldVector<T> mapDivide(T d)
throws NullArgumentException, MathArithmeticException {
- if (d == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(d);
T[] out = MathArrays.buildArray(field, data.length);
for (int i = 0; i < data.length; i++) {
out[i] = data[i].divide(d);
@@ -531,9 +507,7 @@ public class ArrayFieldVector<T extends
/** {@inheritDoc} */
public FieldVector<T> mapDivideToSelf(T d)
throws NullArgumentException, MathArithmeticException {
- if (d == null) {
- throw new NullArgumentException();
- }
+ MathUtils.checkNotNull(d);
for (int i = 0; i < data.length; i++) {
data[i] = data[i].divide(d);
}
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=1455233&r1=1455232&r2=1455233&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 Mon Mar 11 17:00:41 2013
@@ -43,6 +43,13 @@ import org.apache.commons.math3.exceptio
* <pre>
* RealVector result = v.mapAddToSelf(3.0).mapTanToSelf().mapSquareToSelf();
* </pre>
+ * <p>
+ * Note that as almost all operations on {@link FieldElement} throw {@link
+ * NullArgumentException} when operating on a null element, it is the responsibility
+ * of <code>FieldVector</code> implementations to make sure no null elements
+ * are inserted into the vector. This must be done in all constructors and
+ * all setters.
+ * <p>
*
* @param <T> the type of the field elements
* @version $Id$
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SparseFieldVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SparseFieldVector.java?rev=1455233&r1=1455232&r2=1455233&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SparseFieldVector.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SparseFieldVector.java Mon Mar 11 17:00:41 2013
@@ -27,6 +27,7 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.MathArrays;
+import org.apache.commons.math3.util.MathUtils;
import org.apache.commons.math3.util.OpenIntToFieldHashMap;
/**
@@ -109,8 +110,10 @@ public class SparseFieldVector<T extends
*
* @param field Field to which the elements belong.
* @param values Set of values to create from.
+ * @exception NullArgumentException if values is null
*/
- public SparseFieldVector(Field<T> field, T[] values) {
+ public SparseFieldVector(Field<T> field, T[] values) throws NullArgumentException {
+ MathUtils.checkNotNull(values);
this.field = field;
virtualSize = values.length;
entries = new OpenIntToFieldHashMap<T>(field);
@@ -197,8 +200,11 @@ public class SparseFieldVector<T extends
}
}
- /** {@inheritDoc} */
- public FieldVector<T> append(T d) {
+ /** {@inheritDoc}
+ * @exception NullArgumentException if d is null
+ */
+ public FieldVector<T> append(T d) throws NullArgumentException {
+ MathUtils.checkNotNull(d);
FieldVector<T> res = new SparseFieldVector<T>(this, 1);
res.setEntry(virtualSize, d);
return res;
@@ -409,15 +415,21 @@ public class SparseFieldVector<T extends
return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
}
- /** {@inheritDoc} */
+ /** {@inheritDoc}
+ * @exception NullArgumentException if value is null
+ */
public void set(T value) {
+ MathUtils.checkNotNull(value);
for (int i = 0; i < virtualSize; i++) {
setEntry(i, value);
}
}
- /** {@inheritDoc} */
- public void setEntry(int index, T value) throws OutOfRangeException {
+ /** {@inheritDoc}
+ * @exception NullArgumentException if value is null
+ */
+ public void setEntry(int index, T value) throws NullArgumentException, OutOfRangeException {
+ MathUtils.checkNotNull(value);
checkIndex(index);
entries.put(index, value);
}