How to do Matrix Multiplication in python

0 votes

I'm trying to multiply two matrices together using pure python.

X1 =  [[1.0016, 0.0, -16.0514], 
       [0.0, 10000.0, -40000.0], 
       [-16.0514, -40000.0, 160513.6437]]
Xt =  [(1.0, 1.0), 
       (0.0, 0.25), 
       (0.0, 0.0625)]

where Xt is the zip transpose of another matrix. Now here is the code:

def matrixmult (A, B):
    C = [[0 for row in range(len(A))] for col in range(len(B[0]))]
    for i in range(len(A)):
        for j in range(len(B[0])):
            for k in range(len(B)):
                C[i][j] += A[i][k]*B[k][j]
    return C

Getting this error: IndexError: list index out of range. Now I'm not sure if Xt is recognised as an matrix and is still a list object, but technically this should work.

Nov 15, 2018 in Python by Jino
• 5,820 points
1,971 views

1 answer to this question.

0 votes
def matmult(a,b):
    zip_b = zip(*b)
    # uncomment next line if python 3 : 
    # zip_b = list(zip_b)
    return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) 
             for col_b in zip_b] for row_a in a]

x = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
y = [[1,2],[1,2],[3,4]]

import numpy as np # I want to check my solution with numpy

mx = np.matrix(x)
my = np.matrix(y)       

Result:

>>> matmult(x,y)
[[12, 18], [27, 42], [42, 66], [57, 90]]
>>> mx * my
matrix([[12, 18],
        [27, 42],
        [42, 66],
        [57, 90]])
answered Nov 15, 2018 by findingbugs
• 3,260 points

Related Questions In Python

0 votes
2 answers

How do I connect to a MySQL Database in Python?

connect mysql database with python import MySQLdb db = ...READ MORE

answered Mar 28, 2019 in Python by rajesh
• 1,270 points
1,892 views
0 votes
1 answer

How do I append one string to another in Python?

If you only have one reference to ...READ MORE

answered Oct 22, 2018 in Python by SDeb
• 13,300 points
744 views
0 votes
1 answer

How do you add a background thread to flask in Python?

The example below creates a background thread ...READ MORE

answered Nov 19, 2018 in Python by Nymeria
• 3,560 points
36,851 views
0 votes
1 answer

List comprehension on a nested list - How to do it in Python?

Not sure what your desired output is, ...READ MORE

answered Nov 21, 2018 in Python by Nymeria
• 3,560 points
1,521 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,442 views
0 votes
1 answer
0 votes
2 answers

How to implement Stack in python

There are different method to implement stack ...READ MORE

answered Jun 21, 2020 in Python by pagarsach
• 160 points
1,355 views
0 votes
1 answer

How to write to CSV line by line in python

The simple way of doing this will ...READ MORE

answered Nov 15, 2018 in Python by findingbugs
• 3,260 points
5,657 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP