Github user dbtsai commented on the pull request:
https://github.com/apache/spark/pull/3462#issuecomment-64505454
Using `foreachActive` instead of `while loop`
DenseVector: 12.95secs
SparseVector: 2.89secs
```scala
private[spark] def norm(p: Double): Double = {
require(p >= 1.0)
if (p == 1) {
var sum = 0.0
this.foreachActive { (_, value) =>
sum += math.abs(value)
}
sum
} else if (p == 2) {
var sum = 0.0
this.foreachActive { (_, value) =>
sum += value * value
}
math.sqrt(sum)
} else if (p == Double.PositiveInfinity) {
var max = 0.0
this.foreachActive { (_, value) =>
val absValue = math.abs(value)
if (absValue > max) max = absValue
}
max
} else {
var sum = 0.0
this.foreachActive { (_, value) =>
sum += math.pow(math.abs(value), p)
}
math.pow(sum, 1.0 / p)
}
}
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]