Author: tn
Date: Thu Mar 21 21:24:45 2013
New Revision: 1459534

URL: http://svn.apache.org/r1459534
Log:
[MATH-862] Correct fix for matrix dimension check in copySubMatrix.

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractRealMatrix.java
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/Array2DRowRealMatrixTest.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractRealMatrix.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractRealMatrix.java?rev=1459534&r1=1459533&r2=1459534&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractRealMatrix.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractRealMatrix.java
 Thu Mar 21 21:24:45 2013
@@ -353,6 +353,13 @@ public abstract class AbstractRealMatrix
                                                        rowsCount, 
columnsCount);
         }
 
+        for (int i = 1; i < rowsCount; i++) {
+            if (destination[i].length < columnsCount) {
+                throw new MatrixDimensionMismatchException(destination.length, 
destination[i].length,
+                                                           rowsCount, 
columnsCount);
+            }
+        }
+
         walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
 
             /** Initial row index. */
@@ -385,14 +392,19 @@ public abstract class AbstractRealMatrix
         throws OutOfRangeException, NullArgumentException, NoDataException,
         MatrixDimensionMismatchException {
         MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns);
+        final int nCols = selectedColumns.length;
         if ((destination.length < selectedRows.length) ||
-            (destination[0].length < selectedColumns.length)) {
+            (destination[0].length < nCols)) {
             throw new MatrixDimensionMismatchException(destination.length, 
destination[0].length,
                                                        selectedRows.length, 
selectedColumns.length);
         }
 
         for (int i = 0; i < selectedRows.length; i++) {
             final double[] destinationI = destination[i];
+            if (destinationI.length < nCols) {
+                throw new MatrixDimensionMismatchException(destination.length, 
destinationI.length,
+                                                           
selectedRows.length, selectedColumns.length);
+            }
             for (int j = 0; j < selectedColumns.length; j++) {
                 destinationI[j] = getEntry(selectedRows[i], 
selectedColumns[j]);
             }

Modified: 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/Array2DRowRealMatrixTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/Array2DRowRealMatrixTest.java?rev=1459534&r1=1459533&r2=1459534&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/Array2DRowRealMatrixTest.java
 (original)
+++ 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/Array2DRowRealMatrixTest.java
 Thu Mar 21 21:24:45 2013
@@ -537,6 +537,11 @@ public final class Array2DRowRealMatrixT
         checkCopy(m, null,  1, 0, 2, 4, true);
         checkCopy(m, null, new int[] {},    new int[] { 0 }, true);
         checkCopy(m, null, new int[] { 0 }, new int[] { 4 }, true);
+        
+        // rectangular check
+        double[][] copy = new double[][] { { 0, 0, 0 }, { 0, 0 } };
+        checkCopy(m, copy, 0, 1, 0, 2, true);
+        checkCopy(m, copy, new int[] { 0, 1 }, new int[] { 0, 1, 2 }, true);
     }
 
     private void checkCopy(RealMatrix m, double[][] reference,


Reply via email to