我怎样才能在 Scala Squants 中用矢量做指数(幂)?

How can I do exponents (power) with vector quantities in Scala Squants?

使用 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.

感谢您的帮助!

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 公开任何类型的 "map" 功能,这在这里会很好。您可以随时要求维护者添加此内容。

我希望它能像这样工作:

val pow3 = (id: Double) => math.pow(id, 3)
vectorQuantity.map(_.pow3)

今天最新的 SNAPSHOT 版本 (0.6.1-SNAPSHOT) 解决了这个问题中概述的几个关键问题:

  1. 输入已更正,因此 crossProduct 操作在原始问题中按预期工作。也就是说,现在整个 API 都使用 DoubleVector,因此不会再与 Vector[Double]

  2. 发生冲突
  3. map 操作已添加到 Vector API,它支持对基础 Vector 坐标的大多数操作,包括维度转换(例如,长度 => 面积) .要执行原始问题中的操作,请尝试类似于 kpbochenek 的建议的以下代码:

val pow3 = (id: Double) => math.pow(id, 3) vectorQuantity.map[Double](pow3)

请注意,需要为 map 方法提供类型,因为 map 也可用于其他维度转换。例如

vectorQuantity.map[Length](Meters(_))