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

 ##########
 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) {
 
 Review comment:
   yes, you are absolutely right, I forgot that `String.split` works by regexp.
   Thanks

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