Scala breeze 中的矩阵乘法向量

Vector by Matrix multiplication in Scala breeze

在这里,我尝试在从列表创建每个矩阵后将 2x2 矩阵乘以 2x1 向量:

val x1 = List[Double](1.0,2.0);
val x2 = List[List[Double]](List(1,2) , List(3,4));
val dv1 = DenseVector[Double]((x1.toArray):_*) 
val dv2 = DenseMatrix(((x2).toArray):_*) 
val h = dv1 :* dv2

但是乘法运算会抛出错误:

 \Main.scala:50: could not find implicit value for parameter op: breeze.linalg.operators.OpMulScalar.Impl2[breeze.linalg.DenseVector[Double],breeze.linalg.DenseMatrix[Double],That]
[error] val h = dv1 :* dv2

尺寸正确,因此不应引发此错误?

这部分错误:DenseMatrix[Double],That]应该That是return类型的矩阵,如果是怎么设置?

dv1 * dv2 不是有效操作,ncol(dv1) = 1 != nrow(dv2) = 2。切换顺序并使用 * 运算符可以为您提供所需的内容:

scala> import breeze.linalg._
import breeze.linalg._

scala> :pa
// Entering paste mode (ctrl-D to finish)

val x1 = List[Double](1.0,2.0);
val x2 = List[List[Double]](List(1,2) , List(3,4));
val dv1 = DenseVector[Double]((x1.toArray):_*)
val dv2 = DenseMatrix(((x2).toArray):_*)

// Exiting paste mode, now interpreting.

x1: List[Double] = List(1.0, 2.0)
x2: List[List[Double]] = List(List(1.0, 2.0), List(3.0, 4.0))
dv1: breeze.linalg.DenseVector[Double] = DenseVector(1.0, 2.0)
dv2: breeze.linalg.DenseMatrix[Double] =
1.0  2.0
3.0  4.0

scala> dv1 * dv2
java.lang.IllegalArgumentException: requirement failed: b.rows == 1 (2 != 1)
  at breeze.linalg.operators.DenseMatrixMultiplyStuff$$anon.apply(DenseMatrixOps.scala:100)
  at breeze.linalg.operators.DenseMatrixMultiplyStuff$$anon.apply(DenseMatrixOps.scala:98)
  at breeze.linalg.ImmutableNumericOps$class.$times(NumericOps.scala:135)
  at breeze.linalg.DenseVector.$times(DenseVector.scala:51)
  ... 43 elided

scala> dv2 * dv1
res2: breeze.linalg.DenseVector[Double] = DenseVector(5.0, 11.0)