Re: [scala-breeze] generics question

25 views
Skip to first unread message

David Hall

unread,
Apr 25, 2013, 11:08:57 AM4/25/13
to scala-...@googlegroups.com
So, one of the places where Breeze beats Scalala is at being generic,
in general. That said, in Breeze, Rows are not Vectors, but Matrices,
so it's easier to work on the transpose of the matrix. So that gives:


def iota[T](m: DenseMatrix[T], v: DenseVector[T]) : Int = {
for(i <- 0 until m.rows) {
val row = m.t(::, i)
if(row.equals(v))
return i
}
-1
}

iota(DenseMatrix((1.0,2.0,3.0), (4.0,5.0,6.0)), DenseVector(4.0, 5.0, 6.0))
res5: Int = 1


In Scalala the compiler error was:

scala> def iota[T](m: DenseMatrix[T], v: DenseVector[T]) : Int = {
| for(i <- 0 until m.numRows) {
| val row = m(i, ::)
| if(row.equals(v))
| return i
| }
| -1
| }
<console>:37: error: could not find implicit value for parameter bf:
scalala.generic.collection.CanSliceRow[scalala.tensor.dense.DenseMatrix[T],Int,That]
val row = m(i, ::)

Which means you need to provide the method with the CanSlice implicit:

scala> def iota[T](m: DenseMatrix[T], v: DenseVector[T])(implicit
csr: scalala.generic.collection.CanSliceRow[scalala.tensor.dense.DenseMatrix[T],Int,DenseVector[T]])
: Int = {
| for(i <- 0 until m.numRows) {
| val row = m(i, ::)
| if(row.equals(v))
| return i
| }
| -1
| }
iota: [T](m: scalala.tensor.dense.DenseMatrix[T], v:
scalala.tensor.dense.DenseVector[T])(implicit csr:
scalala.generic.collection.CanSliceRow[scalala.tensor.dense.DenseMatrix[T],Int,scalala.tensor.dense.DenseVector[T]])Int

scala> iota(DenseMatrix((1.0,2.0,3.0), (4.0,5.0,6.0)),
DenseVector(4.0, 5.0, 6.0))
res0: Int = 1

HTH,

David

On Thu, Apr 25, 2013 at 2:31 AM, Daniel Petyus <petyus...@gmail.com> wrote:
> Hi,
>
> I'm using scalala 2.9.0 but I think this would be a question for breeze to.
> I have this very simple method to find the index of a row in a matrix:
>
> def iota(m: DenseMatrix[Int], v: DenseVector[Int]) : Int = {
> for(i <- 0 until m.numRows) {
> val row = m(i, ::)
> if(row.equals(v))
> return i
> }
> -1
> }
>
> I've tried to make it generic by changing the signature to
> - def iota[T <% Numeric[T]](m: DenseMatrix[T], v: DenseVector[T]) : Int = {
> }
> or
> - def iota[T](m: DenseMatrix[T], v: DenseVector[T])(implicit num :
> Numeric[T] ) : Int = { }
>
> none of these works.
>
> Can you help me with this?
>
> Thanks,
> `Daniel
>
> --
> 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.
> To post to this group, send email to scala-...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/scala-breeze/-/xSj9eBVJ9zsJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Daniel Petyus

unread,
Apr 25, 2013, 12:17:58 PM4/25/13
to scala-...@googlegroups.com
Hi David,

Thank you, it does help. Will check the solution in breeze.

`Daniel
Reply all
Reply to author
Forward
0 new messages