You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ka...@apache.org on 2008/04/17 02:47:49 UTC
svn commit: r648908 - in /lucene/mahout/trunk/src:
main/java/org/apache/mahout/matrix/ test/java/org/apache/mahout/matrix/
Author: kalle
Date: Wed Apr 16 17:47:42 2008
New Revision: 648908
URL: http://svn.apache.org/viewvc?rev=648908&view=rev
Log:
MAHOUT-41, VectorWritable
Added:
lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVectorWritable.java
lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVectorWritable.java
lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorWritable.java
lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorWritable.java
Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVectorWritable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVectorWritable.java?rev=648908&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVectorWritable.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVectorWritable.java Wed Apr 16 17:47:42 2008
@@ -0,0 +1,72 @@
+package org.apache.mahout.matrix;
+
+/* 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.
+ */
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+public class DenseVectorWritable implements VectorWritable {
+
+ private DenseVector vector;
+
+
+ public DenseVectorWritable() {
+ }
+
+
+ public DenseVectorWritable(Vector vector) {
+ set(vector);
+ }
+
+
+ public void write(DataOutput dataOutput) throws IOException {
+ dataOutput.writeInt(vector.size());
+ for (Vector.Element element : vector) {
+ dataOutput.writeDouble(element.get());
+ }
+ }
+
+ public void readFields(DataInput dataInput) throws IOException {
+ int cardinality = dataInput.readInt();
+ if (vector == null || vector.cardinality() != cardinality) {
+ vector = new DenseVector(cardinality);
+ }
+ for (int i = 0; i < cardinality; i++) {
+ vector.set(i, dataInput.readDouble());
+ }
+ }
+
+ public DenseVector get() {
+ return vector;
+ }
+
+ public void set(Vector vector) {
+ if (vector == null) {
+ this.vector = null;
+ } else {
+ this.vector = new DenseVector(vector.toArray());
+ }
+ }
+
+ public void set(DenseVector denseVector) {
+ this.vector = denseVector;
+ }
+
+}
+
Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVectorWritable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVectorWritable.java?rev=648908&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVectorWritable.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVectorWritable.java Wed Apr 16 17:47:42 2008
@@ -0,0 +1,77 @@
+package org.apache.mahout.matrix;
+
+/* 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.
+ */
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+
+public class SparseVectorWritable implements VectorWritable {
+
+ private SparseVector vector;
+
+ public SparseVectorWritable() {
+ }
+
+ public SparseVectorWritable(Vector vector) {
+ set(vector);
+ }
+
+ public void write(DataOutput dataOutput) throws IOException {
+ dataOutput.writeInt(vector.cardinality());
+ dataOutput.writeInt(vector.size());
+ for (Vector.Element element : vector) {
+ if (element.get() != 0d) {
+ dataOutput.writeInt(element.index());
+ dataOutput.writeDouble(element.get());
+ }
+ }
+ }
+
+ public void readFields(DataInput dataInput) throws IOException {
+ int cardinality = dataInput.readInt();
+ if (vector == null || vector.cardinality() != cardinality) {
+ vector = new SparseVector(cardinality);
+ }
+ int size = dataInput.readInt();
+ for (int i = 0; i < size; i++) {
+ vector.set(dataInput.readInt(), dataInput.readDouble());
+ }
+ }
+
+ public Vector get() {
+ return vector;
+ }
+
+
+ public void set(Vector vector) {
+ if (vector == null) {
+ this.vector = null;
+ } else {
+ this.vector = new SparseVector(vector.cardinality());
+ for (Vector.Element e : vector) {
+ this.vector.set(e.index(), e.get());
+ }
+ }
+ }
+
+ public void set(SparseVector vector) {
+ this.vector = vector;
+ }
+}
Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorWritable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorWritable.java?rev=648908&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorWritable.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorWritable.java Wed Apr 16 17:47:42 2008
@@ -0,0 +1,28 @@
+package org.apache.mahout.matrix;
+
+/* 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.
+ */
+
+import org.apache.hadoop.io.Writable;
+
+public interface VectorWritable extends Writable {
+
+ public abstract Vector get();
+ public abstract void set(Vector vector);
+
+
+}
Added: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorWritable.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorWritable.java?rev=648908&view=auto
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorWritable.java (added)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorWritable.java Wed Apr 16 17:47:42 2008
@@ -0,0 +1,66 @@
+package org.apache.mahout.matrix;
+
+import junit.framework.TestCase;
+import org.apache.hadoop.io.DataOutputBuffer;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+
+/**
+ * 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.
+ */
+
+public class TestVectorWritable extends TestCase {
+
+ private static final int cardinality = 10;
+
+ public void test(VectorWritable writable) throws Exception {
+ for (int i = 0; i < cardinality; i++) {
+ writable.get().set(i, i);
+ }
+ DataOutputBuffer out = new DataOutputBuffer();
+ writable.write(out);
+ out.close();
+
+ DataInputStream in = new DataInputStream(new ByteArrayInputStream(out.getData()));
+ writable.readFields(in);
+ in.close();
+
+ assertEquals(cardinality, writable.get().cardinality());
+ for (int i = 0; i < cardinality; i++) {
+ assertEquals((double)i, writable.get().get(i));
+ }
+
+ // also make sure it creates the vector correct even if it is not set.
+ writable.set(null);
+
+ in = new DataInputStream(new ByteArrayInputStream(out.getData()));
+ writable.readFields(in);
+ in.close();
+
+ assertEquals(cardinality, writable.get().cardinality());
+ for (int i = 0; i < cardinality; i++) {
+ assertEquals((double)i, writable.get().get(i));
+ }
+
+
+ }
+
+ public void test() throws Exception {
+ test(new SparseVectorWritable(new SparseVector(cardinality)));
+ test(new DenseVectorWritable(new DenseVector(cardinality)));
+ }
+}