Scala Linear Algebra things
Needed to learn some linear algebra for a few projects. So I decided to code the things I learned while learning.
This is a set of classes in scala for some of the more common linear algebra things you might need to do.
Not efficient or special, but putting it up here in case anyone else finds it useful.
See the scaladocs for details.
Examples
scala> val m = Mat[Double](Seq( Seq(1, 2, 3), Seq(2, -3, 9), Seq(9, -1, 2) ) ) m: org.gnarf.linear.Mat[Double] = DenseMat[double](3,3) = ( 1.0, 2.0, 3.0 2.0, -3.0, 9.0 9.0, -1.0, 2.0 ) scala> m.solveFor(Vec[Double](1,2,3)) res0: org.gnarf.linear.LinearSystemSolution[Double] = UniqueLinearSolution(ColVec[double](3) = [0.3017241379310344,0.07758620689655162,0.1810344827586207])
scala> val m = Mat[Double](Seq( Seq(1, 2, 3, 4), Seq(2, -3, 9, 2), Seq(9, -1, 2, -9) ) ) m: org.gnarf.linear.Mat[Double] = DenseMat[double](3,4) = ( 1.0, 2.0, 3.0, 4.0 2.0, -3.0, 9.0, 2.0 9.0, -1.0, 2.0, -9.0 ) scala> m.solveFor(Vec[Double](1,2,3)) res1: org.gnarf.linear.LinearSystemSolution[Double] = SolutionSpace[double,dim=1] = [0.3017241379310344,0.07758620689655162,0.1810344827586207,0.0] + t0*[-0.8596491228070177,1.0,0.7052631578947368,-0.8140350877192983]
scala> val A = Mat[Double](Seq( Seq(1, -2, 3), Seq(5, 8, -1), Seq(2, 1, 1) ) ) A: org.gnarf.linear.Mat[Double] = DenseMat[double](3,3) = ( 1.0, -2.0, 3.0 5.0, 8.0, -1.0 2.0, 1.0, 1.0 ) scala> val B = A.inverse B: org.gnarf.linear.Mat[Double] = DenseMat[double](3,3) = ( -0.8999999999999999, -0.49999999999999983, 2.1999999999999993 0.6999999999999998, 0.49999999999999983, -1.5999999999999994 1.0999999999999996, 0.4999999999999998, -1.7999999999999992 ) scala> val m = A*B m: org.gnarf.linear.Mat[Double] = DenseMat[double](3,3) = ( 0.9999999999999991, -2.220446049250313E-16, 8.881784197001252E-16 -8.881784197001252E-16, 0.9999999999999998, 2.220446049250313E-16 -4.440892098500626E-16, -5.551115123125783E-17, 0.9999999999999998 ) scala> val rounded = m.map { a: Double => math.round(a * 100)/100.0 } rounded: org.gnarf.linear.Mat[Double] = DenseMat[double](3,3) = ( 1.0, 0.0, 0.0 0.0, 1.0, 0.0 0.0, 0.0, 1.0 )
Resources
SBT
libraryDependencies ++= Seq( "org.gnarf" %% "linear" % "1.0-SNAPSHOT" ) resolvers += "org.gnarf.linear" at "https://bk.gnarf.org/creativity/scalalinear/jars/"
Learn more
Licence
This code is licensed under GPL Ver 2