20 views

Uploaded by vpalazon

Semirings Slides

- Model Reduction 061904
- ANTONY GORMLEY Museo de Arte Contemporaneo Monterrey Mexico ANTONY GORMLEY BETWEEN YOU and ME Artium Vitoria Gasteiz Spain
- 8 IJAEMS-JAN-2016-20-Multi-Attribute Group Decision Making of Internet Public Opinion Emergency
- AP DSC School Assistant (Mathematics) 2015
- Mit6 231f15 Notes
- Codigos en Vb de Orde
- Fushan Li - Principal
- a344928.pdf
- ch05_ism
- MATH.grade3.ArraysAllAround (1)
- DEIM-RT-07-003
- RPT Mate F5 2019.docx
- CSE101L_Cyclesheets
- Introduction to Matlab
- Revised Matlab Notes Fall2004
- How to Create Skill Matrix
- tips for iit.doc
- AM_Radiance+EnergyPlus
- Propagation Matrices From the Finite Element Method
- Scilab Assignments

You are on page 1of 26

**A functional pearl on the abuse of linear algebra
**

Stephen Dolan

Computer Laboratory

University of Cambridge

stephen.dolan@cl.cam.ac.uk

September 25, 2013

Linear algebra is magic

**If your problem can be expressed as vectors and matrices, it is
**

essentially already solved.

**Linear algebra works with fields, like the real or complex numbers:
**

sets with a notion of addition, multiplication, subtraction and

division.

Stephen Dolan

Fun with Semirings

What we have are semirings.We don’t have fields CS has many structures with “multiplication” and “addition”: conjunction and disjunction intersection and union sequencing and choice product type and sum type But very few with a sensible “division” or “subtraction”. not fields. Stephen Dolan Fun with Semirings .

