A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
38
Task/Matrix-multiplication/Python/matrix-multiplication-1.py
Normal file
38
Task/Matrix-multiplication/Python/matrix-multiplication-1.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
a=((1, 1, 1, 1), # matrix A #
|
||||
(2, 4, 8, 16),
|
||||
(3, 9, 27, 81),
|
||||
(4, 16, 64, 256))
|
||||
|
||||
b=(( 4 , -3 , 4/3., -1/4. ), # matrix B #
|
||||
(-13/3., 19/4., -7/3., 11/24.),
|
||||
( 3/2., -2. , 7/6., -1/4. ),
|
||||
( -1/6., 1/4., -1/6., 1/24.))
|
||||
|
||||
|
||||
|
||||
def MatrixMul( mtx_a, mtx_b):
|
||||
tpos_b = zip( *mtx_b)
|
||||
rtn = [[ sum( ea*eb for ea,eb in zip(a,b)) for b in tpos_b] for a in mtx_a]
|
||||
return rtn
|
||||
|
||||
|
||||
v = MatrixMul( a, b )
|
||||
|
||||
print 'v = ('
|
||||
for r in v:
|
||||
print '[',
|
||||
for val in r:
|
||||
print '%8.2f '%val,
|
||||
print ']'
|
||||
print ')'
|
||||
|
||||
|
||||
u = MatrixMul(b,a)
|
||||
|
||||
print 'u = '
|
||||
for r in u:
|
||||
print '[',
|
||||
for val in r:
|
||||
print '%8.2f '%val,
|
||||
print ']'
|
||||
print ')'
|
||||
10
Task/Matrix-multiplication/Python/matrix-multiplication-2.py
Normal file
10
Task/Matrix-multiplication/Python/matrix-multiplication-2.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
from operator import mul
|
||||
|
||||
def matrixMul(m1, m2):
|
||||
return map(
|
||||
lambda row:
|
||||
map(
|
||||
lambda *column:
|
||||
sum(map(mul, row, column)),
|
||||
*m2),
|
||||
m1)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def mm(A, B):
|
||||
return [[sum(x * B[i][col] for i,x in enumerate(row)) for col in range(len(B[0]))] for row in A]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import numpy as np
|
||||
np.dot(a,b)
|
||||
#or if a is an array
|
||||
a.dot(b)
|
||||
Loading…
Add table
Add a link
Reference in a new issue