commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r730838 - /commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java
Date Fri, 02 Jan 2009 20:21:10 GMT
Author: luc
Date: Fri Jan  2 12:21:09 2009
New Revision: 730838

URL: http://svn.apache.org/viewvc?rev=730838&view=rev
Log:
added a static method createBlocksLayout to prepare array before constructing the matrix

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java?rev=730838&r1=730837&r2=730838&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/DenseRealMatrix.java
Fri Jan  2 12:21:09 2009
@@ -104,14 +104,7 @@
         blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE;
 
         // allocate storage blocks, taking care of smaller ones at right and bottom
-        blocks = new double[blockRows * blockColumns][];
-        int blockIndex = 0;
-        for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
-            final int iHeight = blockHeight(iBlock);
-            for (int jBlock = 0; jBlock < blockColumns; ++jBlock) {
-                blocks[blockIndex++] = new double[iHeight * blockWidth(jBlock)];
-            }
-        }
+        blocks = createBlocksLayout(rows, columns);
 
     }
 
@@ -144,6 +137,7 @@
      *
      * @exception IllegalArgumentException if <code>blockData</code> shape is
      * inconsistent with block layout
+     * @see #createBlocksLayout(int, int)
      * @see #toBlocksLayout(double[][])
      * @see #DenseRealMatrix(double[][])
      */
@@ -205,6 +199,7 @@
      * @return a new data array containing the same entries but in blocks layout
      * @exception IllegalArgumentException if <code>rawData</code> is not rectangular
      *  (not all rows have the same length)
+     * @see #createBlocksLayout(int, int)
      * @see #DenseRealMatrix(int, int, double[][], boolean)
      */
     public static double[][] toBlocksLayout(final double[][] rawData)
@@ -252,6 +247,41 @@
 
     }
 
+    /**
+     * Create a data array in blocks layout.
+     * <p>
+     * This method can be used to create the array argument of the {@link
+     * DenseRealMatrix#DenseRealMatrix(int, int, double[][], boolean)} constructor.
+     * </p>
+     * @param rows  the number of rows in the new matrix
+     * @param columns  the number of columns in the new matrix
+     * @return a new data array in blocks layout
+     * @see #toBlocksLayout(double[][])
+     * @see #DenseRealMatrix(int, int, double[][], boolean)
+     */
+    public static double[][] createBlocksLayout(final int rows, final int columns)
+        throws IllegalArgumentException {
+
+        final int blockRows    = (rows    + BLOCK_SIZE - 1) / BLOCK_SIZE;
+        final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE;
+
+        final double[][] blocks = new double[blockRows * blockColumns][];
+        for (int iBlock = 0, blockIndex = 0; iBlock < blockRows; ++iBlock) {
+            final int pStart  = iBlock * BLOCK_SIZE;
+            final int pEnd    = Math.min(pStart + BLOCK_SIZE, rows);
+            final int iHeight = pEnd - pStart;
+            for (int jBlock = 0; jBlock < blockColumns; ++jBlock, ++blockIndex) {
+                final int qStart = jBlock * BLOCK_SIZE;
+                final int qEnd   = Math.min(qStart + BLOCK_SIZE, columns);
+                final int jWidth = qEnd - qStart;
+                blocks[blockIndex] = new double[iHeight * jWidth];
+            }
+        }
+
+        return blocks;
+
+    }
+
     /** {@inheritDoc} */
     public RealMatrix createMatrix(final int rowDimension, final int columnDimension)
         throws IllegalArgumentException {



Mime
View raw message