You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2010/04/28 06:35:42 UTC
svn commit: r938781 - in /lucene/mahout/trunk:
core/src/main/java/org/apache/mahout/math/
math/src/main/java/org/apache/mahout/math/
math/src/test/java/org/apache/mahout/math/
Author: srowen
Date: Wed Apr 28 04:35:41 2010
New Revision: 938781
URL: http://svn.apache.org/viewvc?rev=938781&view=rev
Log:
MAHOUT-385 unify vector writable
Removed:
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/DenseVectorWritable.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/NamedVectorWritable.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/RandomAccessSparseVectorWritable.java
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/SequentialAccessSparseVectorWritable.java
Modified:
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/DenseVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java
lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java
lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/math/VectorWritable.java Wed Apr 28 04:35:41 2010
@@ -23,10 +23,15 @@ import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
+import java.util.Iterator;
public class VectorWritable extends Configured implements Writable {
+ public static final int FLAG_DENSE = 0x01;
+ public static final int FLAG_SEQUENTIAL = 0x02;
+ public static final int FLAG_NAMED = 0x04;
+ public static final int NUM_FLAGS = 3;
+
private Vector vector;
public VectorWritable() {
@@ -46,50 +51,71 @@ public class VectorWritable extends Conf
@Override
public void write(DataOutput out) throws IOException {
- VectorWritable writable;
- Class<? extends Vector> vectorClass = vector.getClass();
- String writableClassName = vectorClass.getName() + "Writable";
- try {
- Class<? extends VectorWritable> vectorWritableClass =
- Class.forName(writableClassName).asSubclass(VectorWritable.class);
- writable = vectorWritableClass.getConstructor(vectorClass).newInstance(vector);
- } catch (ClassNotFoundException cnfe) {
- throw new IOException(cnfe);
- } catch (NoSuchMethodException nsme) {
- throw new IOException(nsme);
- } catch (InvocationTargetException ite) {
- throw new IOException(ite);
- } catch (InstantiationException ie) {
- throw new IOException(ie);
- } catch (IllegalAccessException iae) {
- throw new IOException(iae);
+
+ boolean dense = vector.isDense();
+ boolean sequential = vector.isSequentialAccess();
+ boolean named = vector instanceof NamedVector;
+
+ int flags = (dense ? FLAG_DENSE : 0) | (sequential ? FLAG_SEQUENTIAL : 0) | (named ? FLAG_NAMED : 0);
+ out.writeByte(flags);
+
+ if (dense) {
+ out.writeInt(vector.size());
+ for (Vector.Element element : vector) {
+ out.writeDouble(element.get());
+ }
+ } else {
+ out.writeInt(vector.size());
+ out.writeInt(vector.getNumNondefaultElements());
+ Iterator<Vector.Element> iter = vector.iterateNonZero();
+ while (iter.hasNext()) {
+ Vector.Element element = iter.next();
+ out.writeInt(element.index());
+ out.writeDouble(element.get());
+ }
+ }
+ if (named) {
+ out.writeUTF(((NamedVector) vector).getName());
}
- out.writeUTF(writableClassName);
- writable.write(out);
}
@Override
public void readFields(DataInput in) throws IOException {
- String writableClassName = in.readUTF();
- try {
- Class<? extends VectorWritable> writableClass =
- Class.forName(writableClassName).asSubclass(VectorWritable.class);
- VectorWritable writable = writableClass.getConstructor().newInstance();
- writable.readFields(in);
- vector = writable.get();
- } catch (ClassNotFoundException cnfe) {
- throw new IOException(cnfe);
- } catch (ClassCastException cce) {
- throw new IOException(cce);
- } catch (InstantiationException ie) {
- throw new IOException(ie);
- } catch (IllegalAccessException iae) {
- throw new IOException(iae);
- } catch (NoSuchMethodException nsme) {
- throw new IOException(nsme);
- } catch (InvocationTargetException ite) {
- throw new IOException(ite);
+ int flags = in.readByte();
+ if (flags >> NUM_FLAGS != 0) {
+ throw new IllegalArgumentException();
+ }
+ boolean dense = (flags & FLAG_DENSE) != 0;
+ boolean sequential = (flags & FLAG_SEQUENTIAL) != 0;
+ boolean named = (flags & FLAG_NAMED) != 0;
+
+ Vector v;
+ if (dense) {
+ int size = in.readInt();
+ double[] values = new double[size];
+ for (int i = 0; i < size; i++) {
+ values[i] = in.readDouble();
+ }
+ v = new DenseVector(values);
+ } else {
+ int size = in.readInt();
+ int numNonDefaultElements = in.readInt();
+ if (sequential) {
+ v = new SequentialAccessSparseVector(size, numNonDefaultElements);
+ } else {
+ v = new RandomAccessSparseVector(size, numNonDefaultElements);
+ }
+ for (int i = 0; i < numNonDefaultElements; i++) {
+ int index = in.readInt();
+ double value = in.readDouble();
+ v.setQuick(index, value);
+ }
+ }
+ if (named) {
+ String name = in.readUTF();
+ v = new NamedVector(v, name);
}
+ vector = v;
}
/** Write the vector to the output */
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java Wed Apr 28 04:35:41 2010
@@ -519,6 +519,14 @@ public abstract class AbstractMatrix imp
return v;
}
+ public boolean isDense() {
+ return true;
+ }
+
+ public boolean isSequentialAccess() {
+ return true;
+ }
+
@Override
protected Matrix matrixLike(int rows, int columns) {
return matrix.like(rows, columns);
@@ -583,8 +591,7 @@ public abstract class AbstractMatrix imp
v.setQuick(transposeOffset, value);
}
- protected Vector newVector(int cardinality)
- {
+ protected Vector newVector(int cardinality) {
return new DenseVector(cardinality);
}
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/AbstractVector.java Wed Apr 28 04:35:41 2010
@@ -18,9 +18,7 @@
package org.apache.mahout.math;
import java.lang.reflect.Type;
-import java.util.HashMap;
import java.util.Iterator;
-import java.util.Map;
import org.apache.mahout.math.function.BinaryFunction;
import org.apache.mahout.math.function.UnaryFunction;
@@ -32,11 +30,6 @@ import com.google.gson.reflect.TypeToken
/** Implementations of generic capabilities like sum of elements and dot products */
public abstract class AbstractVector implements Vector {
- /**
- * User-settable mapping between String labels and Integer indices. Marked transient so that it will not be serialized
- * with each vector instance.
- */
- private transient Map<String, Integer> bindings;
private int size;
protected double lengthSquared = -1.0;
@@ -80,19 +73,7 @@ public abstract class AbstractVector imp
}
@Override
- public Vector clone() {
- AbstractVector clone;
- try {
- clone = (AbstractVector) super.clone();
- } catch (CloneNotSupportedException cnse) {
- throw new IllegalStateException(cnse); // Can't happen
- }
- if (bindings != null) {
- clone.bindings = (Map<String, Integer>) ((HashMap<String, Integer>) bindings).clone();
- }
- // name is OK
- return clone;
- }
+ public abstract Vector clone();
public Vector divide(double x) {
if (x == 1.0) {
@@ -310,13 +291,23 @@ public abstract class AbstractVector imp
if (size != x.size()) {
throw new CardinalityException(size, x.size());
}
+
+ Vector to = this;
+ Vector from = x;
+ // Clone and edit to the sparse one; if both are sparse, add from the more sparse one
+ if (isDense() || (!x.isDense() &&
+ getNumNondefaultElements() < x.getNumNondefaultElements())) {
+ to = x;
+ from = this;
+ }
+
//TODO: get smarter about this, if we are adding a dense to a sparse, then we should return a dense
- Vector result = clone();
- Iterator<Element> iter = x.iterateNonZero();
+ Vector result = to.clone();
+ Iterator<Element> iter = from.iterateNonZero();
while (iter.hasNext()) {
Element e = iter.next();
int index = e.index();
- result.setQuick(index, getQuick(index) + e.get());
+ result.setQuick(index, result.getQuick(index) + e.get());
}
return result;
}
@@ -358,11 +349,20 @@ public abstract class AbstractVector imp
if (size != x.size()) {
throw new CardinalityException(size, x.size());
}
- Vector result = clone();
+
+ Vector to = this;
+ Vector from = x;
+ // Clone and edit to the sparse one; if both are sparse, edit the more sparse one (more zeroes)
+ if (isDense() || (!x.isDense() && getNumNondefaultElements() > x.getNumNondefaultElements())) {
+ to = x;
+ from = this;
+ }
+
+ Vector result = to.clone();
Iterator<Element> iter = result.iterateNonZero();
while (iter.hasNext()) {
Element element = iter.next();
- element.set(element.get() * x.getQuick(element.index()));
+ element.set(element.get() * from.getQuick(element.index()));
}
return result;
@@ -536,42 +536,4 @@ public abstract class AbstractVector imp
return result.toString();
}
- public double get(String label) throws IndexException, UnboundLabelException {
- if (bindings == null) {
- throw new UnboundLabelException();
- }
- Integer index = bindings.get(label);
- if (index == null) {
- throw new UnboundLabelException();
- }
- return get(index);
- }
-
- public Map<String, Integer> getLabelBindings() {
- return bindings;
- }
-
- public void set(String label, double value) throws IndexException,
- UnboundLabelException {
- if (bindings == null) {
- throw new UnboundLabelException();
- }
- Integer index = bindings.get(label);
- if (index == null) {
- throw new UnboundLabelException();
- }
- set(index, value);
- }
-
- public void setLabelBindings(Map<String, Integer> bindings) {
- this.bindings = bindings;
- }
-
- public void set(String label, int index, double value) throws IndexException {
- if (bindings == null) {
- bindings = new HashMap<String, Integer>();
- }
- bindings.put(label, index);
- set(index, value);
- }
}
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=938781&r1=938780&r2=938781&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 Wed Apr 28 04:35:41 2010
@@ -75,9 +75,15 @@ public class DenseVector extends Abstrac
@Override
public DenseVector clone() {
- DenseVector clone = (DenseVector) super.clone();
- clone.values = values.clone();
- return clone;
+ return new DenseVector(values.clone());
+ }
+
+ public boolean isDense() {
+ return true;
+ }
+
+ public boolean isSequentialAccess() {
+ return true;
}
@Override
@@ -96,15 +102,7 @@ public class DenseVector extends Abstrac
}
public DenseVector like() {
- DenseVector denseVector = new DenseVector(size());
- denseVector.setLabelBindings(getLabelBindings());
- return denseVector;
- }
-
- public Vector like(int cardinality) {
- Vector denseVector = new DenseVector(cardinality);
- denseVector.setLabelBindings(getLabelBindings());
- return denseVector;
+ return new DenseVector(size());
}
public void setQuick(int index, double value) {
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/NamedVector.java Wed Apr 28 04:35:41 2010
@@ -18,7 +18,6 @@
package org.apache.mahout.math;
import java.util.Iterator;
-import java.util.Map;
import org.apache.mahout.math.function.BinaryFunction;
import org.apache.mahout.math.function.UnaryFunction;
@@ -64,11 +63,10 @@ public class NamedVector implements Vect
}
@Override
- public Vector clone() {
+ public NamedVector clone() {
return new NamedVector(delegate.clone(), name);
}
-
public String asFormatString() {
return delegate.asFormatString();
}
@@ -101,20 +99,20 @@ public class NamedVector implements Vect
return delegate.size();
}
- public Iterator<Element> iterator() {
- return delegate.iterator();
+ public boolean isDense() {
+ return delegate.isDense();
}
- public Iterator<Element> iterateNonZero() {
- return delegate.iterateNonZero();
+ public boolean isSequentialAccess() {
+ return delegate.isSequentialAccess();
}
- public double get(String label) throws IndexException, UnboundLabelException {
- return delegate.get(label);
+ public Iterator<Element> iterator() {
+ return delegate.iterator();
}
- public Map<String, Integer> getLabelBindings() {
- return delegate.getLabelBindings();
+ public Iterator<Element> iterateNonZero() {
+ return delegate.iterateNonZero();
}
public Element getElement(int index) {
@@ -137,12 +135,8 @@ public class NamedVector implements Vect
return delegate.getQuick(index);
}
- public Vector like() {
- return delegate.like();
- }
-
- public Vector like(int cardinality) {
- return delegate.like(cardinality);
+ public NamedVector like() {
+ return new NamedVector(delegate.like(), name);
}
public Vector minus(Vector x) {
@@ -177,18 +171,6 @@ public class NamedVector implements Vect
return delegate.plus(x);
}
- public void set(String label, double value) throws IndexException, UnboundLabelException {
- delegate.set(label, value);
- }
-
- public void set(String label, int index, double value) throws IndexException {
- delegate.set(label, index, value);
- }
-
- public void setLabelBindings(Map<String, Integer> bindings) {
- delegate.setLabelBindings(bindings);
- }
-
public void set(int index, double value) {
delegate.set(index, value);
}
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/RandomAccessSparseVector.java Wed Apr 28 04:35:41 2010
@@ -55,7 +55,7 @@ public class RandomAccessSparseVector ex
}
}
- RandomAccessSparseVector(int cardinality, OpenIntDoubleHashMap values) {
+ private RandomAccessSparseVector(int cardinality, OpenIntDoubleHashMap values) {
super(cardinality);
this.values = values;
}
@@ -73,9 +73,7 @@ public class RandomAccessSparseVector ex
@Override
public RandomAccessSparseVector clone() {
- RandomAccessSparseVector clone = (RandomAccessSparseVector) super.clone();
- clone.values = (OpenIntDoubleHashMap)values.clone();
- return clone;
+ return new RandomAccessSparseVector(size(), (OpenIntDoubleHashMap) values.clone());
}
@Override
@@ -92,13 +90,25 @@ public class RandomAccessSparseVector ex
return this;
}
+ public boolean isDense() {
+ return false;
+ }
+
+ public boolean isSequentialAccess() {
+ return false;
+ }
+
public double getQuick(int index) {
return values.get(index);
}
public void setQuick(int index, double value) {
lengthSquared = -1.0;
- values.put(index, value);
+ if (value == 0.0) {
+ values.removeKey(index);
+ } else {
+ values.put(index, value);
+ }
}
public int getNumNondefaultElements() {
@@ -106,19 +116,7 @@ public class RandomAccessSparseVector ex
}
public RandomAccessSparseVector like() {
- int numValues = 256;
- if (values != null) {
- numValues = values.size();
- }
- return new RandomAccessSparseVector(size(), numValues);
- }
-
- public Vector like(int newCardinality) {
- int numValues = 256;
- if (values != null) {
- numValues = values.size();
- }
- return new RandomAccessSparseVector(newCardinality, numValues);
+ return new RandomAccessSparseVector(size(), values.size());
}
/**
@@ -252,7 +250,11 @@ public class RandomAccessSparseVector ex
public void set(double value) {
lengthSquared = -1;
- values.put(index, value);
+ if (value == 0.0) {
+ values.removeKey(index);
+ } else {
+ values.put(index, value);
+ }
}
}
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/SequentialAccessSparseVector.java Wed Apr 28 04:35:41 2010
@@ -80,7 +80,7 @@ public class SequentialAccessSparseVecto
values = other.values.clone();
}
- SequentialAccessSparseVector(int cardinality, OrderedIntDoubleMapping values) {
+ private SequentialAccessSparseVector(int cardinality, OrderedIntDoubleMapping values) {
super(cardinality);
this.values = values;
}
@@ -93,9 +93,15 @@ public class SequentialAccessSparseVecto
@Override
public SequentialAccessSparseVector clone() {
- SequentialAccessSparseVector clone = (SequentialAccessSparseVector) super.clone();
- clone.values = values.clone();
- return clone;
+ return new SequentialAccessSparseVector(size(), values.clone());
+ }
+
+ public boolean isDense() {
+ return false;
+ }
+
+ public boolean isSequentialAccess() {
+ return true;
}
public double getQuick(int index) {
@@ -112,19 +118,7 @@ public class SequentialAccessSparseVecto
}
public SequentialAccessSparseVector like() {
- int numValues = 256;
- if (values != null) {
- numValues = values.getNumMappings();
- }
- return new SequentialAccessSparseVector(size(), numValues);
- }
-
- public Vector like(int newCardinality) {
- int numValues = 256;
- if (values != null) {
- numValues = values.getNumMappings();
- }
- return new SequentialAccessSparseVector(newCardinality, numValues);
+ return new SequentialAccessSparseVector(size(), values.getNumMappings());
}
public Iterator<Element> iterateNonZero() {
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/Vector.java Wed Apr 28 04:35:41 2010
@@ -22,7 +22,6 @@ import org.apache.mahout.math.function.B
import org.apache.mahout.math.function.UnaryFunction;
import java.util.Iterator;
-import java.util.Map;
/**
* The basic interface including numerous convenience functions <p/> NOTE: All implementing classes must have a
@@ -94,6 +93,10 @@ public interface Vector extends Cloneabl
*/
int size();
+ boolean isDense();
+
+ boolean isSequentialAccess();
+
/**
* Return a copy of the recipient
*
@@ -118,23 +121,6 @@ public interface Vector extends Cloneabl
Iterator<Element> iterateNonZero();
/**
- * Return the value at the index defined by the label
- *
- * @param label a String label that maps to an index
- * @return the double at the index
- * @throws IndexException if the index is out of bounds
- * @throws UnboundLabelException if the label is unbound
- */
- double get(String label) throws IndexException, UnboundLabelException;
-
- /**
- * Return a map of the current label bindings of the receiver
- *
- * @return a Map<String, Integer>
- */
- Map<String, Integer> getLabelBindings();
-
- /**
* Return an object of Vector.Element representing an element of this Vector. Useful when designing new iterator
* types.
*
@@ -201,14 +187,6 @@ public interface Vector extends Cloneabl
Vector like();
/**
- * Return an empty vector of the same underlying class as the receiver and of the given cardinality
- *
- * @param cardinality an int specifying the desired cardinality
- * @return a Vector
- */
- Vector like(int cardinality);
-
- /**
* Return a new vector containing the element by element difference of the recipient and the argument
*
* @param x a Vector
@@ -271,31 +249,6 @@ public interface Vector extends Cloneabl
Vector plus(Vector x);
/**
- * Set the value at the index that is mapped to the label
- *
- * @param label a String label that maps to an index
- * @param value the double value at the index
- */
- void set(String label, double value) throws IndexException,
- UnboundLabelException;
-
- /**
- * Set the value at the index and add the label to the bindings
- *
- * @param label a String label that maps to an index
- * @param index an int index
- * @param value a double value
- */
- void set(String label, int index, double value) throws IndexException;
-
- /**
- * Sets a map of label bindings in the receiver
- *
- * @param bindings a {@link Map<String, Integer>} of label bindings
- */
- void setLabelBindings(Map<String, Integer> bindings);
-
- /**
* Set the value at the given index
*
* @param index an int index into the receiver
Modified: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java (original)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/VectorView.java Wed Apr 28 04:35:41 2010
@@ -46,21 +46,23 @@ public class VectorView extends Abstract
@Override
public Vector clone() {
- VectorView clone = (VectorView) super.clone();
- clone.vector = vector.clone();
- return clone;
+ return new VectorView(vector.clone(), offset, size());
}
- public double getQuick(int index) {
- return vector.getQuick(offset + index);
+ public boolean isDense() {
+ return vector.isDense();
+ }
+
+ public boolean isSequentialAccess() {
+ return vector.isSequentialAccess();
}
- public Vector like() {
- return vector.like();
+ public VectorView like() {
+ return new VectorView(vector.like(), offset, size());
}
- public Vector like(int cardinality) {
- return vector.like(cardinality);
+ public double getQuick(int index) {
+ return vector.getQuick(offset + index);
}
public void setQuick(int index, double value) {
Modified: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java (original)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/AbstractTestVector.java Wed Apr 28 04:35:41 2010
@@ -20,9 +20,7 @@ package org.apache.mahout.math;
import junit.framework.TestCase;
import static org.apache.mahout.math.function.Functions.*;
-import java.util.HashMap;
import java.util.Iterator;
-import java.util.Map;
abstract class AbstractTestVector extends TestCase {
@@ -448,26 +446,12 @@ abstract class AbstractTestVector extend
}
}
- public void testAssignBinaryFunctionCardinality() {
- try {
- test.assign(test.like(2), plus);
- fail("Cardinality exception expected");
- } catch (CardinalityException e) {
- }
- }
-
public void testLike() {
Vector other = test.like();
assertTrue("not like", test.getClass().isAssignableFrom(other.getClass()));
assertEquals("size", test.size(), other.size());
}
- public void testLikeN() {
- Vector other = test.like(8);
- assertTrue("not like", test.getClass().isAssignableFrom(other.getClass()));
- assertEquals("size", 8, other.size());
- }
-
public void testCrossProduct() {
Matrix result = test.cross(test);
assertEquals("row size", test.size(), result.size()[0]);
@@ -480,16 +464,4 @@ abstract class AbstractTestVector extend
}
}
- public void testLabelIndexing() {
- Map<String, Integer> bindings = new HashMap<String, Integer>();
- bindings.put("Fee", 0);
- bindings.put("Fie", 1);
- bindings.put("Foe", 2);
- test.setLabelBindings(bindings);
- assertEquals("Fee", test.get(0), test.get("Fee"));
- assertEquals("Fie", test.get(1), test.get("Fie"));
- assertEquals("Foe", test.get(2), test.get("Foe"));
- test.set("Fie", 15.3);
- assertEquals("Fie", test.get(1), test.get("Fie"));
- }
}
Modified: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java (original)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/TestVectorView.java Wed Apr 28 04:35:41 2010
@@ -317,22 +317,8 @@ public class TestVectorView extends Test
}
}
- public void testAssignBinaryFunctionCardinality() {
- try {
- test.assign(test.like(2), plus);
- fail("Cardinality exception expected");
- } catch (CardinalityException e) {
- }
- }
-
public void testLike() {
- assertTrue("not like", test.like() instanceof DenseVector);
- }
-
- public void testLikeN() {
- Vector other = test.like(5);
- assertTrue("not like", other instanceof DenseVector);
- assertEquals("size", 5, other.size());
+ assertTrue("not like", test.like() instanceof VectorView);
}
public void testCrossProduct() {
Modified: lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java?rev=938781&r1=938780&r2=938781&view=diff
==============================================================================
--- lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java (original)
+++ lucene/mahout/trunk/math/src/test/java/org/apache/mahout/math/VectorTest.java Wed Apr 28 04:35:41 2010
@@ -589,31 +589,6 @@ public class VectorTest extends TestCase
v.setQuick(3, 2);
}
- public void testLabelSerializationDense() {
- double[] values = {1.1, 2.2, 3.3};
- Vector test = new DenseVector(values);
- Map<String, Integer> bindings = new HashMap<String, Integer>();
- bindings.put("Fee", 0);
- bindings.put("Fie", 1);
- bindings.put("Foe", 2);
- test.setLabelBindings(bindings);
-
- Type vectorType = new TypeToken<Vector>() {
- }.getType();
-
- GsonBuilder builder = new GsonBuilder();
- builder.registerTypeAdapter(vectorType, new JsonVectorAdapter());
- Gson gson = builder.create();
- String json = gson.toJson(test, vectorType);
- Vector test1 = gson.fromJson(json, vectorType);
- try {
- test1.get("Fee");
- fail();
- } catch (UnboundLabelException e) {
- }
- }
-
-
public void testNameSerialization() throws Exception {
double[] values = {1.1, 2.2, 3.3};
Vector test = new DenseVector(values);
@@ -629,43 +604,6 @@ public class VectorTest extends TestCase
assertEquals("noName and decode are not equal", noName, decode);
}
- public void testLabelSerializationSparse() {
- double[] values = {1.1, 2.2, 3.3};
- Vector test = new RandomAccessSparseVector(3);
- for (int i = 0; i < values.length; i++) {
- test.set(i, values[i]);
- }
- Map<String, Integer> bindings = new HashMap<String, Integer>();
- bindings.put("Fee", 0);
- bindings.put("Fie", 1);
- bindings.put("Foe", 2);
- test.setLabelBindings(bindings);
-
- Type vectorType = new TypeToken<Vector>() {
- }.getType();
-
- GsonBuilder builder = new GsonBuilder();
- builder.registerTypeAdapter(vectorType, new JsonVectorAdapter());
- Gson gson = builder.create();
- String json = gson.toJson(test, vectorType);
- Vector test1 = gson.fromJson(json, vectorType);
- try {
- test1.get("Fee");
- fail();
- } catch (UnboundLabelException e) {
- }
- }
-
- public void testLabelSet() {
- Vector test = new DenseVector(3);
- test.set("Fee", 0, 1.1);
- test.set("Fie", 1, 2.2);
- test.set("Foe", 2, 3.3);
- assertEquals("Fee", 1.1, test.get("Fee"));
- assertEquals("Fie", 2.2, test.get("Fie"));
- assertEquals("Foe", 3.3, test.get("Foe"));
- }
-
public void testHashCodeEquivalence() {
// Hash codes must be equal if the vectors are considered equal
Vector sparseLeft = new RandomAccessSparseVector(3);