commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1102704 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/linear/ArrayFieldVector.java test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java
Date Fri, 13 May 2011 13:03:30 GMT
Author: erans
Date: Fri May 13 13:03:30 2011
New Revision: 1102704

URL: http://svn.apache.org/viewvc?rev=1102704&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/ArrayFieldVector.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayFieldVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayFieldVector.java?rev=1102704&r1=1102703&r2=1102704&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayFieldVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayFieldVector.java
Fri May 13 13:03:30 2011
@@ -668,11 +668,11 @@ public class ArrayFieldVector<T extends 
         try {
             return outerProduct((ArrayFieldVector<T>) v);
         } catch (ClassCastException cce) {
-            checkVectorDimensions(v);
             final int m = data.length;
-            final FieldMatrix<T> out = new Array2DRowFieldMatrix<T>(field, m,
m);
-            for (int i = 0; i < data.length; i++) {
-                for (int j = 0; j < data.length; j++) {
+            final int n = v.getDimension();
+            final FieldMatrix<T> out = new Array2DRowFieldMatrix<T>(field, m,
n);
+            for (int i = 0; i < m; i++) {
+                for (int j = 0; j < n; j++) {
                     out.setEntry(i, j, data[i].multiply(v.getEntry(j)));
                 }
             }
@@ -692,11 +692,11 @@ public class ArrayFieldVector<T extends 
 
     /** {@inheritDoc} */
     public FieldMatrix<T> outerProduct(T[] v) {
-        checkVectorDimensions(v.length);
         final int m = data.length;
-        final FieldMatrix<T> out = new Array2DRowFieldMatrix<T>(field, m, m);
-        for (int i = 0; i < data.length; i++) {
-            for (int j = 0; j < data.length; j++) {
+        final int n = v.length;
+        final FieldMatrix<T> out = new Array2DRowFieldMatrix<T>(field, m, n);
+        for (int i = 0; i < m; i++) {
+            for (int j = 0; j < n; j++) {
                 out.setEntry(i, j, data[i].multiply(v[j]));
             }
         }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java?rev=1102704&r1=1102703&r2=1102704&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java
Fri May 13 13:03:30 2011
@@ -628,6 +628,29 @@ public class ArrayFieldVectorTest {
 
     }
 
+    @Test
+    public void testOuterProduct() {
+        final ArrayFieldVector<Fraction> u
+            = new ArrayFieldVector<Fraction>(FractionField.getInstance(),
+                                             new Fraction[] {new Fraction(1),
+                                                             new Fraction(2),
+                                                             new Fraction(-3)});
+        final ArrayFieldVector<Fraction> v
+            = new ArrayFieldVector<Fraction>(FractionField.getInstance(),
+                                             new Fraction[] {new Fraction(4),
+                                                             new Fraction(-2)});
+
+        final FieldMatrix<Fraction> uv = u.outerProduct(v);
+
+        final double tol = Math.ulp(1d);
+        Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(),
tol);
+        Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(),
tol);
+        Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(),
tol);
+        Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(),
tol);
+        Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(),
tol);
+        Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(),
tol);
+    }
+
     /** verifies that two vectors are equals */
     protected void checkArray(String msg, Fraction[] m, Fraction[] n) {
         if (m.length != n.length) {



Mime
View raw message