From 695dc3bdcc17abe96236116bb8481cd6cc21db4a Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Tue, 17 May 2011 21:38:45 +0000 Subject: [PATCH] MATH-574 Allow outer product of vectors of different sizes. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1104575 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/linear/SparseFieldVector.java | 10 ++++----- .../math/linear/SparseFieldVectorTest.java | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/math/linear/SparseFieldVector.java b/src/main/java/org/apache/commons/math/linear/SparseFieldVector.java index b9f0d2204..13f32a230 100644 --- a/src/main/java/org/apache/commons/math/linear/SparseFieldVector.java +++ b/src/main/java/org/apache/commons/math/linear/SparseFieldVector.java @@ -409,8 +409,8 @@ public class SparseFieldVector> implements FieldVector * if the dimensions do not match. */ public FieldMatrix outerProduct(SparseFieldVector v) { - checkVectorDimensions(v.getDimension()); - SparseFieldMatrix res = new SparseFieldMatrix(field, virtualSize, virtualSize); + final int n = v.getDimension(); + SparseFieldMatrix res = new SparseFieldMatrix(field, virtualSize, n); OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); @@ -425,14 +425,14 @@ public class SparseFieldVector> implements FieldVector /** {@inheritDoc} */ public FieldMatrix outerProduct(T[] v) { - checkVectorDimensions(v.length); - FieldMatrix res = new SparseFieldMatrix(field, virtualSize, virtualSize); + final int n = v.length; + FieldMatrix res = new SparseFieldMatrix(field, virtualSize, n); OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int row = iter.key(); FieldElementvalue = iter.value(); - for (int col = 0; col < virtualSize; col++) { + for (int col = 0; col < n; col++) { res.setEntry(row, col, value.multiply(v[col])); } } diff --git a/src/test/java/org/apache/commons/math/linear/SparseFieldVectorTest.java b/src/test/java/org/apache/commons/math/linear/SparseFieldVectorTest.java index b1976a063..50edb309d 100644 --- a/src/test/java/org/apache/commons/math/linear/SparseFieldVectorTest.java +++ b/src/test/java/org/apache/commons/math/linear/SparseFieldVectorTest.java @@ -170,6 +170,28 @@ public class SparseFieldVectorTest { } + @Test + public void testOuterProduct() { + final SparseFieldVector u + = new SparseFieldVector(FractionField.getInstance(), + new Fraction[] {new Fraction(1), + new Fraction(2), + new Fraction(-3)}); + final SparseFieldVector v + = new SparseFieldVector(FractionField.getInstance(), + new Fraction[] {new Fraction(4), + new Fraction(-2)}); + + final FieldMatrix 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); + } @Test public void testMisc() {