def toBreeze( _v : Vector ) : BZV[Double] = {
_v match {
case x : org.apache.spark.mllib.linalg.DenseVector => new BDV(_v.toArray)
case x : org.apache.spark.mllib.linalg.SparseVector => {
val sparseVector = _v.toSparse
new BSV(sparseVector.indices, sparseVector.values, sparseVector.size)
}
case _ => {new BDV(Array[Double]())}
}
}
def fromBreeze(breezeVector: BZV[Double]): Vector = {
breezeVector match {
case v: BDV[Double] =>
if (v.offset == 0 && v.stride == 1 && v.length == v.data.length) {
new org.apache.spark.mlllib.linalg.DenseVector(v.data)
} else {
new org.apache.spark.mlllib.linalg.DenseVector(v.toArray) // Can't use underlying array directly, so make a new one
}
case v: BSV[Double] =>
if (v.index.length == v.used) {
new org.apache.spark.mlllib.linalg.SparseVector(v.length, v.index, v.data)
} else {
new org.apache.spark.mlllib.linalg.SparseVector(v.length, v.index.slice(0, v.used), v.data.slice(0, v.used))
}
case v: BZV[_] =>
sys.error("Unsupported Breeze vector type: " + v.getClass.getName)
}
}