Github user srowen commented on a diff in the pull request:
https://github.com/apache/spark/pull/12299#discussion_r59363105
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/BLAS.scala ---
@@ -237,25 +237,53 @@ private[spark] object BLAS extends Serializable with
Logging {
}
/**
- * Adds alpha * x * x.t to a matrix in-place. This is the same as BLAS's
?SPR.
+ * Adds alpha * v * v.t to a matrix in-place. This is the same as BLAS's
?SPR.
*
* @param U the upper triangular part of the matrix in a
[[DenseVector]](column major)
*/
def spr(alpha: Double, v: Vector, U: DenseVector): Unit = {
- spr(alpha, v, U.values)
+ spr(alpha, v, U.values, null)
}
/**
- * Adds alpha * x * x.t to a matrix in-place. This is the same as BLAS's
?SPR.
+ * Adds alpha * v * v.t to a matrix in-place. This is the same as BLAS's
?SPR.
*
+ * @param alpha scaling factor
+ * @param v vector argument
* @param U the upper triangular part of the matrix packed in an array
(column major)
+ * @param dv an optional vector of values to subtract from each value of
v before computing
*/
- def spr(alpha: Double, v: Vector, U: Array[Double]): Unit = {
+ def spr(alpha: Double, v: Vector, U: Array[Double], dv: Vector): Unit = {
val n = v.size
v match {
case DenseVector(values) =>
- NativeBLAS.dspr("U", n, alpha, values, 1, U)
- case SparseVector(size, indices, values) =>
+ // If shifted, copy and shift values
+ val shiftedValues =
+ if (dv == null) {
+ values
+ } else {
+ val copy = values.clone()
+ var i = 0
+ while (i < copy.length) {
+ copy(i) -= dv(i)
+ i += 1
+ }
+ copy
+ }
+ NativeBLAS.dspr("U", n, alpha, shiftedValues, 1, U)
+
+ case sparse: SparseVector if dv != null =>
+ // Sparse case, but with shift; treat it as dense
+ val dense = sparse.toArray
--- End diff --
I couldn't think of a meaningfully faster algorithm in this case than just
treating the input as dense
---
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]