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.

A1216674d5c9747bcdcc716872439137?s=128

Lars Hupel

June 08, 2018
Tweet

More Decks by Lars Hupel

Other Decks in Programming

Transcript

  1. Numeric Programming with Spire Lars Hupel June 8th, 2018

  2. Numeric Programming with Cats Kernel, Algebra & Spire Lars Hupel

    June 8th, 2018
  3. What is Spire? “ Spire is a numeric library for

    Scala which is intended to be generic, fast, and precise. ” 3
  4. 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
  5. 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
  6. What’s in Spire? ▶ algebraic tower ▶ number types ▶

    numeric algorithms ▶ pretty syntax ▶ optimization macros ▶ laws 4
  7. Project relationship Discipline Cats Kernel Algebra Cats Spire Algebird 5

  8. Project relationship Discipline Cats Kernel Algebra Cats Spire Algebird 5

  9. “Algebra” “ Algebra is the study of mathematical symbols and

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

    } 7
  15. Semigroup trait Semigroup[A] { def append(x: A, y: A): A

    } Law: Associativity append(x, append(y, z)) == append(append(x, y), z) 7
  16. 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
  17. Monoidal structures Lots of things are monoids. 9

  18. Monoidal structures Lots of things are monoids. ▶ Int, BigInt,

    ... ▶ List[T] ▶ Map[K, V] 9
  19. Demo

  20. Monoidal structures Lots of things are monoids. ▶ Int, BigInt,

    ... ▶ List[T] ▶ Map[K, V] But some are not! ▶ Float, Double 11
  21. Algebraic hierarchy Semigroup Monoid Group 12

  22. Algebraic hierarchy Semigroup Monoid Group AddSemigroup AddMonoid AddGroup 12

  23. Algebraic hierarchy Semigroup Monoid Group AddSemigroup AddMonoid AddGroup MulSemigroup MulMonoid

    MulGroup 12
  24. Algebraic hierarchy AddSemigroup AddMonoid AddGroup MulSemigroup MulMonoid MulGroup Semiring 12

  25. Algebraic hierarchy AddSemigroup AddMonoid AddGroup MulSemigroup MulMonoid MulGroup Semiring AddAbGroup

    MulAbGroup Rig Rng Ring Field 12
  26. 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
  27. Demo

  28. 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
  29. Rational numbers n d ∈ Q where n, d ∈

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

    Z Properties ▶ closed under addition, multiplication, ... ▶ decidable comparison ▶ may grow large 16
  31. Demo

  32. Real numbers We can’t represent all real numbers on a

    computer ... 18
  33. Real numbers We can’t represent all real numbers on a

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

    computer ... ... but we can get arbitrarily close 18
  35. Real numbers, approximated trait Real { def approximate(precision: Int): Rational

    } 19
  36. 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
  37. 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
  38. Irrational numbers val pi: Real = Real(16) * atan(Real(Rational(1, 5)))

    - Real(4) * atan(Real(Rational(1, 239))) 20
  39. Demo

  40. Error bounds ▶ often, inputs are not accurate ▶ e.g.

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

  42. Interval arithmetic case class Interval[A](lower: A, upper: A) { def

    +(that: Interval[A]) = Interval(this.lower + that.lower, this.upper + that.upper) } 23
  43. 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
  44. Demo

  45. What else? Spire is full of tools you didn’t know

    you needed. 25
  46. What else? Spire is full of tools you didn’t know

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

    you needed. ▶ SafeLong: like BigInt, but faster ▶ Trilean: tri-state boolean value 25
  48. 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
  49. 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
  50. Q & A  larsrh  larsr_h

  51. 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