Expressive Linear Algebra in Haskell Henning Thielemann 2019-08-21 - - PowerPoint PPT Presentation

expressive linear algebra in haskell
SMART_READER_LITE
LIVE PREVIEW

Expressive Linear Algebra in Haskell Henning Thielemann 2019-08-21 - - PowerPoint PPT Presentation

Expressive Linear Algebra in Haskell Expressive Linear Algebra in Haskell Henning Thielemann 2019-08-21 Expressive Linear Algebra in Haskell Motivation 1 Motivation 2 Solution 3 More realistic problem 4 More features 5 Closing Expressive


slide-1
SLIDE 1

Expressive Linear Algebra in Haskell

Expressive Linear Algebra in Haskell

Henning Thielemann 2019-08-21

slide-2
SLIDE 2

Expressive Linear Algebra in Haskell Motivation

1 Motivation 2 Solution 3 More realistic problem 4 More features 5 Closing

slide-3
SLIDE 3

Expressive Linear Algebra in Haskell Motivation

Resistor cube

RZ,0,0 RZ,1,0 RZ,1,1 RZ,0,1 RX,0,0 RY,1,0 RX,1,0 RY,0,0 RX,0,1 RY,1,1 RX,1,1 RY,0,1 Wanted: Total resistance

slide-4
SLIDE 4

Expressive Linear Algebra in Haskell Motivation

Ohm’s law

RZ,0,0 RZ,1,0 RZ,1,1 RZ,0,1 RX,0,0 RY,1,0 RX,1,0 RY,0,0 RX,0,1 RY,1,1 RX,1,1 RY,0,1 RX,0,0 = UX,0,0

IX,0,0 ,

RY,1,0 = UY,1,0

IY,1,0 ,

. . .

slide-5
SLIDE 5

Expressive Linear Algebra in Haskell Motivation

Kirchhoff’s current law

IZ,0,0 IZ,1,0 IZ,1,1 IZ,0,1 IX,0,0 IY,1,0 IX,1,0 IY,0,0 IX,0,1 IY,1,1 IX,1,1 IY,0,1 0 = −IX,0,0+IY,1,0+IZ,1,0, 0 = −IX,0,1+IY,1,1−IZ,1,0, . . .

slide-6
SLIDE 6

Expressive Linear Algebra in Haskell Motivation

Kirchhoff’s voltage law

V0,0,0 V0,0,1 UZ,0,0 V1,0,0 V1,0,1 UZ,1,0 V1,1,0 V1,1,1 UZ,1,1 V0,1,0 V0,1,1 UZ,0,1 UX,0,0 UY,1,0 UX,1,0 UY,0,0 UX,0,1 UY,1,1 UX,1,1 UY,0,1 UX,0,0 = −V0,0,0 + V1,0,0, UY,1,0 = −V1,0,0 + V1,1,0, . . .

slide-7
SLIDE 7

Expressive Linear Algebra in Haskell Motivation

                   

0 1 R 0 1 0 −1 R 0 0 1 0 −1 R 0 0 1 0 −1 R 0 0 1 0 −1 R 0 1 0 −1 R 0 0 1 0 −1 R 0 0 1 0 −1 R 0 0 1 0 −1 R 0 1 −1 R 0 0 1 −1 R 0 0 1 −1 R 0 1 −1 1 1 1 1 0 0 1 1 0 −1 0 0 1 0 −1 1 0 0 1 0 −1 0 −1 0 0 0 −1 1 1 0 0 0 −1 1 0 −1 0 0 0 −1 0 −1 1 0 0 −1 0 −1 0 −1 0

                   

·

                   

I I I I I I I I I I I I I V V V V V V V V

                   

=

                   

1A

                   

V0,0,0 = 0 Itotal + IX,0,0 + IY,0,0 + IZ,0,0 = 0 RX,0,0 · IX,0,0 + V0,0,0 − V1,0,0 = 0 −IX,1,1 − IY,1,1 − IZ,1,1 = 1A

slide-8
SLIDE 8

Expressive Linear Algebra in Haskell Motivation

Solution

Rtotal = Utotal Itotal (1) = V1,1,1 − V0,0,0 Itotal (2)

slide-9
SLIDE 9

Expressive Linear Algebra in Haskell Motivation

Matrix blocks

A =

  

uT

0,0,0

R V u0,0,0 I

  

