You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2012/05/04 23:42:16 UTC
svn commit: r1334198 - in /commons/proper/math/trunk/src: changes/changes.xml
main/java/org/apache/commons/math3/linear/ArrayRealVector.java
main/java/org/apache/commons/math3/linear/RealVector.java
Author: tn
Date: Fri May 4 21:42:16 2012
New Revision: 1334198
URL: http://svn.apache.org/viewvc?rev=1334198&view=rev
Log:
[MATH-627] avoid superfluous null checks in (Array)RealVector, thanks to Arno Ploese.
Modified:
commons/proper/math/trunk/src/changes/changes.xml
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.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=1334198&r1=1334197&r2=1334198&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Fri May 4 21:42:16 2012
@@ -52,6 +52,9 @@ If the output is not quite correct, chec
<body>
<release version="3.1" date="TBD" description="
">
+ <action dev="tn" type="fix" issue="MATH-627" due-to="Arne Plöse">
+ Avoid superfluous null check when using iterators in RealVector and ArrayRealVector.
+ </action>
<action dev="tn" type="fix" issue="MATH-781" due-to="Scheiber ErnÅ">
Use epsilon instead of ulp in floating-point comparison when dropping columns after phase 1 in SimplexSolver.
</action>
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java?rev=1334198&r1=1334197&r2=1334198&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java Fri May 4 21:42:16 2012
@@ -302,8 +302,8 @@ public class ArrayRealVector extends Rea
checkVectorDimensions(v);
double[] out = data.clone();
Iterator<Entry> it = v.sparseIterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
out[e.getIndex()] += e.getValue();
}
return new ArrayRealVector(out, false);
@@ -327,8 +327,8 @@ public class ArrayRealVector extends Rea
checkVectorDimensions(v);
double[] out = data.clone();
Iterator<Entry> it = v.sparseIterator();
- Entry e;
- while(it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
out[e.getIndex()] -= e.getValue();
}
return new ArrayRealVector(out, false);
@@ -457,8 +457,8 @@ public class ArrayRealVector extends Rea
checkVectorDimensions(v);
double dot = 0;
Iterator<Entry> it = v.sparseIterator();
- Entry e;
- while(it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
dot += data[e.getIndex()] * e.getValue();
}
return dot;
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java?rev=1334198&r1=1334197&r2=1334198&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java Fri May 4 21:42:16 2012
@@ -202,8 +202,8 @@ public abstract class RealVector {
public RealVector add(RealVector v) {
RealVector result = v.copy();
Iterator<Entry> it = sparseIterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
final int index = e.getIndex();
result.setEntry(index, e.getValue() + result.getEntry(index));
}
@@ -222,8 +222,8 @@ public abstract class RealVector {
public RealVector subtract(RealVector v) {
RealVector result = v.copy();
Iterator<Entry> it = sparseIterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
final int index = e.getIndex();
result.setEntry(index, e.getValue() - result.getEntry(index));
}
@@ -274,8 +274,8 @@ public abstract class RealVector {
checkVectorDimensions(v);
double d = 0;
Iterator<Entry> it = sparseIterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
d += e.getValue() * v.getEntry(e.getIndex());
}
return d;
@@ -337,8 +337,8 @@ public abstract class RealVector {
checkVectorDimensions(v);
double d = 0;
Iterator<Entry> it = iterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
final double diff = e.getValue() - v.getEntry(e.getIndex());
d += diff * diff;
}
@@ -358,8 +358,8 @@ public abstract class RealVector {
public double getNorm() {
double sum = 0;
Iterator<Entry> it = sparseIterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
final double value = e.getValue();
sum += value * value;
}
@@ -379,8 +379,8 @@ public abstract class RealVector {
public double getL1Norm() {
double norm = 0;
Iterator<Entry> it = sparseIterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
norm += FastMath.abs(e.getValue());
}
return norm;
@@ -399,8 +399,8 @@ public abstract class RealVector {
public double getLInfNorm() {
double norm = 0;
Iterator<Entry> it = sparseIterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
norm = FastMath.max(norm, FastMath.abs(e.getValue()));
}
return norm;
@@ -421,8 +421,8 @@ public abstract class RealVector {
checkVectorDimensions(v);
double d = 0;
Iterator<Entry> it = iterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
d += FastMath.abs(e.getValue() - v.getEntry(e.getIndex()));
}
return d;
@@ -446,8 +446,8 @@ public abstract class RealVector {
checkVectorDimensions(v);
double d = 0;
Iterator<Entry> it = iterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
d = FastMath.max(FastMath.abs(e.getValue() - v.getEntry(e.getIndex())), d);
}
return d;
@@ -598,11 +598,11 @@ public abstract class RealVector {
v.getDimension());
}
Iterator<Entry> thisIt = sparseIterator();
- Entry thisE = null;
- while (thisIt.hasNext() && (thisE = thisIt.next()) != null) {
+ while (thisIt.hasNext()) {
+ final Entry thisE = thisIt.next();
Iterator<Entry> otherIt = v.sparseIterator();
- Entry otherE = null;
- while (otherIt.hasNext() && (otherE = otherIt.next()) != null) {
+ while (otherIt.hasNext()) {
+ final Entry otherE = otherIt.next();
product.setEntry(thisE.getIndex(), otherE.getIndex(),
thisE.getValue() * otherE.getValue());
}
@@ -629,8 +629,8 @@ public abstract class RealVector {
*/
public void set(double value) {
Iterator<Entry> it = iterator();
- Entry e = null;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
e.setValue(value);
}
}
@@ -684,6 +684,10 @@ public abstract class RealVector {
* In dense implementations, this method will often delegate to
* {@link #iterator()}.
*
+ * <p>Note: derived classes are required to return an {@link Iterator} that
+ * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()}
+ * returns {@code true}.</p>
+ *
* @return a sparse iterator.
*/
public Iterator<Entry> sparseIterator() {
@@ -694,6 +698,10 @@ public abstract class RealVector {
* Generic dense iterator. Iteration is in increasing order
* of the vector index.
*
+ * <p>Note: derived classes are required to return an {@link Iterator} that
+ * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()}
+ * returns {@code true}.</p>
+ *
* @return a dense iterator.
*/
public Iterator<Entry> iterator() {
@@ -753,8 +761,8 @@ public abstract class RealVector {
*/
public RealVector mapToSelf(UnivariateFunction function) {
Iterator<Entry> it = (function.value(0) == 0) ? sparseIterator() : iterator();
- Entry e;
- while (it.hasNext() && (e = it.next()) != null) {
+ while (it.hasNext()) {
+ final Entry e = it.next();
e.setValue(function.value(e.getValue()));
}
return this;