Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Numeric Programming with Spire (Scala Italy edition)

Lars Hupel
September 14, 2018

Numeric Programming with Spire (Scala Italy edition)

Numeric programming is a notoriously difficult topic. For number crunching, e.g. solving systems of linear equations, we need raw performance.

However, using floating-point numbers may lead to inaccurate results. On top of that, as functional programmers, we’d really like to abstract over concrete number types, which is where abstract algebra comes into play.

This interplay between abstract and concrete, and the fact that everything needs to run on finite hardware, is what makes good library support necessary for writing fast & correct programs.

Spire is such a library in the Typelevel Scala ecosystem. This talk will be an introduction to Spire, showcasing the ‘number tower’, real-ish numbers and how to obey the law.

Lars Hupel

September 14, 2018
Tweet

More Decks by Lars Hupel

Other Decks in Programming

Transcript

  1. What is Spire? “ Spire is a numeric library for

    Scala which is intended to be generic, fast, and precise. ” 3
  2. What is Spire? “ Spire is a numeric library for

    Scala which is intended to be generic, fast, and precise. ” ▶ one of the “oldest” Typelevel libraries ▶ initial work by Eiríkr Åsheim & Tom Switzer ▶ 60 contributors ▶ started out in 2011 as a SIP for improving numerics 3
  3. What’s in Spire? ▶ algebraic tower ▶ number types ▶

    numeric algorithms ▶ pretty syntax ▶ optimization macros ▶ laws 5
  4. “Algebra” “ Algebra is the study of mathematical symbols and

    the rules for manipulating these symbols. ” 7
  5. “Algebra” “ Algebra is the study of mathematical symbols and

    the rules for manipulating these symbols. ” Mathematicians study algebra to discover common properties of various concrete structures. 7
  6. “Algebra” “ Algebra is the study of mathematical symbols and

    the rules for manipulating these symbols. ” Mathematicians study algebra to discover common properties of various concrete structures. Examples ▶ numbers, addition, multiplication ▶ matrices, vector spaces, linear algebra ▶ lattices, boolean algebra 7
  7. “Algebra” “ Algebra is the study of mathematical symbols and

    the rules for manipulating these symbols. ” Mathematicians study algebra to discover common properties of various concrete structures. Examples ▶ numbers, addition, multiplication ▶ matrices, vector spaces, linear algebra ▶ lattices, boolean algebra 7
  8. “Algebra” “ Algebra is the study of mathematical symbols and

    the rules for manipulating these symbols. ” Mathematicians study algebra to discover common properties of various concrete structures. Examples ▶ numbers, addition, multiplication ▶ matrices, vector spaces, linear algebra ▶ lattices, boolean algebra 7
  9. Semigroup trait Semigroup[A] { def append(x: A, y: A): A

    } Law: Associativity append(x, append(y, z)) == append(append(x, y), z) 8
  10. Monoids trait Monoid[A] extends Semigroup[A] { def append(x: A, y:

    A): A // Semigroup def zero: A } Law: Neutral element append(x, zero) == x 9
  11. Monoidal structures Lots of things are monoids. ▶ (Train, no

    train, couple) ▶ (Int, 0, +) ▶ (List[T], Nil, concat) ▶ (Map[K, V], Map.empty, merge) 12
  12. Monoidal structures Lots of things are monoids. ▶ (Train, no

    train, couple) ▶ (Int, 0, +) ▶ (List[T], Nil, concat) ▶ (Map[K, V], Map.empty, merge) But some are not! ▶ (Float, 0, +) 14
  13. Algebraic hierarchy AddSemigroup AddMonoid AddGroup (T, 0, +) MulSemigroup MulMonoid

    MulGroup (T, 1, ·) Semiring (T, +, ·, 0, 1) AddAbGroup MulAbGroup Rig Rng Ring Field 15
  14. Law Checking // Float and Double fail these tests checkAll(”Int”,

    RingLaws[Int].euclideanRing) checkAll(”Long”, RingLaws[Long].euclideanRing) checkAll(”BigInt”, RingLaws[BigInt].euclideanRing) checkAll(”Rational”, RingLaws[Rational].field) checkAll(”Real”, RingLaws[Real].field) 16
  15. Numbers ▶ machine floats are fast, but imprecise ▶ good

    tradeoff for many purposes, but not all! ▶ there is no “one size fits all” number type 18
  16. Rational numbers n d ∈ Q where n, d ∈

    Z Properties ▶ closed under addition, multiplication, ... ▶ decidable comparison 19
  17. Rational numbers n d ∈ Q where n, d ∈

    Z Properties ▶ closed under addition, multiplication, ... ▶ decidable comparison ▶ may grow large 19
  18. Real numbers We can’t represent all real numbers on a

    computer ... ... but we can get arbitrarily close 21
  19. Real numbers We can’t represent all real numbers on a

    computer ... ... but we can get arbitrarily close 21
  20. Real numbers, approximated trait Real { self => def approximate(precision:

    Int): Rational def +(that: Real): Real = new Real { def approximate(precision: Int) = { val r1 = self.approximate(precision + 2) val r2 = that.approximate(precision + 2) r1 + r2 } } } 22
  21. Real numbers, approximated trait Real { def approximate(precision: Int): Rational

    def +(that: Real): Real = new Real { // ... } } object Real { def apply(f: Int => Rational) = // ... def fromRational(rat: Rational) = apply(_ => rat) } 22
  22. Error bounds ▶ often, inputs are not accurate ▶ e.g.

    measurements (temperature, work, time, ...) ▶ What to do with error bounds? 25
  23. Interval arithmetic case class Interval[A](lower: A, upper: A) { def

    +(that: Interval[A]) = Interval(this.lower + that.lower, this.upper + that.upper) } 26
  24. Interval arithmetic case class Interval[A](lower: A, upper: A) { def

    +(that: Interval[A]) = Interval(this.lower + that.lower, this.upper + that.upper) } Spire generalizes this even further: ▶ open/closed intervals ▶ bounded/unbounded intervals 26
  25. What else? Spire is full of tools you didn’t know

    you needed. ▶ SafeLong: like BigInt, but faster 28
  26. What else? Spire is full of tools you didn’t know

    you needed. ▶ SafeLong: like BigInt, but faster ▶ Trilean: tri-state boolean value 28
  27. What else? Spire is full of tools you didn’t know

    you needed. ▶ SafeLong: like BigInt, but faster ▶ Trilean: tri-state boolean value ▶ UByte, UShort, UInt, ULong: unsigned machine words 28
  28. What else? Spire is full of tools you didn’t know

    you needed. ▶ SafeLong: like BigInt, but faster ▶ Trilean: tri-state boolean value ▶ UByte, UShort, UInt, ULong: unsigned machine words ▶ Natural: non-negative, arbitrary-sized integers 28
  29. Image sources ▶ Rubik’s Cube: https://en.wikipedia.org/wiki/File:Rubik%27s_Cube_variants.jpg, Hellbus ▶ Knots: https://en.wikipedia.org/wiki/File:

    Tabela_de_n%C3%B3s_matem%C3%A1ticos_01,_crop.jpg, Rodrigo.Argenton ▶ Intervals: https://commons.wikimedia.org/wiki/File:Confidenceinterval.png, Audrius Meskauskas ▶ Number venn diagram: http://www.science4all.org/article/numbers-and-constructibility/, Lê Nguyên Hoang ▶ Ponte Vecchio: https://commons.wikimedia.org/wiki/File: Panorama_of_the_Ponte_Vecchio_in_Florence,_Italy.jpg, Jan Drewes ▶ Drawings: Yifan Xing 31