0

There are plenty of other questions round the error message could not find implicit value in Scala. The answers are very specific and are not applicable to this specific issue.

In Scala Breeze, I am trying to apply argmax to a SparseVector[Int]. According to the documentation (and intuition), this works easily with

argmax(SparseVector.zeros[Int](5))

after importing breeze.linalg._.

My actual testing code looks like this:

import breeze.linalg.{Vector, argmax, sum}

val counts: Map[Int, Vector[Int]] = ...
counts
  .filter(e => sum(e._2) > 10)
  .take(100)
  .map(e => (e._1, argmax(e._2)))
  .foreach(println)

However, the compiler throws the following error message:

Error:(41, 37) could not find implicit value for parameter impl: breeze.linalg.argmax.Impl[breeze.linalg.Vector[Int],VR]
.map(e => (e._1, argmax(e._2)))
                       ^

Some more or less surprising observations:

  • the sum(e._2) seems to compile fine.
  • using a DenseVector instead of of SparseVector internally does not change anything

How could I solve this or at least narrow down the root cause.

Carsten
  • 1,912
  • 1
  • 28
  • 55

1 Answers1

0

I've solved the issue by explicitly declaring the type as SparseVector instead of Vector:

val counts: Map[Int, SparseVector[Int]] = ...

To my understanding, this solution is far from obvious and it remains unclear to me why argmax() seems to be unable to handle the more generic Vector class, but it works.

Carsten
  • 1,912
  • 1
  • 28
  • 55
  • I find breeze's code quite hard to navigate, but I think the error message is quite clear. argmax cant' find some implicit that it needs for the type Vector. Looking briefly at the implementation of argmax, it seems to need an implicit of type `CanTraverseKeyValuePairs` which can easily be found for types Sparse/Dense Vector, but there is no implementation for Vector. – melps Nov 13 '15 at 10:51
  • This is probably true if you have fully grasped the usage of implicits (in Breeze). – Carsten Nov 13 '15 at 11:04
  • To be honest, it's a generic issue when using typeclasses – melps Nov 13 '15 at 11:13
  • I feel like this is my answer more often than not, but please open a ticket on github. In general, I haven't put as much effort into making sure everything works great with Vector as I have with DenseVector and SparseVector. @melps I agree the code is hard to follow sometimes. Let me know if there's anything in particular I can improve – dlwh Nov 13 '15 at 19:46
  • 2
    ok, this should be fixed in master/the snapshots I'm publishing right now. – dlwh Nov 14 '15 at 09:22