commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r894367 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/linear/ site/xdoc/ test/java/org/apache/commons/math/linear/
Date Tue, 29 Dec 2009 12:24:59 GMT
Author: luc
Date: Tue Dec 29 12:24:58 2009
New Revision: 894367

URL: http://svn.apache.org/viewvc?rev=894367&view=rev
Log:
fixed a wrong implementation of the Linf vector norm
JIRA: MATH-326

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.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=894367&r1=894366&r2=894367&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
Tue Dec 29 12:24:58 2009
@@ -206,6 +206,40 @@
     }
 
     /** {@inheritDoc} */
+    public double getNorm() {
+        double sum = 0;
+        Iterator<Entry> it = sparseIterator();
+        Entry e;
+        while (it.hasNext() && (e = it.next()) != null) {
+            final double value = e.getValue();
+            sum += value * value;
+        }
+        return Math.sqrt(sum);
+    }
+
+    /** {@inheritDoc} */
+    public double getL1Norm() {
+        double norm = 0;
+        Iterator<Entry> it = sparseIterator();
+        Entry e;
+        while (it.hasNext() && (e = it.next()) != null) {
+            norm += Math.abs(e.getValue());
+        }
+        return norm;
+    }
+
+    /** {@inheritDoc} */
+    public double getLInfNorm() {
+        double norm = 0;
+        Iterator<Entry> it = sparseIterator();
+        Entry e;
+        while (it.hasNext() && (e = it.next()) != null) {
+            norm = Math.max(norm, Math.abs(e.getValue()));
+        }
+        return norm;
+    }
+
+    /** {@inheritDoc} */
     public double getDistance(double[] v) throws IllegalArgumentException {
         return getDistance(new ArrayRealVector(v,false));
     }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java?rev=894367&r1=894366&r2=894367&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
Tue Dec 29 12:24:58 2009
@@ -694,6 +694,7 @@
     }
 
     /** {@inheritDoc} */
+    @Override
     public double getNorm() {
         double sum = 0;
         for (double a : data) {
@@ -703,6 +704,7 @@
     }
 
     /** {@inheritDoc} */
+    @Override
     public double getL1Norm() {
         double sum = 0;
         for (double a : data) {
@@ -712,10 +714,11 @@
     }
 
     /** {@inheritDoc} */
+    @Override
     public double getLInfNorm() {
         double max = 0;
         for (double a : data) {
-            max += Math.max(max, Math.abs(a));
+            max = Math.max(max, Math.abs(a));
         }
         return max;
     }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java?rev=894367&r1=894366&r2=894367&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
Tue Dec 29 12:24:58 2009
@@ -495,17 +495,6 @@
         return max;
     }
 
-    /** {@inheritDoc} */
-    public double getL1Norm() {
-        double res = 0;
-        Iterator iter = entries.iterator();
-        while (iter.hasNext()) {
-            iter.advance();
-            res += Math.abs(iter.value());
-        }
-        return res;
-    }
-
     /**
      * Optimized method to compute LInfDistance.
      * @param v The vector to compute from
@@ -557,28 +546,6 @@
     }
 
     /** {@inheritDoc} */
-    public double getLInfNorm() {
-        double max = 0;
-        Iterator iter = entries.iterator();
-        while (iter.hasNext()) {
-            iter.advance();
-            max += iter.value();
-        }
-        return max;
-    }
-
-    /** {@inheritDoc} */
-    public double getNorm() {
-        double res = 0;
-        Iterator iter = entries.iterator();
-        while (iter.hasNext()) {
-            iter.advance();
-            res += iter.value() * iter.value();
-        }
-        return Math.sqrt(res);
-    }
-
-    /** {@inheritDoc} */
     public boolean isInfinite() {
         boolean infiniteFound = false;
         Iterator iter = entries.iterator();

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=894367&r1=894366&r2=894367&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Tue Dec 29 12:24:58 2009
@@ -39,6 +39,9 @@
   </properties>
   <body>
     <release version="2.1" date="TBD" description="TBD">
+      <action dev="luc" type="fix" issue="MATH-326" due-to="Jake Mannix">
+        Fixed a wrong implementation of the Linf norm in vectors.
+      </action>
       <action dev="luc" type="fix" issue="MATH-324" due-to="Vincent Morand">
         Fixed a convergence discrepancy with respect to theory in Gragg-Bulirsch-Stoer
         integrator.

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=894367&r1=894366&r2=894367&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
Tue Dec 29 12:24:58 2009
@@ -40,6 +40,7 @@
     protected double[] vec2 = {4d, 5d, 6d};
     protected double[] vec3 = {7d, 8d, 9d};
     protected double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
+    protected double[] vec5 = { -4d, 0d, 3d, 1d, -6d, 3d};
     protected double[] vec_null = {0d, 0d, 0d};
     protected Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
     protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}};
