假设我有以下Scala代码。我正在运行Scala2.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的内部运作吗?任何帮助都会被接受。
发布于 2016-08-06 06:07:23
如果您使用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)
}https://stackoverflow.com/questions/38800495
复制相似问题