Allow outer product of vectors of different sizes.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1103716 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-05-16 12:30:15 +00:00
parent 541527bdf3
commit 987f659a12
2 changed files with 19 additions and 3 deletions

View File

@ -631,14 +631,14 @@ public class OpenMapRealVector extends AbstractRealVector
/** {@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]);
}
}

View File

@ -928,6 +928,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);