mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tdunn...@apache.org
Subject svn commit: r1023432 - in /mahout/trunk/math/src: main/java/org/apache/mahout/math/JsonMatrixAdapter.java main/java/org/apache/mahout/math/MatrixView.java test/java/org/apache/mahout/math/MatrixTest.java
Date Sun, 17 Oct 2010 07:55:11 GMT
Author: tdunning
Date: Sun Oct 17 07:55:10 2010
New Revision: 1023432

URL: http://svn.apache.org/viewvc?rev=1023432&view=rev
Log:
MAHOUT-528 - Added VectorList type of matrix for use in SVD code
Fixed serialization to not use strings inside JSON
Moved some checking to MatrixView

Modified:
    mahout/trunk/math/src/main/java/org/apache/mahout/math/JsonMatrixAdapter.java
    mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixView.java
    mahout/trunk/math/src/test/java/org/apache/mahout/math/MatrixTest.java

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/JsonMatrixAdapter.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/JsonMatrixAdapter.java?rev=1023432&r1=1023431&r2=1023432&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/JsonMatrixAdapter.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/JsonMatrixAdapter.java Sun Oct
17 07:55:10 2010
@@ -17,7 +17,6 @@
 package org.apache.mahout.math;
 
 import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.google.gson.JsonDeserializationContext;
 import com.google.gson.JsonDeserializer;
 import com.google.gson.JsonElement;
@@ -36,33 +35,31 @@ public class JsonMatrixAdapter implement
 
   public JsonElement serialize(Matrix src, Type typeOfSrc,
                                JsonSerializationContext context) {
-    GsonBuilder builder = new GsonBuilder();
-    builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
-    builder.registerTypeAdapter(Matrix.class, new JsonMatrixAdapter());
-    Gson gson = builder.create();
     JsonObject obj = new JsonObject();
     obj.add(CLASS, new JsonPrimitive(src.getClass().getName()));
-    obj.add(MATRIX, new JsonPrimitive(gson.toJson(src)));
+    obj.add(MATRIX, context.serialize(src));
     return obj;
   }
 
   public Matrix deserialize(JsonElement json, Type typeOfT,
                             JsonDeserializationContext context) {
-    GsonBuilder builder = new GsonBuilder();
-    builder.registerTypeAdapter(Vector.class, new JsonVectorAdapter());
-    builder.registerTypeAdapter(Matrix.class, new JsonMatrixAdapter());
-    Gson gson = builder.create();
     JsonObject obj = json.getAsJsonObject();
     String klass = obj.get(CLASS).getAsString();
-    String matrix = obj.get(MATRIX).getAsString();
     ClassLoader ccl = Thread.currentThread().getContextClassLoader();
-    Class<?> cl;
+    Class<? extends Matrix> cl;
     try {
-      cl = ccl.loadClass(klass);
+      cl = (Class<? extends Matrix>) ccl.loadClass(klass);
     } catch (ClassNotFoundException e) {
       throw new JsonParseException(e);
     }
-    return (Matrix) gson.fromJson(matrix, cl);
+
+    if (obj.get(MATRIX).isJsonPrimitive()) {
+      String matrix = obj.get(MATRIX).getAsString();
+      Gson gson = AbstractMatrix.gson();
+      return gson.fromJson(matrix, cl);
+    } else {
+      return context.deserialize(obj.get(MATRIX), cl);
+    }
   }
 
 }

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixView.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixView.java?rev=1023432&r1=1023431&r2=1023432&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixView.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/MatrixView.java Sun Oct 17 07:55:10
2010
@@ -33,12 +33,31 @@ public class MatrixView extends Abstract
    *
    * @param matrix      an underlying Matrix
    * @param offset      the int[2] offset into the underlying matrix
-   * @param cardinality the int[2] cardinality of the view
+   * @param size        the int[2] size of the view
    */
-  public MatrixView(Matrix matrix, int[] offset, int[] cardinality) {
+  public MatrixView(Matrix matrix, int[] offset, int[] size) {
+    int rowOffset = offset[ROW];
+    if (rowOffset < 0) {
+      throw new IndexException(rowOffset, rowSize());
+    }
+
+    int rowsRequested = size[ROW];
+    if (rowOffset + rowsRequested > matrix.rowSize()) {
+      throw new IndexException(rowOffset + rowsRequested, matrix.rowSize());
+    }
+
+    int columnOffset = offset[COL];
+    if (columnOffset < 0) {
+      throw new IndexException(columnOffset, columnSize());
+    }
+
+    int columnsRequested = size[COL];
+    if (columnOffset + columnsRequested > matrix.columnSize()) {
+      throw new IndexException(columnOffset + columnsRequested, matrix.columnSize());
+    }
     this.matrix = matrix;
     this.offset = offset;
-    this.cardinality = Arrays.copyOf(cardinality, 2);
+    this.cardinality = Arrays.copyOf(size, 2);
   }
 
   @Override

Modified: mahout/trunk/math/src/test/java/org/apache/mahout/math/MatrixTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/math/MatrixTest.java?rev=1023432&r1=1023431&r2=1023432&view=diff
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/math/MatrixTest.java (original)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/math/MatrixTest.java Sun Oct 17 07:55:10
2010
@@ -134,6 +134,8 @@ public abstract class MatrixTest extends
     int[] offset = {1, 1};
     int[] size = {2, 1};
     Matrix view = test.viewPart(offset, size);
+    assertEquals(2, view.rowSize());
+    assertEquals(1, view.columnSize());
     int[] c = view.size();
     for (int row = 0; row < c[ROW]; row++) {
       for (int col = 0; col < c[COL]; col++) {



Mime
View raw message