LoginSignup
5
4

More than 5 years have passed since last update.

ScalaのBreezeを使って行列計算

Last updated at Posted at 2016-07-24

自分用メモ

sbtプロジェクトの準備

scalanlp/breeze

mkdir breeze_test
cd breeze_test
touch build.sbt
build.sbt
libraryDependencies  ++= Seq(
  // Last stable release
  "org.scalanlp" %% "breeze" % "0.12",

  // Native libraries are not included by default. add this if you want them (as of 0.7)
  // Native libraries greatly improve performance, but increase jar sizes. 
  // It also packages various blas implementations, which have licenses that may or may not
  // be compatible with the Apache License. No GPL code, as best I know.
  "org.scalanlp" %% "breeze-natives" % "0.12",

  // The visualization library is distributed separately as well.
  // It depends on LGPL code
  "org.scalanlp" %% "breeze-viz" % "0.12"
)


resolvers += "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
sbt update
> console
scala>

行列計算してみる

scala> import breeze.linalg._

scala> val m1 = DenseMatrix((1,2), (3,4))
m1: breeze.linalg.DenseMatrix[Int] =
1  2
3  4

scala> val m2 = DenseMatrix((5,1), (4,8))
m2: breeze.linalg.DenseMatrix[Int] =
5  1
4  8

scala> m1 + m2
res0: breeze.linalg.DenseMatrix[Int] =
6  3
7  12

scala> m1 * m2
res1: breeze.linalg.DenseMatrix[Int] =
13  17
31  35

scala> val v1 = DenseMatrix.zeros[Int](2,3)
v1: breeze.linalg.DenseMatrix[Int] =
0  0  0
0  0  0

scala> m1(::, 0) := DenseVector(4,7)
res23: breeze.linalg.DenseVector[Int] = DenseVector(4, 7)

scala> m1
res24: breeze.linalg.DenseMatrix[Int] =
4  2
7  4

scala> m2(0, ::) := DenseVector(3,5).t
res25: breeze.linalg.Transpose[breeze.linalg.DenseVector[Int]] = Transpose(DenseVector(3, 5))

scala> m2
res26: breeze.linalg.DenseMatrix[Int] =
3  5
4  8

とりあえず行列計算試せたからここまで

チートシート

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4