Reputation: 53806
Here I'm attempting to multiple a 2x2 Matrix by 2x1 Vector after each has been created from Lists :
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 ?
Upvotes: 2
Views: 2592
Reputation: 5572
dv1 * dv2
is not a valid operation, 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)
Upvotes: 4