2010/1/29 Luc Maisonobe <luc.maison...@free.fr>: > Mikkel Meyer Andersen a écrit : >> 2010/1/29 Luc Maisonobe <luc.maison...@free.fr>: >>> Bill Barker a écrit : >>>> >>>> -------------------------------------------------- >>>> From: "Luc Maisonobe" <luc.maison...@free.fr> >>>> Sent: Thursday, January 28, 2010 12:14 PM >>>> To: "Commons Developers List" <dev@commons.apache.org> >>>> Subject: Re: svn commit: r904231 - in >>>> /commons/proper/math/trunk/src:main/java/org/apache/commons/math/linear/AbstractRealVector.javasite/xdoc/changes.xml >>>> test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java >>>> >>>>> 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. >>>>> >>>> +1 >>>> There is a case for using the sparseIterator, but that probably makes >>>> the methods overly complicated (since zero values have to be handled >>>> specially). >>> I did hesitate to add this. I thought adding special getSparseMinIndex >>> and so on, that would explicitely ignore non-zeros. We can add them too >>> if you think ignoring zeros is the right thing to do. In that case, we >>> simply copy the methods and change iterator into sparse iterator, that's >>> all. >>> >>> Luc >>> >>>>> 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 >>>>> >>>> --------------------------------------------------------------------- >>>> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org >>>> For additional commands, e-mail: dev-h...@commons.apache.org >>>> >>>> >>> >>> --------------------------------------------------------------------- >>> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org >>> For additional commands, e-mail: dev-h...@commons.apache.org >>> >>> >> >> Is it not possible to just make a generic getMaxIndex(Iterator >> iterator) method, and then the user can just supply whichever iterator >> convenient for her? > > It sounds complicated for users and it's not done that way for other > methods like the mapXxx ones. > > Another way could be to say we always use sparse iterator, knowing for > dense vectors it iterates over every entries. > > Luc > >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org >> For additional commands, e-mail: dev-h...@commons.apache.org >> > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > >
For a vector v = (0, NaN, 0), I would still assume that 0 was min and max, and not a NaN as in the proposed implementation. One way to do this is to seach for a 0 if the initial sparse min and max is NaN, and then return 0 for both as soon as a 0 is found. Although it introduces some overhead. --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org