How to create generic code with DenseMatrix?

31 views
Skip to first unread message

Andrey Ilinykh

unread,
Sep 15, 2017, 12:50:31 AM9/15/17
to Scala Breeze
Hello everybody!
I have very simple problem. I want to create generic code which should operate with different matrix type - mostly DenseMatrix[Double] or DenseMatrix[Float]. Something like this:

class MyClass[T](a: DenseMatrix[T], b: DenseMatrix[T]) {
def myfunc = {
  a * b
}
//more methods here ...

}


and later use as 
  new MyClass[Float](a, b) 
or
 new MyClass[Double](a, b)

but it doesn't compile. The error I got is

Error:(87, 9) diverging implicit expansion for type breeze.linalg.operators.OpMulMatrix.Impl2[breeze.linalg.DenseMatrix[T],breeze.linalg.DenseMatrix[T],That]
starting with method opMulMatrixFromSemiring in object OpMulMatrix
      a * b

What would be the right way to do it?

Thank you,
  Andrey

David Hall

unread,
Sep 15, 2017, 2:17:57 PM9/15/17
to scala-...@googlegroups.com
In Breeze, operations are provided via an implicit parameter that provides the implementation. This allows for a lot more flexibility and extensibility in terms of how things are put together. This is provided via the UFunc abstraction (https://github.com/scalanlp/breeze/wiki/Universal-Functions


More concretely, you need class MyClass[T](a: DenseMatrix[T], b: DenseMatrix[T])(implicit mul: OpMulMatrix.Impl2[DenseMatrix[T], DenseMatrix[T], DenseMatrix[T]])

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/b6553c22-3079-45bb-a28e-dcb3cceeab72%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrey Ilinykh

unread,
Sep 15, 2017, 7:26:03 PM9/15/17
to Scala Breeze
Cool. It works. Thank you!


On Friday, September 15, 2017 at 11:17:57 AM UTC-7, David Hall wrote:
In Breeze, operations are provided via an implicit parameter that provides the implementation. This allows for a lot more flexibility and extensibility in terms of how things are put together. This is provided via the UFunc abstraction (https://github.com/scalanlp/breeze/wiki/Universal-Functions


More concretely, you need class MyClass[T](a: DenseMatrix[T], b: DenseMatrix[T])(implicit mul: OpMulMatrix.Impl2[DenseMatrix[T], DenseMatrix[T], DenseMatrix[T]])
On Thu, Sep 14, 2017 at 9:36 PM, Andrey Ilinykh <aili...@gmail.com> wrote:
Hello everybody!
I have very simple problem. I want to create generic code which should operate with different matrix type - mostly DenseMatrix[Double] or DenseMatrix[Float]. Something like this:

class MyClass[T](a: DenseMatrix[T], b: DenseMatrix[T]) {
def myfunc = {
  a * b
}
//more methods here ...

}


and later use as 
  new MyClass[Float](a, b) 
or
 new MyClass[Double](a, b)

but it doesn't compile. The error I got is

Error:(87, 9) diverging implicit expansion for type breeze.linalg.operators.OpMulMatrix.Impl2[breeze.linalg.DenseMatrix[T],breeze.linalg.DenseMatrix[T],That]
starting with method opMulMatrixFromSemiring in object OpMulMatrix
      a * b

What would be the right way to do it?

Thank you,
  Andrey

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages