在这里,我试图在从列表创建每个2x2矩阵之后将其复用2x2矩阵:
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
应该是Matrix的返回类型,如果是,如何设置?
答案 0 :(得分:2)
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$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)