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_r314708905
 
 

 ##########
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/linalg/DenseVector.java
 ##########
 @@ -0,0 +1,463 @@
+/*
+ * 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 org.apache.commons.lang3.StringUtils;
+
+import java.util.Arrays;
+import java.util.Random;
+
+/**
+ * A dense vector represented by a values array.
+ */
+public class DenseVector extends Vector {
+       /**
+        * The array holding the vector data.
+        * <p>
+        * Package private to allow access from {@link MatVecOp} and {@link 
BLAS}.
+        */
+       double[] data;
+
+       /**
+        * Create a zero size vector.
+        */
+       public DenseVector() {
+               this(0);
+       }
+
+       /**
+        * Create a size n vector with all elements zero.
+        *
+        * @param n Size of the vector.
+        */
+       public DenseVector(int n) {
+               this.data = new double[n];
+       }
+
+       /**
+        * Create a dense vector with the user provided data.
+        *
+        * @param data The vector data.
+        */
+       public DenseVector(double[] data) {
+               this.data = data;
+       }
+
+       /**
+        * Get the data array.
+        */
+       public double[] getData() {
+               return this.data;
+       }
+
+       /**
+        * Set the data array.
+        */
+       public void setData(double[] data) {
+               this.data = data;
+       }
+
+       /**
+        * Create a dense vector with all elements one.
+        *
+        * @param n Size of the vector.
+        * @return The newly created dense vector.
+        */
+       public static DenseVector ones(int n) {
+               DenseVector r = new DenseVector(n);
+               Arrays.fill(r.data, 1.0);
+               return r;
+       }
+
+       /**
+        * Create a dense vector with all elements zero.
+        *
+        * @param n Size of the vector.
+        * @return The newly created dense vector.
+        */
+       public static DenseVector zeros(int n) {
+               DenseVector r = new DenseVector(n);
+               Arrays.fill(r.data, 0.0);
+               return r;
+       }
+
+       /**
+        * Create a dense vector with random values uniformly distributed in 
the range of [0.0, 1.0].
+        *
+        * @param n Size of the vector.
+        * @return The newly created dense vector.
+        */
+       public static DenseVector rand(int n) {
+               Random random = new Random();
+               DenseVector v = new DenseVector(n);
+               for (int i = 0; i < n; i++) {
+                       v.data[i] = random.nextDouble();
+               }
+               return v;
+       }
+
+       /**
+        * Delimiter between elements.
+        */
+       private static final char ELEMENT_DELIMITER = ' ';
+
+       @Override
+       public String serialize() {
+               StringBuilder sbd = new StringBuilder();
+
+               for (int i = 0; i < data.length; i++) {
+                       sbd.append(data[i]);
+                       if (i < data.length - 1) {
+                               sbd.append(ELEMENT_DELIMITER);
+                       }
+               }
+               return sbd.toString();
+       }
+
+       /**
+        * Parse the dense vector from a formatted string.
+        *
+        * <p>The format of a dense vector is comma separated values such as "1 
2 3 4".
+        *
+        * @param str A string of space separated values.
+        * @return The parsed vector.
+        */
+       public static DenseVector deserialize(String str) {
+               if 
(org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) {
+                       return new DenseVector();
+               }
+
+               int len = str.length();
+
+               int inDataBuffPos = 0;
+               boolean isInBuff = false;
+
+               for (int i = 0; i < len; ++i) {
+                       if (str.charAt(i) == ELEMENT_DELIMITER) {
+                               if (isInBuff) {
+                                       inDataBuffPos++;
+                               }
+
+                               isInBuff = false;
+                       } else {
+                               isInBuff = true;
+                       }
+               }
+
+               if (isInBuff) {
+                       inDataBuffPos++;
+               }
+
+               double[] data = new double[inDataBuffPos];
+               int lastestInCharBuffPos = 0;
+
+               inDataBuffPos = 0;
+               isInBuff = false;
+
+               for (int i = 0; i < len; ++i) {
+                       if (str.charAt(i) == ELEMENT_DELIMITER) {
+                               if (isInBuff) {
+                                       data[inDataBuffPos++] = 
Double.parseDouble(
+                                               StringUtils.substring(str, 
lastestInCharBuffPos, i).trim()
+                                       );
+
+                                       lastestInCharBuffPos = i + 1;
+                               }
+
+                               isInBuff = false;
+                       } else {
+                               isInBuff = true;
+                       }
+               }
+
+               if (isInBuff) {
+                       data[inDataBuffPos] = Double.valueOf(
+                               StringUtils.substring(str, 
lastestInCharBuffPos).trim()
+                       );
+               }
+
+               return new DenseVector(data);
+       }
+
+       @Override
+       public DenseVector clone() {
+               return new DenseVector(this.data.clone());
+       }
+
+       @Override
+       public String toString() {
+               return this.serialize();
+       }
+
+       @Override
+       public int size() {
+               return data.length;
+       }
+
+       @Override
+       public double get(int i) {
+               return data[i];
+       }
+
+       @Override
+       public void set(int i, double d) {
+               data[i] = d;
+       }
+
+       @Override
+       public void add(int i, double d) {
+               data[i] += d;
+       }
+
+       @Override
+       public double normL1() {
+               double d = 0;
+               for (double t : data) {
+                       d += Math.abs(t);
+               }
+               return d;
+       }
+
+       @Override
+       public double normL2() {
+               double d = 0;
+               for (double t : data) {
+                       d += t * t;
+               }
+               return Math.sqrt(d);
+       }
+
+       @Override
+       public double normL2Square() {
+               double d = 0;
+               for (double t : data) {
+                       d += t * t;
+               }
+               return d;
+       }
+
+       @Override
+       public double normInf() {
+               double d = 0;
+               for (double t : data) {
+                       d = Math.max(Math.abs(t), d);
+               }
+               return d;
+       }
+
+       @Override
+       public DenseVector slice(int[] indices) {
+               double[] values = new double[indices.length];
+               for (int i = 0; i < indices.length; ++i) {
+                       if (indices[i] >= data.length) {
+                               throw new RuntimeException("Index is larger 
than vector size.");
+                       }
+                       values[i] = data[indices[i]];
+               }
+               return new DenseVector(values);
+       }
+
+       @Override
+       public DenseVector prefix(double d) {
+               double[] data = new double[this.size() + 1];
+               data[0] = d;
+               System.arraycopy(this.data, 0, data, 1, this.data.length);
+               return new DenseVector(data);
+       }
+
+       @Override
+       public DenseVector append(double d) {
+               double[] data = new double[this.size() + 1];
+               System.arraycopy(this.data, 0, data, 0, this.data.length);
+               data[this.size()] = d;
+               return new DenseVector(data);
+       }
+
+       @Override
+       public void scaleEqual(double d) {
+               BLAS.scal(d, this);
+       }
+
+       @Override
+       public DenseVector plus(Vector other) {
+               DenseVector r = this.clone();
+               if (other instanceof DenseVector) {
+                       BLAS.axpy(1.0, (DenseVector) other, r);
+               } else {
+                       BLAS.axpy(1.0, (SparseVector) other, r);
+               }
+               return r;
+       }
+
+       @Override
+       public DenseVector minus(Vector other) {
+               DenseVector r = this.clone();
+               if (other instanceof DenseVector) {
+                       BLAS.axpy(-1.0, (DenseVector) other, r);
+               } else {
+                       BLAS.axpy(-1.0, (SparseVector) other, r);
+               }
+               return r;
+       }
+
+       @Override
+       public DenseVector scale(double d) {
+               DenseVector r = this.clone();
+               BLAS.scal(d, r);
+               return r;
+       }
+
+       @Override
+       public double dot(Vector vec) {
+               if (vec instanceof DenseVector) {
+                       return BLAS.dot(this, (DenseVector) vec);
+               } else {
+                       return ((SparseVector) vec).dot(this);
 
 Review comment:
   thx. It has been removed.

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