@@ -1079,19 +1080,23 @@
     public void testBasicFunctions() {
         ArrayRealVector v1 = new ArrayRealVector(vec1);
         ArrayRealVector v2 = new ArrayRealVector(vec2);
+        ArrayRealVector v5 = new ArrayRealVector(vec5);
         ArrayRealVector v_null = new ArrayRealVector(vec_null);
 
         RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2);
 
-        //octave =  sqrt(sumsq(v1))
-        double d_getNorm = v1.getNorm();
-        assertEquals("compare values  ", 3.7416573867739413,d_getNorm);
+        // emacs calc: [-4, 0, 3, 1, -6, 3] A --> 8.4261497731763586307
+        double d_getNorm = v5.getNorm();
+        assertEquals("compare values  ", 8.4261497731763586307, d_getNorm);
+
+        // emacs calc: [-4, 0, 3, 1, -6, 3] vN --> 17
+        double d_getL1Norm = v5.getL1Norm();
+        assertEquals("compare values  ", 17.0, d_getL1Norm);
+
+        // emacs calc: [-4, 0, 3, 1, -6, 3] vn --> 6
+        double d_getLInfNorm = v5.getLInfNorm();
+        assertEquals("compare values  ", 6.0, d_getLInfNorm);
 
-        double d_getL1Norm = v1.getL1Norm();
-        assertEquals("compare values  ",6.0, d_getL1Norm);
-
-        double d_getLInfNorm = v1.getLInfNorm();
-        assertEquals("compare values  ",6.0, d_getLInfNorm);
 
         //octave =  sqrt(sumsq(v1-v2))
         double dist = v1.getDistance(v2);

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java?rev=894367&r1=894366&r2=894367&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java
Tue Dec 29 12:24:58 2009
@@ -40,6 +40,7 @@
     protected double[] vec2 = {4d, 5d, 6d};
     protected double[] vec3 = {7d, 8d, 9d};
     protected double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
+    protected double[] vec5 = { -4d, 0d, 3d, 1d, -6d, 3d};
     protected double[] vec_null = {0d, 0d, 0d};
     protected Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
     protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}};
@@ -949,19 +950,22 @@
     public void testBasicFunctions() {
         OpenMapRealVector v1 = new OpenMapRealVector(vec1);
         OpenMapRealVector v2 = new OpenMapRealVector(vec2);
+        OpenMapRealVector v5 = new OpenMapRealVector(vec5);
         OpenMapRealVector v_null = new OpenMapRealVector(vec_null);
 
         SparseRealVectorTestImpl v2_t = new SparseRealVectorTestImpl(vec2);
 
-        //octave =  sqrt(sumsq(v1))
-        double d_getNorm = v1.getNorm();
-        assertEquals("compare values  ", 3.7416573867739413,d_getNorm);
-
-        double d_getL1Norm = v1.getL1Norm();
-        assertEquals("compare values  ",6.0, d_getL1Norm);
-
-        double d_getLInfNorm = v1.getLInfNorm();
-        assertEquals("compare values  ",6.0, d_getLInfNorm);
+        // emacs calc: [-4, 0, 3, 1, -6, 3] A --> 8.4261497731763586307
+        double d_getNorm = v5.getNorm();
+        assertEquals("compare values  ", 8.4261497731763586307, d_getNorm);
+
+        // emacs calc: [-4, 0, 3, 1, -6, 3] vN --> 17
+        double d_getL1Norm = v5.getL1Norm();
+        assertEquals("compare values  ", 17.0, d_getL1Norm);
+
+        // emacs calc: [-4, 0, 3, 1, -6, 3] vn --> 6
+        double d_getLInfNorm = v5.getLInfNorm();
+        assertEquals("compare values  ", 6.0, d_getLInfNorm);
 
         //octave =  sqrt(sumsq(v1-v2))
         double dist = v1.getDistance(v2);



Mime
View raw message