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

Numeric programming with Spire (LX Scala edition)

Numeric programming with Spire (LX Scala 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

June 08, 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 ▶ ≈ 50 contributors ▶ started out in 2011 as a SIP for improving numerics 3
  3. 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 ▶ ≈ 50 contributors ▶ started out in 2011 as a SIP for improving numerics 3
  4. What’s in Spire? ▶ algebraic tower ▶ number types ▶

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

    the rules for manipulating these symbols. ” 6
  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. 6
  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 6
  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 6
  9. “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 6
  10. Semigroup trait Semigroup[A] { def append(x: A, y: A): A

    } Law: Associativity append(x, append(y, z)) == append(append(x, y), z) 7
  11. 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 8
  12. Monoidal structures Lots of things are monoids. ▶ Int, BigInt,

    ... ▶ List[T] ▶ Map[K, V] But some are not! ▶ Float, Double 11
  13. 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) 13
  14. Numbers ▶ machine floats are fast, but imprecise ▶ good

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

    Z Properties ▶ closed under addition, multiplication, ... ▶ decidable comparison ▶ may grow large 16
  16. Rational numbers n d ∈ Q where n, d ∈

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

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

    computer ... ... but we can get arbitrarily close 18
  19. 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 } } } 19
  20. 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) } 19
  21. Error bounds ▶ often, inputs are not accurate ▶ e.g.

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

    +(that: Interval[A]) = Interval(this.lower + that.lower, this.upper + that.upper) } 23
  23. 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 23
  24. What else? Spire is full of tools you didn’t know

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

    you needed. ▶ SafeLong: like BigInt, but faster ▶ Trilean: tri-state boolean value 25
  26. 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 25
  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 ▶ Natural: non-negative, arbitrary-sized integers 25
  28. 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 ▶ Torre de Belém: https://en.wikipedia.org/wiki/File: Torre_Bel%C3%A9m_April_2009-4a.jpg, Alvesgaspar ▶ Number venn diagram: http://www.science4all.org/article/numbers-and-constructibility/, Lê Nguyên Hoang ▶ Garo do Oriente: https://commons.wikimedia.org/wiki/File: Gare_do_Oriente_(10000658884).jpg, Bobo Boom 27