1) is a monoid. Algebraic Structures for Transitive Closure. 1977.Semirings A closed semiring is a set with some notion of addition and multiplication as well as a unary operation ∗ . with zero a·1=1·a=a a·0=0·a=0 a · (b + c) = a · b + a · c · distributes over + (a + b) · c = a · c + b · c a∗ = 1 + a · a∗ closure operation Daniel J. where: a+b =b+a (+. Stephen Dolan Fun with Semirings . Lehmann. 0) is a commutative monoid a + (b + c) = (a + b) + c a+0=a a · (b · c) = (a · b) · c (·.

we can often think of closure as: a∗ = 1 + a + a2 + a3 + . Stephen Dolan Fun with Semirings . where a∗ = 1 + a · a∗ = 1 + a∗ · a Intuitively. . .Closed semirings A closed semiring has a closure operation ∗ .

) : : r −> r −> r instance Semiring Bool where zero = False one = True closure x = True (@+) = ( | | ) (@. ) = (&&) Stephen Dolan Fun with Semirings .Closed semirings as a Haskell typeclass i n f i x l 9 @. (@. i n f i x l 8 @+ class Semiring r where zero . one : : r closure : : r −> r (@+).

1 1 1 · 2 = 3 0 1 0 0 0 1 0 0 0 2 3 0 1 0 · 0 0 1 0 0 0 (AB)ij = X 2 0 0 1 = 0 0 0 0 0 0 Aik · Bkj k = ∃k such that Aik ∧ Bkj Stephen Dolan 3 Fun with Semirings . G 2 gives the two-hop paths through G .Adjacency matrices Directed graphs are represented as matrices of Booleans.

. Stephen Dolan Fun with Semirings 3 . . ∗ 1 2 3 ∗ 0 1 0 0 0 1 0 0 0 1 = 2 1 1 1 = 0 1 1 0 0 1 A∗ = 1 + A · A∗ = 1 + A + A2 + A3 + .Closure of an adjacency matrix The closure of an adjacency matrix gives us the reflexive transitive closure of the graph.

Stephen Dolan Fun with Semirings . and Lehmann gives an imperative algorithm for calculating the closure of a matrix.. Matrix addition and multiplication is as normal.A semiring of matrices A matrix is represented by a list of lists of elements.. data Matrix a = Matrix [ [ a ] ] instance Semiring a => Semiring (Matrix a) where .

Closure of a matrix The correctness proof of the closure algorithm states: A B If M = C D ∗ A + B 0 · ∆∗ · C 0 B 0 · ∆∗ ∗ then M = ∆∗ · C 0 ∆∗ where B 0 = A∗ · B. Stephen Dolan Fun with Semirings . C 0 = C · A∗ and ∆ = D + C · A∗ · B.

Matrix a .Block matrices We can split a matrix into blocks. type BlockMatrix a = (Matrix a . Matrix a . Matrix a) msplit : : Matrix a −> BlockMatrix a mjoin : : BlockMatrix a −> Matrix a Stephen Dolan Fun with Semirings . and join them back together.

rest ’ @. top ’ @. top . but the correctness proof gives a recursive functional implementation: closure (Matrix [ [ x ] ] ) = Matrix [ [ closure x ] ] closure m = mjoin ( f i r s t ’ @+ top ’ @. rest ) = msplit m f i r s t ’ = closure f i r s t top ’ = f i r s t ’ @.Closure of a matrix The algorithm is imperative. rest ’ @. l e f t ’ . l e f t . rest ’ . l e f t ’ . rest ’ ) where ( f i r s t . top) Stephen Dolan Fun with Semirings . top l e f t ’ = l e f t @. f i r s t ’ rest ’ = closure ( rest @+ l e f t ’ @.

x = Unreachable Distance a @. Distance b = Distance (a + b) Stephen Dolan Fun with Semirings . Unreachable = Unreachable Unreachable @. data ShortestDistance = Distance Int | Unreachable instance Semiring ShortestDistance where zero = Unreachable one = Distance 0 closure x = one x @+ Unreachable = x Unreachable @+ x = x Distance a @+ Distance b = Distance (min a b) x @. The closure algorithm then finds shortest distances.Shortest distances in a graph Distances form a semiring. with · as addition and + as choosing the shorter.

Path a ’ p ’ = Path (a + a ’ ) (p ++ p ’ ) Stephen Dolan Fun with Semirings . x = NoPath Path a p @. NoPath = NoPath NoPath @.Shortest paths in a graph We can also recover the actual path: data ShortestPath n = Path Int [ ( n . n) ] | NoPath instance Ord n => Semiring (ShortestPath n) where zero = NoPath one = Path 0 [ ] closure x = one x @+ NoPath = x NoPath @+ x = x Path a p @+ Path a ’ p ’ | a < a’ = Path a p | a == a ’ && p < p ’ = Path a p | otherwise = Path a ’ p ’ x @.

. . . Stephen Dolan Fun with Semirings . . . Ann xn + Bn then A∗ · B is a solution (for a matrix A and vector B of coefficients) which can be found using closure. A1n xn + B1 .Solving linear equations If we have a linear equation like: x =a·x +b then a∗ · b is a solution: a∗ · b = (a · a∗ + 1) · b = a · (a∗ · b) + b If we have a system of linear equations like: x1 = A11 x1 + A12 x2 + . . xn = An1 x1 + An2 x2 + .

Stephen Dolan Fun with Semirings .Regular expressions and state machines A state machine can be described by a regular grammar: qA x A → xB y B → yA + zC qB C →1 z qC The regular grammar is a system of linear equations. and the regular expression describing it can be found by closure.

Solving linear equations Reconstructing regular expressions Solving equations in the “free” semiring rebuilds regular expressions from a state machine. Dataflow analysis Solving equations in the semiring of sets of variables does live variables analysis (among others). Stephen Dolan Fun with Semirings .

. . Multiplying by x shifts the sequence one place. so: F = 1 + (x 2 + x) · F Stephen Dolan Fun with Semirings .Linear recurrences and power series Suppose the next value in a sequence is a linear combination of previous values: F (0) = 0 F (1) = 1 F (n) = F (n − 2) + F (n − 1) We represent these as formal power series: F = x + x 2 + 2x 3 + 3x 4 + 5x 5 + 8x 6 .

instance Semiring r => Semiring [ r ] where zero = [ ] one = [ one ] Addition is pointwise: [ ] @+ y = y x @+ [ ] = x (x : xs ) @+ (y : ys ) = (x @+ y ) : ( xs @+ ys ) Stephen Dolan Fun with Semirings .Power series are a semiring We represent power series as lists: a::p is a + px.

M. = [ ] @. without needing indices. q))) This is convolution. b ) : (map (a @. power serious. Douglas McIlroy. Journal of Functional Programming. Power series. b) p @+ ( zero : ( p @. 1999. (b : q) = (a @.Multiplying power series Multiplying power series works like this: (a + px)(b + qx) = ab + (aq + pb + pqx)x In Haskell: [ ] @. Stephen Dolan Fun with Semirings . ) q @+ map (@. [ ] = [ ] (a : p) @.

r )) Stephen Dolan Fun with Semirings .Closure of a power series The closure of a + px must satisfy: (a + px)∗ = 1 + (a + px)∗ · (a + px) This has a solution satisfying: (a + px)∗ = a∗ · (1 + px · (a + px)∗ ) which translates neatly into (lazy!) Haskell: closure [ ] = one closure (a : p) = r where r = [ closure a ] @. (one : ( p @.

again F = 1 + (x + x 2 )F = (x + x 2 )∗ f i b = closure [0 .1 .1] Any linear recurrence can be solved with closure.Fibonacci. Stephen Dolan Fun with Semirings .

that reads: bestn = 100 · bestn−10 + 80 · bestn−7 + 3 · bestn−9 which is a linear recurrence. 3 + bestn−9 ) In the (max.Knapsacks Suppose we are trying to fill our baggage allowance with: Knuth books: Haskell books: Java books: weight 10. value 100 weight 7. +)-semiring. value 80 weight 9. Stephen Dolan Fun with Semirings . value 3 The best value we can have with weight n is: bestn = max(100 + bestn−10 . 80 + bestn−7 .

uk Stephen Dolan Fun with Semirings .cam.Thank you! Questions? Many problems are linear. for a suitable notion of “linear”. stephen.dolan@cl.ac.

y} A x := 1 INC = OUTC ∪ {x} B while x < y: C IND = OUTD ∪ {x} OUTA = INB x := x * 2 OUTB = INC ∪ IND OUTC = INB D return x OUTD = ∅ Stephen Dolan Fun with Semirings . INA = OUTA ∩ {x} INB = OUTB ∪ {x. This live variables analysis uses the semiring of sets of variables.Live variables analysis Many dataflow analyses are just linear equations in a semiring.

Petri nets Timed event graphs (a form of Petri net with a notion of time) can be viewed as “linear” systems. in the (max. The nth token is available from this place 5 time units after then (n − 3)th token is available from its input. 1984. Stephen Dolan Fun with Semirings . P. Cohen.P. Quadrat. Moller. G. Linear system theory for discrete event systems. J. M. +)-semiring 5 This transition fires for the nth time after all of its inputs have fired for the nth time. Viot.

- Model Reduction 061904Uploaded byNanda Kumar R D
- ANTONY GORMLEY Museo de Arte Contemporaneo Monterrey Mexico ANTONY GORMLEY BETWEEN YOU and ME Artium Vitoria Gasteiz SpainUploaded byjorgeheymann
- 8 IJAEMS-JAN-2016-20-Multi-Attribute Group Decision Making of Internet Public Opinion EmergencyUploaded byInfogain publication
- AP DSC School Assistant (Mathematics) 2015Uploaded byMruthyunjay
- Mit6 231f15 NotesUploaded byquison_9102138998
- Codigos en Vb de OrdeUploaded byMaximo Plix
- Fushan Li - PrincipalUploaded byMarco Fernandes
- a344928.pdfUploaded byb4suki
- ch05_ismUploaded byAtul Bhatt
- MATH.grade3.ArraysAllAround (1)Uploaded byharisha2suddapalli
- DEIM-RT-07-003Uploaded byPerseu PadreDe Macedo
- RPT Mate F5 2019.docxUploaded byMeiying Kwok
- CSE101L_CyclesheetsUploaded byabhinishtha
- Introduction to MatlabUploaded bymubashir
- Revised Matlab Notes Fall2004Uploaded byNandan Bm
- How to Create Skill MatrixUploaded bythrinad
- tips for iit.docUploaded byaashrayenergy
- AM_Radiance+EnergyPlusUploaded bymlamour
- Propagation Matrices From the Finite Element MethodUploaded byprateek
- Scilab AssignmentsUploaded byChandreyee Mukherjee
- Finite Fields Important.pdfUploaded byanon_705171735

- Boletin Convocatorias Empleo (3)Uploaded byvpalazon
- Boletin Convocatorias Empleo (2)Uploaded byvpalazon
- Tema 6 - TBE_TAS-Versión BreveUploaded byvpalazon
- ROCRUploaded byvpalazon
- Programacion de Controladores Logicos (PLC)Uploaded byJhon Freddy Burgos
- Tema 13_1 Habilidades AfrontamientoUploaded byvpalazon
- Si Siemens Gigaset-M740AV ManualUploaded byvpalazon
- dell_precision_t1500_service_manual.pdfUploaded byvpalazon
- Boletin Convocatorias Empleo (18)Uploaded byvpalazon
- Boletin Convocatorias Empleo (4)Uploaded byvpalazon
- Boletin_Convocatorias_EmpleoUploaded byvpalazon
- NT QuemadoresUploaded byvpalazon
- Boletin Convocatorias Empleo (5)Uploaded byvpalazon
- Preview BookUploaded byगणेश पराजुली
- A Designers Guide to Instrumentation Amplifiers 3rd EdUploaded byCL Maninho
- MIT Lockpicking GuideUploaded byNarrowPathPilgrim
- Evolution of Modern Control TheoryUploaded byakozy
- SMPSRM-DUploaded bybill1022
- Memoria ProyectoUploaded bycaesar71
- DDS-3X25 User ManualUploaded byvpalazon
- FullSolderComic EnUploaded byTakis Perreas
- VPRE-preguntas Frecuentes de Los Nuevos OperadoresUploaded byvpalazon
- Generador de Señales Peaktech 1014Uploaded byvpalazon
- ex2Uploaded byvpalazon
- Math for Machine LearningUploaded byjoekowalsky
- Analisis fundamental de circuitos (Introduccion)Uploaded byblm3000

- COMALGE Prac Ex 3 july 2012.docUploaded byeverytimeyoulie
- Cover Tov Math p1 2015Uploaded byZulhanif Idris
- Competition2005fall(1).pdfUploaded bySahabudeen Salaeh
- Palindromic Numbers And Iterations of the Pseudo-Smarandache FunctionUploaded byMia Amalia
- hsimsek 2016Uploaded byAsse Ten
- A Geek’s Guide to the BeautyUploaded byAnonymous UqsAfi
- Fourier Made Easy 2Uploaded bySubham Jain
- Arithmetic Sequences answers key.pdfUploaded byTeacherbhing Mitrc
- Time Response AnalysisUploaded byJohn Francis S. Ilarde
- [Wiberg]_Schaum's_State_Space_Linear.pdfUploaded byAdib Hashemi
- Method of Differentiation WAUploaded byAmanjot Kaur
- yr9 revision-1Uploaded byapi-239996681
- Lecture2, MatricesUploaded byHisham Mostafa
- 1 SymmUploaded byRex Angelo Jusayan
- The Locker ProblemUploaded bywilson_miranda_37
- L6_3dtrans.pdfUploaded byKtk Zad
- [BankSoalan] Johor Add Maths P1 2017Uploaded byKhor Han Yi
- quad001.pdfUploaded byKumar Sujeet
- MATH 204-Introduction to Formal Mathematics-Dr.Shamim Arif.pdfUploaded byMinhal Malik
- anal geomUploaded byrhedmish
- 2.7. Special Integrating FactorsUploaded byuchiha_rhenzaki
- 04 PotentialUploaded byRajkumar Muthumanickam
- Algorithm Complexity lalit bansalUploaded byLalit Bansal
- 4037_w12_qp_13Uploaded byS.L.L.C
- Some Notes on Eucleadean GeometryUploaded byAnup Saravan
- concept checklistUploaded byapi-317922107
- Economic Dispatch by Equal Incremental Cost Criterion.pdfUploaded bydubuli123
- (University Lecture Series 038) Lars v. Ahlfors-Lectures on Quasiconformal Mappings-American Mathematical Society (2006)Uploaded bydahuang
- Heat Conduction of a Circular Hollow Cylinder amidst Mixed Boundary ConditionsUploaded byInnovative Research Publications
- Btech 1st Sem: Maths: Several VariablesUploaded byTechno India Group