Add an optimized dotProduct to OpenMapRealVector.
Slightly modified from contribution by Jake Mannix. JIRA: MATH-317 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@890159 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b26de3b0aa
commit
01e5f22cc5
|
@ -196,7 +196,7 @@ public abstract class AbstractRealVector implements RealVector {
|
||||||
public double getDistance(RealVector v) throws IllegalArgumentException {
|
public double getDistance(RealVector v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v);
|
checkVectorDimensions(v);
|
||||||
double d = 0;
|
double d = 0;
|
||||||
Iterator<Entry> it = sparseIterator();
|
Iterator<Entry> it = iterator();
|
||||||
Entry e;
|
Entry e;
|
||||||
while (it.hasNext() && (e = it.next()) != null) {
|
while (it.hasNext() && (e = it.next()) != null) {
|
||||||
final double diff = e.getValue() - v.getEntry(e.getIndex());
|
final double diff = e.getValue() - v.getEntry(e.getIndex());
|
||||||
|
@ -207,15 +207,7 @@ public abstract class AbstractRealVector implements RealVector {
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public double getDistance(double[] v) throws IllegalArgumentException {
|
public double getDistance(double[] v) throws IllegalArgumentException {
|
||||||
checkVectorDimensions(v.length);
|
return getDistance(new ArrayRealVector(v,false));
|
||||||
double d = 0;
|
|
||||||
Iterator<Entry> it = iterator();
|
|
||||||
Entry e;
|
|
||||||
while (it.hasNext() && (e = it.next()) != null) {
|
|
||||||
final double diff = e.getValue() - v[e.getIndex()];
|
|
||||||
d += diff * diff;
|
|
||||||
}
|
|
||||||
return Math.sqrt(d);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
|
|
|
@ -303,6 +303,34 @@ public class OpenMapRealVector extends AbstractRealVector implements SparseRealV
|
||||||
return new OpenMapRealVector(this);
|
return new OpenMapRealVector(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optimized method to compute the dot product with an OpenMapRealVector.
|
||||||
|
* Iterates over the smaller of the two.
|
||||||
|
* @param v The vector to compute the dot product with
|
||||||
|
* @return The dot product of <code>this</code> and <code>v</code>
|
||||||
|
* @throws IllegalArgumentException If the dimensions don't match
|
||||||
|
*/
|
||||||
|
public double dotProduct(OpenMapRealVector v) throws IllegalArgumentException {
|
||||||
|
checkVectorDimensions(v.getDimension());
|
||||||
|
boolean thisIsSmaller = entries.size() < v.entries.size();
|
||||||
|
Iterator iter = thisIsSmaller ? entries.iterator() : v.entries.iterator();
|
||||||
|
OpenIntToDoubleHashMap larger = thisIsSmaller ? v.entries : entries;
|
||||||
|
double d = 0;
|
||||||
|
while(iter.hasNext()) {
|
||||||
|
iter.advance();
|
||||||
|
d += iter.value() * larger.get(iter.key());
|
||||||
|
}
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
public double dotProduct(RealVector v) throws IllegalArgumentException {
|
||||||
|
if(v instanceof OpenMapRealVector) {
|
||||||
|
return dotProduct((OpenMapRealVector)v);
|
||||||
|
} else {
|
||||||
|
return super.dotProduct(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public OpenMapRealVector ebeDivide(RealVector v) throws IllegalArgumentException {
|
public OpenMapRealVector ebeDivide(RealVector v) throws IllegalArgumentException {
|
||||||
|
|
Loading…
Reference in New Issue