qiuxiafei 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_r314243632
 
 

 ##########
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/linalg/MatVecOp.java
 ##########
 @@ -0,0 +1,321 @@
+/*
+ * 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.linalg;
+
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+/**
+ * A utility class that provides operations over {@link DenseVector}, {@link 
SparseVector}
+ * and {@link DenseMatrix}.
+ */
+public class MatVecOp {
+       /**
+        * compute vec1 + vec2 .
+        */
+       public static Vector plus(Vector vec1, Vector vec2) {
+               return vec1.plus(vec2);
+       }
+
+       /**
+        * compute vec1 - vec2 .
+        */
+       public static Vector minus(Vector vec1, Vector vec2) {
+               return vec1.minus(vec2);
+       }
+
+       /**
+        * Compute vec1 \cdot vec2 .
+        */
+       public static double dot(Vector vec1, Vector vec2) {
+               return vec1.dot(vec2);
+       }
+
+       /**
+        * Compute || vec1 - vec2 ||_1    .
+        */
+       public static double sumAbsDiff(Vector vec1, Vector vec2) {
+               if (vec1 instanceof DenseVector) {
+                       if (vec2 instanceof DenseVector) {
+                               return MatVecOp.applySum((DenseVector) vec1, 
(DenseVector) vec2, (a, b) -> Math.abs(a - b));
+                       } else {
+                               return MatVecOp.applySum((DenseVector) vec1, 
(SparseVector) vec2, (a, b) -> Math.abs(a - b));
+                       }
+               } else {
+                       if (vec2 instanceof DenseVector) {
+                               return MatVecOp.applySum((SparseVector) vec1, 
(DenseVector) vec2, (a, b) -> Math.abs(a - b));
+                       } else {
+                               return MatVecOp.applySum((SparseVector) vec1, 
(SparseVector) vec2, (a, b) -> Math.abs(a - b));
+                       }
+               }
+       }
+
+       /**
+        * Compute || vec1 - vec2 ||_2^2   .
+        */
+       public static double sumSquaredDiff(Vector vec1, Vector vec2) {
+               if (vec1 instanceof DenseVector) {
+                       if (vec2 instanceof DenseVector) {
+                               return MatVecOp.applySum((DenseVector) vec1, 
(DenseVector) vec2, (a, b) -> (a - b) * (a - b));
+                       } else {
+                               return MatVecOp.applySum((DenseVector) vec1, 
(SparseVector) vec2, (a, b) -> (a - b) * (a - b));
+                       }
+               } else {
+                       if (vec2 instanceof DenseVector) {
+                               return MatVecOp.applySum((SparseVector) vec1, 
(DenseVector) vec2, (a, b) -> (a - b) * (a - b));
+                       } else {
+                               return MatVecOp.applySum((SparseVector) vec1, 
(SparseVector) vec2, (a, b) -> (a - b) * (a - b));
+                       }
+               }
+       }
+
+       /**
+        * y = func(x).
+        */
+       public static void apply(DenseMatrix x, DenseMatrix y, Function<Double, 
Double> func) {
 
 Review comment:
   This is just a different style. 
   We can see that most interfaces in BLAS have no return value, instead, they 
ask the caller to pass a vector/matrix to receive the result. Since this is 
usually a computing hot point, efficiency is preferred to flexibility and API 
elegance.  So I vote to keep the origin design.
   

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to