commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1456931 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/linear/SingularValueDecomposition.java test/java/org/apache/commons/math3/linear/SingularValueDecompositionTest.java
Date Fri, 15 Mar 2013 12:34:35 GMT
Author: luc
Date: Fri Mar 15 12:34:35 2013
New Revision: 1456931

URL: http://svn.apache.org/r1456931
Log:
Fixed infinite loop when NaN occurs in singular value decomposition.

JIRA: MATH-947

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SingularValueDecomposition.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SingularValueDecompositionTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1456931&r1=1456930&r2=1456931&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Fri Mar 15 12:34:35 2013
@@ -55,10 +55,13 @@ This is a minor release: It combines bug
   Changes to existing features were made in a backwards-compatible
   way such as to allow drop-in replacement of the v3.1[.1] JAR file.
 ">
+      <action dev="luc" type="fix" issue="MATH-947" >
+        Fixed infinite loop when NaN occurs in singular value decomposition.
+      </action>
       <action dev="luc" type="add" issue="MATH-460" due-to="Andrew Waterman">
         Added the Lévy distribution.
       </action>
-        <action dev="luc" type="update"  >
+      <action dev="luc" type="update"  >
         Normal distribution now uses a direct implementation of the
         inverse error function to compute inverse cumulative probability
         instead of relying on a numerical solver. This is much faster,

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SingularValueDecomposition.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SingularValueDecomposition.java?rev=1456931&r1=1456930&r2=1456931&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SingularValueDecomposition.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/SingularValueDecomposition.java
Fri Mar 15 12:34:35 2013
@@ -285,10 +285,18 @@ public class SingularValueDecomposition 
                 final double threshold
                     = TINY + EPS * (FastMath.abs(singularValues[k]) +
                                     FastMath.abs(singularValues[k + 1]));
-                if (FastMath.abs(e[k]) <= threshold) {
+
+                // the following condition is written this way in order
+                // to break out of the loop when NaN occurs, writing it
+                // as "if (FastMath.abs(e[k]) <= threshold)" would loop
+                // indefinitely in case of NaNs because comparison on NaNs
+                // always return false, regardless of what is checked
+                // see issue MATH-947
+                if (!(FastMath.abs(e[k]) > threshold)) {
                     e[k] = 0;
                     break;
                 }
+
             }
 
             if (k == p - 2) {

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SingularValueDecompositionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SingularValueDecompositionTest.java?rev=1456931&r1=1456930&r2=1456931&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SingularValueDecompositionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SingularValueDecompositionTest.java
Fri Mar 15 12:34:35 2013
@@ -316,4 +316,16 @@ public class SingularValueDecompositionT
         return u.multiply(d).multiply(v);
     }
 
+    @Test
+    public void testIssue947() {
+        double[][] nans = new double[][] {
+            { Double.NaN, Double.NaN },
+            { Double.NaN, Double.NaN }
+        };
+        RealMatrix m = new Array2DRowRealMatrix(nans, false);
+        SingularValueDecomposition svd = new SingularValueDecomposition(m);
+        Assert.assertTrue(Double.isNaN(svd.getSingularValues()[0]));
+        Assert.assertTrue(Double.isNaN(svd.getSingularValues()[1]));
+    }
+
 }



Mime
View raw message