You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by tj...@apache.org on 2012/07/15 19:08:37 UTC
svn commit: r1361734 [2/2] - in
/hama/trunk/ml/src/main/java/org/apache/hama/ml: math/ writable/
Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java Sun Jul 15 17:08:36 2012
@@ -22,272 +22,272 @@ import java.util.Iterator;
/**
* Vector with doubles. Some of the operations are mutable, unlike the apply and
* math functions, they return a fresh instance every time.
- *
+ *
*/
public interface DoubleVector {
- /**
- * Retrieves the value at given index.
- *
- * @param index the index.
- * @return a double value at the index.
- */
- public double get(int index);
-
- /**
- * Get the length of a vector, for sparse instance it is the actual length.
- * (not the dimension!) Always a constant time operation.
- *
- * @return the length of the vector.
- */
- public int getLength();
-
- /**
- * Get the dimension of a vector, for dense instance it is the same like the
- * length, for sparse instances it is usually not the same. Always a constant
- * time operation.
- *
- * @return the dimension of the vector.
- */
- public int getDimension();
-
- /**
- * Set a value at the given index.
- *
- * @param index the index of the vector to set.
- * @param value the value at the index of the vector to set.
- */
- public void set(int index, double value);
-
- /**
- * Apply a given {@link DoubleVectorFunction} to this vector and return a new
- * one.
- *
- * @param func the function to apply.
- * @return a new vector with the applied function.
- */
- public DoubleVector apply(DoubleVectorFunction func);
-
- /**
- * Apply a given {@link DoubleDoubleVectorFunction} to this vector and the
- * other given vector.
- *
- * @param other the other vector.
- * @param func the function to apply on this and the other vector.
- * @return a new vector with the result of the function of the two vectors.
- */
- public DoubleVector apply(DoubleVector other, DoubleDoubleVectorFunction func);
-
- /**
- * Adds the given {@link DoubleVector} to this vector.
- *
- * @param v the other vector.
- * @return a new vector with the sum of both vectors at each element index.
- */
- public DoubleVector add(DoubleVector v);
-
- /**
- * Adds the given scalar to this vector.
- *
- * @param scalar the scalar.
- * @return a new vector with the result at each element index.
- */
- public DoubleVector add(double scalar);
-
- /**
- * Subtracts this vector by the given {@link DoubleVector}.
- *
- * @param v the other vector.
- * @return a new vector with the difference of both vectors.
- */
- public DoubleVector subtract(DoubleVector v);
-
- /**
- * Subtracts the given scalar to this vector. (vector - scalar).
- *
- * @param scalar the scalar.
- * @return a new vector with the result at each element index.
- */
- public DoubleVector subtract(double scalar);
-
- /**
- * Subtracts the given scalar from this vector. (scalar - vector).
- *
- * @param scalar the scalar.
- * @return a new vector with the result at each element index.
- */
- public DoubleVector subtractFrom(double scalar);
-
- /**
- * Multiplies the given scalar to this vector.
- *
- * @param scalar the scalar.
- * @return a new vector with the result of the operation.
- */
- public DoubleVector multiply(double scalar);
-
- /**
- * Multiplies the given {@link DoubleVector} with this vector.
- *
- * @param vector the other vector.
- * @return a new vector with the result of the operation.
- */
- public DoubleVector multiply(DoubleVector vector);
-
- /**
- * Divides this vector by the given scalar. (= vector/scalar).
- *
- * @param scalar the given scalar.
- * @return a new vector with the result of the operation.
- */
- public DoubleVector divide(double scalar);
-
- /**
- * Divides the given scalar by this vector. (= scalar/vector).
- *
- * @param scalar the given scalar.
- * @return a new vector with the result of the operation.
- */
- public DoubleVector divideFrom(double scalar);
-
- /**
- * Powers this vector by the given amount. (=vector^x).
- *
- * @param x the given exponent.
- * @return a new vector with the result of the operation.
- */
- public DoubleVector pow(int x);
-
- /**
- * Absolutes the vector at each element.
- *
- * @return a new vector that does not contain negative values anymore.
- */
- public DoubleVector abs();
-
- /**
- * Square-roots each element.
- *
- * @return a new vector.
- */
- public DoubleVector sqrt();
-
- /**
- * @return the sum of all elements in this vector.
- */
- public double sum();
-
- /**
- * Calculates the dot product between this vector and the given vector.
- *
- * @param s the given vector s.
- * @return the dot product as a double.
- */
- public double dot(DoubleVector s);
-
- /**
- * Slices this vector from index 0 to the given length.
- *
- * @param length must be > 0 and smaller than the dimension of the vector.
- * @return a new vector that is only length long.
- */
- public DoubleVector slice(int length);
-
- /**
- * Slices this vector from index offset with the given length. So you end at
- * the upper bound of (offset+length).
- *
- * @param offset must be > 0 and smaller than the dimension of the vector
- * @param length must be > 0 and smaller than the dimension of the vector.
- * This must be greater than the offset.
- * @return a new vector that is only (length) long.
- */
- public DoubleVector slice(int offset, int length);
-
- /**
- * @return the maximum element value in this vector.
- */
- public double max();
-
- /**
- * @return the minimum element value in this vector.
- */
- public double min();
-
- /**
- * @return an array representation of this vector.
- */
- public double[] toArray();
-
- /**
- * @return a fresh new copy of this vector, copies all elements to a new
- * vector. (Does not reuse references or stuff).
- */
- public DoubleVector deepCopy();
-
- /**
- * @return an iterator that only iterates over non zero elements.
- */
- public Iterator<DoubleVectorElement> iterateNonZero();
-
- /**
- * @return an iterator that iterates over all elements.
- */
- public Iterator<DoubleVectorElement> iterate();
-
- /**
- * @return true if this instance is a sparse vector. Smarter and faster than
- * instanceof.
- */
- public boolean isSparse();
-
- /**
- * @return true if this instance is a named vector.Smarter and faster than
- * instanceof.
- */
- public boolean isNamed();
-
- /**
- * @return If this vector is a named instance, this will return its name. Or
- * null if this is not a named instance.
- *
- */
- public String getName();
-
- /**
- * Class for iteration of elements, consists of an index and a value at this
- * index. Can be reused for performance purposes.
- */
- public static final class DoubleVectorElement {
-
- private int index;
- private double value;
-
- public DoubleVectorElement() {
- super();
- }
-
- public DoubleVectorElement(int index, double value) {
- super();
- this.index = index;
- this.value = value;
- }
-
- public final int getIndex() {
- return index;
- }
-
- public final double getValue() {
- return value;
- }
-
- public final void setIndex(int in) {
- this.index = in;
- }
-
- public final void setValue(double in) {
- this.value = in;
- }
+ /**
+ * Retrieves the value at given index.
+ *
+ * @param index the index.
+ * @return a double value at the index.
+ */
+ public double get(int index);
+
+ /**
+ * Get the length of a vector, for sparse instance it is the actual length.
+ * (not the dimension!) Always a constant time operation.
+ *
+ * @return the length of the vector.
+ */
+ public int getLength();
+
+ /**
+ * Get the dimension of a vector, for dense instance it is the same like the
+ * length, for sparse instances it is usually not the same. Always a constant
+ * time operation.
+ *
+ * @return the dimension of the vector.
+ */
+ public int getDimension();
+
+ /**
+ * Set a value at the given index.
+ *
+ * @param index the index of the vector to set.
+ * @param value the value at the index of the vector to set.
+ */
+ public void set(int index, double value);
+
+ /**
+ * Apply a given {@link DoubleVectorFunction} to this vector and return a new
+ * one.
+ *
+ * @param func the function to apply.
+ * @return a new vector with the applied function.
+ */
+ public DoubleVector apply(DoubleVectorFunction func);
+
+ /**
+ * Apply a given {@link DoubleDoubleVectorFunction} to this vector and the
+ * other given vector.
+ *
+ * @param other the other vector.
+ * @param func the function to apply on this and the other vector.
+ * @return a new vector with the result of the function of the two vectors.
+ */
+ public DoubleVector apply(DoubleVector other, DoubleDoubleVectorFunction func);
+
+ /**
+ * Adds the given {@link DoubleVector} to this vector.
+ *
+ * @param v the other vector.
+ * @return a new vector with the sum of both vectors at each element index.
+ */
+ public DoubleVector add(DoubleVector v);
+
+ /**
+ * Adds the given scalar to this vector.
+ *
+ * @param scalar the scalar.
+ * @return a new vector with the result at each element index.
+ */
+ public DoubleVector add(double scalar);
+
+ /**
+ * Subtracts this vector by the given {@link DoubleVector}.
+ *
+ * @param v the other vector.
+ * @return a new vector with the difference of both vectors.
+ */
+ public DoubleVector subtract(DoubleVector v);
+
+ /**
+ * Subtracts the given scalar to this vector. (vector - scalar).
+ *
+ * @param scalar the scalar.
+ * @return a new vector with the result at each element index.
+ */
+ public DoubleVector subtract(double scalar);
+
+ /**
+ * Subtracts the given scalar from this vector. (scalar - vector).
+ *
+ * @param scalar the scalar.
+ * @return a new vector with the result at each element index.
+ */
+ public DoubleVector subtractFrom(double scalar);
+
+ /**
+ * Multiplies the given scalar to this vector.
+ *
+ * @param scalar the scalar.
+ * @return a new vector with the result of the operation.
+ */
+ public DoubleVector multiply(double scalar);
+
+ /**
+ * Multiplies the given {@link DoubleVector} with this vector.
+ *
+ * @param vector the other vector.
+ * @return a new vector with the result of the operation.
+ */
+ public DoubleVector multiply(DoubleVector vector);
+
+ /**
+ * Divides this vector by the given scalar. (= vector/scalar).
+ *
+ * @param scalar the given scalar.
+ * @return a new vector with the result of the operation.
+ */
+ public DoubleVector divide(double scalar);
+
+ /**
+ * Divides the given scalar by this vector. (= scalar/vector).
+ *
+ * @param scalar the given scalar.
+ * @return a new vector with the result of the operation.
+ */
+ public DoubleVector divideFrom(double scalar);
+
+ /**
+ * Powers this vector by the given amount. (=vector^x).
+ *
+ * @param x the given exponent.
+ * @return a new vector with the result of the operation.
+ */
+ public DoubleVector pow(int x);
+
+ /**
+ * Absolutes the vector at each element.
+ *
+ * @return a new vector that does not contain negative values anymore.
+ */
+ public DoubleVector abs();
+
+ /**
+ * Square-roots each element.
+ *
+ * @return a new vector.
+ */
+ public DoubleVector sqrt();
+
+ /**
+ * @return the sum of all elements in this vector.
+ */
+ public double sum();
+
+ /**
+ * Calculates the dot product between this vector and the given vector.
+ *
+ * @param s the given vector s.
+ * @return the dot product as a double.
+ */
+ public double dot(DoubleVector s);
+
+ /**
+ * Slices this vector from index 0 to the given length.
+ *
+ * @param length must be > 0 and smaller than the dimension of the vector.
+ * @return a new vector that is only length long.
+ */
+ public DoubleVector slice(int length);
+
+ /**
+ * Slices this vector from index offset with the given length. So you end at
+ * the upper bound of (offset+length).
+ *
+ * @param offset must be > 0 and smaller than the dimension of the vector
+ * @param length must be > 0 and smaller than the dimension of the vector.
+ * This must be greater than the offset.
+ * @return a new vector that is only (length) long.
+ */
+ public DoubleVector slice(int offset, int length);
+
+ /**
+ * @return the maximum element value in this vector.
+ */
+ public double max();
+
+ /**
+ * @return the minimum element value in this vector.
+ */
+ public double min();
+
+ /**
+ * @return an array representation of this vector.
+ */
+ public double[] toArray();
+
+ /**
+ * @return a fresh new copy of this vector, copies all elements to a new
+ * vector. (Does not reuse references or stuff).
+ */
+ public DoubleVector deepCopy();
+
+ /**
+ * @return an iterator that only iterates over non zero elements.
+ */
+ public Iterator<DoubleVectorElement> iterateNonZero();
+
+ /**
+ * @return an iterator that iterates over all elements.
+ */
+ public Iterator<DoubleVectorElement> iterate();
+
+ /**
+ * @return true if this instance is a sparse vector. Smarter and faster than
+ * instanceof.
+ */
+ public boolean isSparse();
+
+ /**
+ * @return true if this instance is a named vector.Smarter and faster than
+ * instanceof.
+ */
+ public boolean isNamed();
+
+ /**
+ * @return If this vector is a named instance, this will return its name. Or
+ * null if this is not a named instance.
+ *
+ */
+ public String getName();
+
+ /**
+ * Class for iteration of elements, consists of an index and a value at this
+ * index. Can be reused for performance purposes.
+ */
+ public static final class DoubleVectorElement {
+
+ private int index;
+ private double value;
+
+ public DoubleVectorElement() {
+ super();
+ }
+
+ public DoubleVectorElement(int index, double value) {
+ super();
+ this.index = index;
+ this.value = value;
+ }
+
+ public final int getIndex() {
+ return index;
+ }
+
+ public final double getValue() {
+ return value;
+ }
+
+ public final void setIndex(int in) {
+ this.index = in;
+ }
+
+ public final void setValue(double in) {
+ this.value = in;
}
+ }
-}
\ No newline at end of file
+}
Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java Sun Jul 15 17:08:36 2012
@@ -23,9 +23,9 @@ package org.apache.hama.ml.math;
*/
public interface DoubleVectorFunction {
- /**
- * Calculates the result with a given index and value of a vector.
- */
- public double calculate(int index, double value);
+ /**
+ * Calculates the result with a given index and value of a vector.
+ */
+ public double calculate(int index, double value);
-}
\ No newline at end of file
+}
Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java Sun Jul 15 17:08:36 2012
@@ -22,64 +22,64 @@ package org.apache.hama.ml.math;
* equals and comparable via the first element.
*/
public final class Tuple<FIRST, SECOND> implements
- Comparable<Tuple<FIRST, SECOND>> {
+ Comparable<Tuple<FIRST, SECOND>> {
- private final FIRST first;
- private final SECOND second;
+ private final FIRST first;
+ private final SECOND second;
- public Tuple(FIRST first, SECOND second) {
- super();
- this.first = first;
- this.second = second;
- }
-
- public final FIRST getFirst() {
- return first;
- }
-
- public final SECOND getSecond() {
- return second;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((first == null) ? 0 : first.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- @SuppressWarnings("rawtypes")
- Tuple other = (Tuple) obj;
- if (first == null) {
- if (other.first != null)
- return false;
- } else if (!first.equals(other.first))
- return false;
- return true;
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public int compareTo(Tuple<FIRST, SECOND> o) {
- if (o.getFirst() instanceof Comparable && getFirst() instanceof Comparable) {
- return ((Comparable<FIRST>) getFirst()).compareTo(o.getFirst());
- } else {
- return 0;
- }
- }
-
- @Override
- public String toString() {
- return "Tuple [first=" + first + ", second=" + second + "]";
- }
+ public Tuple(FIRST first, SECOND second) {
+ super();
+ this.first = first;
+ this.second = second;
+ }
+
+ public final FIRST getFirst() {
+ return first;
+ }
+
+ public final SECOND getSecond() {
+ return second;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((first == null) ? 0 : first.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ @SuppressWarnings("rawtypes")
+ Tuple other = (Tuple) obj;
+ if (first == null) {
+ if (other.first != null)
+ return false;
+ } else if (!first.equals(other.first))
+ return false;
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public int compareTo(Tuple<FIRST, SECOND> o) {
+ if (o.getFirst() instanceof Comparable && getFirst() instanceof Comparable) {
+ return ((Comparable<FIRST>) getFirst()).compareTo(o.getFirst());
+ } else {
+ return 0;
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "Tuple [first=" + first + ", second=" + second + "]";
+ }
-}
\ No newline at end of file
+}
Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/MatrixWritable.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/MatrixWritable.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/MatrixWritable.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/MatrixWritable.java Sun Jul 15 17:08:36 2012
@@ -30,44 +30,44 @@ import org.apache.hama.ml.math.DoubleMat
*/
public final class MatrixWritable implements Writable {
- private DoubleMatrix mat;
+ private DoubleMatrix mat;
- public MatrixWritable() {
- }
-
- public MatrixWritable(DoubleMatrix mat) {
- this.mat = mat;
+ public MatrixWritable() {
+ }
- }
-
- @Override
- public void readFields(DataInput in) throws IOException {
- mat = read(in);
- }
-
- @Override
- public void write(DataOutput out) throws IOException {
- write(mat, out);
- }
-
- public static void write(DoubleMatrix mat, DataOutput out) throws IOException {
- out.writeInt(mat.getRowCount());
- out.writeInt(mat.getColumnCount());
- for (int row = 0; row < mat.getRowCount(); row++) {
- for (int col = 0; col < mat.getColumnCount(); col++) {
- out.writeDouble(mat.get(row, col));
- }
- }
- }
+ public MatrixWritable(DoubleMatrix mat) {
+ this.mat = mat;
- public static DoubleMatrix read(DataInput in) throws IOException {
- DoubleMatrix mat = new DenseDoubleMatrix(in.readInt(), in.readInt());
- for (int row = 0; row < mat.getRowCount(); row++) {
- for (int col = 0; col < mat.getColumnCount(); col++) {
- mat.set(row, col, in.readDouble());
- }
- }
- return mat;
+ }
+
+ @Override
+ public void readFields(DataInput in) throws IOException {
+ mat = read(in);
+ }
+
+ @Override
+ public void write(DataOutput out) throws IOException {
+ write(mat, out);
+ }
+
+ public static void write(DoubleMatrix mat, DataOutput out) throws IOException {
+ out.writeInt(mat.getRowCount());
+ out.writeInt(mat.getColumnCount());
+ for (int row = 0; row < mat.getRowCount(); row++) {
+ for (int col = 0; col < mat.getColumnCount(); col++) {
+ out.writeDouble(mat.get(row, col));
+ }
+ }
+ }
+
+ public static DoubleMatrix read(DataInput in) throws IOException {
+ DoubleMatrix mat = new DenseDoubleMatrix(in.readInt(), in.readInt());
+ for (int row = 0; row < mat.getRowCount(); row++) {
+ for (int col = 0; col < mat.getColumnCount(); col++) {
+ mat.set(row, col, in.readDouble());
+ }
}
+ return mat;
+ }
-}
\ No newline at end of file
+}
Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/VectorWritable.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/VectorWritable.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/VectorWritable.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/VectorWritable.java Sun Jul 15 17:08:36 2012
@@ -27,140 +27,122 @@ import org.apache.hama.ml.math.DoubleVec
import org.apache.hama.ml.math.DenseDoubleVector;
/**
- * New and updated VectorWritable class that has all the other fancy
- * combinations of vectors that are possible in my math library.<br/>
- * This class is not compatible to the one in the clustering package that has a
- * totally other byte alignment in binary files.
- *
- * @author thomas.jungblut
+ * Writable for dense vectors.
*/
public final class VectorWritable implements WritableComparable<VectorWritable> {
- private DoubleVector vector;
+ private DoubleVector vector;
- public VectorWritable() {
- super();
- }
-
- public VectorWritable(VectorWritable v) {
- this.vector = v.getVector();
- }
-
- public VectorWritable(DoubleVector v) {
- this.vector = v;
- }
-
- @Override
- public final void write(DataOutput out) throws IOException {
- writeVector(this.vector, out);
- }
-
- @Override
- public final void readFields(DataInput in) throws IOException {
- this.vector = readVector(in);
- }
-
- @Override
- public final int compareTo(VectorWritable o) {
- return compareVector(this, o);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((vector == null) ? 0 : vector.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- VectorWritable other = (VectorWritable) obj;
- if (vector == null) {
- if (other.vector != null)
- return false;
- } else if (!vector.equals(other.vector))
- return false;
- return true;
- }
-
- /**
- * @return the embedded vector
- */
- public DoubleVector getVector() {
- return vector;
- }
-
- @Override
- public String toString() {
- return vector.toString();
- }
-
- public static void writeVector(DoubleVector vector, DataOutput out)
- throws IOException {
- out.writeBoolean(vector.isSparse());
- out.writeInt(vector.getLength());
- if (vector.isSparse()) {
- out.writeInt(vector.getDimension());
- Iterator<DoubleVector.DoubleVectorElement> iterateNonZero = vector.iterateNonZero();
- while (iterateNonZero.hasNext()) {
- DoubleVector.DoubleVectorElement next = iterateNonZero.next();
- out.writeInt(next.getIndex());
- out.writeDouble(next.getValue());
- }
- } else {
- for (int i = 0; i < vector.getDimension(); i++) {
- out.writeDouble(vector.get(i));
- }
- }
- if (vector.isNamed() && vector.getName() != null) {
- out.writeBoolean(true);
- out.writeUTF(vector.getName());
- } else {
- out.writeBoolean(false);
- }
- }
-
- public static DoubleVector readVector(DataInput in) throws IOException {
-// boolean sparse = in.readBoolean();
- int length = in.readInt();
- DoubleVector vector;
-// if (sparse) {
-// int dim = in.readInt();
-// vector = new SparseDoubleVector(dim);
-// for (int i = 0; i < length; i++) {
-// int index = in.readInt();
-// double value = in.readDouble();
-// vector.set(index, value);
-// }
-// } else {
- vector = new DenseDoubleVector(length);
- for (int i = 0; i < length; i++) {
- vector.set(i, in.readDouble());
- }
-// }
-// if (in.readBoolean()) {
-// vector = new NamedDoubleVector(in.readUTF(), vector);
-// }
- return vector;
- }
-
- public static int compareVector(VectorWritable a, VectorWritable o) {
- return compareVector(a.getVector(), o.getVector());
- }
-
- public static int compareVector(DoubleVector a, DoubleVector o) {
- DoubleVector subtract = a.subtract(o);
- return (int) subtract.sum();
- }
-
- public static VectorWritable wrap(DoubleVector a) {
- return new VectorWritable(a);
- }
-}
\ No newline at end of file
+ public VectorWritable() {
+ super();
+ }
+
+ public VectorWritable(VectorWritable v) {
+ this.vector = v.getVector();
+ }
+
+ public VectorWritable(DoubleVector v) {
+ this.vector = v;
+ }
+
+ @Override
+ public final void write(DataOutput out) throws IOException {
+ writeVector(this.vector, out);
+ }
+
+ @Override
+ public final void readFields(DataInput in) throws IOException {
+ this.vector = readVector(in);
+ }
+
+ @Override
+ public final int compareTo(VectorWritable o) {
+ return compareVector(this, o);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((vector == null) ? 0 : vector.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ VectorWritable other = (VectorWritable) obj;
+ if (vector == null) {
+ if (other.vector != null)
+ return false;
+ } else if (!vector.equals(other.vector))
+ return false;
+ return true;
+ }
+
+ /**
+ * @return the embedded vector
+ */
+ public DoubleVector getVector() {
+ return vector;
+ }
+
+ @Override
+ public String toString() {
+ return vector.toString();
+ }
+
+ public static void writeVector(DoubleVector vector, DataOutput out)
+ throws IOException {
+ out.writeBoolean(vector.isSparse());
+ out.writeInt(vector.getLength());
+ if (vector.isSparse()) {
+ out.writeInt(vector.getDimension());
+ Iterator<DoubleVector.DoubleVectorElement> iterateNonZero = vector
+ .iterateNonZero();
+ while (iterateNonZero.hasNext()) {
+ DoubleVector.DoubleVectorElement next = iterateNonZero.next();
+ out.writeInt(next.getIndex());
+ out.writeDouble(next.getValue());
+ }
+ } else {
+ for (int i = 0; i < vector.getDimension(); i++) {
+ out.writeDouble(vector.get(i));
+ }
+ }
+ if (vector.isNamed() && vector.getName() != null) {
+ out.writeBoolean(true);
+ out.writeUTF(vector.getName());
+ } else {
+ out.writeBoolean(false);
+ }
+ }
+
+ public static DoubleVector readVector(DataInput in) throws IOException {
+ int length = in.readInt();
+ DoubleVector vector;
+ vector = new DenseDoubleVector(length);
+ for (int i = 0; i < length; i++) {
+ vector.set(i, in.readDouble());
+ }
+ return vector;
+ }
+
+ public static int compareVector(VectorWritable a, VectorWritable o) {
+ return compareVector(a.getVector(), o.getVector());
+ }
+
+ public static int compareVector(DoubleVector a, DoubleVector o) {
+ DoubleVector subtract = a.subtract(o);
+ return (int) subtract.sum();
+ }
+
+ public static VectorWritable wrap(DoubleVector a) {
+ return new VectorWritable(a);
+ }
+}