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

Numeric Programming with Spire (KSUG edition)

Lars Hupel
February 21, 2019

Numeric Programming with Spire (KSUG 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.

https://www.meetup.com/Krakow-Scala-User-Group/events/258677272/

Lars Hupel

February 21, 2019
Tweet

More Decks by Lars Hupel

Other Decks in Programming

Transcript

  1. N u m e r i c P r o

    g r a m m i n g w i t h S p i r e L a r s H u p e l K r a k o w S c a l a U s e r G r o u p 2 0 1 9 - 0 2 - 2 1
  2. N u m e r i c P r o

    g r a m m i n g w i t h C a t s , A l g e b r a & S p i r e L a r s H u p e l K r a k o w S c a l a U s e r G r o u p 2 0 1 9 - 0 2 - 2 1
  3. W h a t i s S p i r

    e ? “ S p i r e i s a n u m e r i c l i b r a r y f o r S c a l a w h i c h i s i n t e n d e d t o b e g e n e r i c , f a s t , a n d p r e c i s e . ”
  4. W h a t i s S p i r

    e ? “ S p i r e i s a n u m e r i c l i b r a r y f o r S c a l a w h i c h i s i n t e n d e d t o b e g e n e r i c , f a s t , a n d p r e c i s e . ” • o n e o f t h e “ o l d e s t ” T y p e l e v e l l i b r a r i e s • i n i t i a l w o r k b y E i r í k r Å s h e i m & T o m S w i t z e r • 6 0 c o n t r i b u t o r s • s t a r t e d o u t i n 2 0 1 1 a s a S I P f o r i m p r o v i n g n u m e r i c s
  5. W h a t ’s i n S p i

    r e ? • a l g e b r a i c t o w e r • n u m b e r t y p e s • n u m e r i c a l g o r i t h m s • p r e t t y s y n t a x • o p t i m i z a t i o n m a c r o s • l a w s
  6. P r o j e c t r e l

    a t i o n s h i p D i s c i p l i n e C a t s K e r n e l A l g e b r a C a t s S p i r e A l g e b i r d
  7. P r o j e c t r e l

    a t i o n s h i p D i s c i p l i n e C a t s K e r n e l A l g e b r a C a t s S p i r e A l g e b i r d
  8. A l g e b r a “ A l

    g e b r a i s t h e s t u d y o f m a t h e m a t i c a l s y m b o l s a n d t h e r u l e s f o r m a - n i p u l a t i n g t h e s e s y m b o l s . ”
  9. A l g e b r a “ A l

    g e b r a i s t h e s t u d y o f m a t h e m a t i c a l s y m b o l s a n d t h e r u l e s f o r m a - n i p u l a t i n g t h e s e s y m b o l s . ” M a t h e m a t i c i a n s s t u d y a l g e b r a t o d i s c o v e r c o m m o n p r o p e r t i e s o f v a r i o u s c o n c r e t e s t r u c t u r e s .
  10. A l g e b r a “ A l

    g e b r a i s t h e s t u d y o f m a t h e m a t i c a l s y m b o l s a n d t h e r u l e s f o r m a - n i p u l a t i n g t h e s e s y m b o l s . ” E x a m p l e s • n u m b e r s , a d d i t i o n , m u l t i p l i c a t i o n • m a t r i c e s , v e c t o r s p a c e s , l i n e a r a l g e b r a • l a t t i c e s , b o o l e a n a l g e b r a
  11. A l g e b r a “ A l

    g e b r a i s t h e s t u d y o f m a t h e m a t i c a l s y m b o l s a n d t h e r u l e s f o r m a - n i p u l a t i n g t h e s e s y m b o l s . ” E x a m p l e s • n u m b e r s , a d d i t i o n , m u l t i p l i c a t i o n • m a t r i c e s , v e c t o r s p a c e s , l i n e a r a l g e b r a • l a t t i c e s , b o o l e a n a l g e b r a
  12. A l g e b r a “ A l

    g e b r a i s t h e s t u d y o f m a t h e m a t i c a l s y m b o l s a n d t h e r u l e s f o r m a - n i p u l a t i n g t h e s e s y m b o l s . ” E x a m p l e s • n u m b e r s , a d d i t i o n , m u l t i p l i c a t i o n • m a t r i c e s , v e c t o r s p a c e s , l i n e a r a l g e b r a • l a t t i c e s , b o o l e a n a l g e b r a
  13. S e m i g r o u p trait

    Semigroup[A] { def append(x: A, y: A): A }
  14. S e m i g r o u p trait

    Semigroup[A] { def append(x: A, y: A): A } L a w : A s s o c i a t i v i t y append(x, append(y, z)) == append(append(x, y), z)
  15. M o n o i d s trait Monoid[A] extends

    Semigroup[A] { def append(x: A, y: A): A // Semigroup def zero: A } L a w : N e u t r a l e l e m e n t append(x, zero) == x
  16. M o n o i d a l s t

    r u c t u r e s L o t s o f t h i n g s a r e m o n o i d s .
  17. T r a i n s a r e m

    o n o i d s
  18. T r a i n s a r e m

    o n o i d s
  19. M o n o i d a l s t

    r u c t u r e s L o t s o f t h i n g s a r e m o n o i d s . • ( T r a i n , n o t r a i n , c o u p l e ) • ( Int, 0 , + ) • ( List[T], Nil, c o n c a t ) • ( Map[K, V], Map.empty, m e r g e )
  20. M o n o i d a l s t

    r u c t u r e s L o t s o f t h i n g s a r e m o n o i d s . • ( T r a i n , n o t r a i n , c o u p l e ) • ( Int, 0 , + ) • ( List[T], Nil, c o n c a t ) • ( Map[K, V], Map.empty, m e r g e ) B u t s o m e a r e n o t ! • ( Float, 0 , + )
  21. Semigroup Monoid Group AddSemigroup AddMonoid AddGroup ( T, 0 ,

    + ) MulSemigroup MulMonoid MulGroup ( T, 1 , ·)
  22. AddSemigroup AddMonoid AddGroup ( T, 0 , + ) MulSemigroup

    MulMonoid MulGroup ( T, 1 , ·) Semiring ( T, + , ·, 0 , 1 )
  23. AddSemigroup AddMonoid AddGroup ( T, 0 , + ) MulSemigroup

    MulMonoid MulGroup ( T, 1 , ·) Semiring ( T, + , ·, 0 , 1 ) AddAbGroup MulAbGroup Rig Rng Ring Field
  24. L a w C h e c k i n

    g // 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)
  25. N u m b e r s • m a

    c h i n e f l o a t s a r e f a s t , b u t i m p r e c i s e • g o o d t r a d e o f f f o r m a n y p u r p o s e s , b u t n o t a l l ! • t h e r e i s n o “ o n e s i z e f i t s a l l ” n u m b e r t y p e
  26. R a t i o n a l n u

    m b e r s n d ∈ Q w h e r e n , d ∈ Z P r o p e r t i e s • c l o s e d u n d e r a d d i t i o n , m u l t i p l i c a t i o n , . . . • d e c i d a b l e c o m p a r i s o n
  27. R a t i o n a l n u

    m b e r s n d ∈ Q w h e r e n , d ∈ Z P r o p e r t i e s • c l o s e d u n d e r a d d i t i o n , m u l t i p l i c a t i o n , . . . • d e c i d a b l e c o m p a r i s o n • m a y g r o w l a r g e
  28. R e a l n u m b e r

    s W e c a n ’ t r e p r e s e n t a l l r e a l n u m b e r s o n a c o m p u t e r . . .
  29. R e a l n u m b e r

    s W e c a n ’ t r e p r e s e n t a l l r e a l n u m b e r s o n a c o m p u t e r . . . . . . b u t w e c a n g e t a r b i t r a r i l y c l o s e
  30. R e a l n u m b e r

    s W e c a n ’ t r e p r e s e n t a l l r e a l n u m b e r s o n a c o m p u t e r . . . . . . b u t w e c a n g e t a r b i t r a r i l y c l o s e
  31. R e a l n u m b e r

    s , a p p r o x i m a t e d trait Real { def approximate(precision: Int): Rational }
  32. R e a l n u m b e r

    s , a p p r o x i m a t e d 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 } } }
  33. R e a l n u m b e r

    s , a p p r o x i m a t e d trait Real { def approximate(precision: Int): Rational } object Real { def apply(f: Int => Rational) = // ... def fromRational(rat: Rational) = apply(_ => rat) }
  34. I r r a t i o n a l

    n u m b e r s val pi: Real = Real(16) * atan(Real(Rational(1, 5))) - Real(4) * atan(Real(Rational(1, 239)))
  35. E r r o r b o u n d

    s • o f t e n , i n p u t s a r e n o t a c c u r a t e • e . g . m e a s u r e m e n t s ( t e m p e r a t u r e , w o r k , t i m e , . . . ) • W h a t t o d o w i t h e r r o r b o u n d s ?
  36. I n t e r v a l a r

    i t h m e t i c case class Interval[A](lower: A, upper: A)
  37. I n t e r v a l a r

    i t h m e t i c case class Interval[A](lower: A, upper: A) { def +(that: Interval[A]) = Interval(this.lower + that.lower, this.upper + that.upper) }
  38. I n t e r v a l a r

    i t h m e t i c case class Interval[A](lower: A, upper: A) { def +(that: Interval[A]) = Interval(this.lower + that.lower, this.upper + that.upper) } S p i r e g e n e r a l i z e s t h i s e v e n f u r t h e r : • o p e n / c l o s e d i n t e r v a l s • b o u n d e d / u n b o u n d e d i n t e r v a l s
  39. W h a t e l s e ? S

    p i r e i s f u l l o f t o o l s y o u d i d n ’ t k n o w y o u n e e d e d .
  40. W h a t e l s e ? S

    p i r e i s f u l l o f t o o l s y o u d i d n ’ t k n o w y o u n e e d e d . • SafeLong: l i k e BigInt, b u t f a s t e r
  41. W h a t e l s e ? S

    p i r e i s f u l l o f t o o l s y o u d i d n ’ t k n o w y o u n e e d e d . • SafeLong: l i k e BigInt, b u t f a s t e r • Trilean: t r i - s t a t e b o o l e a n v a l u e
  42. W h a t e l s e ? S

    p i r e i s f u l l o f t o o l s y o u d i d n ’ t k n o w y o u n e e d e d . • SafeLong: l i k e BigInt, b u t f a s t e r • Trilean: t r i - s t a t e b o o l e a n v a l u e • UByte, UShort, UInt, ULong: u n s i g n e d m a c h i n e w o r d s
  43. W h a t e l s e ? S

    p i r e i s f u l l o f t o o l s y o u d i d n ’ t k n o w y o u n e e d e d . • SafeLong: l i k e BigInt, b u t f a s t e r • Trilean: t r i - s t a t e b o o l e a n v a l u e • UByte, UShort, UInt, ULong: u n s i g n e d m a c h i n e w o r d s • Natural: n o n - n e g a t i v e , a r b i t r a r y - s i z e d i n t e g e r s
  44. Q & A L a r s H u p

    e l � l a r s . h u p e l @ i n n o q . c o m � @ l a r s r _ h w w w . i n n o q . c o m i n n o Q D e u t s c h l a n d G m b H K r i s c h e r s t r . 1 0 0 4 0 7 8 9 M o n h e i m a . R h . G e r m a n y + 4 9 2 1 7 3 3 3 6 6 - 0 O h l a u e r S t r . 4 3 1 0 9 9 9 B e r l i n G e r m a n y L u d w i g s t r . 1 8 0 E 6 3 0 6 7 O f f e n b a c h G e r m a n y K r e u z s t r . 1 6 8 0 3 3 1 M ü n c h e n G e r m a n y i n n o Q S c h w e i z G m b H G e w e r b e s t r . 1 1 C H - 6 3 3 0 C h a m S w i t z e r l a n d + 4 1 4 1 7 4 3 0 1 1 1 A l b u l a s t r . 5 5 8 0 4 8 Z ü r i c h S w i t z e r l a n d
  45. L A R S H U P E L C

    o n s u l t a n t i n n o Q D e u t s c h l a n d G m b H L a r s e n j o y s p r o g r a m m i n g i n a v a r i e t y o f l a n - g u a g e s , i n c l u d i n g S c a l a , H a s k e l l , a n d R u s t . H e i s k n o w n a s a f r e q u e n t c o n f e r e n c e s p e a k e r a n d o n e o f t h e f o u n d e r s o f t h e T y p e l e v e l i n i t i a t i v e w h i c h i s d e d i c a t e d t o p r o v i d i n g p r i n c i p l e d , t y p e - d r i v e n S c a l a l i b r a r i e s .
  46. I m a g e s o u r c

    e s • R u b i k ’s C u b e : https://en.wikipedia.org/wiki/File:Rubik%27s_Cube_variants.jpg, H e l l b u s • K n o t s : https://en.wikipedia.org/wiki/File:Tabela_de_n%C3%B3s_matem%C3%A1ticos_01,_crop.jpg, R o d r i g o . A r g e n t o n • I n t e r v a l s : https://commons.wikimedia.org/wiki/File:Confidenceinterval.png, A u d r i u s M e s k a u s k a s • N u m b e r v e n n d i a g r a m : http://www.science4all.org/article/numbers-and-constructibility/, L ê N g u y ê n H o a n g • L a v a u x : https://en.wikipedia.org/wiki/File:Lake_Geneva_with_Vineyards_in_Lavaux.jpg, S e v e r i n . s t a l d e r • D r a w i n g s : Y i f a n X i n g