Earlier this month I was dabbling with WebGL in Scala.js and found myself running into a wall with the matrix math. Since Scala provides no native support for matrices and many WebGL projects leverage Sylvester.js, I was stuck trying to find a comparable Scala library from which I could poach code. (Scala.js interprets Scala source code).
The best I found was Breeze, which does indeed provide a lot of good support for matrices and vectors. Unfortunately, it is a large library and does a lot more than I needed. Pulling down the necessary code was impractical. So I found myself building my own simple library to tide me over. The result was Scalinear: a simple, no-frills library for matrix and vector support in Scala.
Vectors are created simply:
scala> val v = Vector(2,4,6,8) v: com.letstalkdata.scalinear.Vector[Int] = [2, 4, 6, 8]
They also support arithmetic in an intuitive manner:
scala> v / 2 res1: com.letstalkdata.scalinear.Vector[Int] = [1, 2, 3, 4] scala> val u = Vector(3,3,3,3) u: com.letstalkdata.scalinear.Vector[Int] = [3, 3, 3, 3] scala> v + u res2: com.letstalkdata.scalinear.Vector[Int] = [5, 7, 9, 11]
Matrices are syntactically similar to Vertices:
scala> val A = Matrix(Vector(1,2), Vector(3,4), Vector(5,6)) A: com.letstalkdata.scalinear.Matrix[Int] = [1, 2] [3, 4] [5, 6] scala> val B = Matrix(Vector(1,2,3), Vector(4,5,6)) B: com.letstalkdata.scalinear.Matrix[Int] = [1, 2, 3] [4, 5, 6] scala> A * B res0: com.letstalkdata.scalinear.Matrix[Int] = [9, 12, 15] [19, 26, 33] [29, 40, 51]
By design, the library is very limited. It’s definitely not intended as a replacement for something like Breeze. Instead, it’s a simple solution when only basic linear algebra support is needed. Having said that, I’d love to know if there are missing functions that would make your life easier. Please take a look at the project and feel free to leave feedback below.