【问题标题】:Scala Breeze zipValues issueScala Breeze zipValues 问题
【发布时间】:2016-12-12 12:48:52
【问题描述】:

假设我有以下 Scala 代码。我正在运行 Scala 2.11.8 和 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)

我得到一个类型不匹配的编译错误,它被翻译为:

[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)

我尝试过不同的类型等,但没有成功。有人愿意了解 zipValue 的内部工作原理吗?任何帮助将不胜感激。

【问题讨论】:

    标签: scala scala-breeze


    【解决方案1】:

    如果您使用search the repository for zipValues,您会看到它通常用作zipValues(a, b).foreach(...)。您的案例可能需要zipValues(a, b).map((ai, bi) => ai + bi),但不幸的是,目前尚未定义:

    /**
     * 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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2017-06-02
      • 1970-01-01
      相关资源
      最近更新 更多