commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r762117 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/linear/SparseRealMatrix.java site/xdoc/changes.xml test/org/apache/commons/math/linear/SparseRealMatrixTest.java
Date Sun, 05 Apr 2009 16:53:36 GMT
Author: luc
Date: Sun Apr  5 16:53:35 2009
New Revision: 762117

URL: http://svn.apache.org/viewvc?rev=762117&view=rev
Log:
Greatly improved multiplication speed for sparse matrices
Jira: MATH-248

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java?rev=762117&r1=762116&r2=762117&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealMatrix.java
Sun Apr  5 16:53:35 2009
@@ -152,6 +152,75 @@
 
     /** {@inheritDoc} */
     @Override
+    public RealMatrix multiply(final RealMatrix m)
+        throws IllegalArgumentException {
+        try {
+            return multiply((SparseRealMatrix) m);
+        } catch (ClassCastException cce) {
+
+            // safety check
+            checkMultiplicationCompatible(m);
+
+            final int outCols = m.getColumnDimension();
+            final DenseRealMatrix out = new DenseRealMatrix(rowDimension, outCols);
+            for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();)
{
+                iterator.advance();
+                final double value = iterator.value();
+                final int key      = iterator.key();
+                final int i        = key / columnDimension;
+                final int k        = key % columnDimension;
+                for (int j = 0; j < outCols; ++j) {
+                    out.addToEntry(i, j, value * m.getEntry(k, j));
+                }
+            }
+
+            return out;
+
+        }
+    }
+
+    /**
+     * Returns the result of postmultiplying this by m.
+     *
+     * @param m    matrix to postmultiply by
+     * @return     this * m
+     * @throws     IllegalArgumentException
+     *             if columnDimension(this) != rowDimension(m)
+     */
+    public SparseRealMatrix multiply(SparseRealMatrix m) throws IllegalArgumentException
{
+
+        // safety check
+        checkMultiplicationCompatible(m);
+
+        final int outCols = m.getColumnDimension();
+        SparseRealMatrix out = new SparseRealMatrix(rowDimension, outCols);
+        for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();)
{
+            iterator.advance();
+            final double value = iterator.value();
+            final int key      = iterator.key();
+            final int i        = key / columnDimension;
+            final int k        = key % columnDimension;
+            for (int j = 0; j < outCols; ++j) {
+                final int rightKey = m.computeKey(k, j);
+                if (m.entries.containsKey(rightKey)) {
+                    final int outKey = out.computeKey(i, j);
+                    final double outValue =
+                        out.entries.get(outKey) + value * m.entries.get(rightKey);
+                    if (outValue == 0.0) {
+                        out.entries.remove(outKey);
+                    } else {
+                        out.entries.put(outKey, outValue);
+                    }
+                }
+            }
+        }
+
+        return out;
+
+    }
+
+    /** {@inheritDoc} */
+    @Override
     public double getEntry(int row, int column) throws MatrixIndexException {
         checkRowIndex(row);
         checkColumnIndex(column);

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=762117&r1=762116&r2=762117&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Apr  5 16:53:35 2009
@@ -39,6 +39,9 @@
   </properties>
   <body>
     <release version="2.0" date="TBD" description="TBD">
+      <action dev="luc" type="fix" issue="MATH-248" >
+        Greatly improved multiplication speed for sparse matrices
+      </action>
       <action dev="luc" type="fix" issue="MATH-253" due-to="Sebb">
         Fixed threading issues with MathException and MathRuntimeException
       </action>

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java?rev=762117&r1=762116&r2=762117&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealMatrixTest.java
Sun Apr  5 16:53:35 2009
@@ -16,13 +16,13 @@
  */
 package org.apache.commons.math.linear;
 
-import org.apache.commons.math.linear.decomposition.LUDecompositionImpl;
-import org.apache.commons.math.linear.decomposition.NonSquareMatrixException;
-
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.commons.math.linear.decomposition.LUDecompositionImpl;
+import org.apache.commons.math.linear.decomposition.NonSquareMatrixException;
+
 /**
  * Test cases for the {@link SparseRealMatrix} class.
  * 
@@ -196,6 +196,8 @@
         SparseRealMatrix m2 = createSparseMatrix(testData2);
         assertClose("inverse multiply", m.multiply(mInv), identity,
                 entryTolerance);
+        assertClose("inverse multiply", m.multiply(new DenseRealMatrix(testDataInv)), identity,
+                    entryTolerance);
         assertClose("inverse multiply", mInv.multiply(m), identity,
                 entryTolerance);
         assertClose("identity multiply", m.multiply(identity), m,



Mime
View raw message