Github user chiwanpark commented on a diff in the pull request: https://github.com/apache/flink/pull/2152#discussion_r76551066 --- Diff: flink-libraries/flink-ml/src/main/scala/org/apache/flink/ml/math/distributed/BlockMapper.scala --- @@ -0,0 +1,107 @@ +/* + * 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.math.distributed + +import org.apache.flink.ml.math.distributed.DistributedMatrix.{MatrixColIndex, MatrixRowIndex} + +/** + * This class is in charge of handling all the spatial logic required by BlockMatrix. + * It introduces a new space of zero-indexed coordinates (i,j), called "mapped coordinates". + * This is a space where blocks are indexed (starting from zero). + * + * So every coordinate in the original space can be mapped to this space and to the + * corrisponding block. A block have a row and a column coordinate that explicits + * its position. Every set of coordinates in the mapped space corresponds to a square + * of size rowPerBlock x colsPerBlock. + * + */ +case class BlockMapper( //original matrix size + numRows: Int, + numCols: Int, + //block size + rowsPerBlock: Int, + colsPerBlock: Int) { + + require(numRows >= rowsPerBlock && numCols >= colsPerBlock) + val numBlockRows: Int = math.ceil(numRows * 1.0 / rowsPerBlock).toInt + val numBlockCols: Int = math.ceil(numCols * 1.0 / colsPerBlock).toInt + val numBlocks = numBlockCols * numBlockRows + + /** + * Translates absolute coordinates to the mapped coordinates of the block + * these coordinates belong to. + * @param i + * @param j + * @return + */ + def absCoordToMappedCoord(i: MatrixRowIndex, j: MatrixColIndex): (Int, Int) = + getBlockMappedCoordinates(getBlockIdByCoordinates(i, j)) + + /** + * Retrieves a block id from original coordinates + * @param i Original row + * @param j Original column + * @return Block ID + */ + def getBlockIdByCoordinates(i: MatrixRowIndex, j: MatrixColIndex): Int = { + + if (i < 0 || j < 0 || i >= numRows || j >= numCols) { + throw new IllegalArgumentException(s"Invalid coordinates ($i,$j).") --- End diff -- Following block with `require` would be better: ```scala require(0 <= i && i < numRows && 0 <= j && j < numCols, s"Invalid coordinates ($i, $j).") ```
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---