1

I want to iteratively multiply among the array of a matrix

matrix[0] = [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]]
matrix[1] = [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]]
matrix[3] = [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]]
matrix[4] = [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]]
matrix[5] = [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]]
matrix[6] = [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]]

results of multiplication matrix[0] and matrix[1] will be multiply to the next array of matrix, matrix[3]. This result will be again multiply with the next one. The process should be in sequence. any idea or solution? Thanks

5
  • 1
    If you want to multiply arrays in Python, either element-wise or linear algebra-style matrix multiplication, you should use numpy.
    – Marius
    Apr 3, 2015 at 2:03
  • Python 3.5 will add the @ operator for matrix multiplication, if you're not in a hurry. Apr 3, 2015 at 2:05
  • @TigerhawkT3 I knew that was being introduced, but is this actually going to work in vanilla Python for lists of lists, or is it just being made available for numpy etc.?
    – Marius
    Apr 3, 2015 at 2:13
  • As far as I can tell, the whole idea is that it's being added as a basic Python feature. Apr 3, 2015 at 2:14
  • im working with nao robot and the python package in my nao robot does not support numpy. unless i update the python nao software (which i try to avoid). Apr 3, 2015 at 3:09

2 Answers 2

0

What you're describing is a reduce operation:

Python 2.7: built-in functions: reduce

Suppose you've defined this matrix multiplication function:

def multiply(a, b):
    num_r, num_c, num_i = len(a), len(b[0]), len(a[0])
    m = [num_c * [None] for r in range(num_r)]
    for r in range(num_r):
        for c in range(num_c):
            x = 0
            for i in range(num_i):
                x += a[r][i] * b[i][c]
            m[r][c] = x
    return m

And you have this list of matrices:

matrices = [[[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]],
            [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]],
            [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]],
            [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]],
            [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]],
            [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]]]

Now you can multiply the matrices in sequence with one call to reduce:

print reduce(multiply, matrices)
4
  • I have another answer as below, but it seems your solution is more practical (better) than the one I have in mind. Thanks Apr 3, 2015 at 3:45
  • it gives me an error when using the matrix format according to my question above. the error is , the error is num_r, num_c, num_i = len(a), len(b[0]), len(a[0]) TypeError: object of type 'int' has no len(). Apr 3, 2015 at 5:22
  • Each argument to multiply has to be a two-dimensional array. Apparently you're passing it a one-dimensional array. Apr 3, 2015 at 5:26
  • I just realised that. Thanks @ Michael Laszlo Apr 3, 2015 at 5:54
0

I got the idea (operation * is a simplication of matrix multplication process):

for i in range(6):
    A = matrix[i]
    B = matrix[i+1]
    C = A * B
    matrix [i+1] = C
return C

let me know if any other better way to do it. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.