[GitHub] [flink] 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.

2019-08-16 Thread GitBox
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_r314709236
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/linalg/Vector.java
 ##
 @@ -0,0 +1,159 @@
+/*
+ * 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.io.Serializable;
+
+/**
+ * The Vector class defines some common methods for both DenseVector and
+ * SparseVector.
+ */
+public abstract class Vector implements Serializable {
+   /**
+* Get the size of the vector.
+*/
+   public abstract int size();
+
+   /**
+* Get the i-th element of the vector.
+*/
+   public abstract double get(int i);
+
+   /**
+* Set the i-th element of the vector to value "val".
+*/
+   public abstract void set(int i, double val);
+
+   /**
+* Add the i-th element of the vector by value "val".
+*/
+   public abstract void add(int i, double val);
+
+   /**
+* Return the L1 norm of the vector.
+*/
+   public abstract double normL1();
+
+   /**
+* Return the Inf norm of the vector.
+*/
+   public abstract double normInf();
+
+   /**
+* Return the L2 norm of the vector.
+*/
+   public abstract double normL2();
+
+   /**
+* Return the square of L2 norm of the vector.
+*/
+   public abstract double normL2Square();
+
+   /**
+* Scale the vector by value "v" and create a new vector to store the 
result.
+*/
+   public abstract Vector scale(double v);
+
+   /**
+* Scale the vector by value "v".
+*/
+   public abstract void scaleEqual(double v);
+
+   /**
+* Normalize the vector.
+*/
+   public abstract void normalizeEqual(double p);
+
+   /**
+* Standardize the vector.
+*/
+   public abstract void standardizeEqual(double mean, double stdvar);
+
+   /**
+* Create a new vector by adding an element to the head of the vector.
+*/
+   public abstract Vector prefix(double v);
+
+   /**
+* Create a new vector by adding an element to the end of the vector.
+*/
+   public abstract Vector append(double v);
+
+   /**
+* Create a new vector by plussing another vector.
+*/
+   public abstract Vector plus(Vector vec);
+
+   /**
+* Create a new vector by subtracting  another vector.
+*/
+   public abstract Vector minus(Vector vec);
+
+   /**
+* Compute the dot product with another vector.
+*/
+   public abstract double dot(Vector vec);
+
+   /**
+* Get the iterator of the vector.
+*/
+   public abstract VectorIterator iterator();
+
+   /**
+* Serialize the vector to a string.
+*/
+   public abstract String serialize();
+
+   /**
+* Slice the vector.
+*/
+   public abstract Vector slice(int[] indexes);
+
+   /**
+* Compute the outer product with itself.
+*
+* @return The outer product matrix.
+*/
+   public abstract DenseMatrix outer();
+
+   /**
+* Parse either a {@link SparseVector} or a {@link DenseVector} from a 
formatted string.
+*
+* The format of a dense vector is comma separated values such as "1 
2 3 4".
+* The format of a sparse vector is comma separated index-value pairs, 
such as "0:1 2:3 3:4".
+* If the sparse vector has determined vector size, the size is 
prepended to the head. For example,
+* the string "$4$0:1 2:3 3:4" represents a sparse vector with size 4.
+*
+* @param str A formatted string representing a vector.
+* @return The parsed vector.
+*/
+   public static Vector parse(String str) {
+   boolean isSparse = 
org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)
+

[GitHub] [flink] 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.

2019-08-16 Thread GitBox
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_r314709076
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/linalg/Vector.java
 ##
 @@ -0,0 +1,159 @@
+/*
+ * 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.io.Serializable;
+
+/**
+ * The Vector class defines some common methods for both DenseVector and
+ * SparseVector.
+ */
+public abstract class Vector implements Serializable {
+   /**
+* Get the size of the vector.
+*/
+   public abstract int size();
+
+   /**
+* Get the i-th element of the vector.
+*/
+   public abstract double get(int i);
+
+   /**
+* Set the i-th element of the vector to value "val".
+*/
+   public abstract void set(int i, double val);
+
+   /**
+* Add the i-th element of the vector by value "val".
+*/
+   public abstract void add(int i, double val);
+
+   /**
+* Return the L1 norm of the vector.
+*/
+   public abstract double normL1();
+
+   /**
+* Return the Inf norm of the vector.
+*/
+   public abstract double normInf();
+
+   /**
+* Return the L2 norm of the vector.
+*/
+   public abstract double normL2();
+
+   /**
+* Return the square of L2 norm of the vector.
+*/
+   public abstract double normL2Square();
+
+   /**
+* Scale the vector by value "v" and create a new vector to store the 
result.
+*/
+   public abstract Vector scale(double v);
+
+   /**
+* Scale the vector by value "v".
+*/
+   public abstract void scaleEqual(double v);
+
+   /**
+* Normalize the vector.
+*/
+   public abstract void normalizeEqual(double p);
+
+   /**
+* Standardize the vector.
+*/
+   public abstract void standardizeEqual(double mean, double stdvar);
+
+   /**
+* Create a new vector by adding an element to the head of the vector.
+*/
+   public abstract Vector prefix(double v);
+
+   /**
+* Create a new vector by adding an element to the end of the vector.
+*/
+   public abstract Vector append(double v);
+
+   /**
+* Create a new vector by plussing another vector.
+*/
+   public abstract Vector plus(Vector vec);
+
+   /**
+* Create a new vector by subtracting  another vector.
+*/
+   public abstract Vector minus(Vector vec);
+
+   /**
+* Compute the dot product with another vector.
+*/
+   public abstract double dot(Vector vec);
+
+   /**
+* Get the iterator of the vector.
+*/
+   public abstract VectorIterator iterator();
+
+   /**
+* Serialize the vector to a string.
+*/
+   public abstract String serialize();
+
+   /**
+* Slice the vector.
+*/
+   public abstract Vector slice(int[] indexes);
+
+   /**
+* Compute the outer product with itself.
+*
+* @return The outer product matrix.
+*/
+   public abstract DenseMatrix outer();
+
+   /**
+* Parse either a {@link SparseVector} or a {@link DenseVector} from a 
formatted string.
+*
+* The format of a dense vector is comma separated values such as "1 
2 3 4".
 
 Review comment:
   Thanks. It has been fixed.


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


[GitHub] [flink] 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.

2019-08-16 Thread GitBox
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.
+* 
+* 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.
+*
+* 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) {
+ 

[GitHub] [flink] 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.

2019-08-12 Thread GitBox
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_r312796114
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/linalg/VectorIterator.java
 ##
 @@ -0,0 +1,49 @@
+/*
+ * 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.io.Serializable;
+
+/**
+ * An iterator over the elements of a vector.
+ *
+ * Usage:
+ *
+ * 
+ * Vector vector = ...;
+ * VectorIterator iterator = vector.iterator();
+ *
+ * while(iterator.hasNext()) {
+ * int index = iterator.getIndex();
+ * double value = iterator.getValue();
+ * iterator.next();
+ * }
+ * 
+ */
+public interface VectorIterator extends Serializable {
+
+   boolean hasNext();
 
 Review comment:
   Thanks, added more java doc.


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


[GitHub] [flink] 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.

2019-08-12 Thread GitBox
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_r312796157
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/linalg/MatVecOpTest.java
 ##
 @@ -0,0 +1,85 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test cases for {@link MatVecOp}.
+ */
+public class MatVecOpTest {
+   private static final double TOL = 1.0e-6;
+   private DenseVector dv;
+   private SparseVector sv;
+
+   @Before
+   public void setUp() throws Exception {
+   dv = new DenseVector(new double[]{1, 2, 3, 4});
+   sv = new SparseVector(4, new int[]{0, 2}, new double[]{1., 1.});
+   }
+
+   @Test
+   public void testPlus() throws Exception {
+   Vector plusResult1 = MatVecOp.plus(dv, sv);
+   Vector plusResult2 = MatVecOp.plus(sv, dv);
+   Vector plusResult3 = MatVecOp.plus(sv, sv);
+   Vector plusResult4 = MatVecOp.plus(dv, dv);
+   Assert.assertTrue(plusResult1 instanceof DenseVector);
+   Assert.assertTrue(plusResult2 instanceof DenseVector);
+   Assert.assertTrue(plusResult3 instanceof SparseVector);
+   Assert.assertTrue(plusResult4 instanceof DenseVector);
+   Assert.assertArrayEquals(((DenseVector) plusResult1).getData(), 
new double[]{2, 2, 4, 4}, TOL);
+   Assert.assertArrayEquals(((DenseVector) plusResult2).getData(), 
new double[]{2, 2, 4, 4}, TOL);
+   Assert.assertArrayEquals(((SparseVector) 
plusResult3).getIndices(), new int[]{0, 2});
+   Assert.assertArrayEquals(((SparseVector) 
plusResult3).getValues(), new double[]{2., 2.}, TOL);
+   Assert.assertArrayEquals(((DenseVector) plusResult4).getData(), 
new double[]{2, 4, 6, 8}, TOL);
+   }
+
+   @Test
+   public void testMinus() throws Exception {
+   Vector minusResult1 = MatVecOp.minus(dv, sv);
+   Vector minusResult2 = MatVecOp.minus(sv, dv);
+   Vector minusResult3 = MatVecOp.minus(sv, sv);
+   Vector minusResult4 = MatVecOp.minus(dv, dv);
+   Assert.assertTrue(minusResult1 instanceof DenseVector);
+   Assert.assertTrue(minusResult2 instanceof DenseVector);
+   Assert.assertTrue(minusResult3 instanceof SparseVector);
+   Assert.assertTrue(minusResult4 instanceof DenseVector);
+   Assert.assertArrayEquals(((DenseVector) 
minusResult1).getData(), new double[]{0, 2, 2, 4}, TOL);
+   Assert.assertArrayEquals(((DenseVector) 
minusResult2).getData(), new double[]{0, -2, -2, -4}, TOL);
+   Assert.assertArrayEquals(((SparseVector) 
minusResult3).getIndices(), new int[]{0, 2});
+   Assert.assertArrayEquals(((SparseVector) 
minusResult3).getValues(), new double[]{0., 0.}, TOL);
+   Assert.assertArrayEquals(((DenseVector) 
minusResult4).getData(), new double[]{0, 0, 0, 0}, TOL);
+   }
+
+   @Test
 
 Review comment:
   Thanks, added more tests.


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


[GitHub] [flink] 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.

2019-08-09 Thread GitBox
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_r312403047
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/matrix/SparseVectorTest.java
 ##
 @@ -0,0 +1,168 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test cases for SparseVector.
+ */
+public class SparseVectorTest {
+   private static final double TOL = 1.0e-6;
+   private SparseVector v1 = null;
+   private SparseVector v2 = null;
+
+   @Before
+   public void setUp() throws Exception {
 
 Review comment:
   thx, we have made the change.


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r312045127
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/SparseVector.java
 ##
 @@ -0,0 +1,725 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A sparse vector represented by an indices array and a values array.
+ */
+public class SparseVector extends Vector {
+
+   /**
+* Size of the vector. n = -1 indicates that the vector size is 
undetermined.
+*/
+   private int n;
+
+   /**
+* Column indices.
+*/
+   private int[] indices;
+
+   /**
+* Column values.
+*/
+   private double[] values;
+
+   /**
+* Construct an empty sparse vector with undetermined size.
+*/
+   public SparseVector() {
+   this(-1);
+   }
+
+   /**
+* Construct an empty sparse vector with determined size.
+*/
+   public SparseVector(int n) {
+   this.n = n;
+   this.indices = new int[0];
+   this.values = new double[0];
+   }
+
+   /**
+* Construct a sparse vector with the given indices and values.
+*
+* @throws IllegalArgumentException If size of indices array and values 
array differ.
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, int[] indices, double[] values) {
+   this.n = n;
+   this.indices = indices;
+   this.values = values;
+   checkSizeAndIndicesRange();
+   sortIndices();
+   }
+
+   /**
+* Construct a sparse vector with given indices to values map.
+*
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, Map kv) {
+   this.n = n;
+   int nnz = kv.size();
+   int[] indices = new int[nnz];
+   double[] values = new double[nnz];
+
+   int pos = 0;
+   for (Map.Entry entry : kv.entrySet()) {
+   indices[pos] = entry.getKey();
+   values[pos] = entry.getValue();
+   pos++;
+   }
+
+   this.indices = indices;
+   this.values = values;
+   checkSizeAndIndicesRange();
+
+   if (!(kv instanceof TreeMap)) {
+   sortIndices();
+   }
+   }
+
+   /**
+* Parse the sparse vector from a formatted string.
+*
+* @throws IllegalArgumentException If the string is of invalid format.
+*/
+   public static SparseVector deserialize(String str) {
+   try {
+   if 
(org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) {
+   return new SparseVector();
+   }
+
+   int n = -1;
+   int firstDollarPos = str.indexOf('$');
+   int lastDollarPos = -1;
+   if (firstDollarPos >= 0) {
+   lastDollarPos = StringUtils.lastIndexOf(str, 
'$');
+   String sizeStr = StringUtils.substring(str, 
firstDollarPos + 1, lastDollarPos);
+   n = Integer.valueOf(sizeStr);
+   if (lastDollarPos == str.length() - 1) {
+   return new SparseVector(n);
+   }
+   }
+
+   int numValues = StringUtils.countMatches(str, ",") + 1;
+   double[] data = new double[numValues];
+   int[

[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r312001594
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/matrix/SparseVectorTest.java
 ##
 @@ -0,0 +1,168 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test cases for SparseVector.
+ */
+public class SparseVectorTest {
+   private static final double TOL = 1.0e-6;
+   private SparseVector v1 = null;
+   private SparseVector v2 = null;
+
+   @Before
+   public void setUp() throws Exception {
+   {
+   int n = 8;
+   int[] indices = new int[]{1, 3, 5, 7};
+   double[] values = new double[]{2.0, 2.0, 2.0, 2.0};
+   v1 = new SparseVector(n, indices, values);
+   }
+
+   {
+   int n = 8;
+   int[] indices = new int[]{3, 4, 5};
+   double[] values = new double[]{1.0, 1.0, 1.0};
+   v2 = new SparseVector(n, indices, values);
+   }
+   }
+
+   @Test
+   public void testSize() throws Exception {
+   Assert.assertEquals(v1.size(), 8);
+   }
+
+   @Test
+   public void testPrefix() throws Exception {
+   SparseVector prefixed = v1.prefix(0.2);
+   Assert.assertArrayEquals(prefixed.getIndices(), new int[]{0, 2, 
4, 6, 8});
+   Assert.assertArrayEquals(prefixed.getValues(), new 
double[]{0.2, 2, 2, 2, 2}, 0);
+   }
+
+   @Test
+   public void testAppend() throws Exception {
+   SparseVector prefixed = v1.append(0.2);
+   Assert.assertArrayEquals(prefixed.getIndices(), new int[]{1, 3, 
5, 7, 8});
+   Assert.assertArrayEquals(prefixed.getValues(), new double[]{2, 
2, 2, 2, 0.2}, 0);
+   }
+
+   @Test
+   public void testSortIndices() throws Exception {
+   int n = 8;
+   int[] indices = new int[]{7, 5, 3, 1};
+   double[] values = new double[]{7, 5, 3, 1};
+   v1 = new SparseVector(n, indices, values);
 
 Review comment:
   thx, we initialize the fields using constructor directly


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r312001594
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/matrix/SparseVectorTest.java
 ##
 @@ -0,0 +1,168 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test cases for SparseVector.
+ */
+public class SparseVectorTest {
+   private static final double TOL = 1.0e-6;
+   private SparseVector v1 = null;
+   private SparseVector v2 = null;
+
+   @Before
+   public void setUp() throws Exception {
+   {
+   int n = 8;
+   int[] indices = new int[]{1, 3, 5, 7};
+   double[] values = new double[]{2.0, 2.0, 2.0, 2.0};
+   v1 = new SparseVector(n, indices, values);
+   }
+
+   {
+   int n = 8;
+   int[] indices = new int[]{3, 4, 5};
+   double[] values = new double[]{1.0, 1.0, 1.0};
+   v2 = new SparseVector(n, indices, values);
+   }
+   }
+
+   @Test
+   public void testSize() throws Exception {
+   Assert.assertEquals(v1.size(), 8);
+   }
+
+   @Test
+   public void testPrefix() throws Exception {
+   SparseVector prefixed = v1.prefix(0.2);
+   Assert.assertArrayEquals(prefixed.getIndices(), new int[]{0, 2, 
4, 6, 8});
+   Assert.assertArrayEquals(prefixed.getValues(), new 
double[]{0.2, 2, 2, 2, 2}, 0);
+   }
+
+   @Test
+   public void testAppend() throws Exception {
+   SparseVector prefixed = v1.append(0.2);
+   Assert.assertArrayEquals(prefixed.getIndices(), new int[]{1, 3, 
5, 7, 8});
+   Assert.assertArrayEquals(prefixed.getValues(), new double[]{2, 
2, 2, 2, 0.2}, 0);
+   }
+
+   @Test
+   public void testSortIndices() throws Exception {
+   int n = 8;
+   int[] indices = new int[]{7, 5, 3, 1};
+   double[] values = new double[]{7, 5, 3, 1};
+   v1 = new SparseVector(n, indices, values);
 
 Review comment:
   Can you point out which variable is static? I guess all the variables are 
local?


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311995986
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/SparseVector.java
 ##
 @@ -0,0 +1,725 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A sparse vector represented by an indices array and a values array.
+ */
+public class SparseVector extends Vector {
+
+   /**
+* Size of the vector. n = -1 indicates that the vector size is 
undetermined.
+*/
+   private int n;
+
+   /**
+* Column indices.
+*/
+   private int[] indices;
+
+   /**
+* Column values.
+*/
+   private double[] values;
+
+   /**
+* Construct an empty sparse vector with undetermined size.
+*/
+   public SparseVector() {
+   this(-1);
+   }
+
+   /**
+* Construct an empty sparse vector with determined size.
+*/
+   public SparseVector(int n) {
+   this.n = n;
+   this.indices = new int[0];
+   this.values = new double[0];
+   }
+
+   /**
+* Construct a sparse vector with the given indices and values.
+*
+* @throws IllegalArgumentException If size of indices array and values 
array differ.
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, int[] indices, double[] values) {
+   this.n = n;
+   this.indices = indices;
+   this.values = values;
+   checkSizeAndIndicesRange();
+   sortIndices();
+   }
+
+   /**
+* Construct a sparse vector with given indices to values map.
+*
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, Map kv) {
+   this.n = n;
+   int nnz = kv.size();
+   int[] indices = new int[nnz];
+   double[] values = new double[nnz];
+
+   int pos = 0;
+   for (Map.Entry entry : kv.entrySet()) {
+   indices[pos] = entry.getKey();
+   values[pos] = entry.getValue();
+   pos++;
+   }
+
+   this.indices = indices;
+   this.values = values;
+   checkSizeAndIndicesRange();
+
+   if (!(kv instanceof TreeMap)) {
+   sortIndices();
+   }
+   }
+
+   /**
+* Parse the sparse vector from a formatted string.
+*
+* @throws IllegalArgumentException If the string is of invalid format.
+*/
+   public static SparseVector deserialize(String str) {
+   try {
+   if 
(org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) {
+   return new SparseVector();
+   }
+
+   int n = -1;
+   int firstDollarPos = str.indexOf('$');
+   int lastDollarPos = -1;
+   if (firstDollarPos >= 0) {
+   lastDollarPos = StringUtils.lastIndexOf(str, 
'$');
+   String sizeStr = StringUtils.substring(str, 
firstDollarPos + 1, lastDollarPos);
+   n = Integer.valueOf(sizeStr);
+   if (lastDollarPos == str.length() - 1) {
+   return new SparseVector(n);
+   }
+   }
+
+   int numValues = StringUtils.countMatches(str, ",") + 1;
+   double[] data = new double[numValues];
+   int[

[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311995109
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/matrix/SparseVectorTest.java
 ##
 @@ -0,0 +1,168 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test cases for SparseVector.
+ */
+public class SparseVectorTest {
+   private static final double TOL = 1.0e-6;
+   private SparseVector v1 = null;
+   private SparseVector v2 = null;
+
+   @Before
+   public void setUp() throws Exception {
 
 Review comment:
   Since `setUp` is called before every tests, so I guess they are the same.


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311994612
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/matrix/DenseMatrixTest.java
 ##
 @@ -0,0 +1,174 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for DenseMatrix.
+ */
+public class DenseMatrixTest {
+
+   private static final double TOL = 1.0e-6;
+
+   private static void assertEqual2D(double[][] matA, double[][] matB) {
+   assert (matA.length == matB.length);
+   assert (matA[0].length == matB[0].length);
+   int m = matA.length;
+   int n = matA[0].length;
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   Assert.assertEquals(matA[i][j], matB[i][j], 
TOL);
+   }
+   }
+   }
+
+   private static double[][] simpleMM(double[][] matA, double[][] matB) {
+   int m = matA.length;
+   int n = matB[0].length;
+   int k = matA[0].length;
+   double[][] matC = new double[m][n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   matC[i][j] = 0.;
+   for (int l = 0; l < k; l++) {
+   matC[i][j] += matA[i][l] * matB[l][j];
+   }
+   }
+   }
+   return matC;
+   }
+
+   private static double[] simpleMV(double[][] matA, double[] x) {
+   int m = matA.length;
+   int n = matA[0].length;
+   assert (n == x.length);
+   double[] y = new double[m];
+   for (int i = 0; i < m; i++) {
+   y[i] = 0.;
+   for (int j = 0; j < n; j++) {
+   y[i] += matA[i][j] * x[j];
+   }
+   }
+   return y;
+   }
+
+   @Test
+   public void testPlus() throws Exception {
+   DenseMatrix matA = DenseMatrix.rand(4, 3);
+   DenseMatrix matB = DenseMatrix.ones(4, 3);
+   matA.plusEquals(matB);
+   matA.plusEquals(3.0);
+   }
+
+   @Test
+   public void testMinus() throws Exception {
+   DenseMatrix matA = DenseMatrix.rand(4, 3);
+   DenseMatrix matB = DenseMatrix.ones(4, 3);
+   matA.minusEquals(matB);
 
 Review comment:
   Thanks. It has been fixed.


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311994487
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/matrix/DenseMatrixTest.java
 ##
 @@ -0,0 +1,174 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for DenseMatrix.
+ */
+public class DenseMatrixTest {
+
+   private static final double TOL = 1.0e-6;
+
+   private static void assertEqual2D(double[][] matA, double[][] matB) {
+   assert (matA.length == matB.length);
+   assert (matA[0].length == matB[0].length);
+   int m = matA.length;
+   int n = matA[0].length;
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   Assert.assertEquals(matA[i][j], matB[i][j], 
TOL);
+   }
+   }
+   }
+
+   private static double[][] simpleMM(double[][] matA, double[][] matB) {
+   int m = matA.length;
+   int n = matB[0].length;
+   int k = matA[0].length;
+   double[][] matC = new double[m][n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   matC[i][j] = 0.;
+   for (int l = 0; l < k; l++) {
+   matC[i][j] += matA[i][l] * matB[l][j];
+   }
+   }
+   }
+   return matC;
+   }
+
+   private static double[] simpleMV(double[][] matA, double[] x) {
+   int m = matA.length;
+   int n = matA[0].length;
+   assert (n == x.length);
+   double[] y = new double[m];
+   for (int i = 0; i < m; i++) {
+   y[i] = 0.;
+   for (int j = 0; j < n; j++) {
+   y[i] += matA[i][j] * x[j];
+   }
+   }
+   return y;
+   }
+
+   @Test
+   public void testPlus() throws Exception {
+   DenseMatrix matA = DenseMatrix.rand(4, 3);
+   DenseMatrix matB = DenseMatrix.ones(4, 3);
+   matA.plusEquals(matB);
+   matA.plusEquals(3.0);
 
 Review comment:
   Thanks for pointing out the problem. We have fixed it.


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311994360
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/Vector.java
 ##
 @@ -0,0 +1,340 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.io.Serializable;
+
+/**
+ * The Vector class defines some common methods for both DenseVector and
+ * SparseVector.
+ */
+public abstract class Vector implements Serializable {
+
+   /**
+* Parse a DenseVector from a formatted string.
+*/
+   public static DenseVector dense(String str) {
+   return DenseVector.deserialize(str);
+   }
+
+   /**
+* Parse a SparseVector from a formatted string.
+*/
+   public static SparseVector sparse(String str) {
+   return SparseVector.deserialize(str);
+   }
+
+   /**
+* To check whether the formatted string represents a SparseVector.
+*/
+   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;
+   }
+
+   /**
+* Parse the tensor from a formatted string.
+*/
+   public static Vector deserialize(String str) {
+   Vector vec;
+   if (isSparse(str)) {
+   vec = Vector.sparse(str);
+   } else {
+   vec = Vector.dense(str);
+   }
+   return vec;
+   }
+
+   /**
+* Plus two vectors and create a new vector to store the result.
+*/
+   public static Vector plus(Vector vec1, Vector vec2) {
+   return vec1.plus(vec2);
+   }
+
+   /**
+* Minus two vectors and create a new vector to store the result.
+*/
+   public static Vector minus(Vector vec1, Vector vec2) {
+   return vec1.minus(vec2);
+   }
+
+   /**
+* Compute the dot product of two vectors.
+*/
+   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 applySum((DenseVector) vec1, 
(DenseVector) vec2, (a, b) -> Math.abs(a - b));
+   } else {
+   return applySum((DenseVector) vec1, 
(SparseVector) vec2, (a, b) -> Math.abs(a - b));
+   }
+   } else {
+   if (vec2 instanceof DenseVector) {
+   return applySum((SparseVector) vec1, 
(DenseVector) vec2, (a, b) -> Math.abs(a - b));
+   } else {
+   return 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 applySum((DenseVector) vec1, 
(DenseVector) vec2, (a, b) -> (a - b) * (a - b));
+   } else {
+   return applySum((DenseVector) vec1, 
(SparseVector) vec2, (a, b) -> (a - b) * (a - b));
+   }
+   } else {
+   if (vec2 instanceof DenseVector) {
+   return applySum((SparseVector) vec1, 
(DenseVector) vec2, (a, b) -> (a - b) * (a - b));
+   

[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311994163
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/Vector.java
 ##
 @@ -0,0 +1,340 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.io.Serializable;
+
+/**
+ * The Vector class defines some common methods for both DenseVector and
+ * SparseVector.
+ */
+public abstract class Vector implements Serializable {
+
+   /**
+* Parse a DenseVector from a formatted string.
+*/
+   public static DenseVector dense(String str) {
+   return DenseVector.deserialize(str);
+   }
+
+   /**
+* Parse a SparseVector from a formatted string.
+*/
+   public static SparseVector sparse(String str) {
+   return SparseVector.deserialize(str);
+   }
+
+   /**
+* To check whether the formatted string represents a SparseVector.
+*/
+   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;
+   }
+
+   /**
+* Parse the tensor from a formatted string.
+*/
+   public static Vector deserialize(String str) {
 
 Review comment:
   Since there are only two sub classes (i.e., DenseVector and SparseVector), 
and there should  probably be no more sub classes. So I guess the problem is 
not that bad when considering the convenience it brings to algorithm developers.


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311993044
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/Vector.java
 ##
 @@ -0,0 +1,340 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.io.Serializable;
+
+/**
+ * The Vector class defines some common methods for both DenseVector and
+ * SparseVector.
+ */
+public abstract class Vector implements Serializable {
+
+   /**
+* Parse a DenseVector from a formatted string.
+*/
+   public static DenseVector dense(String str) {
 
 Review comment:
   Thanks, I have moved this method down to `DenseVector`.


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311992246
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,763 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * DenseMatrix stores dense matrix data and provides some methods to operate on
+ * the matrix it represents.
+ */
+public class DenseMatrix implements Serializable {
+
+   /**
+* Row dimension.
+*/
+   private int m;
+
+   /**
+* Column dimension.
+*/
+   private int n;
+
+   /**
+* Array for internal storage of elements.
+*
+* The matrix data is stored in column major format internally.
+*/
+   private double[] data;
+
+   /**
+* Construct an m-by-n matrix of zeros.
+*
+* @param m Number of rows.
+* @param n Number of colums.
+*/
+   public DenseMatrix(int m, int n) {
+   this(m, n, new double[m * n], false);
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array should 
organize
+* in column major.
+*
+* @param mNumber of rows.
+* @param nNumber of cols.
+* @param data One-dimensional array of doubles.
+*/
+   public DenseMatrix(int m, int n, double[] data) {
+   this(m, n, data, false);
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array is 
organized
+* in column major or in row major, which is specified by parameter 
'inRowMajor'
+*
+* @param m  Number of rows.
+* @param n  Number of cols.
+* @param data   One-dimensional array of doubles.
+* @param inRowMajor Whether the matrix in 'data' is in row major 
format.
+*/
+   public DenseMatrix(int m, int n, double[] data, boolean inRowMajor) {
+   assert (data.length == m * n);
+   this.m = m;
+   this.n = n;
+   if (inRowMajor) {
+   toColumnMajor(m, n, data);
+   }
+   this.data = data;
+   }
+
+   /**
+* Construct a matrix from a 2-D array.
+*
+* @param data Two-dimensional array of doubles.
+* @throws IllegalArgumentException All rows must have the same size
+*/
+   public DenseMatrix(double[][] data) {
+   this.m = data.length;
+   if (this.m == 0) {
+   this.n = 0;
+   this.data = new double[0];
+   return;
+   }
+   this.n = data[0].length;
+   for (int i = 0; i < m; i++) {
+   if (data[i].length != n) {
+   throw new IllegalArgumentException("All rows 
must have the same size.");
+   }
+   }
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, data[i][j]);
+   }
+   }
+   }
+
+   /**
+* Create an identity matrix.
+*
+* @param n
+* @return
+*/
+   public static DenseMatrix eye(int n) {
+   return eye(n, n);
+   }
+
+   /**
+* Create a identity matrix.
+*
+* @param m
+* @param n
+* @return
+*/
+   public static DenseMatrix eye(int m, int n) {
+   DenseMatrix mat = new DenseMatrix(m, n);
+   int k = Math.min(m, n);
+   for (int i = 0; i < k; i++) {
+   mat.data[i * m + i] = 1.0;
+   }
+   return mat;
+   }
+
+   /**
+* Create a zero matrix.
+*
+

[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311990712
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/BinaryOp.java
 ##
 @@ -0,0 +1,27 @@
+/*
+ * 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;
+
+/**
+ * Defines the matrix or vector element-wise binary operation.
+ */
+public interface BinaryOp {
 
 Review comment:
   The idea is great. We have followed the suggestion.


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311990429
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/BLAS.java
 ##
 @@ -0,0 +1,140 @@
+/*
+ * 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;
 
 Review comment:
   I have renamed the package "matrix" to "linalg", which is short hand for 
"linear algebra".


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


[GitHub] [flink] 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.

2019-08-08 Thread GitBox
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_r311990014
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/BLAS.java
 ##
 @@ -0,0 +1,140 @@
+/*
+ * 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;
+
+/**
+ * A utility class that wraps netlib BLAS and provides some operations on 
dense matrix
+ * and dense vector.
+ */
+public class BLAS {
+   private static final com.github.fommil.netlib.BLAS BLAS = 
com.github.fommil.netlib.BLAS.getInstance();
+
+   /**
+* y += a * x .
+*/
+   public static void axpy(double a, double[] x, double[] y) {
+   BLAS.daxpy(x.length, a, x, 1, y, 1);
+   }
+
+   /**
+* y += a * x .
+*/
+   public static void axpy(double a, DenseVector x, DenseVector y) {
+   axpy(a, x.getData(), y.getData());
+   }
+
+   /**
+* x \cdot y .
+*/
+   public static double dot(double[] x, double[] y) {
+   return BLAS.ddot(x.length, x, 1, y, 1);
+   }
+
+   /**
+* x \cdot y .
+*/
+   public static double dot(DenseVector x, DenseVector y) {
+   return dot(x.getData(), y.getData());
+   }
+
+   /**
+* x = x * a .
+*/
+   public static void scal(double a, double[] x) {
+   BLAS.dscal(x.length, a, x, 1);
+   }
+
+   /**
+* x = x * a .
+*/
+   public static void scal(double a, DenseVector x) {
+   scal(a, x.getData());
+   }
+
+   /**
+* || x - y ||^2 .
+*/
+   public static double dsquared(double[] x, double[] y) {
+   double s = 0.;
+   for (int i = 0; i < x.length; i++) {
+   double d = x[i] - y[i];
+   s += d * d;
+   }
+   return s;
+   }
+
+   /**
+* | x - y | .
+*/
+   public static double dabs(double[] x, double[] y) {
+   double s = 0.;
+   for (int i = 0; i < x.length; i++) {
+   double d = x[i] - y[i];
+   s += Math.abs(d);
+   }
+   return s;
+   }
+
+   /**
+* C := alpha * A * B + beta * C .
+*/
+   public static void gemm(double alpha, DenseMatrix matA, boolean transA, 
DenseMatrix matB, boolean transB,
+   double beta, 
DenseMatrix matC) {
+   if (transA) {
+   assert matA.numCols() == matC.numRows();
 
 Review comment:
   Thanks for the suggestion. We have added the messages.


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


[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310030132
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/SparseVector.java
 ##
 @@ -0,0 +1,791 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A sparse vector represented by an indices array and a values array.
+ */
+public class SparseVector extends Vector {
+
+   /**
+* Size of the vector. n = -1 indicates that the vector size is 
undetermined.
+*/
+   int n;
+
+   /**
+* Column indices.
+*/
+   int[] indices;
+
+   /**
+* Column values.
+*/
+   double[] values;
+
+   /**
+* Construct an empty sparse vector with undetermined size.
+*/
+   public SparseVector() {
+   this(-1);
+   }
+
+   /**
+* Construct an empty sparse vector with determined size.
+*/
+   public SparseVector(int n) {
+   this.n = n;
+   this.indices = new int[0];
+   this.values = new double[0];
+   }
+
+   /**
+* Construct a sparse vector with the given indices and values.
+*
+* @throws IllegalArgumentException If size of indices array and values 
array differ.
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, int[] indices, double[] values) {
+   this.n = n;
+   this.indices = indices.clone();
+   this.values = values.clone();
+   checkIndices();
+   sortIndices();
+   }
+
+   /**
+* Construct a sparse vector with given indices to values map.
+*
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, Map kv) {
+   this.n = n;
+   int nnz = kv.size();
+   int[] indices = new int[nnz];
+   double[] values = new double[nnz];
+
+   int pos = 0;
+   for (Map.Entry entry : kv.entrySet()) {
+   indices[pos] = entry.getKey();
+   values[pos] = entry.getValue();
+   pos++;
+   }
+
+   this.indices = indices;
+   this.values = values;
+   checkIndices();
+
+   if (!(kv instanceof TreeMap)) {
+   sortIndices();
+   }
+   }
+
+   private void checkIndices() {
+   if (indices.length != values.length) {
+   throw new IllegalArgumentException("Indices size and 
values size should be the same.");
+   }
+   for (int i = 0; i < indices.length; i++) {
+   if (indices[i] < 0 || (n >= 0 && indices[i] >= n)) {
+   throw new IllegalArgumentException("Index out 
of bound.");
+   }
+   }
+   }
+
+   private void sortIndices() {
+   boolean outOfOrder = false;
+   for (int i = 0; i < this.indices.length - 1; i++) {
+   if (this.indices[i] >= this.indices[i + 1]) {
+   outOfOrder = true;
+   break;
+   }
+   }
+
+   if (!outOfOrder) {
+   return;
+   }
+
+   // sort
+   Integer[] order = new Integer[this.indices.length];
+   for (int i = 0; i < order.length; i++) {
+   order[i] = i;
+   }
+
+   Arrays.sort(order, new Comparator() {
+   @Override
+   pub

[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310029903
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/SparseVector.java
 ##
 @@ -0,0 +1,791 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A sparse vector represented by an indices array and a values array.
+ */
+public class SparseVector extends Vector {
+
+   /**
+* Size of the vector. n = -1 indicates that the vector size is 
undetermined.
+*/
+   int n;
+
+   /**
+* Column indices.
+*/
+   int[] indices;
+
+   /**
+* Column values.
+*/
+   double[] values;
+
+   /**
+* Construct an empty sparse vector with undetermined size.
+*/
+   public SparseVector() {
+   this(-1);
+   }
+
+   /**
+* Construct an empty sparse vector with determined size.
+*/
+   public SparseVector(int n) {
+   this.n = n;
+   this.indices = new int[0];
+   this.values = new double[0];
+   }
+
+   /**
+* Construct a sparse vector with the given indices and values.
+*
+* @throws IllegalArgumentException If size of indices array and values 
array differ.
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, int[] indices, double[] values) {
+   this.n = n;
+   this.indices = indices.clone();
+   this.values = values.clone();
+   checkIndices();
+   sortIndices();
+   }
+
+   /**
+* Construct a sparse vector with given indices to values map.
+*
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, Map kv) {
+   this.n = n;
+   int nnz = kv.size();
+   int[] indices = new int[nnz];
+   double[] values = new double[nnz];
+
+   int pos = 0;
+   for (Map.Entry entry : kv.entrySet()) {
+   indices[pos] = entry.getKey();
+   values[pos] = entry.getValue();
+   pos++;
+   }
+
+   this.indices = indices;
+   this.values = values;
+   checkIndices();
+
+   if (!(kv instanceof TreeMap)) {
+   sortIndices();
+   }
+   }
+
+   private void checkIndices() {
+   if (indices.length != values.length) {
+   throw new IllegalArgumentException("Indices size and 
values size should be the same.");
+   }
+   for (int i = 0; i < indices.length; i++) {
+   if (indices[i] < 0 || (n >= 0 && indices[i] >= n)) {
+   throw new IllegalArgumentException("Index out 
of bound.");
+   }
+   }
+   }
+
+   private void sortIndices() {
+   boolean outOfOrder = false;
+   for (int i = 0; i < this.indices.length - 1; i++) {
+   if (this.indices[i] >= this.indices[i + 1]) {
+   outOfOrder = true;
+   break;
+   }
+   }
+
+   if (!outOfOrder) {
+   return;
+   }
+
+   // sort
+   Integer[] order = new Integer[this.indices.length];
+   for (int i = 0; i < order.length; i++) {
+   order[i] = i;
+   }
+
+   Arrays.sort(order, new Comparator() {
+   @Override
+   pub

[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310029003
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrixUtil.java
 ##
 @@ -0,0 +1,72 @@
+/*
+ * 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;
+
+/**
+ * DenseMatrixUtil provides some operations over DenseMatrix.
+ */
+public class DenseMatrixUtil {
+
+   /**
+* Provides access to DenseMatrix's internal data buffer. Be aware that
+* the data is stored in column major.
+*
+* @param mat The matrix.
+* @return The matrix's internal data buffer.
+*/
+   public static double[] getDataBuffer(DenseMatrix mat) {
+   return mat.data;
 
 Review comment:
   We have removed this method.


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


[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310028338
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,725 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * DenseMatrix stores dense matrix data and provides some methods to operate on
+ * the matrix it represents.
+ */
+public class DenseMatrix implements Serializable {
 
 Review comment:
   At the moment, there are only {@link DenseMatrix}. Perhaps in the future 
there will be a SparseMatrix class. We may add a Matrix superclass in that case.


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


[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310027507
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/BLAS.java
 ##
 @@ -0,0 +1,132 @@
+/*
+ * 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;
+
+/**
+ * A utility class that wraps netlib BLAS and provides some operations on 
dense matrix
+ * and dense vector.
+ */
+public class BLAS {
+   private static final com.github.fommil.netlib.BLAS BLAS = 
com.github.fommil.netlib.BLAS.getInstance();
+
+   /**
+* y += a * x .
+*/
+   public static void axpy(double a, double[] x, double[] y) {
 
 Review comment:
   Thank you for the suggestion. But as mentioned before, blas is a standard 
library that works on dense matrix and vector only. So it seems not necessary 
to provide the SparseVector impl.


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


[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310026611
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/BLAS.java
 ##
 @@ -0,0 +1,132 @@
+/*
+ * 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;
+
+/**
+ * A utility class that wraps netlib BLAS and provides some operations on 
dense matrix
+ * and dense vector.
+ */
+public class BLAS {
+   private static final com.github.fommil.netlib.BLAS BLAS = 
com.github.fommil.netlib.BLAS.getInstance();
+
+   /**
+* y += a * x .
+*/
+   public static void axpy(double a, double[] x, double[] y) {
+   BLAS.daxpy(x.length, a, x, 1, y, 1);
+   }
+
+   public static void axpy(double a, DenseVector x, DenseVector y) {
+   axpy(a, x.getData(), y.getData());
+   }
+
+   /**
+* x \cdot y .
+*/
+   public static double dot(double[] x, double[] y) {
+   return BLAS.ddot(x.length, x, 1, y, 1);
+   }
+
+   public static double dot(DenseVector x, DenseVector y) {
 
 Review comment:
   Blas is a package that provides high performance linear algebra operations, 
and it is not intended to work on sparse vector and sparse matrix cases. So it 
is better to be what it is now.


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


[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310025504
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/matrix/DenseMatrixTest.java
 ##
 @@ -0,0 +1,196 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for DenseMatrix.
+ */
+public class DenseMatrixTest {
+
+   private static final double TOL = 1.0e-6;
+
+   private static void assertEqual2D(double[][] matA, double[][] matB) {
+   assert (matA.length == matB.length);
+   assert (matA[0].length == matB[0].length);
+   int m = matA.length;
+   int n = matA[0].length;
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   Assert.assertEquals(matA[i][j], matB[i][j], 
TOL);
+   }
+   }
+   }
+
+   private static double[][] simpleMM(double[][] matA, double[][] matB) {
+   int m = matA.length;
+   int n = matB[0].length;
+   int k = matA[0].length;
+   double[][] matC = new double[m][n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   matC[i][j] = 0.;
+   for (int l = 0; l < k; l++) {
+   matC[i][j] += matA[i][l] * matB[l][j];
+   }
+   }
+   }
+   return matC;
+   }
+
+   private static double[] simpleMV(double[][] matA, double[] x) {
+   int m = matA.length;
+   int n = matA[0].length;
+   assert (n == x.length);
+   double[] y = new double[m];
+   for (int i = 0; i < m; i++) {
+   y[i] = 0.;
+   for (int j = 0; j < n; j++) {
+   y[i] += matA[i][j] * x[j];
+   }
+   }
+   return y;
+   }
+
+   @Test
+   public void plus() throws Exception {
 
 Review comment:
   Thanks for your advices. We have renamed all methods in the test cases to 
conform to naming convention.


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


[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310025801
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/test/java/org/apache/flink/ml/common/matrix/DenseMatrixTest.java
 ##
 @@ -0,0 +1,196 @@
+/*
+ * 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.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for DenseMatrix.
+ */
+public class DenseMatrixTest {
+
+   private static final double TOL = 1.0e-6;
+
+   private static void assertEqual2D(double[][] matA, double[][] matB) {
+   assert (matA.length == matB.length);
+   assert (matA[0].length == matB[0].length);
+   int m = matA.length;
+   int n = matA[0].length;
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   Assert.assertEquals(matA[i][j], matB[i][j], 
TOL);
+   }
+   }
+   }
+
+   private static double[][] simpleMM(double[][] matA, double[][] matB) {
+   int m = matA.length;
+   int n = matB[0].length;
+   int k = matA[0].length;
+   double[][] matC = new double[m][n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   matC[i][j] = 0.;
+   for (int l = 0; l < k; l++) {
+   matC[i][j] += matA[i][l] * matB[l][j];
+   }
+   }
+   }
+   return matC;
+   }
+
+   private static double[] simpleMV(double[][] matA, double[] x) {
+   int m = matA.length;
+   int n = matA[0].length;
+   assert (n == x.length);
+   double[] y = new double[m];
+   for (int i = 0; i < m; i++) {
+   y[i] = 0.;
+   for (int j = 0; j < n; j++) {
+   y[i] += matA[i][j] * x[j];
+   }
+   }
+   return y;
+   }
+
+   @Test
+   public void plus() throws Exception {
+   DenseMatrix matA = DenseMatrix.rand(4, 3);
+   DenseMatrix matB = DenseMatrix.ones(4, 3);
+   matA.plusEquals(matB);
+   matA.plusEquals(3.0);
+   }
+
+   @Test
+   public void minus() throws Exception {
+   DenseMatrix matA = DenseMatrix.rand(4, 3);
+   DenseMatrix matB = DenseMatrix.ones(4, 3);
+   matA.minusEquals(matB);
+   }
+
+   @Test
+   public void times() throws Exception {
+   DenseMatrix matA = DenseMatrix.rand(4, 3);
+   DenseMatrix matB = DenseMatrix.rand(3, 5);
+
+   DenseMatrix matC1 = matA.times(matB);
+   assertEqual2D(matC1.getArrayCopy2D(), 
simpleMM(matA.getArrayCopy2D(), matB.getArrayCopy2D()));
+
+   DenseMatrix matC2 = matA.times(matB);
+   assertEqual2D(matC2.getArrayCopy2D(), 
simpleMM(matA.getArrayCopy2D(), matB.getArrayCopy2D()));
+
+   DenseMatrix matC3 = DenseMatrix.zeros(5, 4);
+   BLAS.gemm(1., matB, true, matA, true, 0., matC3);
+   assertEqual2D(matC3.getArrayCopy2D(),
+   simpleMM(matB.transpose().getArrayCopy2D(), 
matA.transpose().getArrayCopy2D()));
+
+   DenseMatrix matC4 = DenseMatrix.zeros(5, 4);
+   BLAS.gemm(1., matB, true, matA, true, 0., matC4);
+   assertEqual2D(matC4.getArrayCopy2D(),
+   simpleMM(matB.transpose().getArrayCopy2D(), 
matA.transpose().getArrayCopy2D()));
+
+   DenseMatrix matC5 = matA.times(matB);
+   assertEqual2D(matC5.getArrayCopy2D(), 
simpleMM(matA.getArrayCopy2D(), matB.getArrayCopy2D()));
+   }
+
+   @Test
+   public void times1() throws Exception {
 
 Review comment:
   Yes, we changed the name.

---

[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310025105
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/SparseVector.java
 ##
 @@ -0,0 +1,791 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A sparse vector represented by an indices array and a values array.
+ */
+public class SparseVector extends Vector {
+
+   /**
+* Size of the vector. n = -1 indicates that the vector size is 
undetermined.
+*/
+   int n;
+
+   /**
+* Column indices.
+*/
+   int[] indices;
+
+   /**
+* Column values.
+*/
+   double[] values;
+
+   /**
+* Construct an empty sparse vector with undetermined size.
+*/
+   public SparseVector() {
+   this(-1);
+   }
+
+   /**
+* Construct an empty sparse vector with determined size.
+*/
+   public SparseVector(int n) {
+   this.n = n;
+   this.indices = new int[0];
+   this.values = new double[0];
+   }
+
+   /**
+* Construct a sparse vector with the given indices and values.
+*
+* @throws IllegalArgumentException If size of indices array and values 
array differ.
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, int[] indices, double[] values) {
+   this.n = n;
+   this.indices = indices.clone();
+   this.values = values.clone();
+   checkIndices();
+   sortIndices();
+   }
+
+   /**
+* Construct a sparse vector with given indices to values map.
+*
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, Map kv) {
+   this.n = n;
+   int nnz = kv.size();
+   int[] indices = new int[nnz];
+   double[] values = new double[nnz];
+
+   int pos = 0;
+   for (Map.Entry entry : kv.entrySet()) {
+   indices[pos] = entry.getKey();
+   values[pos] = entry.getValue();
+   pos++;
+   }
+
+   this.indices = indices;
+   this.values = values;
+   checkIndices();
+
+   if (!(kv instanceof TreeMap)) {
+   sortIndices();
+   }
+   }
+
+   private void checkIndices() {
+   if (indices.length != values.length) {
+   throw new IllegalArgumentException("Indices size and 
values size should be the same.");
+   }
+   for (int i = 0; i < indices.length; i++) {
+   if (indices[i] < 0 || (n >= 0 && indices[i] >= n)) {
+   throw new IllegalArgumentException("Index out 
of bound.");
+   }
+   }
+   }
+
+   private void sortIndices() {
+   boolean outOfOrder = false;
+   for (int i = 0; i < this.indices.length - 1; i++) {
+   if (this.indices[i] >= this.indices[i + 1]) {
+   outOfOrder = true;
+   break;
+   }
+   }
+
+   if (!outOfOrder) {
+   return;
+   }
+
+   // sort
+   Integer[] order = new Integer[this.indices.length];
+   for (int i = 0; i < order.length; i++) {
+   order[i] = i;
+   }
+
+   Arrays.sort(order, new Comparator() {
+   @Override
+   pub

[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310024836
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/SparseVector.java
 ##
 @@ -0,0 +1,791 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A sparse vector represented by an indices array and a values array.
+ */
+public class SparseVector extends Vector {
+
+   /**
+* Size of the vector. n = -1 indicates that the vector size is 
undetermined.
+*/
+   int n;
+
+   /**
+* Column indices.
+*/
+   int[] indices;
+
+   /**
+* Column values.
+*/
+   double[] values;
+
+   /**
+* Construct an empty sparse vector with undetermined size.
+*/
+   public SparseVector() {
+   this(-1);
+   }
+
+   /**
+* Construct an empty sparse vector with determined size.
+*/
+   public SparseVector(int n) {
+   this.n = n;
+   this.indices = new int[0];
+   this.values = new double[0];
+   }
+
+   /**
+* Construct a sparse vector with the given indices and values.
+*
+* @throws IllegalArgumentException If size of indices array and values 
array differ.
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, int[] indices, double[] values) {
+   this.n = n;
+   this.indices = indices.clone();
+   this.values = values.clone();
+   checkIndices();
+   sortIndices();
+   }
+
+   /**
+* Construct a sparse vector with given indices to values map.
+*
+* @throws IllegalArgumentException If n >= 0 and the indices are out 
of bound.
+*/
+   public SparseVector(int n, Map kv) {
+   this.n = n;
+   int nnz = kv.size();
+   int[] indices = new int[nnz];
+   double[] values = new double[nnz];
+
+   int pos = 0;
+   for (Map.Entry entry : kv.entrySet()) {
+   indices[pos] = entry.getKey();
+   values[pos] = entry.getValue();
+   pos++;
+   }
+
+   this.indices = indices;
+   this.values = values;
+   checkIndices();
+
+   if (!(kv instanceof TreeMap)) {
+   sortIndices();
+   }
+   }
+
+   private void checkIndices() {
+   if (indices.length != values.length) {
+   throw new IllegalArgumentException("Indices size and 
values size should be the same.");
+   }
+   for (int i = 0; i < indices.length; i++) {
+   if (indices[i] < 0 || (n >= 0 && indices[i] >= n)) {
+   throw new IllegalArgumentException("Index out 
of bound.");
+   }
+   }
+   }
+
+   private void sortIndices() {
+   boolean outOfOrder = false;
+   for (int i = 0; i < this.indices.length - 1; i++) {
+   if (this.indices[i] >= this.indices[i + 1]) {
+   outOfOrder = true;
+   break;
+   }
+   }
+
+   if (!outOfOrder) {
+   return;
+   }
+
+   // sort
+   Integer[] order = new Integer[this.indices.length];
+   for (int i = 0; i < order.length; i++) {
+   order[i] = i;
+   }
+
+   Arrays.sort(order, new Comparator() {
+   @Override
+   pub

[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310024334
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseVector.java
 ##
 @@ -0,0 +1,508 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.Random;
+
+/**
+ * A dense vector represented by a values array.
+ */
+public class DenseVector extends Vector {
+
+   /**
+* Vector data.
+*/
+   double[] data;
+
+   public DenseVector() {
+   this(0);
+   }
+
+   public DenseVector(int n) {
+   this.data = new double[n];
+   }
+
+   public DenseVector(double[] data) {
+   this.data = data.clone();
+   }
+
+   @Override
+   public DenseVector clone() {
+   DenseVector c = new DenseVector();
+   c.setData(this.data.clone());
+   return c;
+   }
+
+   public static DenseVector ones(int n) {
+   DenseVector r = new DenseVector(n);
+   for (int i = 0; i < r.data.length; i++) {
+   r.data[i] = 1.0;
+   }
+   return r;
+   }
+
+   public static DenseVector zeros(int n) {
+   DenseVector r = new DenseVector(n);
+   for (int i = 0; i < r.data.length; i++) {
+   r.data[i] = 0.0;
+   }
+   return r;
+   }
+
+   public static DenseVector rand(int n) {
+   Random random = new Random();
+   DenseVector v = new DenseVector(n);
+   for (int i = 0; i < n; i++) {
+   v.set(i, random.nextDouble());
+   }
+   return v;
+   }
+
+   @Override
+   public String toString() {
+   StringBuilder sbd = new StringBuilder();
+
+   for (int i = 0; i < data.length; i++) {
+   sbd.append(data[i]);
+   if (i < data.length - 1) {
+   sbd.append(",");
+   }
+   }
+
+   return sbd.toString();
+   }
+
+   @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
+   pu

[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310023665
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseVector.java
 ##
 @@ -0,0 +1,508 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.Random;
+
+/**
+ * A dense vector represented by a values array.
+ */
+public class DenseVector extends Vector {
+
+   /**
+* Vector data.
+*/
+   double[] data;
+
+   public DenseVector() {
+   this(0);
+   }
+
+   public DenseVector(int n) {
+   this.data = new double[n];
+   }
+
+   public DenseVector(double[] data) {
+   this.data = data.clone();
+   }
+
+   @Override
+   public DenseVector clone() {
+   DenseVector c = new DenseVector();
+   c.setData(this.data.clone());
+   return c;
+   }
+
+   public static DenseVector ones(int n) {
+   DenseVector r = new DenseVector(n);
+   for (int i = 0; i < r.data.length; i++) {
+   r.data[i] = 1.0;
+   }
+   return r;
+   }
+
+   public static DenseVector zeros(int n) {
+   DenseVector r = new DenseVector(n);
+   for (int i = 0; i < r.data.length; i++) {
+   r.data[i] = 0.0;
+   }
+   return r;
+   }
+
+   public static DenseVector rand(int n) {
+   Random random = new Random();
+   DenseVector v = new DenseVector(n);
+   for (int i = 0; i < n; i++) {
+   v.set(i, random.nextDouble());
+   }
+   return v;
+   }
+
+   @Override
+   public String toString() {
+   StringBuilder sbd = new StringBuilder();
+
+   for (int i = 0; i < data.length; i++) {
+   sbd.append(data[i]);
+   if (i < data.length - 1) {
+   sbd.append(",");
+   }
+   }
+
+   return sbd.toString();
+   }
+
+   @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
+   pu

[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310023139
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseVector.java
 ##
 @@ -0,0 +1,508 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.Random;
+
+/**
+ * A dense vector represented by a values array.
+ */
+public class DenseVector extends Vector {
+
+   /**
+* Vector data.
+*/
+   double[] data;
 
 Review comment:
   Yes, it is private now.


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


[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310023397
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseVector.java
 ##
 @@ -0,0 +1,508 @@
+/*
+ * 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.commons.lang3.StringUtils;
+
+import java.util.Random;
+
+/**
+ * A dense vector represented by a values array.
+ */
+public class DenseVector extends Vector {
+
+   /**
+* Vector data.
+*/
+   double[] data;
+
+   public DenseVector() {
+   this(0);
+   }
+
+   public DenseVector(int n) {
+   this.data = new double[n];
+   }
+
+   public DenseVector(double[] data) {
+   this.data = data.clone();
+   }
+
+   @Override
+   public DenseVector clone() {
+   DenseVector c = new DenseVector();
+   c.setData(this.data.clone());
+   return c;
+   }
+
+   public static DenseVector ones(int n) {
+   DenseVector r = new DenseVector(n);
+   for (int i = 0; i < r.data.length; i++) {
+   r.data[i] = 1.0;
+   }
+   return r;
+   }
+
+   public static DenseVector zeros(int n) {
+   DenseVector r = new DenseVector(n);
+   for (int i = 0; i < r.data.length; i++) {
+   r.data[i] = 0.0;
+   }
+   return r;
+   }
+
+   public static DenseVector rand(int n) {
+   Random random = new Random();
+   DenseVector v = new DenseVector(n);
+   for (int i = 0; i < n; i++) {
+   v.set(i, random.nextDouble());
+   }
+   return v;
+   }
+
+   @Override
+   public String toString() {
+   StringBuilder sbd = new StringBuilder();
+
+   for (int i = 0; i < data.length; i++) {
+   sbd.append(data[i]);
+   if (i < data.length - 1) {
+   sbd.append(",");
+   }
+   }
+
+   return sbd.toString();
+   }
+
+   @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
+   pu

[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310023003
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,725 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * DenseMatrix stores dense matrix data and provides some methods to operate on
+ * the matrix it represents.
+ */
+public class DenseMatrix implements Serializable {
+
+   /**
+* Row dimension.
+*/
+   int m;
+
+   /**
+* Column dimension.
+*/
+   int n;
+
+   /**
+* Array for internal storage of elements.
+* 
+* The matrix data is stored in column major format internally.
+*/
+   double[] data;
+
+   /**
+* The default constructor.
+*/
+   DenseMatrix() {
+   }
+
+   /**
+* Construct an m-by-n matrix of zeros.
+*
+* @param m Number of rows.
+* @param n Number of colums.
+*/
+   public DenseMatrix(int m, int n) {
+   this(m, n, new double[m * n]);
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array should 
organize
+* in row major.
+*
+* @param mNumber of rows.
+* @param nNumber of cols.
+* @param data One-dimensional array of doubles.
+*/
+   public DenseMatrix(int m, int n, double[] data) {
+   this(m, n, data, true);
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array is 
organized
+* in column major or in row major, which is specified by parameter 
'inRowMajor'
+*
+* @param m  Number of rows.
+* @param n  Number of cols.
+* @param data   One-dimensional array of doubles.
+* @param inRowMajor Whether the matrix in 'data' is in row major 
format.
+*/
+   public DenseMatrix(int m, int n, double[] data, boolean inRowMajor) {
+   assert (data.length == m * n);
+   this.m = m;
+   this.n = n;
+   if (inRowMajor) {
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, data[i * n + j]);
+   }
+   }
+   } else {
+   this.data = data.clone();
+   }
+   }
+
+   /**
+* Construct a matrix from a 2-D array.
+*
+* @param data Two-dimensional array of doubles.
+* @throws IllegalArgumentException All rows must have the same size
+*/
+   public DenseMatrix(double[][] data) {
+   this.m = data.length;
+   this.n = data[0].length;
+   for (int i = 0; i < m; i++) {
+   if (data[i].length != n) {
+   throw new IllegalArgumentException("All rows 
must have the same size.");
+   }
+   }
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, data[i][j]);
+   }
+   }
+   }
+
+   /**
+* Create an identity matrix.
+*
+* @param n
 
 Review comment:
   Thanks. We added java docs to all methods.


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


[GitHub] [flink] 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.

2019-08-02 Thread GitBox
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_r310022422
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,725 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * DenseMatrix stores dense matrix data and provides some methods to operate on
+ * the matrix it represents.
+ */
+public class DenseMatrix implements Serializable {
+
+   /**
+* Row dimension.
 
 Review comment:
   We removed {@link DenseMatrixUtil}, so it is no longer necessary to be 
package private now. 


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


[GitHub] [flink] 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.

2019-06-20 Thread GitBox
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  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 instanceo

[GitHub] [flink] 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.

2019-06-20 Thread GitBox
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_r296091478
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,751 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * Dense Matrix.
+ */
+public class DenseMatrix implements Serializable {
+   /* 
+   Class variables
+ *  */
+
+   /**
+* Row and column dimensions.
+*/
+   int m, n;
+
+   /**
+* Array for internal storage of elements.
+* 
+* The matrix data is stored in column major format internally.
+*/
+   double[] data;
+
+/* ---
+* Constructors
+ * --- */
+
+   /**
+* Construct an empty matrix.
+*/
+   public DenseMatrix() {
+   }
+
+   /**
+* Construct an m-by-n matrix of zeros.
+*
+* @param m Number of rows.
+* @param n Number of colums.
+*/
+   public DenseMatrix(int m, int n) {
+   this.m = m;
+   this.n = n;
+   this.data = new double[m * n];
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array should 
organize
+* in row major.
+*
+* @param mNumber of rows.
+* @param nNumber of cols.
+* @param data One-dimensional array of doubles.
+*/
+   public DenseMatrix(int m, int n, double[] data) {
+   this(m, n, data, true);
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array is 
organized
+* in column major or in row major, which is specified by parameter 
'inRowMajor'
+*
+* @param m  Number of rows.
+* @param n  Number of cols.
+* @param data   One-dimensional array of doubles.
+* @param inRowMajor Whether the matrix in 'data' is in row major 
format.
+*/
+   public DenseMatrix(int m, int n, double[] data, boolean inRowMajor) {
+   assert (data.length == m * n);
+   this.m = m;
+   this.n = n;
+   if (inRowMajor) {
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, data[i * n + j]);
+   }
+   }
+   } else {
+   this.data = data.clone();
+   }
+   }
+
+   /**
+* Construct a matrix from a 2-D array.
+*
+* @param matA Two-dimensional array of doubles.
+* @throws IllegalArgumentException All rows must have the same size
+*/
+   public DenseMatrix(double[][] matA) {
+   this.m = matA.length;
+   this.n = matA[0].length;
+   for (int i = 0; i < m; i++) {
+   if (matA[i].length != n) {
+   throw new IllegalArgumentException("All rows 
must have the same size.");
+   }
+   }
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, matA[i][j]);
+   }
+   }
+   }
+
+   /**
+* Construct a matrix with provided data buffer.
+* This is for internal use only, so it is package private.
+*
+* @param mNumber of rows.
+* @param nNumber of cols.
+* @param data One-dimensional array of doubles.
+*/
+   public static DenseMatrix fromDataBuffer(int m, int n, dou

[GitHub] [flink] 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.

2019-06-20 Thread GitBox
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_r296091344
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,751 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * Dense Matrix.
+ */
+public class DenseMatrix implements Serializable {
+   /* 
+   Class variables
+ *  */
+
+   /**
+* Row and column dimensions.
+*/
+   int m, n;
+
+   /**
+* Array for internal storage of elements.
+* 
+* The matrix data is stored in column major format internally.
+*/
+   double[] data;
+
+/* ---
+* Constructors
+ * --- */
+
+   /**
+* Construct an empty matrix.
+*/
+   public DenseMatrix() {
+   }
+
+   /**
+* Construct an m-by-n matrix of zeros.
+*
+* @param m Number of rows.
+* @param n Number of colums.
+*/
+   public DenseMatrix(int m, int n) {
+   this.m = m;
+   this.n = n;
+   this.data = new double[m * n];
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array should 
organize
+* in row major.
+*
+* @param mNumber of rows.
+* @param nNumber of cols.
+* @param data One-dimensional array of doubles.
+*/
+   public DenseMatrix(int m, int n, double[] data) {
+   this(m, n, data, true);
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array is 
organized
+* in column major or in row major, which is specified by parameter 
'inRowMajor'
+*
+* @param m  Number of rows.
+* @param n  Number of cols.
+* @param data   One-dimensional array of doubles.
+* @param inRowMajor Whether the matrix in 'data' is in row major 
format.
+*/
+   public DenseMatrix(int m, int n, double[] data, boolean inRowMajor) {
+   assert (data.length == m * n);
+   this.m = m;
+   this.n = n;
+   if (inRowMajor) {
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, data[i * n + j]);
+   }
+   }
+   } else {
+   this.data = data.clone();
+   }
+   }
+
+   /**
+* Construct a matrix from a 2-D array.
+*
+* @param matA Two-dimensional array of doubles.
+* @throws IllegalArgumentException All rows must have the same size
+*/
+   public DenseMatrix(double[][] matA) {
+   this.m = matA.length;
+   this.n = matA[0].length;
+   for (int i = 0; i < m; i++) {
+   if (matA[i].length != n) {
+   throw new IllegalArgumentException("All rows 
must have the same size.");
+   }
+   }
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, matA[i][j]);
+   }
+   }
+   }
+
+   /**
+* Construct a matrix with provided data buffer.
+* This is for internal use only, so it is package private.
+*
+* @param mNumber of rows.
+* @param nNumber of cols.
+* @param data One-dimensional array of doubles.
+*/
+   public static DenseMatrix fromDataBuffer(int m, int n, dou

[GitHub] [flink] 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.

2019-06-20 Thread GitBox
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_r296091185
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,751 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * Dense Matrix.
+ */
+public class DenseMatrix implements Serializable {
+   /* 
+   Class variables
+ *  */
+
+   /**
+* Row and column dimensions.
+*/
+   int m, n;
+
+   /**
+* Array for internal storage of elements.
+* 
+* The matrix data is stored in column major format internally.
+*/
+   double[] data;
+
+/* ---
+* Constructors
+ * --- */
+
+   /**
+* Construct an empty matrix.
+*/
+   public DenseMatrix() {
+   }
+
+   /**
+* Construct an m-by-n matrix of zeros.
+*
+* @param m Number of rows.
+* @param n Number of colums.
+*/
+   public DenseMatrix(int m, int n) {
+   this.m = m;
+   this.n = n;
+   this.data = new double[m * n];
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array should 
organize
+* in row major.
+*
+* @param mNumber of rows.
+* @param nNumber of cols.
+* @param data One-dimensional array of doubles.
+*/
+   public DenseMatrix(int m, int n, double[] data) {
+   this(m, n, data, true);
+   }
+
+   /**
+* Construct a matrix from a 1-D array. The data in the array is 
organized
+* in column major or in row major, which is specified by parameter 
'inRowMajor'
+*
+* @param m  Number of rows.
+* @param n  Number of cols.
+* @param data   One-dimensional array of doubles.
+* @param inRowMajor Whether the matrix in 'data' is in row major 
format.
+*/
+   public DenseMatrix(int m, int n, double[] data, boolean inRowMajor) {
+   assert (data.length == m * n);
+   this.m = m;
+   this.n = n;
+   if (inRowMajor) {
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, data[i * n + j]);
+   }
+   }
+   } else {
+   this.data = data.clone();
+   }
+   }
+
+   /**
+* Construct a matrix from a 2-D array.
+*
+* @param matA Two-dimensional array of doubles.
+* @throws IllegalArgumentException All rows must have the same size
+*/
+   public DenseMatrix(double[][] matA) {
+   this.m = matA.length;
+   this.n = matA[0].length;
+   for (int i = 0; i < m; i++) {
+   if (matA[i].length != n) {
+   throw new IllegalArgumentException("All rows 
must have the same size.");
+   }
+   }
+   this.data = new double[m * n];
+   for (int i = 0; i < m; i++) {
+   for (int j = 0; j < n; j++) {
+   this.set(i, j, matA[i][j]);
+   }
+   }
+   }
+
+   /**
+* Construct a matrix with provided data buffer.
+* This is for internal use only, so it is package private.
+*
+* @param mNumber of rows.
+* @param nNumber of cols.
+* @param data One-dimensional array of doubles.
+*/
+   public static DenseMatrix fromDataBuffer(int m, int n, dou

[GitHub] [flink] 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.

2019-06-20 Thread GitBox
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_r296090612
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,751 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * Dense Matrix.
+ */
+public class DenseMatrix implements Serializable {
+   /* 
+   Class variables
+ *  */
+
+   /**
+* Row and column dimensions.
+*/
+   int m, n;
+
+   /**
+* Array for internal storage of elements.
+* 
+* The matrix data is stored in column major format internally.
+*/
+   double[] data;
+
+/* ---
+* Constructors
+ * --- */
+
+   /**
+* Construct an empty matrix.
+*/
+   public DenseMatrix() {
+   }
+
+   /**
+* Construct an m-by-n matrix of zeros.
+*
+* @param m Number of rows.
+* @param n Number of colums.
+*/
+   public DenseMatrix(int m, int n) {
+   this.m = m;
 
 Review comment:
   OK, changed.


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


[GitHub] [flink] 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.

2019-06-20 Thread GitBox
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_r296090504
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,751 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * Dense Matrix.
+ */
+public class DenseMatrix implements Serializable {
+   /* 
+   Class variables
+ *  */
+
+   /**
+* Row and column dimensions.
+*/
+   int m, n;
+
+   /**
+* Array for internal storage of elements.
+* 
+* The matrix data is stored in column major format internally.
+*/
+   double[] data;
+
+/* ---
+* Constructors
+ * --- */
+
+   /**
+* Construct an empty matrix.
+*/
+   public DenseMatrix() {
 
 Review comment:
   Yes, this is not necessary for the user, we have changed to package scope.


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


[GitHub] [flink] 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.

2019-06-20 Thread GitBox
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_r296089776
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/BLAS.java
 ##
 @@ -0,0 +1,109 @@
+/*
+ * 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;
+
+/**
+ * BLAS.
 
 Review comment:
   Thanks, added more description.


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


[GitHub] [flink] 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.

2019-06-20 Thread GitBox
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_r296089885
 
 

 ##
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/matrix/DenseMatrix.java
 ##
 @@ -0,0 +1,751 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * Dense Matrix.
+ */
+public class DenseMatrix implements Serializable {
+   /* 
+   Class variables
+ *  */
+
+   /**
+* Row and column dimensions.
+*/
+   int m, n;
 
 Review comment:
   declared in 2 lines.


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