Luis dos Reis Luis dos Reis - 4 months ago 30
Scala Question

Scala Breeze zipValues issue

Say I have the following Scala code. I am running Scala 2.11.8 and Breeze 0.13.

val a: DenseVector[Double] = DenseVector(1.1, 1.2, 1.3)
val b: DenseVector[Double] = DenseVector(1.1, 1.2, 1.3)

val v: DenseVector[Double] = zipValues(a, b) ((ai: Double, bi: Double) => ai + bi)


I get a type mismatch compilation error which gets translated to:

[error] /Users/luishreis/Documents/projects/scala/sbt/GA/src/main/scala/ga_class.scala:119: type mismatch;
[error] found : (Double, Double) => Double
[error] required: breeze.linalg.zipValues.Impl2[breeze.linalg.DenseVector[Double],breeze.linalg.DenseVector[Double],?]
[error] (which expands to) breeze.generic.UFunc.UImpl2[breeze.linalg.zipValues.type,breeze.linalg.DenseVector[Double],breeze.linalg.DenseVector[Double],?]
[error] val v: DenseVector[Double] = zipValues(a, b) ((ai: Double, bi: Double) => ai + bi)


I have tried with different types and such, but no success. Anyone care to shine a light on the inner workings of zipValue? Any help would be apreciated.

Answer

If you search the repository for zipValues, you'll see it's normally used as zipValues(a, b).foreach(...). Your case would probably need zipValues(a, b).map((ai, bi) => ai + bi), but unfortunately, it isn't currently defined:

/**
 * Usually used as the return type from zipValues
 * @tparam V1
 * @tparam V2
 */
trait ZippedValues[@specialized(Double) V1, @specialized(Double) V2] {
  def foreach(f: (V1,V2) => Unit)

  def exists(f: (V1, V2)=>Boolean):Boolean = {
    foreach((a,b) => if (f(a,b)) return true)
    false
  }

  def forall(f: (V1, V2)=>Boolean):Boolean = {
    foreach((a,b) => if (!f(a,b)) return false)
    true
  }
  // TODO: define map for this.
//  def map[A](a: Coll1, b: Coll2, f: (V1,V2)=>A)(implicit canZipMapValues)
}