Search code examples
scalascala-breezescala-nlp

Multiplying matrices using ScalaNLP and Breeze


In below method I'm attempting to multiply a DenseVector by DenseVector where one of DenseVector is method parameter :

   def mult(features : DenseVector[Array[Int]]) = {

     val dv = new DenseVector(Array(-30, 20 , 20))

     features :* dv

   }

This causes compiler error :

Multiple markers at this line:
◾could not find implicit value for parameter op: breeze.linalg.operators.OpMulScalar.Impl2[breeze.linalg.DenseVector[Array[Int]],breeze.linalg.DenseVector[Int],That]
◾not enough arguments for method :*: (implicit op: breeze.linalg.operators.OpMulScalar.Impl2[breeze.linalg.DenseVector[Array[Int]],breeze.linalg.DenseVector[Int],That])That. Unspecified value parameter op.
◾could not find implicit value for parameter op: breeze.linalg.operators.OpMulScalar.Impl2[breeze.linalg.DenseVector[Array[Int]],breeze.linalg.DenseVector[Int],That]
◾not enough arguments for method :*: (implicit op: breeze.linalg.operators.OpMulScalar.Impl2[breeze.linalg.DenseVector[Array[Int]],breeze.linalg.DenseVector[Int],That])That. Unspecified value parameter op.

This code compiles :

def mult(features : breeze.linalg.DenseVector[Array[Int]]) = {

     val dv1 = new breeze.linalg.DenseVector(Array(-30, 20 , 20))
     val dv2 = new breeze.linalg.DenseVector(Array(-30, 20 , 20))

     dv1 :* dv2 

   }

How to multiple the features parameter by dv ? I'm not sure of the meaning of the compiler error, is related to that I do not specify the size of the DenseVector within method ?


Solution

  • I suspect your method signature to be wrong.

    Try it with

    def mult(features : DenseVector[Int])
    

    instead of

    def mult(features : DenseVector[Array[Int]]).
    

    In your case, you have a vector, where each entry is of type Array[Int].