0

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!

ticofab
  • 7,551
  • 13
  • 49
  • 90
  • Did you use any imports there? What is this csvwrite? – michaJlS Oct 15 '16 at 13:25
  • hey @michaJlS , csvwrite is a Breeze method (see https://github.com/scalanlp/breeze/wiki/Linear-Algebra-Cheat-Sheet). I added my import in the question. The issue is in .mapValues . – ticofab Oct 15 '16 at 13:31

1 Answers1

0

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

michaJlS
  • 2,465
  • 1
  • 16
  • 22
  • It compiles! I am not sure why though: as you say, the concrete CanMapValues is not implemented anywhere. So where does it find its implicit now, and why it compiles now? – ticofab Oct 15 '16 at 19:05
  • 1
    In this case, csvwrite enclosed within another func has no chance to find valid implicit value, so you have to "let it in" through param. Try to use it and let see what happens. There are `CanMapValues` for some classes extending `Matrix`. – michaJlS Oct 15 '16 at 19:15
  • awesome. Thank you! – ticofab Oct 15 '16 at 20:34