Skip to content

Commit 40168db

Browse files
Peng, Mengsrowen
authored andcommitted
[ML][MLLIB] The require condition and message doesn't match in SparseMatrix.
## What changes were proposed in this pull request? The require condition and message doesn't match, and the condition also should be optimized. Small change. Please kindly let me know if JIRA required. ## How was this patch tested? No additional test required. Author: Peng, Meng <[email protected]> Closes #14824 from mpjlu/smallChangeForMatrixRequire.
1 parent cd0ed31 commit 40168db

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

mllib-local/src/main/scala/org/apache/spark/ml/linalg/Matrices.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,13 @@ class SparseMatrix @Since("2.0.0") (
454454

455455
require(values.length == rowIndices.length, "The number of row indices and values don't match! " +
456456
s"values.length: ${values.length}, rowIndices.length: ${rowIndices.length}")
457-
// The Or statement is for the case when the matrix is transposed
458-
require(colPtrs.length == numCols + 1 || colPtrs.length == numRows + 1, "The length of the " +
459-
"column indices should be the number of columns + 1. Currently, colPointers.length: " +
460-
s"${colPtrs.length}, numCols: $numCols")
457+
if (isTransposed) {
458+
require(colPtrs.length == numRows + 1,
459+
s"Expecting ${numRows + 1} colPtrs when numRows = $numRows but got ${colPtrs.length}")
460+
} else {
461+
require(colPtrs.length == numCols + 1,
462+
s"Expecting ${numCols + 1} colPtrs when numCols = $numCols but got ${colPtrs.length}")
463+
}
461464
require(values.length == colPtrs.last, "The last value of colPtrs must equal the number of " +
462465
s"elements. values.length: ${values.length}, colPtrs.last: ${colPtrs.last}")
463466

mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,13 @@ class SparseMatrix @Since("1.3.0") (
572572

573573
require(values.length == rowIndices.length, "The number of row indices and values don't match! " +
574574
s"values.length: ${values.length}, rowIndices.length: ${rowIndices.length}")
575-
// The Or statement is for the case when the matrix is transposed
576-
require(colPtrs.length == numCols + 1 || colPtrs.length == numRows + 1, "The length of the " +
577-
"column indices should be the number of columns + 1. Currently, colPointers.length: " +
578-
s"${colPtrs.length}, numCols: $numCols")
575+
if (isTransposed) {
576+
require(colPtrs.length == numRows + 1,
577+
s"Expecting ${numRows + 1} colPtrs when numRows = $numRows but got ${colPtrs.length}")
578+
} else {
579+
require(colPtrs.length == numCols + 1,
580+
s"Expecting ${numCols + 1} colPtrs when numCols = $numCols but got ${colPtrs.length}")
581+
}
579582
require(values.length == colPtrs.last, "The last value of colPtrs must equal the number of " +
580583
s"elements. values.length: ${values.length}, colPtrs.last: ${colPtrs.last}")
581584

0 commit comments

Comments
 (0)