commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1518820 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/linear/DiagonalMatrix.java test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java
Date Thu, 29 Aug 2013 20:57:10 GMT
Author: tn
Date: Thu Aug 29 20:57:09 2013
New Revision: 1518820

URL: http://svn.apache.org/r1518820
Log:
[MATH-999] Improve performance of DiagonalMatrix.preMultiple(RealVector), thanks to Ajo Fod.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.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=1518820&r1=1518819&r2=1518820&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Thu Aug 29 20:57:09 2013
@@ -51,6 +51,9 @@ If the output is not quite correct, chec
   </properties>
   <body>
     <release version="x.y" date="TBD" description="TBD">
+      <action dev="tn" type="fix" issue="MATH-999" due-to="Ajo Fod">
+        Improve performance of "DiagonalMatrix#preMultiply(RealVector)".
+      </action>
       <action dev="psteitz" type="add" issue="MATH-1025">
         Added CombinatoricsUtils to the util package, moving binomial
         coefficients, factorials and Stirling numbers there and adding

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java?rev=1518820&r1=1518819&r2=1518820&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java
Thu Aug 29 20:57:09 2013
@@ -297,6 +297,18 @@ public class DiagonalMatrix extends Abst
         return operate(v);
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public RealVector preMultiply(final RealVector v) throws DimensionMismatchException {
+        double[] vectorData = null;
+        if (v instanceof ArrayRealVector) {
+            vectorData = ((ArrayRealVector) v).getDataRef();
+        } else {
+            vectorData = v.toArray();
+        }
+        return MatrixUtils.createRealVector(preMultiply(vectorData));
+    }
+
     /** Ensure a value is zero.
      * @param value value to check
      * @exception NumberIsTooLargeException if value is not zero

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java?rev=1518820&r1=1518819&r2=1518820&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java
Thu Aug 29 20:57:09 2013
@@ -258,6 +258,20 @@ public class DiagonalMatrixTest {
         TestUtils.assertEquals(diagResult, denseResult, 0d);
     }
 
+    @Test
+    public void testPreMultiplyVector() {
+        final double[] data = { -1.2, 3.4, 5 };
+        final DiagonalMatrix diag = new DiagonalMatrix(data);
+        final RealMatrix dense = new Array2DRowRealMatrix(diag.getData());
+
+        final double[] v = { 6.7, 890.1, 23.4 };
+        final RealVector vector = MatrixUtils.createRealVector(v);
+        final RealVector diagResult = diag.preMultiply(vector);
+        final RealVector denseResult = dense.preMultiply(vector);
+
+        TestUtils.assertEquals("preMultiply(Vector) returns wrong result", diagResult, denseResult,
0d);
+    }
+
     @Test(expected=NumberIsTooLargeException.class)
     public void testSetNonDiagonalEntry() {
         final DiagonalMatrix diag = new DiagonalMatrix(3);



Mime
View raw message