Name: Anonymous 2010-07-08 0:58
I'm trying to figure out how to code a recursive matrix multiplication algorithm. Could someone please post pseudo-code.
(define matrix-times
(λ(one two)
(let ((sum (λ(d)
(foldl + 0 d))))
(letrec ((transpose (λ(l)
(if (empty? (first l))
empty
(cons (map first l)
(transpose (map rest l)))))))
(if (number? one)
(map (λ(x) (map (λ(y) (* one y)) x)) two)
(let ((two (transpose two)))
(map (λ(x) (map (λ(y) (sum (map * x y))) two)) one)))))))newtype Matrix a = Matrix [[a]] deriving (Eq, Show)
instance Num a => Num (Matrix a) where
Matrix as + Matrix bs = Matrix (zipWith (zipWith (+)) as bs)
Matrix as - Matrix bs = Matrix (zipWith (zipWith (-)) as bs)
Matrix as * Matrix bs = Matrix [[sum $ zipWith (*) a b | b <- transpose bs] | a <- as]
negate (Matrix as) = Matrix (map (map negate) as)
fromInteger x = Matrix (iterate (0:) (fromInteger x : repeat 0))
abs m = m
signum _ = 1
{-# OPTIONS -fglasgow-exts #-}
import Data.List
class BuildList a r | r-> a where
build' :: [a] -> a -> r
instance BuildList a [a] where
build' l x = reverse $ x:l
instance BuildList a r => BuildList a (a->r) where
build' l x y = build' (x:l) y
build :: BuildList a r => a -> r
build x = build' [] x
map * for two lists, and then was shocked to see zipWith3.