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