Mikkel Meyer Andersen a écrit : > Hi. > > Thanks! > > Why use an iterator instead of just a simple for-loop? And what about saving > the values until the vector invalidates - I see pros and cons for both > approaches, so it's more to hear what your thoughts were?
It is because there already are several implementations of the class and they may use different strategies to access elements. Such iterators have been introduced by recent changes so I thought it was better to stick with them now. As for saving the values, we cannot be sure when the vector changes. Users may do this several ways and we simply did not implement any control on that. Luc > > Cheers, Mikkel. > > On 28/01/2010 8:42 PM, <l...@apache.org> wrote: > > Author: luc > Date: Thu Jan 28 19:42:31 2010 > New Revision: 904231 > > URL: http://svn.apache.org/viewvc?rev=904231&view=rev > Log: > added min/max getters for real vectors > For compatibility reasons, these methods have been put in the topmost > abstract class but not in the interface yet. It could be pushed to the > interface when next major version will be released. > JIRA: MATH-334 > > Modified: > > > commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java > commons/proper/math/trunk/src/site/xdoc/changes.xml > > > commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java > > Modified: > commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java > URL: > http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java?rev=904231&r1=904230&r2=904231&view=diff > ============================================================================== > --- > commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java > (original) > +++ > commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java > Thu Jan 28 19:42:31 2010 > @@ -292,6 +292,58 @@ > return d; > } > > + /** Get the index of the minimum entry. > + * @return index of the minimum entry or -1 if vector length is 0 > + * or all entries are NaN > + */ > + public int getMinIndex() { > + int minIndex = -1; > + double minValue = Double.POSITIVE_INFINITY; > + Iterator<Entry> iterator = iterator(); > + while (iterator.hasNext()) { > + final Entry entry = iterator.next(); > + if (entry.getValue() <= minValue) { > + minIndex = entry.getIndex(); > + minValue = entry.getValue(); > + } > + } > + return minIndex; > + } > + > + /** Get the value of the minimum entry. > + * @return value of the minimum entry or NaN if all entries are NaN > + */ > + public double getMinValue() { > + final int minIndex = getMinIndex(); > + return minIndex < 0 ? Double.NaN : getEntry(minIndex); > + } > + > + /** Get the index of the maximum entry. > + * @return index of the maximum entry or -1 if vector length is 0 > + * or all entries are NaN > + */ > + public int getMaxIndex() { > + int maxIndex = -1; > + double maxValue = Double.NEGATIVE_INFINITY; > + Iterator<Entry> iterator = iterator(); > + while (iterator.hasNext()) { > + final Entry entry = iterator.next(); > + if (entry.getValue() >= maxValue) { > + maxIndex = entry.getIndex(); > + maxValue = entry.getValue(); > + } > + } > + return maxIndex; > + } > + > + /** Get the value of the maximum entry. > + * @return value of the maximum entry or NaN if all entries are NaN > + */ > + public double getMaxValue() { > + final int maxIndex = getMaxIndex(); > + return maxIndex < 0 ? Double.NaN : getEntry(maxIndex); > + } > + > /** {...@inheritdoc} */ > public RealVector mapAbs() { > return copy().mapAbsToSelf(); > > Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml > URL: > http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=904231&r1=904230&r2=904231&view=diff > ============================================================================== > --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) > +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Thu Jan 28 19:42:31 > 2010 > @@ -39,6 +39,10 @@ > </properties> > <body> > <release version="2.1" date="TBD" description="TBD"> > + <action dev="luc" type="add" issue="MATH-334" > > + Added min/max getters for real vectors (not yet in the RealVector > interface for > + compatibility purposes, but in the AbstractRealVector abstract > class). > + </action> > <action dev="luc" type="fix" issue="MATH-338" due-to="Vincent Morand"> > Fixed automatic step initialization in embedded Runge-Kutta > integrators. > The relative tolerance setting was never used, only the absolute > tolerance > > Modified: > commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java > URL: > http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java?rev=904231&r1=904230&r2=904231&view=diff > ============================================================================== > --- > commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java > (original) > +++ > commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java > Thu Jan 28 19:42:31 2010 > @@ -1308,6 +1308,30 @@ > } > > > + public void testMinMax() { > + ArrayRealVector v1 = new ArrayRealVector(new double[] { 0, -6, 4, > 12, 7 }); > + assertEquals(1, v1.getMinIndex()); > + assertEquals(-6, v1.getMinValue(), 1.0e-12); > + assertEquals(3, v1.getMaxIndex()); > + assertEquals(12, v1.getMaxValue(), 1.0e-12); > + ArrayRealVector v2 = new ArrayRealVector(new double[] { Double.NaN, > 3, Double.NaN, -2 }); > + assertEquals(3, v2.getMinIndex()); > + assertEquals(-2, v2.getMinValue(), 1.0e-12); > + assertEquals(1, v2.getMaxIndex()); > + assertEquals(3, v2.getMaxValue(), 1.0e-12); > + ArrayRealVector v3 = new ArrayRealVector(new double[] { Double.NaN, > Double.NaN }); > + assertEquals(-1, v3.getMinIndex()); > + assertTrue(Double.isNaN(v3.getMinValue())); > + assertEquals(-1, v3.getMaxIndex()); > + assertTrue(Double.isNaN(v3.getMaxValue())); > + ArrayRealVector v4 = new ArrayRealVector(new double[0]); > + assertEquals(-1, v4.getMinIndex()); > + assertTrue(Double.isNaN(v4.getMinValue())); > + assertEquals(-1, v4.getMaxIndex()); > + assertTrue(Double.isNaN(v4.getMaxValue())); > + } > + > + > /** verifies that two vectors are close (sup norm) */ > protected void assertClose(String msg, double[] m, double[] n, > double tolerance) { > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org