Spire is a Scala library for fast, generic, and precise numerics. It allows us to write generic numeric algorithms, provides the ‘number tower’ and offers a lot of utilities you didn’t know you needed.
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, in functional programming, 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.
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
J o n t h e B e a c h
2 0 1 9 - 0 5 - 1 6
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
J o n t h e B e a c h
2 0 1 9 - 0 5 - 1 6
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 .
”
• 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
• s t a r t e d o u t i n 2 0 1 1 a s a S c a l a s t a n d a r d s p r o p o s a l
• 6 0 c o n t r i b u t o r s
5.
6. 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
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. 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
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 .
”
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 .
”
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 .
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. 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
14. S e m i g r o u p
t r a i t S e m i g r o u p [ A ] {
d e f a p p e n d ( x : A , y : A ) : A
}
15. S e m i g r o u p
t r a i t S e m i g r o u p [ A ] {
d e f a p p e n d ( x : A , y : A ) : A
}
L a w : A s s o c i a t i v i t y
a p p e n d ( x , a p p e n d ( y , z ) ) = = a p p e n d ( a p p e n d ( x , y ) , z )
16. M o n o i d s
t r a i t M o n o i d [ A ] e x t e n d s S e m i g r o u p [ A ] {
d e f a p p e n d ( x : A , y : A ) : A / / S e m i g r o u p
d e f z e r o : A
}
L a w : N e u t r a l e l e m e n t
a p p e n d ( x , z e r o ) = = x
17. 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 .
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 )
• ( I n t , 0 , + )
• ( L i s t [ T ] , N i l , c o n c a t )
• ( M a p [ K , V ] , M a p . e m p t y , m e r g e )
22. 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 )
• ( I n t , 0 , + )
• ( L i s t [ T ] , N i l , c o n c a t )
• ( M a p [ K , V ] , M a p . e m p t y , m e r g e )
B u t s o m e a r e n o t !
• ( F l o a t , 0 , + )
24. S e m i g r o u p
M o n o i d
G r o u p
A d d S e m i g r o u p
A d d M o n o i d
A d d G r o u p
( T , 0 , + )
25. S e m i g r o u p
M o n o i d
G r o u p
A d d S e m i g r o u p
A d d M o n o i d
A d d G r o u p
( T , 0 , + )
M u l S e m i g r o u p
M u l M o n o i d
M u l G r o u p
( T , 1 , ·)
26. A d d S e m i g r o u p
A d d M o n o i d
A d d G r o u p
( T , 0 , + )
M u l S e m i g r o u p
M u l M o n o i d
M u l G r o u p
( T , 1 , ·)
S e m i r i n g
( T , + , ·, 0 , 1 )
27. A d d S e m i g r o u p
A d d M o n o i d
A d d G r o u p
( T , 0 , + )
M u l S e m i g r o u p
M u l M o n o i d
M u l G r o u p
( T , 1 , ·)
S e m i r i n g
( T , + , ·, 0 , 1 )
A d d A b G r o u p M u l A b G r o u p
R i g
R n g R i n g
F i e l d
28. L a w C h e c k i n g
/ / F l o a t a n d D o u b l e f a i l t h e s e t e s t s
c h e c k A l l ( " I n t " , R i n g L a w s [ I n t ] . e u c l i d e a n R i n g )
c h e c k A l l ( " L o n g " , R i n g L a w s [ L o n g ] . e u c l i d e a n R i n g )
c h e c k A l l ( " B i g I n t " , R i n g L a w s [ B i g I n t ] . e u c l i d e a n R i n g )
c h e c k A l l ( " R a t i o n a l " , R i n g L a w s [ R a t i o n a l ] . f i e l d )
c h e c k A l l ( " R e a l " , R i n g L a w s [ R e a l ] . f i e l d )
30. 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
31. 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
32. 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
34. 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 . . .
35. 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
36. 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
37. R e a l n u m b e r s , a p p r o x i m a t e d
t r a i t R e a l {
d e f a p p r o x i m a t e ( p r e c i s i o n : I n t ) : R a t i o n a l
}
38. R e a l n u m b e r s , a p p r o x i m a t e d
t r a i t R e a l { s e l f = >
d e f a p p r o x i m a t e ( p r e c i s i o n : I n t ) : R a t i o n a l
d e f + ( t h a t : R e a l ) : R e a l = n e w R e a l {
d e f a p p r o x i m a t e ( p r e c i s i o n : I n t ) = {
v a l r 1 = s e l f . a p p r o x i m a t e ( p r e c i s i o n + 2 )
v a l r 2 = t h a t . a p p r o x i m a t e ( p r e c i s i o n + 2 )
r 1 + r 2
}
}
}
39. R e a l n u m b e r s , a p p r o x i m a t e d
t r a i t R e a l {
d e f a p p r o x i m a t e ( p r e c i s i o n : I n t ) : R a t i o n a l
}
o b j e c t R e a l {
d e f a p p l y ( f : I n t = > R a t i o n a l ) = / / . . .
d e f f r o m R a t i o n a l ( r a t : R a t i o n a l ) =
a p p l y ( _ = > r a t )
}
40. I r r a t i o n a l n u m b e r s
v a l p i : R e a l =
R e a l ( 1 6 ) * a t a n ( R e a l ( R a t i o n a l ( 1 , 5 ) ) ) -
R e a l ( 4 ) * a t a n ( R e a l ( R a t i o n a l ( 1 , 2 3 9 ) ) )
42. 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 ?
43. I n t e r v a l a r i t h m e t i c
c a s e c l a s s I n t e r v a l [ A ] ( l o w e r : A , u p p e r : A )
44. I n t e r v a l a r i t h m e t i c
c a s e c l a s s I n t e r v a l [ A ] ( l o w e r : A , u p p e r : A ) {
d e f + ( t h a t : I n t e r v a l [ A ] ) =
I n t e r v a l ( t h i s . l o w e r + t h a t . l o w e r ,
t h i s . u p p e r + t h a t . u p p e r )
}
45. I n t e r v a l a r i t h m e t i c
c a s e c l a s s I n t e r v a l [ A ] ( l o w e r : A , u p p e r : A ) {
d e f + ( t h a t : I n t e r v a l [ A ] ) =
I n t e r v a l ( t h i s . l o w e r + t h a t . l o w e r ,
t h i s . u p p e r + t h a t . u p p e r )
}
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
47. 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 .
48. 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 .
• S a f e L o n g : l i k e B i g I n t , b u t f a s t e r
49. 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 .
• S a f e L o n g : l i k e B i g I n t , b u t f a s t e r
• T r i l e a n : t r i - s t a t e b o o l e a n v a l u e
50. 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 .
• S a f e L o n g : l i k e B i g I n t , b u t f a s t e r
• T r i l e a n : t r i - s t a t e b o o l e a n v a l u e
• U B y t e , U S h o r t , U I n t , U L o n g : u n s i g n e d m a c h i n e w o r d s
51. 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 .
• S a f e L o n g : l i k e B i g I n t , b u t f a s t e r
• T r i l e a n : t r i - s t a t e b o o l e a n v a l u e
• U B y t e , U S h o r t , U I n t , U L o n g : u n s i g n e d m a c h i n e w o r d s
• N a t u r a l : 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
52. 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
c / o W e W o r k
H e r m a n n s t r a s s e 1 3
2 0 0 9 5 H a m b u r g
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
53. 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 .
54. I m a g e s o u r c e s
• R u b i k ’s C u b e : h t t p s : / / e n . w i k i p e d i a . o r g / w i k i / F i l e : R u b i k % 2 7 s _ C u b e _ v a r i a n t s . j p g , H e l l b u s
• K n o t s : h t t p s : / / e n . w i k i p e d i a . o r g / w i k i / F i l e : T a b e l a _ d e _ n % C 3 % B 3 s _ m a t e m % C 3 % A 1 t i c o s _ 0 1 , _ c r o p . j p g ,
R o d r i g o . A r g e n t o n
• I n t e r v a l s : h t t p s : / / c o m m o n s . w i k i m e d i a . o r g / w i k i / F i l e : C o n f i d e n c e i n t e r v a l . p n g , 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 : h t t p : / / w w w . s c i e n c e 4 a l l . o r g / a r t i c l e / n u m b e r s - a n d - c o n s t r u c t i b i l i t y / , L ê
N g u y ê n H o a n g
• M a r b e l l a : h t t p s : / / p i x a b a y . c o m / p h o t o s / p o r t - m a r b e l l a - a n d a l u s i a - 2 7 0 7 9 9 5 /
• D r a w i n g s : Y i f a n X i n g