Skip to content

Commit

Permalink
RowMatrix.scala
Browse files Browse the repository at this point in the history
* numCols(): Added check for numRows = 0, with error message.
* computeCovariance(): Added check for numRows <= 1, with error message.
  • Loading branch information
jkbradley committed Aug 10, 2014
1 parent 65e4ebc commit ab48f6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ class RowMatrix(
/** Gets or computes the number of columns. */
override def numCols(): Long = {
if (nCols <= 0) {
// Calling `first` will throw an exception if `rows` is empty.
nCols = rows.first().size
try {
// Calling `first` will throw an exception if `rows` is empty.
nCols = rows.first().size
} catch {
case err: UnsupportedOperationException =>
sys.error("Cannot determine the number of cols because it is not specified in the " +
"constructor and the rows RDD is empty.")
}
}
nCols
}
Expand Down Expand Up @@ -293,6 +299,10 @@ class RowMatrix(
(s1._1 + s2._1, s1._2 += s2._2)
)

if (m <= 1) {
sys.error(s"RowMatrix.computeCovariance called on matrix with only $m rows." +
" Cannot compute the covariance of a RowMatrix with <= 1 row.")
}
updateNumRows(m)

mean :/= m.toDouble
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ class CorrelationSuite extends FunSuite with LocalSparkContext {
test("corr(x, y) pearson, 1 value in data") {
val x = sc.parallelize(Array(1.0))
val y = sc.parallelize(Array(4.0))
assert(Statistics.corr(x, y, "pearson").isNaN)
assert(Statistics.corr(x, y, "spearman").isNaN)
intercept[RuntimeException] {
Statistics.corr(x, y, "pearson")
}
intercept[RuntimeException] {
Statistics.corr(x, y, "spearman")
}
}

test("corr(x, y) default, pearson") {
Expand Down

0 comments on commit ab48f6e

Please sign in to comment.