{"id":977,"date":"2019-06-04T10:10:19","date_gmt":"2019-06-04T07:10:19","guid":{"rendered":"http:\/\/leonidassavvides.com\/blog\/?p=977"},"modified":"2019-06-04T10:10:19","modified_gmt":"2019-06-04T07:10:19","slug":"addition-and-multiplication-of-matrix-in-python-3","status":"publish","type":"post","link":"https:\/\/www.leonidassavvides.com\/blog\/2019\/06\/addition-and-multiplication-of-matrix-in-python-3\/","title":{"rendered":"Addition and Multiplication of Matrix in Python 3"},"content":{"rendered":"<div style=\"margin-top: 0px; margin-bottom: 0px;\" class=\"sharethis-inline-share-buttons\" ><\/div><h3 style=\"text-align: center;\">Addition and Multiplication of Matrix in Python 3<\/h3>\n<p style=\"text-align: center;\">=========<\/p>\n<p>m1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]<\/p>\n<div>\n<div>m2 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]<\/div>\n<div>m3 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]<\/div>\n<div>m4 = [[1,2,3,4],[4,5,6,7],[7,8,9,0]]<\/div>\n<div>class TwoDimMatrixOp:<\/div>\n<div style=\"padding-left: 30px;\"><\/div>\n<div style=\"padding-left: 30px;\">def __init__(self, m1, m2):<\/div>\n<div style=\"padding-left: 60px;\">self._m1 = m1<\/div>\n<div style=\"padding-left: 60px;\">self._m2 = m2<\/div>\n<div style=\"padding-left: 60px;\">self._result = [ [0]*(len(m1)-1) for _ in xrange(len(m2)) ]<\/div>\n<div style=\"padding-left: 30px;\">def sum(self):<\/div>\n<div style=\"padding-left: 60px;\"># iterate through rows<\/div>\n<div style=\"padding-left: 60px;\">for i in range(len(self._m1)):<\/div>\n<div style=\"padding-left: 90px;\"># iterate through columns<\/div>\n<div style=\"padding-left: 90px;\">for j in range(len(self._m1[i])):<\/div>\n<div style=\"padding-left: 120px;\">self._result[i][j] = self._m1[i][j] + self._m2[i][j]<\/div>\n<div style=\"padding-left: 60px;\">for r in self._result:<\/div>\n<div style=\"padding-left: 90px;\">print(r)<\/div>\n<div style=\"padding-left: 30px;\">def multi(self):<\/div>\n<div style=\"padding-left: 60px;\">self._result_mult = [ [0]*(len(m1)) for _ in xrange(len(m2[0])+1) ]<\/div>\n<div style=\"padding-left: 60px;\"># iterate through rows of X<\/div>\n<div style=\"padding-left: 60px;\">for i in range(len(self._m1)):<\/div>\n<div style=\"padding-left: 90px;\"># iterate through columns of Y<\/div>\n<div style=\"padding-left: 90px;\">for j in range(len(self._m2[0])):<\/div>\n<div style=\"padding-left: 120px;\"># iterate through rows of Y<\/div>\n<div style=\"padding-left: 120px;\">for k in range(len(self._m2)):<\/div>\n<div style=\"padding-left: 150px;\">self._result_mult[i][j] += self._m1[i][k] * self._m2[k][j]<\/div>\n<div style=\"padding-left: 60px;\">for r in self._result_mult:<\/div>\n<div style=\"padding-left: 90px;\">print(r)<\/div>\n<div>TwoDimMatrixOp(m1, m2).sum()<\/div>\n<div>TwoDimMatrixOp(m3, m4).multi()<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Addition and Multiplication of Matrix in Python 3 ========= m1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] m2 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] m3 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] m4 = [[1,2,3,4],[4,5,6,7],[7,8,9,0]] class TwoDimMatrixOp: def __init__(self, m1, m2): self._m1 = m1 self._m2 = m2 self._result = [ [0]*(len(m1)-1) for _ in xrange(len(m2)) ] def sum(self): # iterate through rows for i in range(len(self._m1)): # iterate through &hellip; <a href=\"https:\/\/www.leonidassavvides.com\/blog\/2019\/06\/addition-and-multiplication-of-matrix-in-python-3\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Addition and Multiplication of Matrix in Python 3&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-977","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/posts\/977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/comments?post=977"}],"version-history":[{"count":0,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/posts\/977\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/media?parent=977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/categories?post=977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/tags?post=977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}