https://github.com/scalanlp/breeze/wiki/Quickstart点击打开链接
https://github.com/scalanlp/breeze/wiki/Linear-Algebra-Cheat-Sheet点击打开链接
根据(一)中讲的,我们只需要在sbt命令行下set 包以及链接,就可以引入breeze线性代数包。然后console进入scala交互行。
scala> import breeze.linalg._ scala >import breeze.numerics._1 创建一个全零向量
scala> val x = DenseVector.zeros[Double](5) x: breeze.linalg.DenseVector[Double] = DenseVector(0.0, 0.0, 0.0, 0.0, 0.0)
2 创建一个全一向量
scala> val x=DenseVector.ones[Double](5)
x: breeze.linalg.DenseVector[Double] = DenseVector(1.0, 1.0, 1.0, 1.0, 1.0)
3 创建一个n个特殊值的向量
scala> val x=DenseVector.fill(6){5.0}
x: breeze.linalg.DenseVector[Double] = DenseVector(5.0, 5.0, 5.0, 5.0, 5.0, 5.0)
scala> val x=DenseVector.fill(6){5}
x: breeze.linalg.DenseVector[Int] = DenseVector(5, 5, 5, 5, 5, 5)
4 创建一个列向量
scala> val x=DenseVector(1,2,3,4)
x: breeze.linalg.DenseVector[Int] = DenseVector(1, 2, 3, 4)
5 创建一个行向量
scala> val x=DenseVector(1,2,3,4).t
x: breeze.linalg.Transpose[breeze.linalg.DenseVector[Int]] = Transpose(DenseVector(1, 2, 3, 4))
6 通过一个array创建一个向量
scala> val x=DenseVector(Array(1,2,3,4))
x: breeze.linalg.DenseVector[Int] = DenseVector(1, 2, 3, 4)
7 随机产生有n个值的向量
scala> val x=DenseVector.rand(4)
x: breeze.linalg.DenseVector[Double] = DenseVector(0.7879941928154828, 0.6786788014317569, 0.12750871172966338, 0.16731062371901584)
8 通过函数产生向量
scala> val x=DenseVector.tabulate(3){i=>2*i}
x: breeze.linalg.DenseVector[Int] = DenseVector(0, 2, 4)
1 产生一个全零的矩阵
scala> val m=DenseMatrix.zeros[Double](3,4)
m: breeze.linalg.DenseMatrix[Double] =
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
2 创建一个对角线全1.0的方阵
scala> val m=DenseMatrix.eye[Double](4)
m: breeze.linalg.DenseMatrix[Double] =
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
3 创建一个对角方阵
scala> val m=diag(DenseVector(1.0,2.0,3.0))
m: breeze.linalg.DenseMatrix[Double] =
1.0 0.0 0.0
0.0 2.0 0.0
0.0 0.0 3.0
4 可以这样创建矩阵,也可以这样赋值
scala> val m=DenseMatrix((1.0,2.0),(3.0,4.0))
m: breeze.linalg.DenseMatrix[Double] =
1.0 2.0
3.0 4.0
5 通过函数产算矩阵
scala> val m=DenseMatrix.tabulate(3,2){case (i,j) =>i+j}
m: breeze.linalg.DenseMatrix[Int] =
0 1
1 2
2 3
6 从array创建矩阵 (有问题)
scala> val m=DenseMatrix(2,3,Array(11,12,13,21,22,23))
<console>:13: error: could not find implicit value for parameter rl: breeze.linalg.support.LiteralRow[Any,V]
val m=DenseMatrix(2,3,Array(11,12,13,21,22,23))
^
7 随机产生0-1直接的数 矩阵
scala> val m=DenseMatrix.rand(2,3)
m: breeze.linalg.DenseMatrix[Double] =
0.32907072371138946 0.1005430688361415 0.8768005538730448
0.9834372148171295 0.29091726792818084 0.39060144466469726