Author: celestin
Date: Thu Jun 28 06:09:51 2012
New Revision: 1354822

URL: http://svn.apache.org/viewvc?rev=1354822&view=rev
Log:
MATH-795:
In org.apache.commons.math3.linear.RealVectorAbstractTest
  - factored out unit tests of RealVector RealVector.set(double),
  - created unit tests of double[] RealVector.toArray(),
  - factored out unit tests of RealVector RealVector.unitVector(),
  - factored out unit tests of void RealVector.unitize(),
  - created unit tests of Iterator<RealVector.Entry> RealVector.iterator().

In org.apache.commons.math3.linear.ArrayRealVector, removed unnecessary 
overrides of unitVector() and unitize().

In org.apache.commons.math3.linear.RealVector
  - unitVector() and unitize() now throw an ArithmeticException when the norm 
is 0 (as specified in the Javadoc),
  - the returned iterator() returns NoSuchElementException as specified in the 
general contract of iterators.


Modified:
    
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
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java

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=1354822&r1=1354821&r2=1354822&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
 Thu Jun 28 06:09:51 2012
@@ -568,26 +568,6 @@ public class ArrayRealVector extends Rea
 
     /** {@inheritDoc} */
     @Override
-    public RealVector unitVector() {
-        final double norm = getNorm();
-        if (norm == 0) {
-            throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
-        }
-        return mapDivide(norm);
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public void unitize() {
-        final double norm = getNorm();
-        if (norm == 0) {
-            throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
-        }
-        mapDivideToSelf(norm);
-    }
-
-    /** {@inheritDoc} */
-    @Override
     public RealVector projection(RealVector v) {
         return v.mapMultiply(dotProduct(v) / v.dotProduct(v));
     }

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=1354822&r1=1354821&r2=1354822&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
 Thu Jun 28 06:09:51 2012
@@ -709,9 +709,11 @@ public abstract class RealVector {
      * @throws ArithmeticException if the norm is {@code null}.
      */
     public RealVector unitVector() {
-        RealVector copy = copy();
-        copy.unitize();
-        return copy;
+        final double norm = getNorm();
+        if (norm == 0) {
+            throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
+        }
+        return mapDivide(norm);
     }
 
     /**
@@ -722,6 +724,10 @@ public abstract class RealVector {
      * if the norm is zero.
      */
     public void unitize() {
+        final double norm = getNorm();
+        if (norm == 0) {
+            throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
+        }
         mapDivideToSelf(getNorm());
     }
 
@@ -771,8 +777,12 @@ public abstract class RealVector {
 
             /** {@inheritDoc} */
             public Entry next() {
-                e.setIndex(i++);
-                return e;
+                if (i < dim) {
+                    e.setIndex(i++);
+                    return e;
+                } else {
+                    throw new NoSuchElementException();
+                }
             }
 
             /** {@inheritDoc} */

Modified: 
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=1354822&r1=1354821&r2=1354822&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
 (original)
+++ 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
 Thu Jun 28 06:09:51 2012
@@ -17,6 +17,8 @@
 package org.apache.commons.math3.linear;
 
 import java.util.Arrays;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
 import java.util.Random;
 
 import org.apache.commons.math3.TestUtils;
@@ -964,6 +966,109 @@ public abstract class RealVectorAbstract
     }
 
     @Test
+    public void testSet() {
+        for (int i = 0; i < values.length; i++) {
+            final double expected = values[i];
+            final RealVector v = create(values);
+            v.set(expected);
+            for (int j = 0; j < values.length; j++) {
+                Assert.assertEquals("entry #" + j, expected, v.getEntry(j), 0);
+            }
+        }
+    }
+
+    @Test
+    public void testToArray() {
+        final double[] data = create(values).toArray();
+        Assert.assertNotSame(values, data);
+        for (int i = 0; i < values.length; i++) {
+            Assert.assertEquals("entry #" + i, values[i], data[i], 0);
+        }
+    }
+
+    private void doTestUnitVector(final boolean inPlace) {
+        final double x = getPreferredEntryValue();
+        final double[] data = {
+            x, 1d, x, x, 2d, x, x, x, 3d, x, x, x, x
+        };
+        double norm = 0d;
+        for (int i = 0; i < data.length; i++) {
+            norm += data[i] * data[i];
+        }
+        norm = FastMath.sqrt(norm);
+        final double[] expected = new double[data.length];
+        for (int i = 0; i < expected.length; i++) {
+            expected[i] = data[i] / norm;
+        }
+        final RealVector v = create(data);
+        final RealVector actual;
+        if (inPlace) {
+            v.unitize();
+            actual = v;
+        } else {
+            actual = v.unitVector();
+            Assert.assertNotSame(v, actual);
+        }
+        TestUtils.assertEquals("", expected, actual, 0d);
+    }
+
+    @Test
+    public void testUnitVector() {
+        doTestUnitVector(false);
+    }
+
+    @Test
+    public void testUnitize() {
+        doTestUnitVector(true);
+    }
+
+    private void doTestUnitVectorNullVector(final boolean inPlace) {
+        final double[] data = {
+            0d, 0d, 0d, 0d, 0d
+        };
+        if (inPlace) {
+            create(data).unitize();
+        } else {
+            create(data).unitVector();
+        }
+    }
+
+    @Test(expected=ArithmeticException.class)
+    public void testUnitVectorNullVector() {
+        doTestUnitVectorNullVector(false);
+    }
+
+    @Test(expected=ArithmeticException.class)
+    public void testUnitizeNullVector() {
+        doTestUnitVectorNullVector(true);
+    }
+
+    @Test
+    public void testIterator() {
+        final RealVector v = create(values);
+        final Iterator<RealVector.Entry> it = v.iterator();
+        for (int i = 0; i < values.length; i++) {
+            Assert.assertTrue("entry #" + i, it.hasNext());
+            final RealVector.Entry e = it.next();
+            Assert.assertEquals("", i, e.getIndex());
+            Assert.assertEquals("", values[i], e.getValue(), 0d);
+            try {
+                it.remove();
+                Assert.fail("UnsupportedOperationException should have been 
thrown");
+            } catch (UnsupportedOperationException exc) {
+                // Expected behavior
+            }
+        }
+        Assert.assertFalse(it.hasNext());
+        try {
+            it.next();
+            Assert.fail("NoSuchElementException should have been thrown");
+        } catch (NoSuchElementException e) {
+            // Expected behavior
+        }
+    }
+
+    @Test
     public void testDataInOut() {
         final RealVector v1 = create(vec1);
         final RealVector v2 = create(vec2);
@@ -991,17 +1096,6 @@ public abstract class RealVectorAbstract
             // expected behavior
         }
 
-        final RealVector v_set3 = v1.copy();
-        v_set3.set(13.0);
-        Assert.assertEquals("testData is 13.0 ", 13.0, v_set3.getEntry(2), 0);
-
-        try {
-            v_set3.getEntry(23);
-            Assert.fail("OutOfRangeException expected");
-        } catch (OutOfRangeException ex) {
-            // expected behavior
-        }
-
         final RealVector v_set4 = v4.copy();
         v_set4.setSubVector(3, v2_t);
         Assert.assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3), 0);
@@ -1037,28 +1131,6 @@ public abstract class RealVectorAbstract
         double dot_2 = v1.dotProduct(v2_t);
         Assert.assertEquals("compare val ", 32d, dot_2, normTolerance);
 
-        RealVector v_unitVector = v1.unitVector();
-        RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm());
-        assertClose("compare vect", v_unitVector.toArray(),
-                    v_unitVector_2.toArray(), normTolerance);
-
-        try {
-            v_null.unitVector();
-            Assert.fail("Expecting MathArithmeticException");
-        } catch (MathArithmeticException ex) {
-            // expected behavior
-        }
-
-        RealVector v_unitize = v1.copy();
-        v_unitize.unitize();
-        assertClose("compare vect" 
,v_unitVector_2.toArray(),v_unitize.toArray(),normTolerance);
-        try {
-            v_null.unitize();
-            Assert.fail("Expecting MathArithmeticException");
-        } catch (MathArithmeticException ex) {
-            // expected behavior
-        }
-
         RealVector v_projection = v1.projection(v2);
         double[] result_projection = {1.662337662337662, 2.0779220779220777, 
2.493506493506493};
         assertClose("compare vect", v_projection.toArray(), result_projection, 
normTolerance);

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=1354822&r1=1354821&r2=1354822&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
 Thu Jun 28 06:09:51 2012
@@ -356,16 +356,6 @@ public class RealVectorTest extends Real
     }
 
     @Test
-    public void testIterator() throws Exception {
-        RealVector v = new TestVectorImpl(vec2.clone());
-        Entry e;
-        int i = 0;
-        for(Iterator<Entry> it = v.iterator(); it.hasNext() && (e = it.next()) 
!= null; i++) {
-            Assert.assertEquals(vec2[i], e.getValue(), 0);
-        }
-    }
-
-    @Test
     public void testSparseIterator() throws Exception {
         RealVector v = new TestVectorImpl(vec2.clone());
         Entry e;


Reply via email to