简体   繁体   中英

Problem with evidence parameters in breeze

I'm trying to build a DenseMatrix of Vectors in breeze. However I keep getting the error message:

could not find implicit value for evidence parameter of type breeze.storage.Zero[breeze.linalg.DenseVector[Double]]

for the line:

  val som: DenseMatrix[DenseVector[Double]] = DenseMatrix.tabulate(5, 5){ (i, j) => DenseVector.rand(20)}

Even though doing something similar with a Scala Array works fine:

val som = Array.tabulate(5, 5)((i, j) => DenseVector.rand(20))

I'm not sure what it is I'm doing wrong or what I'm missing? To be honest I don't understand what the error message is telling me... I don't do enough Scala programming to understand this? What even is an Evidence parameter and can I explicitly specify it or do I need an implicit?

This is because DenseMatrix.tabulate[V] firstly fills the matrix with zeroes. So there should be an instance of type class Zero for V , ie in our case for DenseVector[Double] . You can define it yourself eg

implicit def denseVectorZero[V: Zero : ClassTag]: Zero[DenseVector[V]] =
  new Zero(DenseVector.zeros(0))

ie if we know Zero for V then we know Zero for DenseVector[V] .

Or even easier

implicit def ev[V: ClassTag]: Zero[DenseVector[V]] = new Zero(DenseVector(Array.empty))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM