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/05/28 10:31:13 UTC
svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/
test/java/org/apache/commons/math3/linear/
Author: celestin
Date: Mon May 28 08:31:13 2012
New Revision: 1343163
URL: http://svn.apache.org/viewvc?rev=1343163&view=rev
Log:
MATH-792:
- various methods to visit the entries of a RealVector (entries are unmodified),
- default implementation in RealVector abstract class,
- unit tests in abstract RealVectorAbstractClass,
- all XxxRealVectorTest classes now extend RealVectorAbstractClass.
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java (with props)
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVectorPreservingVisitor.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java
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=1343163&r1=1343162&r2=1343163&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 Mon May 28 08:31:13 2012
@@ -22,6 +22,7 @@ import java.util.NoSuchElementException;
import org.apache.commons.math3.exception.MathUnsupportedOperationException;
import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.MathArithmeticException;
import org.apache.commons.math3.analysis.FunctionUtils;
@@ -191,6 +192,37 @@ public abstract class RealVector {
}
/**
+ * Checks that the indices of a subvector are valid.
+ *
+ * @param start the index of the first entry of the subvector
+ * @param end the index of the last entry of the subvector (inclusive)
+ * @throws OutOfRangeException if {@code start} of {@code end} are not valid
+ * @throws NumberIsTooSmallException if {@code end < start}
+ */
+ protected void checkIndices(final int start, final int end) {
+ final int dim = getDimension();
+ if ((start < 0) || (start >= dim)) {
+ throw new OutOfRangeException(LocalizedFormats.INDEX,
+ Integer.valueOf(start),
+ Integer.valueOf(0),
+ Integer.valueOf(dim - 1));
+ }
+ if ((end < 0) || (end >= dim)) {
+ throw new OutOfRangeException(LocalizedFormats.INDEX,
+ Integer.valueOf(end),
+ Integer.valueOf(0),
+ Integer.valueOf(dim - 1));
+ }
+ if (end < start){
+ // TODO Use more specific error message
+ throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW,
+ Integer.valueOf(end),
+ Integer.valueOf(start),
+ false);
+ }
+ }
+
+ /**
* Compute the sum of this vector and {@code v}.
* Returns a new vector. Does not change instance data.
*
@@ -807,9 +839,82 @@ public abstract class RealVector {
return this;
}
+
/**
- * An entry in the vector.
+ * Visits (but does not change) all entries of this vector in default order
+ * (increasing index).
+ *
+ * @param visitor the visitor to be used to process the entries of this
+ * vector
+ * @return the value returned by {@link RealVectorPreservingVisitor#end()}
+ * at the end of the walk
*/
+ public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) {
+ final int dim = getDimension();
+ visitor.start(dim, 0, dim - 1);
+ for (int i = 0; i < dim; i++) {
+ visitor.visit(i, getEntry(i));
+ }
+ return visitor.end();
+ }
+
+ /**
+ * Visits (and possibly change) some entries of this vector in default order
+ * (increasing index).
+ *
+ * @param visitor visitor to be used to process the entries of this vector
+ * @param start the index of the first entry to be visited
+ * @param end the index of the last entry to be visited (inclusive)
+ * @return the value returned by {@link RealVectorPreservingVisitor#end()}
+ * at the end of the walk
+ * @throws org.apache.commons.math3.exception.OutOfRangeException if
+ * the indices are not valid.
+ */
+ public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor,
+ int start, int end) {
+ checkIndices(start, end);
+ visitor.start(getDimension(), start, end);
+ for (int i = start; i <= end; i++) {
+ visitor.visit(i, getEntry(i));
+ }
+ return visitor.end();
+ }
+
+ /**
+ * Visits (but does not change) all entries of this vector in optimized
+ * order. The order in which the entries are visited is selected so as to
+ * lead to the most efficient implementation; it might depend on the
+ * concrete implementation of this abstract class.
+ *
+ * @param visitor the visitor to be used to process the entries of this
+ * vector
+ * @return the value returned by {@link RealVectorPreservingVisitor#end()}
+ * at the end of the walk
+ */
+ public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor) {
+ return walkInDefaultOrder(visitor);
+ }
+
+ /**
+ * Visits (and possibly change) some entries of this vector in default order
+ * (increasing index). The order in which the entries are visited is
+ * selected so as to lead to the most efficient implementation; it might
+ * depend on the concrete implementation of this abstract class.
+ *
+ * @param visitor visitor to be used to process the entries of this vector
+ * @param start the index of the first entry to be visited
+ * @param end the index of the last entry to be visited (inclusive)
+ * @return the value returned by {@link RealVectorPreservingVisitor#end()}
+ * at the end of the walk
+ * @throws org.apache.commons.math3.exception.OutOfRangeException if
+ * the indices are not valid.
+ */
+ public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor,
+ int start, int end) {
+ return walkInDefaultOrder(visitor, start, end);
+ }
+
+ /** An entry in the vector. */
protected class Entry {
/** Index of this entry. */
private int index;
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVectorPreservingVisitor.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVectorPreservingVisitor.java?rev=1343163&r1=1343162&r2=1343163&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVectorPreservingVisitor.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVectorPreservingVisitor.java Mon May 28 08:31:13 2012
@@ -47,9 +47,12 @@ public interface RealVectorPreservingVis
* End visiting a vector. This method is called once, after all entries of
* the vector have been visited.
*
- * @return the value that the {@code walkInXxxOrder} must return
+ * @return the value returned by
+ * {@link RealVector#walkInDefaultOrder(RealVectorPreservingVisitor)},
+ * {@link RealVector#walkInDefaultOrder(RealVectorPreservingVisitor, int, int)},
+ * {@link RealVector#walkInOptimizedOrder(RealVectorPreservingVisitor)}
+ * or
+ * {@link RealVector#walkInOptimizedOrder(RealVectorPreservingVisitor, int, int)
*/
- // TODO Check above javadoc comment (add proper link to methods in
- // RealVector class)
double end();
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java?rev=1343163&r1=1343162&r2=1343163&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java Mon May 28 08:31:13 2012
@@ -59,7 +59,7 @@ import org.junit.Test;
*
* @version $Id$
*/
-public class ArrayRealVectorTest {
+public class ArrayRealVectorTest extends RealVectorAbstractTest {
//
protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}};
@@ -1430,4 +1430,9 @@ public class ArrayRealVectorTest {
Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance);
}
}
+
+ @Override
+ public RealVector create(final double[] data) {
+ return new ArrayRealVector(data, true);
+ }
}
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java?rev=1343163&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java Mon May 28 08:31:13 2012
@@ -0,0 +1,231 @@
+package org.apache.commons.math3.linear;
+
+import java.util.Arrays;
+
+import junit.framework.Assert;
+
+import org.apache.commons.math3.exception.NumberIsTooSmallException;
+import org.apache.commons.math3.exception.OutOfRangeException;
+import org.junit.Test;
+
+
+public abstract class RealVectorAbstractTest {
+
+ public abstract RealVector create(double[] data);
+
+ /** The whole vector is visited. */
+ @Test
+ public void testWalkInDefaultOrderPreservingVisitor1() {
+ final double[] data = new double[] {
+ 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
+ };
+ final RealVector v = create(data);
+ final RealVectorPreservingVisitor visitor;
+ visitor = new RealVectorPreservingVisitor() {
+
+ private int expectedIndex;
+
+ public void visit(final int actualIndex, final double actualValue) {
+ Assert.assertEquals(expectedIndex, actualIndex);
+ Assert.assertEquals(Integer.toString(actualIndex),
+ data[actualIndex], actualValue, 0d);
+ ++expectedIndex;
+ }
+
+ public void start(final int actualSize, final int actualStart,
+ final int actualEnd) {
+ Assert.assertEquals(data.length, actualSize);
+ Assert.assertEquals(0, actualStart);
+ Assert.assertEquals(data.length - 1, actualEnd);
+ expectedIndex = 0;
+ }
+
+ public double end() {
+ return 0;
+ }
+ };
+ v.walkInDefaultOrder(visitor);
+ }
+
+ /** Visiting an invalid subvector. */
+ @Test
+ public void testWalkInDefaultOrderPreservingVisitor2() {
+ final RealVector v = create(new double[5]);
+ try {
+ v.walkInDefaultOrder(null, -1, 4);
+ Assert.fail();
+ } catch (OutOfRangeException e) {
+ // Expected behavior
+ }
+ try {
+ v.walkInDefaultOrder(null, 5, 4);
+ Assert.fail();
+ } catch (OutOfRangeException e) {
+ // Expected behavior
+ }
+ try {
+ v.walkInDefaultOrder(null, 0, -1);
+ Assert.fail();
+ } catch (OutOfRangeException e) {
+ // Expected behavior
+ }
+ try {
+ v.walkInDefaultOrder(null, 0, 5);
+ Assert.fail();
+ } catch (OutOfRangeException e) {
+ // Expected behavior
+ }
+ try {
+ v.walkInDefaultOrder(null, 4, 0);
+ Assert.fail();
+ } catch (NumberIsTooSmallException e) {
+ // Expected behavior
+ }
+ }
+
+ /** Visiting a valid subvector. */
+ @Test
+ public void testWalkInDefaultOrderPreservingVisitor3() {
+ final double[] data = new double[] {
+ 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
+ };
+ final int expectedStart = 2;
+ final int expectedEnd = 7;
+ final RealVector v = create(data);
+ final RealVectorPreservingVisitor visitor;
+ visitor = new RealVectorPreservingVisitor() {
+
+ private int expectedIndex;
+
+ public void visit(final int actualIndex, final double actualValue) {
+ Assert.assertEquals(expectedIndex, actualIndex);
+ Assert.assertEquals(Integer.toString(actualIndex),
+ data[actualIndex], actualValue, 0d);
+ ++expectedIndex;
+ }
+
+ public void start(final int actualSize, final int actualStart,
+ final int actualEnd) {
+ Assert.assertEquals(data.length, actualSize);
+ Assert.assertEquals(expectedStart, actualStart);
+ Assert.assertEquals(expectedEnd, actualEnd);
+ expectedIndex = expectedStart;
+ }
+
+ public double end() {
+ return 0;
+ }
+ };
+ v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
+ }
+
+ /** The whole vector is visited. */
+ @Test
+ public void testWalkInOptimizedOrderPreservingVisitor1() {
+ final double[] data = new double[] {
+ 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
+ };
+ final RealVector v = create(data);
+ final RealVectorPreservingVisitor visitor;
+ visitor = new RealVectorPreservingVisitor() {
+ private final boolean[] visited = new boolean[data.length];
+
+ public void visit(final int actualIndex, final double actualValue) {
+ visited[actualIndex] = true;
+ Assert.assertEquals(Integer.toString(actualIndex),
+ data[actualIndex], actualValue, 0d);
+ }
+
+ public void start(final int actualSize, final int actualStart,
+ final int actualEnd) {
+ Assert.assertEquals(data.length, actualSize);
+ Assert.assertEquals(0, actualStart);
+ Assert.assertEquals(data.length - 1, actualEnd);
+ Arrays.fill(visited, false);
+ }
+
+ public double end() {
+ for (int i = 0; i < data.length; i++) {
+ Assert.assertTrue("entry " + i + "has not been visited",
+ visited[i]);
+ }
+ return 0;
+ }
+ };
+ v.walkInOptimizedOrder(visitor);
+ }
+
+ /** Visiting an invalid subvector. */
+ @Test
+ public void testWalkInOptimizedOrderPreservingVisitor2() {
+ final RealVector v = create(new double[5]);
+ try {
+ v.walkInOptimizedOrder(null, -1, 4);
+ Assert.fail();
+ } catch (OutOfRangeException e) {
+ // Expected behavior
+ }
+ try {
+ v.walkInOptimizedOrder(null, 5, 4);
+ Assert.fail();
+ } catch (OutOfRangeException e) {
+ // Expected behavior
+ }
+ try {
+ v.walkInOptimizedOrder(null, 0, -1);
+ Assert.fail();
+ } catch (OutOfRangeException e) {
+ // Expected behavior
+ }
+ try {
+ v.walkInOptimizedOrder(null, 0, 5);
+ Assert.fail();
+ } catch (OutOfRangeException e) {
+ // Expected behavior
+ }
+ try {
+ v.walkInOptimizedOrder(null, 4, 0);
+ Assert.fail();
+ } catch (NumberIsTooSmallException e) {
+ // Expected behavior
+ }
+ }
+
+ /** Visiting a valid subvector. */
+ @Test
+ public void testWalkInOptimizedOrderPreservingVisitor3() {
+ final double[] data = new double[] {
+ 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
+ };
+ final int expectedStart = 2;
+ final int expectedEnd = 7;
+ final RealVector v = create(data);
+ final RealVectorPreservingVisitor visitor;
+ visitor = new RealVectorPreservingVisitor() {
+ private final boolean[] visited = new boolean[data.length];
+
+ public void visit(final int actualIndex, final double actualValue) {
+ Assert.assertEquals(Integer.toString(actualIndex),
+ data[actualIndex], actualValue, 0d);
+ visited[actualIndex] = true;
+ }
+
+ public void start(final int actualSize, final int actualStart,
+ final int actualEnd) {
+ Assert.assertEquals(data.length, actualSize);
+ Assert.assertEquals(expectedStart, actualStart);
+ Assert.assertEquals(expectedEnd, actualEnd);
+ Arrays.fill(visited, true);
+ }
+
+ public double end() {
+ for (int i = expectedStart; i <= expectedEnd; i++) {
+ Assert.assertTrue("entry " + i + "has not been visited",
+ visited[i]);
+ }
+ return 0;
+ }
+ };
+ v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java?rev=1343163&r1=1343162&r2=1343163&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java Mon May 28 08:31:13 2012
@@ -28,7 +28,7 @@ import java.util.Random;
/**
* Tests for {@link RealVector}.
*/
-public class RealVectorTest {
+public class RealVectorTest extends RealVectorAbstractTest{
private double[] vec1 = { 1d, 2d, 3d, 4d, 5d };
private double[] vec2 = { -3d, 0d, 0d, 2d, 1d };
@@ -330,4 +330,9 @@ public class RealVectorTest {
Assert.assertEquals(v[i] + inc, x.getEntry(i), 0);
}
}
+
+ @Override
+ public RealVector create(final double[] data) {
+ return new OpenMapRealVector(data);
+ }
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java?rev=1343163&r1=1343162&r2=1343163&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java Mon May 28 08:31:13 2012
@@ -57,7 +57,7 @@ import org.apache.commons.math3.analysis
*
* @version $Id$
*/
-public class SparseRealVectorTest {
+public class SparseRealVectorTest extends RealVectorAbstractTest {
//
protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}};
@@ -955,4 +955,9 @@ public class SparseRealVectorTest {
u.ebeMultiply(v1);
u.ebeDivide(v1);
}
+
+ @Override
+ public RealVector create(double[] data) {
+ return new OpenMapRealVector(data);
+ }
}
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/
test/java/org/apache/commons/math3/linear/
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.
> >> [...]
> >> + protected void checkIndices(final int start, final int end) {
> >> + final int dim = getDimension();
> >> + if ((start < 0) || (start >= dim)) {
> >> + throw new OutOfRangeException(LocalizedFormats.INDEX,
> >> + Integer.valueOf(start),
> > ^^^^^^^^^^^^^^^
> >
> > Why do you call "valueOf"?
> >
>
> I am not Sébastien, but this should be obvious:
>
> The constructor used here is
>
> public OutOfRangeException(Localizable specific,
> Number wrong,
> Number lo,
> Number hi)
>
> Integer.valueOf() is the best way to convert int -> Integer
No, the best way is to let the compiler do the conversion, in order to have
a cleaner source code.
Regards,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/
Posted by Konstantin Kolinko <kn...@gmail.com>.
2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
> Hi Sébastien.
>
>> [...]
>> 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=1343163&r1=1343162&r2=1343163&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 Mon May 28 08:31:13 2012
>> @@ -22,6 +22,7 @@ import java.util.NoSuchElementException;
>>
>> import org.apache.commons.math3.exception.MathUnsupportedOperationException;
>> import org.apache.commons.math3.exception.DimensionMismatchException;
>> +import org.apache.commons.math3.exception.NumberIsTooSmallException;
>> import org.apache.commons.math3.exception.OutOfRangeException;
>> import org.apache.commons.math3.exception.MathArithmeticException;
>> import org.apache.commons.math3.analysis.FunctionUtils;
>> @@ -191,6 +192,37 @@ public abstract class RealVector {
>> }
>>
>> /**
>> + * Checks that the indices of a subvector are valid.
>> + *
>> + * @param start the index of the first entry of the subvector
>> + * @param end the index of the last entry of the subvector (inclusive)
>> + * @throws OutOfRangeException if {@code start} of {@code end} are not valid
>> + * @throws NumberIsTooSmallException if {@code end < start}
>> + */
>> + protected void checkIndices(final int start, final int end) {
>> + final int dim = getDimension();
>> + if ((start < 0) || (start >= dim)) {
>> + throw new OutOfRangeException(LocalizedFormats.INDEX,
>> + Integer.valueOf(start),
> ^^^^^^^^^^^^^^^
>
> Why do you call "valueOf"?
>
I am not Sébastien, but this should be obvious:
The constructor used here is
public OutOfRangeException(Localizable specific,
Number wrong,
Number lo,
Number hi)
Integer.valueOf() is the best way to convert int -> Integer
Best regards,
Konstantin Kolinko
>> + Integer.valueOf(0),
>> + Integer.valueOf(dim - 1));
>> + }
>> + if ((end < 0) || (end >= dim)) {
>> + throw new OutOfRangeException(LocalizedFormats.INDEX,
>> + Integer.valueOf(end),
>> + Integer.valueOf(0),
>> + Integer.valueOf(dim - 1));
>> + }
>> + if (end < start){
>> + // TODO Use more specific error message
>> + throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW,
>> + Integer.valueOf(end),
>> + Integer.valueOf(start),
>> + false);
>> + }
>> + }
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/
Posted by Sébastien Brisard <se...@m4x.org>.
Hello again,
2012/5/28 sebb <se...@gmail.com>:
> On 28 May 2012 13:28, Sébastien Brisard <se...@m4x.org> wrote:
>> Hi Gilles,
>>
>> 2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
>>> Hello.
>>>
>>>>
>>>> >
>>>> > Why do you call "valueOf"?
>>>> >
>>>> I have to say I do not like implicit conversions, that's why I tend to
>>>> always use Integer.valueOf and the likes.
>>>
>>> Why?
>>>
>> This is going to get "philosophical": I would not dare to claim that
>> I'm holding *the* truth, this is only my way of seeing things. I do
>> not like things to happen implicitely, because I think that it opens a
>> door to errors (this would not be true of professional programmers,
>> but remember that I work in an environment where people are not real
>> computer scientists... so my philosophy is "close as many doors as you
>> can"...). I even think that J. Bloch has a nice example of potential
>> issues with auto-boxing. However, I agree with you: calling valueOf in
>> this context (that is: building a new exception) is certainly
>> far-fetched.
>>
>> That was the initial reason why I moved from C++ to Java ten years
>> ago: too many things happened "behind my back". Again, I'm not saying
>> that C++ is evil. I'm just saying that I did not have the background
>> with C++ to be fully aware of these implicit assumptions, and their
>> potential consequences.
>>
>> I'm pretty sure I will not convince you on this. I hope I have at
>> least convinced you that I have a good reason to do it this way :-)
>>
>
> I agree with you - making boxing explicit is better.
>
> The reasoning is that implicit boxing/unboxing can hide ineffiicient
> conversions as well as subtle bugs.
> I've seen code which treats the same item variously as a Integer and
> an int for no good reason.
> The compiler warnings allow the careful programmer to investigate and
> choose one or the other.
>
> Yes, the code is a bit longer, but it clearly shows that the
> boxing/unboxing was intended, and not an accident of compiler
> "helpfulness".
>
>>>> However, if you think that
>>>> it leads to less readable code, I'm OK with your way of seeing things.
>>>
>>> Good! ;-)
>>>
>>> Gilles
>>>
>> As I said, I will remove these explicit boxings (but be warned: I've
>> used valueOf() in the past, and do apologize for any unwanted
>> occurence...).
>>
>> Best regards,
>> Sébastien
>>
Wowww!
Who would have thought that this would raise so many answers!
While I also think explicit conversion is better, I think we can all
agree that Gilles has a point for this specific case
throw new OutOfRangeException(LocalizedFormats.INDEX,
Integer.valueOf(start),
Integer.valueOf(0),
Integer.valueOf(dim - 1));
is a bit far-fetched, since the code ends right there. I think we can
safely follow Gilles' advice, here.
Best regards,
Sébastien
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/
Posted by sebb <se...@gmail.com>.
On 28 May 2012 13:28, Sébastien Brisard <se...@m4x.org> wrote:
> Hi Gilles,
>
> 2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
>> Hello.
>>
>>>
>>> >
>>> > Why do you call "valueOf"?
>>> >
>>> I have to say I do not like implicit conversions, that's why I tend to
>>> always use Integer.valueOf and the likes.
>>
>> Why?
>>
> This is going to get "philosophical": I would not dare to claim that
> I'm holding *the* truth, this is only my way of seeing things. I do
> not like things to happen implicitely, because I think that it opens a
> door to errors (this would not be true of professional programmers,
> but remember that I work in an environment where people are not real
> computer scientists... so my philosophy is "close as many doors as you
> can"...). I even think that J. Bloch has a nice example of potential
> issues with auto-boxing. However, I agree with you: calling valueOf in
> this context (that is: building a new exception) is certainly
> far-fetched.
>
> That was the initial reason why I moved from C++ to Java ten years
> ago: too many things happened "behind my back". Again, I'm not saying
> that C++ is evil. I'm just saying that I did not have the background
> with C++ to be fully aware of these implicit assumptions, and their
> potential consequences.
>
> I'm pretty sure I will not convince you on this. I hope I have at
> least convinced you that I have a good reason to do it this way :-)
>
I agree with you - making boxing explicit is better.
The reasoning is that implicit boxing/unboxing can hide ineffiicient
conversions as well as subtle bugs.
I've seen code which treats the same item variously as a Integer and
an int for no good reason.
The compiler warnings allow the careful programmer to investigate and
choose one or the other.
Yes, the code is a bit longer, but it clearly shows that the
boxing/unboxing was intended, and not an accident of compiler
"helpfulness".
>>> However, if you think that
>>> it leads to less readable code, I'm OK with your way of seeing things.
>>
>> Good! ;-)
>>
>> Gilles
>>
> As I said, I will remove these explicit boxings (but be warned: I've
> used valueOf() in the past, and do apologize for any unwanted
> occurence...).
>
> Best regards,
> Sébastien
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/
test/java/org/apache/commons/math3/linear/
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.
>
> That was the initial reason why I moved from C++ to Java ten years
> ago: too many things happened "behind my back".
Arguably a lot more happens so with Java!
Gilles
> [...]
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/
test/java/org/apache/commons/math3/linear/
Posted by Thomas Neidhart <th...@gmail.com>.
On 05/28/2012 02:28 PM, Sébastien Brisard wrote:
> Hi Gilles,
>
> 2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
>> Hello.
>>
>>>
>>>>
>>>> Why do you call "valueOf"?
>>>>
>>> I have to say I do not like implicit conversions, that's why I tend to
>>> always use Integer.valueOf and the likes.
>>
>> Why?
>>
> This is going to get "philosophical": I would not dare to claim that
> I'm holding *the* truth, this is only my way of seeing things. I do
> not like things to happen implicitely, because I think that it opens a
> door to errors (this would not be true of professional programmers,
> but remember that I work in an environment where people are not real
> computer scientists... so my philosophy is "close as many doors as you
> can"...). I even think that J. Bloch has a nice example of potential
> issues with auto-boxing. However, I agree with you: calling valueOf in
> this context (that is: building a new exception) is certainly
> far-fetched.
Do not mind, I can't tell you what I have already seen at my workplace
from *professional* programmers ;-)
btw, a nice example how dangerous auto-boxing is:
Map<Short, Object> map = new HashMap<Short, Object>();
// fill the map with objects, keys in the range 1..10
...
// get the object with key 3
Object o = map.get(3);
o == null or not?
Thomas
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/
Posted by Sébastien Brisard <se...@m4x.org>.
Hi Gilles,
2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
> Hello.
>
>>
>> >
>> > Why do you call "valueOf"?
>> >
>> I have to say I do not like implicit conversions, that's why I tend to
>> always use Integer.valueOf and the likes.
>
> Why?
>
This is going to get "philosophical": I would not dare to claim that
I'm holding *the* truth, this is only my way of seeing things. I do
not like things to happen implicitely, because I think that it opens a
door to errors (this would not be true of professional programmers,
but remember that I work in an environment where people are not real
computer scientists... so my philosophy is "close as many doors as you
can"...). I even think that J. Bloch has a nice example of potential
issues with auto-boxing. However, I agree with you: calling valueOf in
this context (that is: building a new exception) is certainly
far-fetched.
That was the initial reason why I moved from C++ to Java ten years
ago: too many things happened "behind my back". Again, I'm not saying
that C++ is evil. I'm just saying that I did not have the background
with C++ to be fully aware of these implicit assumptions, and their
potential consequences.
I'm pretty sure I will not convince you on this. I hope I have at
least convinced you that I have a good reason to do it this way :-)
>> However, if you think that
>> it leads to less readable code, I'm OK with your way of seeing things.
>
> Good! ;-)
>
> Gilles
>
As I said, I will remove these explicit boxings (but be warned: I've
used valueOf() in the past, and do apologize for any unwanted
occurence...).
Best regards,
Sébastien
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/
test/java/org/apache/commons/math3/linear/
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hello.
>
> >
> > Why do you call "valueOf"?
> >
> I have to say I do not like implicit conversions, that's why I tend to
> always use Integer.valueOf and the likes.
Why?
> However, if you think that
> it leads to less readable code, I'm OK with your way of seeing things.
Good! ;-)
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/
Posted by Sébastien Brisard <se...@m4x.org>.
Hi Gilles,
>
> Why do you call "valueOf"?
>
I have to say I do not like implicit conversions, that's why I tend to
always use Integer.valueOf and the likes. However, if you think that
it leads to less readable code, I'm OK with your way of seeing things.
Best regards,
Sébastien
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: svn commit: r1343163 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/linear/
test/java/org/apache/commons/math3/linear/
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi Sébastien.
> [...]
> 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=1343163&r1=1343162&r2=1343163&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 Mon May 28 08:31:13 2012
> @@ -22,6 +22,7 @@ import java.util.NoSuchElementException;
>
> import org.apache.commons.math3.exception.MathUnsupportedOperationException;
> import org.apache.commons.math3.exception.DimensionMismatchException;
> +import org.apache.commons.math3.exception.NumberIsTooSmallException;
> import org.apache.commons.math3.exception.OutOfRangeException;
> import org.apache.commons.math3.exception.MathArithmeticException;
> import org.apache.commons.math3.analysis.FunctionUtils;
> @@ -191,6 +192,37 @@ public abstract class RealVector {
> }
>
> /**
> + * Checks that the indices of a subvector are valid.
> + *
> + * @param start the index of the first entry of the subvector
> + * @param end the index of the last entry of the subvector (inclusive)
> + * @throws OutOfRangeException if {@code start} of {@code end} are not valid
> + * @throws NumberIsTooSmallException if {@code end < start}
> + */
> + protected void checkIndices(final int start, final int end) {
> + final int dim = getDimension();
> + if ((start < 0) || (start >= dim)) {
> + throw new OutOfRangeException(LocalizedFormats.INDEX,
> + Integer.valueOf(start),
^^^^^^^^^^^^^^^
Why do you call "valueOf"?
> + Integer.valueOf(0),
> + Integer.valueOf(dim - 1));
> + }
> + if ((end < 0) || (end >= dim)) {
> + throw new OutOfRangeException(LocalizedFormats.INDEX,
> + Integer.valueOf(end),
> + Integer.valueOf(0),
> + Integer.valueOf(dim - 1));
> + }
> + if (end < start){
> + // TODO Use more specific error message
> + throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW,
> + Integer.valueOf(end),
> + Integer.valueOf(start),
> + false);
> + }
> + }
Best,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org