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
This commit is contained in:
parent
987f659a12
commit
695dc3bdcc
|
@ -409,8 +409,8 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
|||
* if the dimensions do not match.
|
||||
*/
|
||||
public FieldMatrix<T> outerProduct(SparseFieldVector<T> v) {
|
||||
checkVectorDimensions(v.getDimension());
|
||||
SparseFieldMatrix<T> res = new SparseFieldMatrix<T>(field, virtualSize, virtualSize);
|
||||
final int n = v.getDimension();
|
||||
SparseFieldMatrix<T> res = new SparseFieldMatrix<T>(field, virtualSize, n);
|
||||
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
iter.advance();
|
||||
|
@ -425,14 +425,14 @@ public class SparseFieldVector<T extends FieldElement<T>> implements FieldVector
|
|||
|
||||
/** {@inheritDoc} */
|
||||
public FieldMatrix<T> outerProduct(T[] v) {
|
||||
checkVectorDimensions(v.length);
|
||||
FieldMatrix<T> res = new SparseFieldMatrix<T>(field, virtualSize, virtualSize);
|
||||
final int n = v.length;
|
||||
FieldMatrix<T> res = new SparseFieldMatrix<T>(field, virtualSize, n);
|
||||
OpenIntToFieldHashMap<T>.Iterator iter = entries.iterator();
|
||||
while (iter.hasNext()) {
|
||||
iter.advance();
|
||||
int row = iter.key();
|
||||
FieldElement<T>value = iter.value();
|
||||
for (int col = 0; col < virtualSize; col++) {
|
||||
for (int col = 0; col < n; col++) {
|
||||
res.setEntry(row, col, value.multiply(v[col]));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -170,6 +170,28 @@ public class SparseFieldVectorTest {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOuterProduct() {
|
||||
final SparseFieldVector<Fraction> u
|
||||
= new SparseFieldVector<Fraction>(FractionField.getInstance(),
|
||||
new Fraction[] {new Fraction(1),
|
||||
new Fraction(2),
|
||||
new Fraction(-3)});
|
||||
final SparseFieldVector<Fraction> v
|
||||
= new SparseFieldVector<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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMisc() {
|
||||
|
|
Loading…
Reference in New Issue