Lars Hupel
September 14, 2018
130

# 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

## Transcript

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

September 14th, 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 ▶ 60 contributors ▶ started out in 2011 as a SIP for improving numerics 3
5. None
6. ### What’s in Spire? ▶ algebraic tower ▶ number types ▶

numeric algorithms ▶ pretty syntax ▶ optimization macros ▶ laws 5

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

the rules for manipulating these symbols. ” 7
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. 7
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 7
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 7
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 7

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

} Law: Associativity append(x, append(y, z)) == append(append(x, y), z) 8
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 9

20. ### Monoidal structures Lots of things are monoids. ▶ (Train, no

train, couple) ▶ (Int, 0, +) ▶ (List[T], Nil, concat) ▶ (Map[K, V], Map.empty, merge) 12

22. ### 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
23. ### Algebraic hierarchy Semigroup Monoid Group 15

+) 15

+) MulSemigroup MulMonoid MulGroup (T, 1, ·) 15

MulGroup (T, 1, ·) Semiring (T, +, ·, 0, 1) 15

MulGroup (T, 1, ·) Semiring (T, +, ·, 0, 1) AddAbGroup MulAbGroup Rig Rng Ring Field 15
28. ### 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

30. ### Numbers ▶ machine ﬂoats are fast, but imprecise ▶ good

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

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

Z Properties ▶ closed under addition, multiplication, ... ▶ decidable comparison ▶ may grow large 19

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

computer ... 21
35. ### Real numbers We can’t represent all real numbers on a

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

computer ... ... but we can get arbitrarily close 21

} 22
38. ### 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
39. ### 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
40. ### Irrational numbers val pi: Real = Real(16) * atan(Real(Rational(1, 5)))

- Real(4) * atan(Real(Rational(1, 239))) 23

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

measurements (temperature, work, time, ...) ▶ What to do with error bounds? 25

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

+(that: Interval[A]) = Interval(this.lower + that.lower, this.upper + that.upper) } 26
45. ### 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

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

you needed. 28
48. ### What else? Spire is full of tools you didn’t know

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

you needed. ▶ SafeLong: like BigInt, but faster ▶ Trilean: tri-state boolean value 28
50. ### 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
51. ### 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

54. ### 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