commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1102356 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/linear/ArrayRealVector.java test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
Date Thu, 12 May 2011 15:49:40 GMT
Author: erans
Date: Thu May 12 15:49:40 2011
New Revision: 1102356

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

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java?rev=1102356&r1=1102355&r2=1102356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
Thu May 12 15:49:40 2011
@@ -763,11 +763,11 @@ public class ArrayRealVector extends Abs
         if (v instanceof ArrayRealVector) {
             return outerProduct((ArrayRealVector) v);
         } else {
-            checkVectorDimensions(v);
             final int m = data.length;
-            final RealMatrix out = MatrixUtils.createRealMatrix(m, m);
-            for (int i = 0; i < data.length; i++) {
-                for (int j = 0; j < data.length; j++) {
+            final int n = v.getDimension();
+            final RealMatrix out = MatrixUtils.createRealMatrix(m, n);
+            for (int i = 0; i < m; i++) {
+                for (int j = 0; j < n; j++) {
                     out.setEntry(i, j, data[i] * v.getEntry(j));
                 }
             }
@@ -789,11 +789,11 @@ public class ArrayRealVector extends Abs
     /** {@inheritDoc} */
     @Override
     public RealMatrix outerProduct(double[] v) {
-        checkVectorDimensions(v.length);
         final int m = data.length;
-        final RealMatrix out = MatrixUtils.createRealMatrix(m, m);
-        for (int i = 0; i < data.length; i++) {
-            for (int j = 0; j < data.length; j++) {
+        final int n = v.length;
+        final RealMatrix out = MatrixUtils.createRealMatrix(m, n);
+        for (int i = 0; i < m; i++) {
+            for (int j = 0; j < n; j++) {
                 out.setEntry(i, j, data[i] * v[j]);
             }
         }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java?rev=1102356&r1=1102355&r2=1102356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
Thu May 12 15:49:40 2011
@@ -1190,6 +1190,22 @@ public class ArrayRealVectorTest {
         v.cosine(w);
     }
 
+    @Test
+    public void testOuterProduct() {
+        final ArrayRealVector u = new ArrayRealVector(new double[] {1, 2, -3});
+        final ArrayRealVector v = new ArrayRealVector(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);
+    }
+
     /** verifies that two vectors are close (sup norm) */
     protected void assertClose(String msg, double[] m, double[] n,
             double tolerance) {



Mime
View raw message