Spire

Spire is a numerical library for Scala that aims to be universal, fast, and precise.

Using features such as specialization, macros, type classes, and implicit conversions, Spire strives to challenge conventional notions about the trade-off between performance and precision. The main goal is to enable developers to write efficient numerical code without the need to "bake in" specific numeric representations. In most cases, generic implementations using Spire's specialized type classes work identically to corresponding direct implementations.

Example of using the Spire library:

import spire.*
import spire.implicits.*
import spire.math.*

Complex(3.0,5.0).sin
// val res0: spire.math.Complex[Double] = (10.47250853394392 + -73.460621695677i)

Numeric Types

In addition to supporting all of Scala's built-in numeric types, Spire introduces several new ones, all of which can be found in spire.math:

Type Classes

Spire provides type classes to support a wide range of unary and binary numeric operations. The type classes are specialized, do not perform boxing, and use implicit conversions to provide convenient infix syntax.

General-purpose type classes can be found in the spire.math package:

Some general-purpose type classes are built on top of a set of more fundamental type classes defined in spire.algebra. Many of them correspond to concepts from abstract algebra:

Variants of Semigroup/Monoid/Group/Action with partial operations are defined in the spire.algebra.partial subpackage.

In addition to the type classes themselves, spire.implicits defines many implicit functions that provide unary and infix operators for the type classes. The simplest way to use them is to import the files spire.implicits.*.

Numbers

These high-level type classes will include all relevant algebraic type classes.

The Numeric type class is unique because it provides the same functionality as Fractional and all numeric types. Each type will attempt to "do the right thing" as much as possible; otherwise, it will throw errors. Users who are wary of such behavior are recommended to use more precise type classes.

Getting Started

Spire contains many types, as well as other mechanisms that ensure ease of use. The simplest way to use Spire is to import the dependencies:

import spire.algebra.*   // provides algebraic type classes
import spire.implicits.* // provides infix operators, instances, and conversion
import spire.math.*      // provides functions, types, and type classes

Syntax

Using string interpolation and macros, Spire provides convenient syntax for numeric types. These macros are evaluated at compile time, and any errors they encounter occur at compile time.

For example:

object LiteralsDemo:
  import spire.syntax.literals.*

  // bytes and shorts
  val x    = b"100" // without type annotation!
  val y    = h"999"
  val mask = b"255" // unsigned constant converted to signed (-1)

  // fractions
  val n1 = r"1/3"
  val n2 = r"1599/115866" // simplifies at compile time to 13/942
object SIDemo:
  // SI notation for large numbers
  import spire.syntax.literals.si.* // `.us` and `.eu` are also available
  
  val w = i"1 944 234 123" // Int
  val x = j"89 234 614 123 234 772" // Long
  val y = big"123 234 435 456 567 678 234 123 112 234 345" // BigInt
  val z = dec"1 234 456 789.123456789098765" // BigDecimal

Spire also provides a cfor loop macro, whose syntax is somewhat similar to the traditional for loop from C or Java. This macro expands to a tail-recursive function that inlines literal function arguments.

The macro can nest within itself and has advantages over other loop constructs in Scala, such as for and while:

import spire.syntax.cfor.*

// prints numbers from 0 to 9
cfor(0)(_ < 10, _ + 1): i =>
  println(i)

// a simple sorting algorithm
def selectionSort(ns: Array[Int]): Unit =
  val limit = ns.length - 1
  cfor(0)(_ < limit, _ + 1): i =>
    var k = i
    val n = ns(i)
    cfor(i + 1)(_ <= limit, _ + 1): j =>
      if ns(j) < ns(k) then k = j
    ns(i) = ns(k)
    ns(k) = n

Miscellaneous

In addition, Spire provides many other methods that are "missing" from java.Math (and scala.math), for example:

Tests

In addition to unit tests, Spire comes with a relatively detailed suite of JMH micro-benchmarks. To run the tests from SBT, navigate to the benchmark subproject and then run Jmh / run -l to view the list of tests:

$ sbt
> project benchmark
> Jmh / run -l

[info] Benchmarks:
[info] spire.benchmark.AddBenchmarks.addComplexDoubleStateDirect
[info] spire.benchmark.AddBenchmarks.addComplexDoubleStateGeneric
[info] spire.benchmark.AddBenchmarks.addComplexFloatStateDirect
...

To run all available tests:

> Jmh / run

To run a specific test method:

> Jmh / run spire.benchmark.AddBenchmarks.addComplexDoubleStateDirect

To run all tests in a specific class:

> Jmh / run spire.benchmark.AddBenchmarks

To view all available JMH options:

> Jmh / run -h

If you plan to contribute to Spire, be sure to run the relevant tests to ensure your changes do not affect performance. Tests usually include comparisons with equivalent Scala or Java classes, attempting to measure both relative and absolute performance.


References: