Skip to content

Commit

Permalink
Merge pull request rasbt#28 from rasbt/matmul
Browse files Browse the repository at this point in the history
The 5fth way to multiply 2 matrices
  • Loading branch information
rasbt authored Oct 22, 2022
2 parents 7deca80 + 0503ac6 commit e5458d2
Showing 1 changed file with 163 additions and 2 deletions.
165 changes: 163 additions & 2 deletions math/Four-matrix-multiplications.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 6,
"id": "f1560377",
"metadata": {},
"outputs": [
Expand All @@ -232,7 +232,7 @@
" [61.82, 62.96, 64.1 ]])"
]
},
"execution_count": 11,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -283,6 +283,167 @@
" \n",
"AB"
]
},
{
"cell_type": "markdown",
"id": "75c9ea85-8a99-4442-a097-f2d83cad8cbb",
"metadata": {},
"source": [
"## Bonus: The 5th way -- Block multiplication\n",
"\n",
"In block multiplication, we can divide up a large matrix into smaller blocks and then multiply them separately.\n",
"\n",
"Suppose we have the following matrix multiplication\n",
"\n",
"$$\n",
"A=\\begin{bmatrix}\n",
"1.1 & 1.2 & 1.3 & 1.4\\\\\n",
"2.1 & 2.2 & 2.3 & 2.4\\\\\n",
"3.1 & 3.2 & 3.3 & 3.4\\\\\n",
"4.1 & 4.2 & 4.3 & 4.4\n",
"\\end{bmatrix}\n",
"B=\\begin{bmatrix}\n",
"5.1 & 5.2 & 5.3 & 5.4\\\\\n",
"6.1 & 6.2 & 6.3 & 6.4\\\\\n",
"7.1 & 7.2 & 7.3 & 7.4\\\\\n",
"8.1 & 8.2 & 8.3 & 8.4\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"$$\n",
"AB=\\begin{bmatrix}\n",
"33.5 & 34.0 & 34.5 & 35.0\\\\\n",
"59.9 & 60.8 & 61.7 & 62.6\\\\\n",
"86.3 & 87.6 & 88.9 & 90.2\\\\\n",
"112.7 & 114.4 & 116.1 & 117.8\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"The multiplication of the blocks looks as follows:"
]
},
{
"cell_type": "markdown",
"id": "2840e3f0-a99d-4676-a74d-34c62199d526",
"metadata": {},
"source": [
"$$\n",
"A = \\left[\\begin{array}{ll}\n",
"A_1 & A_2 \\\\\n",
"A_3 & A_4\n",
"\\end{array}\\right]\n",
"B = \\left[\\begin{array}{ll}\n",
"B_1 & B_2 \\\\\n",
"B_3 & B_4\n",
"\\end{array}\\right] \\\\ \n",
"AB =\n",
"\\left[\\begin{array}{ll} A_1 B_1+A_2 B_3 & A_1 B_2+A_2 B_4 \\\\\n",
"A_3 B_1+A_4 B_3 & A_3 B_2+A_4 B_4\n",
"\\end{array}\\right]\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d4f153c6-c698-44af-8cb3-6c177315173b",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1.1, 1.2, 1.3, 1.4],\n",
" [2.1, 2.2, 2.3, 2.4],\n",
" [3.1, 3.2, 3.3, 3.4],\n",
" [4.1, 4.2, 4.3, 4.4]])\n",
"\n",
"B = np.array([[5.1, 5.2, 5.3, 5.4],\n",
" [6.1, 6.2, 6.3, 6.4],\n",
" [7.1, 7.2, 7.3, 7.4],\n",
" [8.1, 8.2, 8.3, 8.4]])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "57540937-2e52-4a8a-bb87-42c9ed55966e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 33.5, 34. , 34.5, 35. ],\n",
" [ 59.9, 60.8, 61.7, 62.6],\n",
" [ 86.3, 87.6, 88.9, 90.2],\n",
" [112.7, 114.4, 116.1, 117.8]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A @ B"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a217d3bf-62a6-43c8-a71d-6544e1e79ea1",
"metadata": {},
"outputs": [],
"source": [
"A1, A2 = A[:2, :2], A[:2, 2:]\n",
"A3, A4 = A[2:, :2], A[2:, 2:]\n",
"\n",
"B1, B2 = B[:2, :2], B[:2, 2:]\n",
"B3, B4 = B[2:, :2], B[2:, 2:]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "1b888d6b-5ede-4dd2-a619-298fdcbcc41c",
"metadata": {},
"outputs": [],
"source": [
"C1, C2 = A1@B1 + A2@B3, A1@B2 + A2@B4\n",
"C3, C4 = A3@B1 + A4@B3, A3@B2 + A4@B4"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "947f1463-c956-4098-882a-9ea22a51e7ef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 33.5, 34. , 34.5, 35. ],\n",
" [ 59.9, 60.8, 61.7, 62.6],\n",
" [ 86.3, 87.6, 88.9, 90.2],\n",
" [112.7, 114.4, 116.1, 117.8]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"AB = np.vstack((np.hstack((C1, C2)),\n",
" np.hstack((C3, C4))))\n",
"\n",
"AB"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15be5d4a-4c1f-4c2c-ba27-7eed5875711c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit e5458d2

Please sign in to comment.