mirror of
https://github.com/apache/commons-math.git
synced 2025-02-06 01:59:13 +00:00
MATH-574
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:
parent
541527bdf3
commit
987f659a12
@ -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]);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user