Matrix Vector Subtraction

251 views
Skip to first unread message

Kishore Nair

unread,
Apr 29, 2015, 7:16:15 AM4/29/15
to scala-...@googlegroups.com
I want to subtract the DenseMatrix with DenseVector

val matrix = new DenseMatrix[Double](3, 3, Array[Double](1.7,2.9,3.9,4.6,5,6.7,7.9,8.9,9))
  
val vectorMatrix = new DenseVector[Double](Array[Double](1,2,3))
  
println(matrix :- vectorMatrix.t )


This is throwing an error. Am I doing something wrong ?


David Hall

unread,
Apr 29, 2015, 12:06:03 PM4/29/15
to scala-...@googlegroups.com
You can't subtract differently shaped things in Breeze without using broadcasting. (The below ought to require vectorMatrix.t, but it currently doesn't...)

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

scala> new DenseMatrix[Double](3, 3, Array[Double](1.7,2.9,3.9,4.6,5,6.7,7.9,8.9,9))
res0: breeze.linalg.DenseMatrix[Double] =
1.7  4.6  7.9
2.9  5.0  8.9
3.9  6.7  9.0

scala> val dm = new DenseMatrix[Double](3, 3, Array[Double](1.7,2.9,3.9,4.6,5,6.7,7.9,8.9,9))
dm: breeze.linalg.DenseMatrix[Double] =
1.7  4.6  7.9
2.9  5.0  8.9
3.9  6.7  9.0

scala> val vectorMatrix = new DenseVector[Double](Array[Double](1,2,3))
vectorMatrix: breeze.linalg.DenseVector[Double] = DenseVector(1.0, 2.0, 3.0)

scala> dm(*, ::) - vectorMatrix
Apr 29, 2015 9:05:21 AM com.github.fommil.netlib.BLAS <clinit>
WARNING: Failed to load implementation from: com.github.fommil.netlib.NativeSystemBLAS
Apr 29, 2015 9:05:21 AM com.github.fommil.netlib.BLAS <clinit>
WARNING: Failed to load implementation from: com.github.fommil.netlib.NativeRefBLAS
res1: breeze.linalg.DenseMatrix[Double] =
0.7  2.5999999999999996  4.9
1.9  3.0                 5.9
2.9  4.7                 6.0

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze...@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/2bcadd7d-6578-4e56-8fec-cdebb6f91260%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kishore Nair

unread,
Apr 29, 2015, 2:41:45 PM4/29/15
to scala-...@googlegroups.com
Thanks David Hall, I understand it.
Reply all
Reply to author
Forward
0 new messages