novicecoder123
novicecoder123

Reputation: 19

How to convert RowMatrix to BDM (Breeze Dense Matrix) and more questions

trying to convert a RowMatrix into BDM (Breeze Dense Matrix), not sure how to proceed need to implement

  def getDenseMatrix(A: RowMatrix): BDM[Double] = {
  //write code here
  }

additional questions:

  1. how to convert a RowMatrix into a Matrix?
  2. How to access a particular Row in the RowMatrix?

    for(i <- 0 to (RowM.numCols().toInt-1)){ //How to access RowM.rows(i) }

  3. How to access a particular column in the RowMatrix?

    for(i <- 0 to (RowM.numCols().toInt-1)){ //How to access RowM.rows.map(f=>f(i)) }

  4. How to multiply 2 RowMatrices note: RowMatrix has a API 'multiply' but it need the argument of type Matrix say A and B are RowMatices, then AB = A.multiply(B), this will not work, as B
    is a RowMatrix and not Matrix
  5. And lastly how to convert a BDM to a RowMatrix?

Upvotes: 2

Views: 770

Answers (2)

Miguel
Miguel

Reputation: 3952

If you don't have foreachActive available use this:

def toBreeze(X: RowMatrix): BDM[Double]{
    val m = X.numRows().toInt
    val n = X.numCols().toInt
    val mat = BDM.zeros[Double](m, n)
    var i = 0
    X.rows.collect().map{
      case sp: SparseVector => (sp.indices, sp.values)
      case dp: DenseVector  => (Range(0,n).toArray, dp.values)
    }.foreach {
      case (indices, values) => indices.zip(values).foreach { case (j, v) =>mat(i, j) = v }
      i += 1
    }
    mat
}

This is parallel up to a certain point and do the same.

Upvotes: 2

user48135
user48135

Reputation: 481

Read the source code in rowMatrix, you want the source code, it is in private method. The code is below:

 def toBreeze(mat:RowMatrix):BDM[Double] = {
    val m = mat.numRows()
    val n = mat.numCols()
    val result = BDM.zeros[Double](m.toInt,n.toInt)
    var i = 0

    mat.rows.collect().foreach{Vector =>
    Vector.foreachActive { case(index,value) =>
        result(i,index) = value
    }
      i+=1
    }
    result

    }

Upvotes: 4

Related Questions