R: encodes Ohm’s law V: encodes Kirchhoff’s voltage law I: encodes Kirchhoff’s current law

slide-10
SLIDE 10

Expressive Linear Algebra in Haskell Motivation

Matrix symmetry

A =

  

uT

0,0,0

R V u0,0,0 I

  

R =

     

RX,0,0 RX,0,1 ... RZ,1,1

     

I = VT

slide-11
SLIDE 11

Expressive Linear Algebra in Haskell Motivation

Matrix features

Matrix A is symmetric, composed from blocks, and every block has a special sub-structure. Can we represent this with Haskell’s type system?

slide-12
SLIDE 12

Expressive Linear Algebra in Haskell Solution

1 Motivation 2 Solution 3 More realistic problem 4 More features 5 Closing

slide-13
SLIDE 13

Expressive Linear Algebra in Haskell Solution

Solution of simultaneous linear equations

Specialised: (#\|) :: (Shape height , Eq Eq Eq height , Floating Floating Floating a) => => => Matrix.Symmetric height a -> Vector height a -> Vector height a Generic: (#\|) :: (Solve typ , HeightOf typ ˜ height , Eq Eq Eq height , Floating Floating Floating a) => => => Matrix typ a -> Vector height a -> Vector height a Infix operator reads as: “Matrix divides column vector”. Implemented by LAPACK’s SPTRS

slide-14
SLIDE 14

Expressive Linear Algebra in Haskell Solution

Array shapes and indices

comfort-array Index type is a type function of the array shape. class class class Shape.C shape where where where size :: shape

  • > Int

Int Int class class class Shape.C shape => => => Shape.Indexed shape where where where type type type Index shape Read a single element: (!) :: Array Array Array sh a -> Index sh -> a

slide-15
SLIDE 15

Expressive Linear Algebra in Haskell Solution

Zero-based indexing shape

newtype newtype newtype Shape.ZeroBased n = ZeroBased n instance instance instance (Integral Integral Integral n) => => => Shape.Indexed (ZeroBased n) where where where type type type Index (ZeroBased n) = n (!) :: Array Array Array (ZeroBased Int Int Int) a -> Int Int Int -> a array array array ! 0 Classical zero-based indexing scheme as in hmatrix In contrast to array lower bound is statically fixed to zero

slide-16
SLIDE 16

Expressive Linear Algebra in Haskell Solution

Enumeration shape

data data data Shape.Enumeration enum = Enumeration instance instance instance (Enum Enum Enum enum , Bounded Bounded Bounded enum) => => => Shape.Indexed (Enumeration enum) where where where type type type Index (Enumeration enum) = enum (!) :: Array Array Array (Enumeration Ordering Ordering Ordering) a -> Ordering Ordering Ordering

  • > a

array array array ! compare compare compare x y Shape statically determined by an Enum Enum Enum type

slide-17
SLIDE 17

Expressive Linear Algebra in Haskell Solution

Cartesian product shape

instance instance instance (Shape.Indexed sha , Shape.Indexed shb) => => => Shape.Indexed (sha , shb) where where where type type type Index (sha , shb) = (Index sha , Index shb) type type type E = Enumeration (!) :: Array Array Array (E Ordering Ordering Ordering , E Bool Bool Bool) a -> (Ordering Ordering Ordering , Bool Bool Bool) -> a array array array ! (compare compare compare x y, odd

  • dd
  • dd x)

Represents a two-dimensional array of rectangular shape

slide-18
SLIDE 18

Expressive Linear Algebra in Haskell Solution

Sum shape

instance instance instance (Shape.Indexed sha , Shape.Indexed shb) => => => Shape.Indexed (sha :+: shb) where where where type type type Index (sha :+: shb) = Either Either Either (Index sha) (Index shb) type type type E = Enumeration (!) :: Array Array Array (E Ordering Ordering Ordering :+: E Bool Bool Bool) a -> Either Either Either Ordering Ordering Ordering Bool Bool Bool -> a array array array ! Right Right Right False False False Useful for block matrices

slide-19
SLIDE 19

Expressive Linear Algebra in Haskell Solution

Array shapes for corners and edges

data data data Coord = C0 | C1 deriving deriving deriving (Eq Eq Eq , Ord Ord Ord , Show Show Show , Enum Enum Enum , Bounded Bounded Bounded) data data data Dim = DX | DY | DZ deriving deriving deriving (Eq Eq Eq , Ord Ord Ord , Show Show Show , Enum Enum Enum , Bounded Bounded Bounded) type type type Corner = (Coord ,Coord ,Coord) type type type Edge = (Dim ,Coord ,Coord) type type type CoordSh = Shape.Enumeration Coord type type type DimSh = Shape.Enumeration Dim type type type CornerShape = (CoordSh , CoordSh , CoordSh) type type type EdgeShape = (DimSh , CoordSh , CoordSh) type type type BlockShape = ():+: EdgeShape :+: CornerShape

slide-20
SLIDE 20

Expressive Linear Algebra in Haskell Solution

Achievement

no need to write index flattening function yourself consistent structure of matrix and vectors no fight over zero- vs. one-based index counting no off-by-one errors

slide-21
SLIDE 21

Expressive Linear Algebra in Haskell Solution

Voltage matrix

voltageMatrix :: Matrix EdgeShape CornerShape a voltageMatrix = Matrix.fromRowArray cornerShape $ fmap (\e -> Array Array

  • Array. fromAssociations

cornerShape 0 [( edgeCorner e C0 , 1), (edgeCorner e C1 ,

  • 1)]) $

BoxedArray.indices indices indices edgeShape edgeCorner :: Edge -> Coord

  • > Corner

edgeCorner (ed ,e0 ,e1) coord = case case case ed of

  • f
  • f

DX -> (coord ,e0 ,e1) DY -> (e0 ,coord ,e1) DZ -> (e0 ,e1 ,coord)

slide-22
SLIDE 22

Expressive Linear Algebra in Haskell Solution

Stacking symmetric matrices

#%%%# :: (Matrix.Symmetric sha a, Matrix.General sha shb a) -> Matrix.Symmetric shb a -> Matrix.Symmetric (sha :+: shb) a (a,b) #%%%# c ∼

  • A

B BT C

slide-23
SLIDE 23

Expressive Linear Algebra in Haskell Solution

Complete symmetric matrix

fullMatrix :: Vector EdgeShape a -> Matrix.Symmetric (():+: EdgeShape :+: CornerShape) a fullMatrix resistances = let let let symmetricZero = Matrix.zero zero zero . MatrixShape.symmetric in in in ( symmetricZero (), Matrix.singleRow $ Vector.unit (edgeShape :+: cornerShape) (Right Right Right sourceCorner )) #%%%# (diagonal resistances , voltageMatrix ) #%%%# symmetricZero cornerShape

slide-24
SLIDE 24

Expressive Linear Algebra in Haskell Solution

Result

sourceCorner , destCorner :: Corner sourceCorner = (C0 ,C0 ,C0) destCorner = (C1 ,C1 ,C1) totalResistance :: Double Double Double totalResistance = let let let matrix = fullMatrix resistances ix = Right Right Right (Right Right Right destCorner) solutionVector = matrix #\| Vector.unit (Symmetric.size matrix) ix in in in - solutionVector ! ix

slide-25
SLIDE 25

Expressive Linear Algebra in Haskell Solution

Answer

For RX,0,0 = RX,0,1 = · · · = RZ,1,1: Rtotal = 5 6 · RX,0,0

slide-26
SLIDE 26

Expressive Linear Algebra in Haskell Solution

Advantage

Matrix symmetry Specialized solvers Halved space usage Algebraic properties encoded in types, e.g. eigenvalues :: Hermitian sh a -> Vector sh (RealOf a) Complete example: resistor-cube

slide-27
SLIDE 27

Expressive Linear Algebra in Haskell Solution

However

Pretty artificial exercise The lapack bindings still have something to offer for the general problem.

slide-28
SLIDE 28

Expressive Linear Algebra in Haskell More realistic problem

1 Motivation 2 Solution 3 More realistic problem 4 More features 5 Closing

slide-29
SLIDE 29

Expressive Linear Algebra in Haskell More realistic problem

Set shape

import import import Data.Set (Set) instance instance instance Ord Ord Ord ix => => => Shape.Indexed (Set ix) where where where type type type Index (Set ix) = ix (!) :: Array Array Array (Set ix) a -> ix -> a Array Array Array (Set ix) a is isomorphic to Map ix a logarithmic lookup lookup lookup no insert insert insert or delete delete delete instead fast Vector.add etc. caveat: size compatibility check means comparision of sets use as Matrix dimension

slide-30
SLIDE 30

Expressive Linear Algebra in Haskell More realistic problem

Symmetric matrix for general problem

fullMatrix :: (Graph.Edge edge , Ord Ord Ord node) => => => Graph edge node a () -> node -> Matrix.Symmetric (() :+: Set (edge node) :+: Set node) a fullMatrix graph source = ... Graph structure: comfort-graph Complete implementation: linear-circuit

slide-31
SLIDE 31

Expressive Linear Algebra in Haskell More realistic problem

Sparsity

Matrix A is sparse Most big matrix problems are sparse LAPACK has no specialised algorithms for this case so we don’t currently provide ones, as well

slide-32
SLIDE 32

Expressive Linear Algebra in Haskell More realistic problem

Block structure

LAPACK ignores the block structure We could optimize using blockwise inversion

slide-33
SLIDE 33

Expressive Linear Algebra in Haskell More features

1 Motivation 2 Solution 3 More realistic problem 4 More features 5 Closing

slide-34
SLIDE 34

Expressive Linear Algebra in Haskell More features

More features

conversion between Vector and Matrix preserves shape structure i.e. matrices can be vectors in an equation system lapack-ffi: auto-generated via lapack-ffi-tools Test framework for generating consistent and inconsistent matrix dimensions using unique-logic-tf faster comfort-array processing via LLVM and knead support for hyper (strongly hyped interactive notebook editor)

slide-35
SLIDE 35

Expressive Linear Algebra in Haskell More features Closed-world classes

Closed-world classes

lapack: all functions support all element types that LAPACK supports, i.e. Float Float Float, Double Double Double, Complex Complex Complex Float Float Float, Complex Complex Complex Double Double Double closed-world classes:

add methods without extending class you cannot add more types

plain Haskell 98

slide-36
SLIDE 36

Expressive Linear Algebra in Haskell More features Closed-world classes

Closed-world class for Float Float Float and Double Double Double

class class class (Floating Floating Floating a, Prelude Prelude Prelude.RealFloat RealFloat RealFloat a) => => => Real Real Real a where where where switchReal :: f Float Float Float

  • > f Double

Double Double

  • > f a

instance instance instance Real Real Real Float Float Float where where where switchReal f _ = f instance instance instance Real Real Real Double Double Double where where where switchReal _ f = f type type type ASUM_ a = Ptr CInt -> Ptr a -> Ptr CInt -> IO IO IO a newtype newtype newtype ASUM a = ASUM {getASUM :: ASUM_ a} asum :: Real Real Real a => => => ASUM_ a asum = getASUM $ switchReal (ASUM S.asum) (ASUM D.asum)

slide-37
SLIDE 37

Expressive Linear Algebra in Haskell More features Closed-world classes

Closed-world class for all LAPACK number types

class class class (Prelude Prelude Prelude.Fractional Fractional Fractional a) => => => Floating Floating Floating a where where where switchFloating :: f Float Float Float

  • > f Double

Double Double

  • >

f (Complex Complex Complex Float Float Float) -> f (Complex Complex Complex Double Double Double) -> f a instance instance instance Floating Floating Floating Float Float Float where where where switchFloating f _ _ _ = f instance instance instance Floating Floating Floating Double Double Double where where where switchFloating _ f _ _ = f instance instance instance (Real Real Real a) => => => Floating Floating Floating (Complex Complex Complex a) where where where switchFloating _ _ fz fc = getCompose $ switchReal (Compose fz) (Compose fc)

slide-38
SLIDE 38

Expressive Linear Algebra in Haskell More features Transposition

Matrix type tags for triangle-based matrices

type type type Diagonal sh a = Triangular Empty Empty sh a type type type Lower sh a = Triangular Filled Empty sh a type type type Upper sh a = Triangular Empty Filled sh a type type type Symmetric sh a = Triangular Filled Filled sh a diagonal :: Vector sh a -> Triangular lo up sh a transpose transpose transpose :: Triangular lo up sh a -> Triangular up lo sh a GHC can infer: diagonal matrix is also lower triangular and symmetric transposition of transposition maintains original shape transposition of lower triangular matrix is upper triangular transposition of diagonal or symmetric matrix maintains shape

slide-39
SLIDE 39

Expressive Linear Algebra in Haskell More features Transposition

Matrix type tags for triangle-based matrices

class class class Content c where where where instance instance instance Content Empty where where where instance instance instance Content Filled where where where class class class (Content lo , Content up) => => => DiagUpLo lo up where where where instance instance instance DiagUpLo Empty Empty where where where instance instance instance DiagUpLo Empty Filled where where where instance instance instance DiagUpLo Filled Empty where where where square :: (Content lo , Content up) => => => Triangular lo up sh a -> Triangular lo up sh a multiply :: (DiagUpLo lo up , DiagUpLo up lo) => => => Triangular lo up sh a -> Triangular lo up sh a -> Triangular lo up sh a

slide-40
SLIDE 40

Expressive Linear Algebra in Haskell More features Transposition

Matrix type tags for triangle-based matrices

square :: (Content lo , Content up) => => => Triangular lo up sh a -> Triangular lo up sh a multiply :: (DiagUpLo lo up , DiagUpLo up lo) => => => Triangular lo up sh a -> Triangular lo up sh a -> Triangular lo up sh a

GHC can infer: if multiplication preserves triangular shape, then multiplication of transposed matrices does that, too e.g. multiply (transpose transpose transpose a) (transpose transpose transpose b) is accepted without intervention square preserves shape wherever multiply does (superclass relation)

slide-41
SLIDE 41

Expressive Linear Algebra in Haskell More features Transposition

Matrix size relations

type type type Square height width a = Matrix.Full Small Small height width a type type type Tall height width a = Matrix.Full Big Small height width a type type type Wide height width a = Matrix.Full Small Big height width a type type type General height width a = Matrix.Full Big Big height width a

Relations: Square: height == width Tall: size height >= size width Wide: size height <= size width General: arbitrary height, width all relations are transitive

slide-42
SLIDE 42

Expressive Linear Algebra in Haskell More features Transposition

Matrix size relations

transpose transpose transpose :: Matrix.Full vert horiz height width a -> Matrix.Full horiz vert width height a multiply :: Matrix.Full vert horiz height width a -> Matrix.Full vert horiz height width a -> Matrix.Full vert horiz height width a

GHC can infer: transposition of transposition maintains original shape transposition of square matrix is square transposition of tall matrix is wide square times square is square tall times tall is tall

slide-43
SLIDE 43

Expressive Linear Algebra in Haskell More features Transposition

Matrix size relations

transpose transpose transpose :: Matrix.Full vert horiz height width a -> Matrix.Full horiz vert width height a multiply :: Matrix.Full vert horiz height width a -> Matrix.Full vert horiz height width a -> Matrix.Full vert horiz height width a

ugly: you must explicitly relax size relations

slide-44
SLIDE 44

Expressive Linear Algebra in Haskell Closing

1 Motivation 2 Solution 3 More realistic problem 4 More features 5 Closing

slide-45
SLIDE 45

Expressive Linear Algebra in Haskell Closing

LAPACK+BLAS

The lapack binding is based on LAPACK+BLAS library. Advantages: standard solution, ubiquitous availability many implementations optimized for usage of caches and vector units (OpenBLAS, ATLAS, MKL) many advanced functions:

linear solvers, least squares and minimum norm solvers, eigenvalues and singular value decompositions

  • f many common matrix types
slide-46
SLIDE 46

Expressive Linear Algebra in Haskell Closing

LAPACK+BLAS

Disadvantages: restricted to Float Float Float, Double Double Double, Complex Complex Complex Float Float Float, Complex Complex Complex Double Double Double i.e. no QuadDouble, no finite fields, no interval arithmetics no sparse matrices no batch processing, i.e. optimized simultaneous solution of many equally sized problems missing basic functions: matrix transpose, minimum and maximum, product

slide-47
SLIDE 47

Expressive Linear Algebra in Haskell Closing

Existing Alternatives

Alternatives in Haskell: hmatrix: matrices with dynamic and static sizes of natural numbers repa, accelerate: for efficiency reasons restricted to cubic shapes array: flexible indexing schemes but shape and index types coincide

slide-48
SLIDE 48

Expressive Linear Algebra in Haskell Closing

Conclusion

There is more to static checks on matrix computations than type-level natural numbers. Special matrix types: better documentation + optimized algorithms Transpositions treatable with simple type tricks – Can be generalized to operations on (small) permutations.