You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by jm...@apache.org on 2010/01/21 14:36:10 UTC

svn commit: r901698 - /lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java

Author: jmannix
Date: Thu Jan 21 13:36:10 2010
New Revision: 901698

URL: http://svn.apache.org/viewvc?rev=901698&view=rev
Log:
Vector.addTo() can be nicely optimized, but what about Vector.assign(otherVector, new PlusWithScaleFunction(d)) - where you want to basically do v += d * otherVector, making as little vector copies as possible?  Optimization for the Dense / Sparse case.

Modified:
    lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java

Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java?rev=901698&r1=901697&r2=901698&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java Thu Jan 21 13:36:10 2010
@@ -117,6 +117,26 @@
   }
 
   @Override
+  public Vector assign(Vector other, BinaryFunction function) {
+    if (other.size() != size()) {
+      throw new CardinalityException();
+    }
+    // is there some other way to know if function.apply(0, x) = x for all x?
+    if(function instanceof PlusFunction || function instanceof PlusWithScaleFunction) {
+      Iterator<Vector.Element> it = other.iterateNonZero();
+      Vector.Element e;
+      while(it.hasNext() && (e = it.next()) != null) {
+        values[e.index()] = function.apply(values[e.index()], e.get());
+      }
+    } else {
+      for (int i = 0; i < size(); i++) {
+        values[i] = function.apply(values[i], other.getQuick(i));
+      }
+    }
+    return this;
+  }
+
+  @Override
   public int getNumNondefaultElements() {
     return values.length;
   }