简体   繁体   English

Scala微风中的向量乘矩阵乘法

[英]Vector by Matrix multiplication in Scala breeze

Here I'm attempting to multiple a 2x2 Matrix by 2x1 Vector after each has been created from Lists : 在这里,我尝试从Lists创建每个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

But the multiply operation throws error : 但是乘法运算会引发错误:

 \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

The dimensions are correct so this error should not be thrown ? 尺寸正确,因此不应引发此错误?

For this part of error : DenseMatrix[Double],That] should That be the return type of the Matrix, if so how to set ? 对于错误的这一部分: DenseMatrix[Double],That]应该That是矩阵的返回类型,如果是的话怎么设置?

dv1 * dv2 is not a valid operation, ncol(dv1) = 1 != nrow(dv2) = 2 . dv1 * dv2不是有效的操作, ncol(dv1) = 1 != nrow(dv2) = 2 Switching the order and using the * operator gives you what you want: 切换顺序并使用*运算符可提供所需的内容:

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$36.apply(DenseMatrixOps.scala:100)
  at breeze.linalg.operators.DenseMatrixMultiplyStuff$$anon$36.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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM