You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by to...@apache.org on 2012/07/10 12:12:11 UTC
svn commit: r1359571 - in
/hama/trunk/ml/src/main/java/org/apache/hama/ml/writable: ./
MatrixWritable.java VectorWritable.java
Author: tommaso
Date: Tue Jul 10 10:12:11 2012
New Revision: 1359571
URL: http://svn.apache.org/viewvc?rev=1359571&view=rev
Log:
[HAMA-604] - adding writable package from Thomas commons package
Added:
hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/
hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/MatrixWritable.java
hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/VectorWritable.java
Added: 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=1359571&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/MatrixWritable.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/MatrixWritable.java Tue Jul 10 10:12:11 2012
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.writable;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.ml.math.DenseDoubleMatrix;
+import org.apache.hama.ml.math.DoubleMatrix;
+
+/**
+ * Majorly designed for dense matrices, can be extended for sparse ones as well.
+ */
+public final class MatrixWritable implements Writable {
+
+ private DoubleMatrix mat;
+
+ public MatrixWritable() {
+ }
+
+ public MatrixWritable(DoubleMatrix mat) {
+ this.mat = 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
Added: 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=1359571&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/VectorWritable.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/VectorWritable.java Tue Jul 10 10:12:11 2012
@@ -0,0 +1,166 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.writable;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.Iterator;
+
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hama.ml.math.DoubleVector;
+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
+ */
+public final class VectorWritable implements WritableComparable<VectorWritable> {
+
+ 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