0

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?

Steve Gailey
  • 129
  • 1
  • 10
  • you are trying to create a dense matrix where each element is a dense vector. Is that where you are trying to do?, or do you want to create a matrix stacking dense vectors? – Emiliano Martinez Jun 16 '22 at 08:57
  • Yes, I'm trying to create a DenseMatrix when each element is a DenseVector. I can do it with an Array, or I can do it converting that array into a DenseMatrix. In case you are wondering, I'm implanting a form of neural network. – Steve Gailey Jun 19 '22 at 11:06

1 Answers1

0

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

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

i.e. 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))
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66