scalascala-breeze

Cant Access Matrix Rows Inside DiffFunction


I am trying to run this simple program:

object Test {
  
  def foo(args: (Double, Double, Double)*) = {
    val x = DenseMatrix(args.toList :_*)
    val r = DenseVector(5.0, 5.0, 0.0)
    println(sum(x(*, ::) - r)) // works
    val func = new DiffFunction[DenseVector[Double]] {
      def calculate(r: DenseVector[Double]) = {
        sum(x(*, ::) - r) // Doesn't work
      }
    }
  }

  def main(args: Array[String]) {
    foo((0.0, 0.0, 0.0), (5.0, 5.0, 0.0), (10.0, 0.0, 0.0))
  }
}

But I get the following error:

Missing arguments for method *(B)(OpMulMatrix.Impl2[TT, B, That])
Missing arguments for method *(B)(OpMulMatrix.Impl2[TT, B, That])
Cannot resolve symbol *

It seems i can't use the operator * to refer to each row of the matrix inside DiffFunction.

Why outside DiffFunction accessing the matrix rows with operator * and subtracting vector from them works but inside DiffFunction it doesn't ?


Solution

  • So, this is unfortunate and I haven't seen this before. What's going on is that DiffFunction actually implements ImmutableNumericOps, which means that it has all the various operators (including def *) as members. This means that when it sees * inside the class body for DiffFunction, it resolves to that method rather than the object named *.

    To fix it, you'll have to say sum(x(breeze.linalg.*, ::) - r)