You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/07/31 22:42:22 UTC

[GitHub] [flink] becketqin commented on a change in pull request #8631: [FLINK-12745][ml] add sparse and dense vector class, and dense matrix class with basic operations.

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

 ##########
 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
+	public DenseVector prefix(double d) {
+		double[] newVec = new double[this.size() + 1];
+		newVec[0] = d;
+		for (int i = 0; i < this.size(); i++) {
+			newVec[i + 1] = this.get(i);
+		}
+		return new DenseVector(newVec);
+	}
+
+	@Override
+	public DenseVector append(double d) {
+		double[] newVec = new double[this.size() + 1];
+		for (int i = 0; i < this.size(); i++) {
+			newVec[i] = this.get(i);
+		}
+		newVec[this.size()] = d;
+		return new DenseVector(newVec);
+	}
+
+	@Override
+	public void scaleEqual(double d) {
+		for (int i = 0; i < this.size(); i++) {
+			this.data[i] *= d;
+		}
+	}
+
+	public DenseVector plus(DenseVector other) {
+		DenseVector r = this.clone();
+		DenseVector.apply(this, other, r, ((a, b) -> a + b));
+		return r;
+	}
+
+	@Override
+	public Vector plus(Vector other) {
+		if (other instanceof DenseVector) {
+			return plus((DenseVector) other);
+		} else {
+			return ((SparseVector) other).plus(this);
+		}
+	}
+
+	public DenseVector minus(DenseVector other) {
+		DenseVector r = this.clone();
+		DenseVector.apply(this, other, r, ((a, b) -> a - b));
+		return r;
+	}
+
+	@Override
+	public Vector minus(Vector other) {
+		if (other instanceof DenseVector) {
+			return minus((DenseVector) other);
+		} else {
+			return ((SparseVector) other).scale(-1.0).plus(this);
+		}
+	}
+
+	@Override
+	public DenseVector scale(double d) {
+		DenseVector r = new DenseVector(this.data);
+		for (int i = 0; i < this.size(); i++) {
+			r.data[i] *= d;
+		}
+		return r;
+	}
+
+	public void setEqual(DenseVector other) {
 
 Review comment:
   Can we add a simple java doc to these non-overriding public 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:
users@infra.apache.org


With regards,
Apache Git Services