Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Recursive Matrix Multiplication

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.

Name: Anonymous 2010-07-08 1:06

(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)))))))

This will let you multiply a scalar by a matrix, or a matrix by a matrix. It does no error checking. Enjoy.

Name: Anonymous 2010-07-08 1:09

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

Name: Anonymous 2010-07-08 1:17

>>3
Did I just correctly realize that Haskell cannot have arbitrary arity functions?

Name: Anonymous 2010-07-08 1:18

>>4
{-# 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

Name: Anonymous 2010-07-08 1:20

>>5
*Main> (build 1) :: [Int]
[1]
*Main> (build 1 2) :: [Int]
[1,2]
*Main> (build 1 2 3) :: [Int]
[1,2,3]
*Main> (build 1 2 3 4) :: [Int]
[1,2,3,4]
*Main>

Name: Anonymous 2010-07-08 1:20

>>5
Cool. I just googled zipWith, saw it was just map * for two lists, and then was shocked to see zipWith3.

I mean... sheesh.

Name: Anonymous 2010-07-08 1:27

Anyone have a c implementation I could look at?

Name: Anonymous 2010-07-08 1:30

>>8
Welcome to /prog/.

Name: Anonymous 2010-07-08 2:01

>>7
zipWith3
python quality

Name: Anonymous 2010-07-08 4:33

>>7
We've already had done that complaint to death. the tl;dr was: It's the type system, deal with it

Name: Anonymous 2010-07-08 22:19

>>11
Ah, the wonders of a type system, will they ever cease?

Name: Anonymous 2010-07-08 22:35

ITT inefficient toy functional bullshite.

Name: Anonymous 2010-07-08 22:50

>>13
Recursion too hard for you bro?

Name: Anonymous 2010-07-09 6:49

>>14
It's unnecessary when good (tail) recursion is equivalent to a while loop.

Name: Anonymous 2010-07-09 12:25

>>14
To answer your question: yes, recursion is clearly too hard for >>15

Name: air max shoes 2010-07-23 10:56

http://www.cheapairmaxs.com air max
http://www.cheapairmaxs.com air max shoes
http://www.cheapairmaxs.com/nike-air-max-2012-c-111.html nike air max 2012
http://www.cheapairmaxs.com/mens-air-max-2010-c-93.html mens nike air max 2010
http://www.cheapairmaxs.com/womens-air-max-2010-c-96.html womens nike air max 2010
http://www.cheapairmaxs.com/mens-air-max-2009-c-95.html mens nike air max 2009
http://www.cheapairmaxs.com/womens-air-max-2009-c-98.html womens nike air max 2009
http://www.cheapairmaxs.com/nike-air-max-2003-c-101.html nike air max 2003
http://www.cheapairmaxs.com/nike-air-max-97-c-94.html nike air max 97
http://www.cheapairmaxs.com/mens-air-max-95-c-102.html mens nike air max 95
http://www.cheapairmaxs.com/womens-air-max-95-c-103.html womens nike air max 95
http://www.cheapairmaxs.com/nike-air-max-93-c-106.html nike air max 93
http://www.cheapairmaxs.com/mens-air-max-91-c-104.html mens nike air max 91
http://www.cheapairmaxs.com/womens-air-max-91-c-105.html womens nike air max 91
http://www.cheapairmaxs.com/nike-air-max-89-c-121.html nike air max 89
http://www.cheapairmaxs.com/nike-air-max-88-c-112.html nike air max 88
http://www.cheapairmaxs.com/mens-air-max-87-c-108.html mens nike air max 87
http://www.cheapairmaxs.com/womens-air-max-87-c-109.html womens nike air max 87
http://www.cheapairmaxs.com/nike-air-max-180-c-123.html nike air max 180
http://www.cheapairmaxs.com/nike-air-max-360-c-124.html nike air max 360
http://www.cheapairmaxs.com/mens-air-max-ltd-c-122.html mens air max ltd
http://www.cheapairmaxs.com/womens-air-max-ltd-c-116.html womens air max ltd
http://www.cheapairmaxs.com/nike-air-max-bw-c-117.html nike air max bw
http://www.cheapairmaxs.com/air-max-premium-c-118.html air max premium
http://www.cheapairmaxs.com/air-max-skyline-c-114.html air max skyline
http://www.cheapairmaxs.com/air-max-zenyth-c-125.html air max zenyth
http://www.cheapairmaxs.com/nike-air-max-tn-c-115.html nike air max tn
http://www.cheapairmaxs.com/kids-air-max-90-c-119.html kids air max 90
http://www.cheapairmaxs.com/kids-air-max-bw-c-120.html kids air max bw

Name: Anonymous 2011-02-04 15:52


Don't change these.
Name: Email:
Entire Thread Thread List