Download code

Jump to: navigation, search

Back to Matrix_multiplication_(Haskell)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

MMListExample.hs

 1 {- The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Matrix_multiplication_(Haskell)?oldid=19547
14 -}
15 
16 
17 module MMListExample where
18 
19 import MMList
20 import Control.Monad
21 
22 ---------------------------------------------------------------------------------
23 -- Compute the Fibonacci sequence by repeated matrix multiplication.
24 -- See: <http://mathworld.wolfram.com/FibonacciQ-Matrix.html>
25 -- and: <http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form>.
26 
27 idM2 = [[1, 0],
28         [0, 1]]
29 
30 fibQ = [[1, 1],
31         [1, 0]]
32 
33 fibs :: [Matrix Integer]
34 fibs = iterate (mulMM fibQ) idM2
35 
36 main = forM_ fibs $ \m -> print m >> putStrLn "Press Return" >> getLine


hijacker
hijacker
hijacker
hijacker

MMList.hs

 1 {- The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Matrix_multiplication_(Haskell)?oldid=19547
14 -}
15 
16 
17 module MMList where
18 
19 import Data.List
20 
21 --------------------------------------------------------------------------------
22 -- We can represent vectors as lists of numbers, and matrices as lists of row
23 -- vectors. This is a simple solution, but it lacks type safety and is
24 -- inefficient compared to possible uses of Haskell's arrays.
25 type Vector a = [a]
26 type Matrix a = [Vector a]
27 
28 
29 --------------------------------------------------------------------------------
30 -- The dot product of vectors u and v, i.e. the sum of the products of
31 -- corresponding entries. Will be used to define the matrix-by-vector product.
32 mulVV :: Num a => Vector a -> Vector a -> a
33 mulVV u v = sum $ zipWith (*) u v
34 
35 --------------------------------------------------------------------------------
36 -- Matrix m right-multiplied by column vector v, i.e. a row vector containing
37 -- the dot product of each row of m with v.
38 -- Will be used to define the matrix-by-matrix product.
39 mulMV :: Num a => Matrix a -> Vector a -> Vector a
40 mulMV m v = map (mulVV v) m
41 
42 --------------------------------------------------------------------------------
43 -- Matrix m right-multiplied by matrix n, i.e. the matrix n after each of its
44 -- columns has been left-multiplied by m.
45 mulMM :: Num a => Matrix a -> Matrix a -> Matrix a
46 mulMM m n = transpose $ map (mulMV m) $ transpose $ n


hijacker
hijacker
hijacker
hijacker