Search code examples
scalamatriximplicitscala-breeze

Scala Breeze implicit CanMapValues


I am using the Breeze library for matrices operations in Scala. Everything looks good but it cannot find an implicit at compilation time:

could not find implicit value for parameter bf: breeze.linalg.support.CanMapValues[breeze.linalg.Matrix[Int],Int,Double,That]

The offending function is this:

import breeze.linalg._   // this is the only import
def writeMatrixToCsv(path: String, matrix: Matrix[Int]) = csvwrite(new File(path), matrix.mapValues(_.toDouble), separator = ',')

I am not sure how to proceed - I looked for a default CanMapValues in the Breeze code but couldn't find it. How can I solve this? Thanks!


Solution

  • To overcome that issue, you can add an implicit parameter of type CanMapValues to your writeMatrixToCsv function. Then it will compile. I can see that Matrix is a trait and it doesn't provide a general implicit CanMapValues, so you may have to provide one for concrete matrix , which you will be using.

    def writeMatrixToCsv(path: String, matrix: Matrix[Int])(
      implicit bf:support.CanMapValues[Matrix[Int], Int, Double, Matrix[Double]]
    ) = csvwrite(
        new File(path),
        matrix.mapValues(_.toDouble),
        separator = ','
      )
    

    CanMapValues is located in support package object