Skip to content

Commit 52c3439

Browse files
Juliet Houglandmengxr
authored andcommitted
SPARK-6938: All require statements now have an informative error message.
This pr adds informative error messages to all require statements in the Vectors class that did not previously have them. This references [SPARK-6938](https://issues.apache.org/jira/browse/SPARK-6938). Author: Juliet Hougland <[email protected]> Closes apache#5532 from jhlch/SPARK-6938 and squashes the following commits: ab321bb [Juliet Hougland] Remove braces from string interpolation when not required. 1221f94 [Juliet Hougland] All require statements now have an informative error message.
1 parent 8a53de1 commit 52c3439

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,16 @@ object Vectors {
227227
* @param elements vector elements in (index, value) pairs.
228228
*/
229229
def sparse(size: Int, elements: Seq[(Int, Double)]): Vector = {
230-
require(size > 0)
230+
require(size > 0, "The size of the requested sparse vector must be greater than 0.")
231231

232232
val (indices, values) = elements.sortBy(_._1).unzip
233233
var prev = -1
234234
indices.foreach { i =>
235235
require(prev < i, s"Found duplicate indices: $i.")
236236
prev = i
237237
}
238-
require(prev < size)
238+
require(prev < size, s"You may not write an element to index $prev because the declared " +
239+
s"size of your vector is $size")
239240

240241
new SparseVector(size, indices.toArray, values.toArray)
241242
}
@@ -309,7 +310,8 @@ object Vectors {
309310
* @return norm in L^p^ space.
310311
*/
311312
def norm(vector: Vector, p: Double): Double = {
312-
require(p >= 1.0)
313+
require(p >= 1.0, "To compute the p-norm of the vector, we require that you specify a p>=1. " +
314+
s"You specified p=$p.")
313315
val values = vector match {
314316
case DenseVector(vs) => vs
315317
case SparseVector(n, ids, vs) => vs
@@ -360,7 +362,8 @@ object Vectors {
360362
* @return squared distance between two Vectors.
361363
*/
362364
def sqdist(v1: Vector, v2: Vector): Double = {
363-
require(v1.size == v2.size, "vector dimension mismatch")
365+
require(v1.size == v2.size, s"Vector dimensions do not match: Dim(v1)=${v1.size} and Dim(v2)" +
366+
s"=${v2.size}.")
364367
var squaredDistance = 0.0
365368
(v1, v2) match {
366369
case (v1: SparseVector, v2: SparseVector) =>
@@ -518,7 +521,9 @@ class SparseVector(
518521
val indices: Array[Int],
519522
val values: Array[Double]) extends Vector {
520523

521-
require(indices.length == values.length)
524+
require(indices.length == values.length, "Sparse vectors require that the dimension of the" +
525+
s" indices match the dimension of the values. You provided ${indices.size} indices and " +
526+
s" ${values.size} values.")
522527

523528
override def toString: String =
524529
"(%s,%s,%s)".format(size, indices.mkString("[", ",", "]"), values.mkString("[", ",", "]"))

0 commit comments

Comments
 (0)