Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active March 27, 2016 05:32
Show Gist options
  • Save louisswarren/4092d629e66a87f41ee7 to your computer and use it in GitHub Desktop.
Save louisswarren/4092d629e66a87f41ee7 to your computer and use it in GitHub Desktop.
def scale_matrix(m, s):
return list(list(s * x for x in row) for row in m)
def print_matrix(m):
print('--' + ' ' * (len(m[0]) * 10) + '--')
for row in m:
print('| ', end='')
for x in row:
print('{:10.2f}'.format(x), end='')
print(' |')
print('--' + ' ' * (len(m[0]) * 10) + '--')
def mul_matrix(a, b):
return list(list(
sum(ax * by for ax, by in zip(a[i], (b[k][j] for k in range(len(b)))))
for j in range(len(a))) for i in range(len(a)))
a = [[1,2,3],[4,5,6],[7,8,10]]
b = scale_matrix([[-2,-4,3],[-2,11,-6],[3,-6,3]], 1/3)
print_matrix(a)
print_matrix(b)
print_matrix(mul_matrix(a, b))
print()
c = list([x] for x in range(6))
d = [list(range(6))]
print_matrix(c)
print_matrix(d)
print_matrix(mul_matrix(c, d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment