Author: erans
Date: Mon May 16 12:30:15 2011
New Revision: 1103716

URL: http://svn.apache.org/viewvc?rev=1103716&view=rev
Log:
MATH-574
Allow outer product of vectors of different sizes.

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java?rev=1103716&r1=1103715&r2=1103716&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
 Mon May 16 12:30:15 2011
@@ -631,14 +631,14 @@ public class OpenMapRealVector extends A
      /** {@inheritDoc} */
     @Override
     public RealMatrix outerProduct(double[] v) {
-        checkVectorDimensions(v.length);
-        RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize);
+        final int n = v.length;
+        RealMatrix res = new OpenMapRealMatrix(virtualSize, n);
         Iterator iter = entries.iterator();
         while (iter.hasNext()) {
             iter.advance();
             int row = iter.key();
             double value = iter.value();
-            for (int col = 0; col < virtualSize; col++) {
+            for (int col = 0; col < n; col++) {
                 res.setEntry(row, col, value * v[col]);
             }
         }

Modified: 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java?rev=1103716&r1=1103715&r2=1103716&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java
 (original)
+++ 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java
 Mon May 16 12:30:15 2011
@@ -929,6 +929,22 @@ public class SparseRealVectorTest {
     }
 
     @Test
+    public void testOuterProduct() {
+        final OpenMapRealVector u = new OpenMapRealVector(new double[] {1, 2, 
-3});
+        final OpenMapRealVector v = new OpenMapRealVector(new double[] {4, 
-2});
+
+        final RealMatrix uv = u.outerProduct(v);
+
+        final double tol = Math.ulp(1d);
+        Assert.assertEquals(4, uv.getEntry(0, 0), tol);
+        Assert.assertEquals(-2, uv.getEntry(0, 1), tol);
+        Assert.assertEquals(8, uv.getEntry(1, 0), tol);
+        Assert.assertEquals(-4, uv.getEntry(1, 1), tol);
+        Assert.assertEquals(-12, uv.getEntry(2, 0), tol);
+        Assert.assertEquals(6, uv.getEntry(2, 1), tol);
+    }
+
+    @Test
     public void testMisc() {
         OpenMapRealVector v1 = new OpenMapRealVector(vec1);
 


Reply via email to