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:
sum(e._2)
seems to compile fine.DenseVector
instead of of SparseVector
internally does not change anythingHow could I solve this or at least narrow down the root cause.
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.