diff --git a/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala b/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala index 1bfb61dd6a21b..81f11cf147a74 100644 --- a/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala +++ b/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala @@ -92,45 +92,6 @@ sealed trait Vector extends Serializable { * @return norm in L^p^ space. */ private[spark] def norm(p: Double): Double - - protected def norm(p: Double, values: Array[Double]): Double = { - require(p >= 1.0) - val size = values.size - if (p == 1) { - var sum = 0.0 - var i = 0 - while (i < size) { - sum += math.abs(values(i)) - i += 1 - } - sum - } else if (p == 2) { - var sum = 0.0 - var i = 0 - while (i < size) { - sum += values(i) * values(i) - i += 1 - } - math.sqrt(sum) - } else if (p == Double.PositiveInfinity) { - var max = 0.0 - var i = 0 - while (i < size) { - val value = math.abs(values(i)) - if (value > max) max = value - i += 1 - } - max - } else { - var sum = 0.0 - var i = 0 - while (i < size) { - sum += math.pow(math.abs(values(i)), p) - i += 1 - } - math.pow(sum, 1.0 / p) - } - } } /** @@ -307,6 +268,45 @@ object Vectors { sys.error("Unsupported Breeze vector type: " + v.getClass.getName) } } + + private[linalg] def norm(p: Double, values: Array[Double]): Double = { + require(p >= 1.0) + val size = values.size + if (p == 1) { + var sum = 0.0 + var i = 0 + while (i < size) { + sum += math.abs(values(i)) + i += 1 + } + sum + } else if (p == 2) { + var sum = 0.0 + var i = 0 + while (i < size) { + sum += values(i) * values(i) + i += 1 + } + math.sqrt(sum) + } else if (p == Double.PositiveInfinity) { + var max = 0.0 + var i = 0 + while (i < size) { + val value = math.abs(values(i)) + if (value > max) max = value + i += 1 + } + max + } else { + var sum = 0.0 + var i = 0 + while (i < size) { + sum += math.pow(math.abs(values(i)), p) + i += 1 + } + math.pow(sum, 1.0 / p) + } + } } /** @@ -340,7 +340,7 @@ class DenseVector(val values: Array[Double]) extends Vector { } } - private[spark] override def norm(p: Double): Double = norm(p, values) + private[spark] override def norm(p: Double): Double = Vectors.norm(p, values) } /** @@ -390,5 +390,5 @@ class SparseVector( } } - private[spark] override def norm(p: Double): Double = norm(p, values) + private[spark] override def norm(p: Double): Double = Vectors.norm(p, values) }