Skip to content

Get your first result

This page takes Gale from an sbt dependency to a first program: make a matrix, multiply it by a vector, and solve a linear system.

Gale 0.1.0-M1 is available from Maven Central. Use Scala 3.7.4 and JDK 21 for the baseline build. In a JVM project’s build.sbt:

scalaVersion := "3.7.4"
libraryDependencies += "io.github.canardlapin" %% "gale-core" % "0.1.0-M1"

For Scala.js or a cross-project, use %%% in place of %% with the matching build plugin enabled. No Vector API or native-library setup is needed for the portable core artifact. Continue to the first program.

For source integration instead of an artifact dependency, the following pins the exact commit behind v0.1.0-M1:

scalaVersion := "3.7.4"
lazy val galeRevision = "62e4998c03a1eae1bed55cefa2d131f7d9e33568"
lazy val galeBuild = uri(
s"https://github.com/canardlapin/gale.git#$galeRevision"
)
lazy val galeCoreJVM = ProjectRef(galeBuild, "coreJVM")
lazy val app = project.in(file(".")).dependsOn(galeCoreJVM)

For an existing project, add .dependsOn(galeCoreJVM) to its definition. Add --add-modules=jdk.incubator.vector to .jvmopts when consuming the source build. Use coreJS for a Scala.js project. For a cross-project, depend on the matching coreJVM and coreJS references from its platform projections.

Most dense work starts with one import:

import gale.linalg.*

Matrix(rows, cols) takes its values in row-major order, so the code reads like the matrix on paper. Indices start at zero.

val a = Matrix(3, 3)(
2.0, 1.0, 0.0,
1.0, 3.0, 1.0,
0.0, 1.0, 4.0
)
val x = Vec(1.0, 2.0, 3.0)
a(1, 2)
// res0: Double = 1.0
a.row(1).toSeq
// res1: Seq[Double] = Vector(1.0, 3.0, 1.0)
(a * x).toSeq
// res2: Seq[Double] = Vector(4.0, 10.0, 14.0)
(a.t * a + Matrix.eye(3)).valuesRowMajor
// res3: Seq[Double] = Vector(6.0, 5.0, 1.0, 5.0, 12.0, 7.0, 1.0, 7.0, 18.0)

* is the matrix product, a.t is the transpose, and + adds entry by entry. DVec and DMat have no readable toString, so these examples print them with toSeq and valuesRowMajor.

a.solve(b) finds the x for which a * x equals b:

val b = a * x
val solved = a.solve(b).orThrow
solved.toSeq
// res4: Seq[Double] = Vector(1.0, 2.0, 3.0)
assert(solved.toSeq.zip(x.toSeq).forall((s, e) => math.abs(s - e) < 1e-12))

The solution recovers x.

solve returns Either[LinAlgError, DVec], not a bare vector. The call above used .orThrow, which suits a script or a test. Library code can keep the error and decide what to do with it:

val singular = Matrix(2, 2)(
1.0, 2.0,
2.0, 4.0
)
singular.solve(Vec(1.0, 1.0)).isLeft
// res6: Boolean = true

A singular matrix, a non-positive Cholesky pivot, a rank-deficient least-squares design, and a mismatched right-hand side each have their own LinAlgError case.

Arithmetic is different. a + b, a * x, and x.dot(y) return plain values. They check shapes and throw a LinAlgError if the shapes do not match, since a mismatch there is a bug in the calling code. Solves, factorizations, and spectral methods return Either because a singular or indefinite matrix is an ordinary outcome that correct code must handle.