xuyang1706 commented on a change in pull request #8631: [FLINK-12745][ml] add 
sparse and dense vector class, and dense matrix class with basic operations.
URL: https://github.com/apache/flink/pull/8631#discussion_r296091739
 
 

 ##########
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/Vector.java
 ##########
 @@ -0,0 +1,298 @@
+/*
+ * 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.flink.ml.common.matrix;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.Serializable;
+
+/**
+ * Base class of vector.
+ */
+public abstract class Vector implements Serializable {
+       private static final long serialVersionUID = 7690668399245109611L;
+
+       public Vector() {
+       }
+
+       public static DenseVector dense(String str) {
+               return DenseVector.deserialize(str);
+       }
+
+       public static SparseVector sparse(String str) {
+               return SparseVector.deserialize(str);
+       }
+
+       public static Tuple2 <int[], float[]> parseSparseTensor(String str) {
+               int numValues = 1;
+               for (int i = 0; i < str.length(); i++) {
+                       if (str.charAt(i) == ',') {
+                               numValues++;
+                       }
+               }
+               int[] indices = new int[numValues];
+               float[] values = new float[numValues];
+
+               int startPos = StringUtils.lastIndexOf(str, '$') + 1;
+               int endPos = -1;
+               int delimiterPos;
+
+               for (int i = 0; i < numValues; i++) {
+                       // extract the value string
+                       endPos = StringUtils.indexOf(str, ',', startPos);
+                       if (endPos == -1) {
+                               endPos = str.length();
+                       }
+                       delimiterPos = StringUtils.indexOf(str, ':', startPos);
+                       if (delimiterPos == -1) {
+                               throw new RuntimeException("invalid data: " + 
str);
+                       }
+                       indices[i] = Integer.valueOf(StringUtils.substring(str, 
startPos, delimiterPos));
+                       values[i] = Float.valueOf(StringUtils.substring(str, 
delimiterPos + 1, endPos));
+                       startPos = endPos + 1;
+               }
+
+               return Tuple2.of(indices, values);
+       }
+
+       public static boolean isSparse(String str) {
+               if 
(org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) {
+                       return true;
+               }
+               return StringUtils.indexOf(str, ':') != -1 || 
StringUtils.indexOf(str, "$") != -1;
+       }
+
+       public static Vector deserialize(String str) {
+               Vector vec;
+               if (isSparse(str)) {
+                       vec = Vector.sparse(str);
+               } else {
+                       vec = Vector.dense(str);
+               }
+               return vec;
+       }
+
+       public static Vector plus(Vector vec1, Vector vec2) {
+               if (vec1 instanceof DenseVector && vec2 instanceof DenseVector) 
{
+                       return ((DenseVector) vec1).plus((DenseVector) vec2);
+               } else if (vec1 instanceof SparseVector && vec2 instanceof 
SparseVector) {
+                       return ((SparseVector) vec1).plus((SparseVector) vec2);
+               } else if (vec1 instanceof SparseVector && vec2 instanceof 
DenseVector) {
+                       return ((SparseVector) vec1).plus((DenseVector) vec2);
+               } else if (vec1 instanceof DenseVector && vec2 instanceof 
SparseVector) {
+                       return ((SparseVector) vec2).plus((DenseVector) vec1);
+               } else {
+                       throw new RuntimeException("Not implemented yet!");
+               }
+       }
+
+       public static Vector minus(Vector vec1, Vector vec2) {
+               if (vec1 instanceof DenseVector && vec2 instanceof DenseVector) 
{
+                       return ((DenseVector) vec1).minus((DenseVector) vec2);
+               } else if (vec1 instanceof SparseVector && vec2 instanceof 
SparseVector) {
+                       return ((SparseVector) vec1).minus((SparseVector) vec2);
+               } else if (vec1 instanceof SparseVector && vec2 instanceof 
DenseVector) {
+                       return ((SparseVector) vec1).minus((DenseVector) vec2);
+               } else if (vec1 instanceof DenseVector && vec2 instanceof 
SparseVector) {
+                       return ((SparseVector) 
vec2).scale(-1.0).plus((DenseVector) vec1);
+               } else {
+                       throw new RuntimeException("Not implemented yet!");
+               }
+       }
+
+       public static double dot(Vector vec1, Vector vec2) {
+               if (vec1 instanceof DenseVector && vec2 instanceof DenseVector) 
{
+                       return ((DenseVector) vec1).dot((DenseVector) vec2);
 
 Review comment:
   Thanks for your suggestion, we refactored the implementation.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to