{"id":403,"date":"2021-10-09T09:44:46","date_gmt":"2021-10-09T14:44:46","guid":{"rendered":"https:\/\/davidwdrell.net\/wordpress\/?p=403"},"modified":"2023-05-22T06:05:37","modified_gmt":"2023-05-22T11:05:37","slug":"so-what-is-numpy-matrix-multiplication","status":"publish","type":"post","link":"https:\/\/davidwdrell.net\/wordpress\/?p=403","title":{"rendered":"So what is numpy Matrix Multiplication?"},"content":{"rendered":"\n<p>What is the product of two matrices?<\/p>\n\n\n\n<p>What is the dot product of two matrices?<\/p>\n\n\n\n<p>What is the multiplication of two matrices?<\/p>\n\n\n\n<p>I studied linear algebra in collage in 1984. I wrote my first computer program in Fortran in 1982 using punch cards. I wrote my first matrix multiplication program on an Texas Instruments Calculator in 1985. I started working with numpy matrices in 2019, which is 36 computer-age-years later. <\/p>\n\n\n\n<p>At my first attempt at numpy matrix multiplication I did not get the expected results. As I dug into the documenation, I was very confused because the use of the words &#8220;multiply&#8221; and &#8220;matrix&#8221; have different meanings between my text book from late 20th century and early 21st century numpy. <\/p>\n\n\n\n<p>Since I have not studied mathematics formally since completing my masters in the 1990s, I will not claim the linear algebra definitions have not changed, I will only refer to the &#8220;text book&#8221; I used in the 1980s.<\/p>\n\n\n\n<p>Here is what I learned: numpy matrix multiplication is not mathematical matrix multiplication. But the numpy dot product is what I learned as matrix multiplication back in 1984, sort of&#8230;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Text book definition<\/h2>\n\n\n\n<p>Here is my text book definition of the &#8220;product&#8221; of two matrices.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/uploads\/2021\/10\/text_book_maxmul.png\"><img loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"1024\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/uploads\/2021\/10\/text_book_maxmul-643x1024.png\" alt=\"\" class=\"wp-image-404\" srcset=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/uploads\/2021\/10\/text_book_maxmul-643x1024.png 643w, https:\/\/davidwdrell.net\/wordpress\/wp-content\/uploads\/2021\/10\/text_book_maxmul-188x300.png 188w, https:\/\/davidwdrell.net\/wordpress\/wp-content\/uploads\/2021\/10\/text_book_maxmul-768x1224.png 768w, https:\/\/davidwdrell.net\/wordpress\/wp-content\/uploads\/2021\/10\/text_book_maxmul-964x1536.png 964w, https:\/\/davidwdrell.net\/wordpress\/wp-content\/uploads\/2021\/10\/text_book_maxmul-1285x2048.png 1285w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/a><figcaption class=\"wp-element-caption\">Elementary Linear Algebra, Fourth Edition, by Howard Anton<\/figcaption><\/figure>\n\n\n\n<p>Notice the introduction to the concept at the top of the page, I quote: <\/p>\n\n\n\n<p><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Perhaps the most natural definition of matrix multiplication would seem to be: &#8220;multiply corresponding entries together.&#8221; Surprisingly, however, this definition would not be very useful for most problems.<\/p>\n<cite>P 26, Elementary Linear Algebra, Fourth Edition, by Howard Anton<\/cite><\/blockquote>\n\n\n\n<p>The author explicitly says multiplying corresponding elements is <em>not<\/em> the definition of matrix multiplication, <em>but this is precisely the numpy definition of matrix multiplication<\/em>. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>However, numpy does provide a mathematically correct method to compute the product of two matrices, referred to as the &#8216;dot&#8217; product. To confuse matters more, in my text book, dot product refers only to vectors (1D matrices). In numpy, it refers to N-D matrices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Definition of numpy Matrix Multiplication<\/h2>\n\n\n\n<p>For reference:<\/p>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/user\/basics.broadcasting.html\">https:\/\/numpy.org\/doc\/stable\/user\/basics.broadcasting.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.multiply.html\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.multiply.html<\/a><\/p>\n\n\n\n<p>The numpy.multiply() function simply multiplies corresponding elements. This requires that arrays have the same dimensions. If you attempt to multiply two matrices of different dimensions, numpy will either &#8216;broadcast&#8217; the array to create a matching size, or throw an error if broadcasting will not work. For example:<\/p>\n\n\n<p><\/p>\n<p>Given&nbsp; <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-11cdfa5e736802e5d836a0463cba9627_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#119;&#32;&#61;&#32;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#32;&#49;&#32;&#38;&#32;&#53;&#32;&#38;&#32;&#45;&#51;&#32;&#38;&#32;&#50;&#32;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#94;&#84;\" title=\"Rendered by QuickLaTeX.com\" height=\"26\" width=\"159\" style=\"vertical-align: -7px;\"\/>&nbsp; &nbsp; and&nbsp; <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-2400ba2719d19adf924bf32742fb0a99_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#61;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#32;&#56;&#32;&#38;&#32;&#50;&#32;&#38;&#32;&#52;&#32;&#38;&#32;&#55;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#32;&#94;&#84;\" title=\"Rendered by QuickLaTeX.com\" height=\"26\" width=\"143\" style=\"vertical-align: -7px;\"\/><\/p>\n\n\n<p>What is <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-8bdd912d95ef2264cf3d023278420e8c_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#119;&#94;&#84;&#32;&#120;&#63;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"42\" style=\"vertical-align: 0px;\"\/><\/p>\n\n\n\n<p> <\/p>\n\n\n\n<p>numpy array math is not standard mathmatics. As a python numpy developer you have to be very aware of the nuances of lists of numbers, lists of lists, numpy vectors and numpy arrays as the results you get on common operations will differ. <\/p>\n\n\n\n<p>In this example I will only discuss the case of mumpy matrices (array with two dimensions). So, in python, using standard multiplication (* == np.multiply()):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; import numpy as np\n&gt;&gt;&gt; w = np.array(&#91;&#91;1,5,-3,2] ]).transpose()    # defined as a numpy 2-D array of 1 x 4, if we used vectors, \n&gt;&gt;&gt; x = np.array(&#91; &#91;8,2,4,7] ]).transpose()    #  the transpose would not work\n&gt;&gt;&gt; wt = w.transpose()\n&gt;&gt;&gt; wtx = wt * x\n&gt;&gt;&gt; wtx\narray(&#91;&#91;  8,  40, -24,  16],\n       &#91;  2,  10,  -6,   4],\n       &#91;  4,  20, -12,   8],\n       &#91;  7,  35, -21,  14]])<\/code><\/pre>\n\n\n\n<p>Notice we get a 4 x 4 matrix as a result. Not at all what we expect from the text book definition. The text book definition of multiplying a 1 x 4 vector with a 4 x 1 vector is a 1 dimensional entity, a scalar (in the example, the result should be the value of 20).<\/p>\n\n\n\n<p>What happened was numpy first broad cast the arrays  to match dimensions:<\/p>\n\n\n<p><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-6b1e5fc8601eb86a126b0fdef4c21119_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#119;&#116;&#32;&#61;&#32;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#32;&#49;&#32;&#38;&#32;&#53;&#32;&#38;&#32;&#45;&#51;&#32;&#38;&#32;&#50;&#32;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"22\" width=\"152\" style=\"vertical-align: -7px;\"\/>&nbsp; &nbsp; becomes:<\/p>\n<p> <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-afb080c448f089b613a2cc7e23dc2eb5_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125; &#49;&#32;&#38;&#32;&#53;&#32;&#38;&#32;&#45;&#51;&#32;&#38;&#32;&#50;&#32;&#32;&#92;&#92; &#49;&#32;&#38;&#32;&#53;&#32;&#38;&#32;&#45;&#51;&#32;&#38;&#32;&#50;&#32;&#92;&#92; &#49;&#32;&#38;&#32;&#53;&#32;&#38;&#32;&#45;&#51;&#32;&#38;&#32;&#50;&#32;&#32;&#92;&#92; &#49;&#32;&#38;&#32;&#53;&#32;&#38;&#32;&#45;&#51;&#32;&#38;&#32;&#50;&#32;&#32;&#92;&#92; &#32;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"85\" width=\"111\" style=\"vertical-align: -38px;\"\/>&nbsp; &nbsp;  <\/p>\n<p><\/p>\n\n\n<p>and,<\/p>\n\n\n<p><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-2e0513854fc9fdeec2fbb418a5fe4e78_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#32;&#61;&#32;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#32;&#56;&#32;&#92;&#92;&#32;&#50;&#32;&#92;&#92;&#32;&#52;&#32;&#92;&#92;&#32;&#55;&#32;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"85\" width=\"60\" style=\"vertical-align: -38px;\"\/>&nbsp; &nbsp; becomes:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-8adaeb409aa43e3373a829b76fafa777_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#56;&#32;&#38;&#32;&#56;&#32;&#38;&#32;&#56;&#32;&#38;&#32;&#56;&#32;&#92;&#92;&#50;&#32;&#38;&#32;&#50;&#32;&#38;&#32;&#50;&#32;&#38;&#32;&#50;&#32;&#92;&#92;&#52;&#32;&#38;&#32;&#52;&#32;&#38;&#32;&#52;&#32;&#38;&#32;&#52;&#32;&#92;&#92;&#55;&#32;&#38;&#32;&#55;&#32;&#38;&#32;&#55;&#32;&#38;&#32;&#55;&#32;&#92;&#92;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"85\" width=\"97\" style=\"vertical-align: -38px;\"\/>&nbsp; &nbsp;<\/p>\n<p>So simply multiplying the corresponding elements we get a 4 x 4 matrix:<\/p>\n<p><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-8e400f2a4d3d9e2a7b81063a6bcca5c1_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#56;&#32;&#38;&#32;&#52;&#48;&#32;&#38;&#32;&#45;&#50;&#52;&#32;&#38;&#32;&#49;&#54;&#32;&#92;&#92;&#50;&#32;&#38;&#32;&#49;&#48;&#32;&#38;&#32;&#45;&#54;&#32;&#38;&#32;&#52;&#32;&#92;&#92;&#52;&#32;&#38;&#32;&#50;&#48;&#32;&#38;&#32;&#45;&#49;&#50;&#32;&#38;&#32;&#56;&#32;&#92;&#92;&#55;&#32;&#38;&#32;&#51;&#53;&#32;&#38;&#32;&#45;&#50;&#49;&#32;&#38;&#32;&#49;&#52;&#32;&#92;&#92;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"85\" width=\"137\" style=\"vertical-align: -38px;\"\/>&nbsp; &nbsp;<\/p>\n\n\n<h2 class=\"wp-block-heading\">Getting the Mathematically Correct Answer<\/h2>\n\n\n\n<p>To get the text book correct result, just use the numpy dot() method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; wtx = wt.dot(x)\n&gt;&gt;&gt; wtx\narray(&#91;&#91;20]])<\/code><\/pre>\n\n\n\n<p>But be aware, the correct format of the answer should be the scaler value 20, which numpy displays a 1 x 1 matrix.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the product of two matrices? What is the dot product of two matrices? What is the multiplication of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[10],"tags":[],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403"}],"version-history":[{"count":10,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":488,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions\/488"}],"wp:attachment":[{"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}