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.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to