scalanlp / breeze

Breeze is/was a numerical processing library for Scala.
https://www.scalanlp.org
Apache License 2.0
3.44k stars 692 forks source link

Diverging Implicits with MutableOptimizationSpace Context Bound #404

Open tomerk opened 9 years ago

tomerk commented 9 years ago

We are currently trying to make templatized methods that can operate on various numeric vectors & matrices.

At @dlwh's suggestion we have tried the following:

type BreezeNumeric[T] = MutableOptimizationSpace[DenseMatrix[T], DenseVector[T], T]
def reducePoint[T : BreezeNumeric](x: DenseVector[T], mat: DenseMatrix[T]): DenseVector[T] = {
  (x.t * mat).t
}

But on compilation we are getting a diverging implicits error:

Error:(41, 10) diverging implicit expansion for type breeze.linalg.operators.OpMulMatrix.Impl2[breeze.linalg.Transpose[breeze.linalg.DenseVector[T]],breeze.linalg.DenseMatrix[T],That]
starting with method implOpMulMatrix_DVTt_DMT_eq_DMT in trait DenseMatrixMultiplyStuff
    (x.t * mat).t
         ^
dlwh commented 9 years ago

oh, that's different than I thought.

try:

def reducePoint[T : BreezeNumeric](x: DenseVector[T], mat: DenseMatrix[T]):
DenseVector[T] = {

  val ops = implicitly[BreezeNumerics[T]]

  import ops._
  (x.t * mat).t
}

Hopefully you're ok with that much magic?

tomerk commented 9 years ago

That works, thanks!. But, now I'm trying to do something more complicated:

def colMeans[T : BreezeNumeric](dataMat: DenseMatrix[T]) = {
  val ops = implicitly[BreezeNumerics[T]]
  import ops._

  val means = (mean(dataMat(::, *))).toDenseVector
}

And I'm getting the following error on compilation:

Error:(56, 22) could not find implicit value for parameter impl: breeze.stats.mean.Impl[breeze.linalg.BroadcastedColumns[breeze.linalg.DenseMatrix[T],breeze.linalg.DenseVector[T]],VR]
    val means = (mean(dataMat(::, *))).toDenseVector
                     ^

Any suggestions on how to get broadcasting to work correctly here too?

dlwh commented 9 years ago

Hrm, I don't think anyone's tried broadcasting using these spaces. Let me see what I can do.

gabeos commented 9 years ago

if you look at the MutableOptimizationSpace companion object, you'll be able to see all the implicits that are included. I don't believe that broadcasting is one of them, possibly because the CSC Matrix isn't at parity with dense? Will take a look to cite code locations tomorrow.

etrain commented 9 years ago

@gabeos @dlwh - any further thoughts on this?