使用Scala Squants库执行带矢量的跨产品操作。目前我正在研究Scala中的等式:
Squants有一个功能,我可以执行矢量的交叉产品,但问题是我做不到这样的事情:
val vectorQuantity = DoubleVector(1.2, 1.2, 1.2)
math.pow(vectorQuantity, 3)
有办法做到这一点吗?我也试过像vectorQuantity crossProduct vectorQuantity
这样的东西,但我只是得到编译错误type mismatch; found: squants.Vector[Double]; required: Double
。
感谢您的帮助!
答案 0 :(得分:2)
import squants.DoubleVector
object Main extends App {
println("Start :)")
val vectorQuantity = DoubleVector(1.2, 1.2, 1.2)
println(vectorQuantity)
val product = vectorQuantity crossProduct DoubleVector(0.7, 2.3, 1)
println(product)
val result = DoubleVector(vectorQuantity.coordinates.map(c => math.pow(c, 3)):_*)
println(result)
}
[sbt 0.13.7] [scala 2.11.5] [squants 0.4.2]
这可以按预期编译和工作。 不幸的是,API没有为DoubleVector公开任何类型的“地图”功能,这在这里会很不错。您可以随时要求维护人员添加此内容。
我希望这会像这样工作:
val pow3 = (id: Double) => math.pow(id, 3)
vectorQuantity.map(_.pow3)
答案 1 :(得分:1)
使用今天推出的最新SNAPSHOT版本(0.6.1-SNAPSHOT),可以解决此问题中列出的几个关键问题:
输入已更正,因此crossProduct操作在原始问题中按预期工作。也就是说,DoubleVector现在在整个API中使用,因此不再与Vector [Double]
向Vector API添加了map
操作,该操作支持对底层Vector坐标的大多数操作,包括尺寸转换(例如,Length => Area)。要在原始问题中执行操作,请尝试以下代码,类似于kpbochenek的建议:
val pow3 = (id: Double) => math.pow(id, 3)
vectorQuantity.map[Double](pow3)
请注意需要为map方法提供类型,因为map也可用于其他维度转换。例如
vectorQuantity.map[Length](Meters(_))