Skip to content

Commit

Permalink
SPARK-6938: All require statements now have an informative error mess…
Browse files Browse the repository at this point in the history
…age.

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.
  • Loading branch information
Juliet Hougland authored and mengxr committed Apr 16, 2015
1 parent 8a53de1 commit 52c3439
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,16 @@ object Vectors {
* @param elements vector elements in (index, value) pairs.
*/
def sparse(size: Int, elements: Seq[(Int, Double)]): Vector = {
require(size > 0)
require(size > 0, "The size of the requested sparse vector must be greater than 0.")

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

new SparseVector(size, indices.toArray, values.toArray)
}
Expand Down Expand Up @@ -309,7 +310,8 @@ object Vectors {
* @return norm in L^p^ space.
*/
def norm(vector: Vector, p: Double): Double = {
require(p >= 1.0)
require(p >= 1.0, "To compute the p-norm of the vector, we require that you specify a p>=1. " +
s"You specified p=$p.")
val values = vector match {
case DenseVector(vs) => vs
case SparseVector(n, ids, vs) => vs
Expand Down Expand Up @@ -360,7 +362,8 @@ object Vectors {
* @return squared distance between two Vectors.
*/
def sqdist(v1: Vector, v2: Vector): Double = {
require(v1.size == v2.size, "vector dimension mismatch")
require(v1.size == v2.size, s"Vector dimensions do not match: Dim(v1)=${v1.size} and Dim(v2)" +
s"=${v2.size}.")
var squaredDistance = 0.0
(v1, v2) match {
case (v1: SparseVector, v2: SparseVector) =>
Expand Down Expand Up @@ -518,7 +521,9 @@ class SparseVector(
val indices: Array[Int],
val values: Array[Double]) extends Vector {

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

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

0 comments on commit 52c3439

Please sign in to comment.