My solution is kind of a hybrid of those of @barclar and @lev, above. You don't need to put your code in the org.apache.spark.mllib.linalg
if you don't make use of the spark-ml implicit conversions. You can define your own implicit conversions in your own package, like:
package your.package
import org.apache.spark.ml.linalg.DenseVector
import org.apache.spark.ml.linalg.SparseVector
import org.apache.spark.ml.linalg.Vector
import breeze.linalg.{DenseVector => BDV, SparseVector => BSV, Vector => BV}
object BreezeConverters
{
implicit def toBreeze( dv: DenseVector ): BDV[Double] =
new BDV[Double](dv.values)
implicit def toBreeze( sv: SparseVector ): BSV[Double] =
new BSV[Double](sv.indices, sv.values, sv.size)
implicit def toBreeze( v: Vector ): BV[Double] =
v match {
case dv: DenseVector => toBreeze(dv)
case sv: SparseVector => toBreeze(sv)
}
implicit def fromBreeze( dv: BDV[Double] ): DenseVector =
new DenseVector(dv.toArray)
implicit def fromBreeze( sv: BSV[Double] ): SparseVector =
new SparseVector(sv.length, sv.index, sv.data)
implicit def fromBreeze( bv: BV[Double] ): Vector =
bv match {
case dv: BDV[Double] => fromBreeze(dv)
case sv: BSV[Double] => fromBreeze(sv)
}
}
Then you can import these implicits into your code with:
import your.package.BreezeConverters._