Matrix multiplication (Python)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Python

Category:Matrix multiplication

Contents

[edit] Multiply matrix by vector

[edit] Recursion

<<mvmul_recursion_with_dot.py>>=
def mvmul(matrix, vector):
    if matrix:
        return [dot(matrix[0], vector)] +\
            mvmul(matrix[1:], vector)
    else:
        return []

[edit] List Comprehension

<<mvmul_list_comprehension_with_dot.py>>=
def mvmul(matrix, vector):
    return [dot(m, vector) for m in matrix]

[edit] Higher Order

<<mvmul_higher_order.py>>=
def mvmul(matrix, vector):
    return map(lambda m: sum(map(lambda u, v: u * v, m, vector)), matrix)
<<mvmul_higher_order_with_dot.py>>=
def mvmul(matrix, vector):
    return map(lambda m: dot(m, vector), matrix)

[edit] Multiply matrix by matrix

[edit] Recursion


[edit] List Comprehension


[edit] Higher Order


Download code
hijacker
hijacker
hijacker
hijacker