Skip to content

Commit

Permalink
Debug strassen()
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrettrobertson committed Oct 14, 2020
1 parent f7ab665 commit 0b8d9d8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -94,86 +94,110 @@
" B_row_top, B_row_btm, B_col_lft, B_col_rgt):\n",
" '''Helper function for strassen().'''\n",
" \n",
" print(A_row_top, A_row_btm, A_col_lft, A_col_rgt, B_row_top, B_row_btm, B_col_lft, B_col_rgt)\n",
" \n",
" n = A_row_btm - A_row_top\n",
" print('Check', A_row_btm - A_row_top, B_row_btm - B_row_top, A_col_rgt - A_col_lft, B_col_rgt - B_col_lft)\n",
" \n",
" if n == 0:\n",
" if n == 1:\n",
" C = A[A_row_top][A_col_lft] * B[B_row_top][B_col_lft]\n",
" return [[C]]\n",
" else:\n",
" A_row_mid = (A_row_top + A_row_btm) // 2\n",
" A_col_mid = (A_col_lft + A_col_rgt) // 2\n",
" B_row_mid = (B_row_top + B_row_btm) // 2\n",
" B_col_mid = (B_row_top + B_row_btm) // 2\n",
" B_col_mid = (B_col_lft + B_col_rgt) // 2\n",
" #print(A_row_mid, A_col_mid, B_row_mid, B_col_mid)\n",
" \n",
" C11 = \\\n",
" strassen_helper(A, B,\n",
" A_row_top, A_row_mid - 1, A_col_lft, A_col_mid - 1,\n",
" B_row_top, B_row_mid - 1, B_col_lft, B_col_mid - 1) +\\\n",
" A_row_top, A_row_mid, A_col_lft, A_col_mid,\n",
" B_row_top, B_row_mid, B_col_lft, B_col_mid) +\\\n",
" strassen_helper(A, B,\n",
" A_row_top, A_row_mid - 1, A_col_mid + 1, A_col_rgt,\n",
" B_row_mid + 1, B_row_btm, B_col_lft, B_col_mid - 1)\n",
" A_row_top, A_row_mid, A_col_mid, A_col_rgt,\n",
" B_row_mid, B_row_btm, B_col_lft, B_col_mid)\n",
" \n",
" C12 = \\\n",
" strassen_helper(A, B,\n",
" A_row_top, A_row_mid - 1, A_col_lft, A_col_mid - 1,\n",
" B_row_top, B_row_mid - 1, B_col_mid + 1, B_col_rgt) +\\\n",
" A_row_top, A_row_mid, A_col_lft, A_col_mid,\n",
" B_row_top, B_row_mid, B_col_mid, B_col_rgt) +\\\n",
" strassen_helper(A, B,\n",
" A_row_top, A_row_mid - 1, A_col_mid + 1, A_col_rgt,\n",
" B_row_mid + 1, B_row_btm, B_col_mid + 1, B_col_rgt)\n",
" A_row_top, A_row_mid, A_col_mid, A_col_rgt,\n",
" B_row_mid, B_row_btm, B_col_mid + 1, B_col_rgt)\n",
" \n",
" C21 = \\\n",
" strassen_helper(A, B,\n",
" A_row_mid + 1, A_row_btm, A_col_lft, A_col_mid - 1,\n",
" B_row_top, B_row_mid - 1, B_col_lft, B_col_mid - 1) +\\\n",
" A_row_mid, A_row_btm, A_col_lft, A_col_mid,\n",
" B_row_top, B_row_mid, B_col_lft, B_col_mid) +\\\n",
" strassen_helper(A, B,\n",
" A_row_mid + 1, A_row_btm, A_col_mid + 1, A_col_rgt,\n",
" B_row_mid + 1, B_row_btm, B_col_lft, B_col_mid - 1)\n",
" A_row_mid, A_row_btm, A_col_mid, A_col_rgt,\n",
" B_row_mid, B_row_btm, B_col_lft, B_col_mid)\n",
" \n",
" C22 = \\\n",
" strassen_helper(A, B,\n",
" A_row_mid + 1, A_row_btm, A_col_lft, A_col_mid - 1,\n",
" B_row_top, B_row_mid - 1, B_col_mid + 1, B_col_rgt) +\\\n",
" A_row_mid, A_row_btm, A_col_lft, A_col_mid,\n",
" B_row_top, B_row_mid, B_col_mid, B_col_rgt) +\\\n",
" strassen_helper(A, B,\n",
" A_row_mid + 1, A_row_btm, A_col_mid + 1, A_col_rgt,\n",
" B_row_mid + 1, B_row_btm, B_col_mid + 1, B_col_rgt)\n",
" A_row_mid , A_row_btm, A_col_mid, A_col_rgt,\n",
" B_row_mid, B_row_btm, B_col_mid, B_col_rgt)\n",
" \n",
" C = [C11[i] + C12[i] for i in range(n)] + [C21[i] + C22[i] for i in range(n)]\n",
" C = [C11[i] + C12[i] for i in range(n - 1)] + [C21[i] + C22[i] for i in range(n - 1)]\n",
" #print(C)\n",
" return C"
]
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": 107,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 1, 5, 6], [3, 4, 7, 8], [0, 1, 5, 6], [3, 4, 7, 8]]\n"
"Check 3 3 3 3\n",
"Check 1 1 1 1\n",
"Check 1 2 2 1\n",
"Check 1 1 1 2\n",
"Check 1 2 2 1\n",
"Check 2 1 1 1\n",
"Check 1 0 0 0\n",
"Check 1 1 1 0\n",
"Check 1 0 0 1\n",
"Check 1 1 1 0\n",
"Check 1 0 0 0\n",
"Check 1 1 1 0\n",
"Check 1 0 0 1\n",
"Check 1 1 1 1\n",
"Check 2 2 2 1\n",
"Check 1 1 1 0\n",
"Check 1 1 1 0\n",
"Check 1 1 1 1\n",
"Check 1 1 1 0\n",
"Check 1 1 1 0\n",
"Check 1 1 1 0\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 2 1 1 2\n",
"Check 1 0 0 1\n",
"Check 1 1 1 1\n",
"Check 1 0 0 1\n",
"Check 1 1 1 0\n",
"Check 1 0 0 1\n",
"Check 1 1 1 1\n",
"Check 1 0 0 1\n",
"Check 1 1 1 1\n",
"Check 2 2 2 2\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 1 1 1 0\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"[[0, 0], [4, 6], [40, 40, 44, 48], [80, 80, 88, 96]]\n"
]
}
],
"source": [
"A = [[0, 1],\n",
" [3, 4]]\n",
"B = [[5, 6],\n",
" [7, 8]]\n",
"n = 2\n",
"\n",
"C = [A[i] + B[i] for i in range(n)] + [A[i] + B[i] for i in range(n)]\n",
"print(C)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = [[0, 1, 2, 3],\n",
" [4, 5, 6, 7],\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -94,86 +94,110 @@
" B_row_top, B_row_btm, B_col_lft, B_col_rgt):\n",
" '''Helper function for strassen().'''\n",
" \n",
" print(A_row_top, A_row_btm, A_col_lft, A_col_rgt, B_row_top, B_row_btm, B_col_lft, B_col_rgt)\n",
" \n",
" n = A_row_btm - A_row_top\n",
" print('Check', A_row_btm - A_row_top, B_row_btm - B_row_top, A_col_rgt - A_col_lft, B_col_rgt - B_col_lft)\n",
" \n",
" if n == 0:\n",
" if n == 1:\n",
" C = A[A_row_top][A_col_lft] * B[B_row_top][B_col_lft]\n",
" return [[C]]\n",
" else:\n",
" A_row_mid = (A_row_top + A_row_btm) // 2\n",
" A_col_mid = (A_col_lft + A_col_rgt) // 2\n",
" B_row_mid = (B_row_top + B_row_btm) // 2\n",
" B_col_mid = (B_row_top + B_row_btm) // 2\n",
" B_col_mid = (B_col_lft + B_col_rgt) // 2\n",
" #print(A_row_mid, A_col_mid, B_row_mid, B_col_mid)\n",
" \n",
" C11 = \\\n",
" strassen_helper(A, B,\n",
" A_row_top, A_row_mid - 1, A_col_lft, A_col_mid - 1,\n",
" B_row_top, B_row_mid - 1, B_col_lft, B_col_mid - 1) +\\\n",
" A_row_top, A_row_mid, A_col_lft, A_col_mid,\n",
" B_row_top, B_row_mid, B_col_lft, B_col_mid) +\\\n",
" strassen_helper(A, B,\n",
" A_row_top, A_row_mid - 1, A_col_mid + 1, A_col_rgt,\n",
" B_row_mid + 1, B_row_btm, B_col_lft, B_col_mid - 1)\n",
" A_row_top, A_row_mid, A_col_mid, A_col_rgt,\n",
" B_row_mid, B_row_btm, B_col_lft, B_col_mid)\n",
" \n",
" C12 = \\\n",
" strassen_helper(A, B,\n",
" A_row_top, A_row_mid - 1, A_col_lft, A_col_mid - 1,\n",
" B_row_top, B_row_mid - 1, B_col_mid + 1, B_col_rgt) +\\\n",
" A_row_top, A_row_mid, A_col_lft, A_col_mid,\n",
" B_row_top, B_row_mid, B_col_mid, B_col_rgt) +\\\n",
" strassen_helper(A, B,\n",
" A_row_top, A_row_mid - 1, A_col_mid + 1, A_col_rgt,\n",
" B_row_mid + 1, B_row_btm, B_col_mid + 1, B_col_rgt)\n",
" A_row_top, A_row_mid, A_col_mid, A_col_rgt,\n",
" B_row_mid, B_row_btm, B_col_mid + 1, B_col_rgt)\n",
" \n",
" C21 = \\\n",
" strassen_helper(A, B,\n",
" A_row_mid + 1, A_row_btm, A_col_lft, A_col_mid - 1,\n",
" B_row_top, B_row_mid - 1, B_col_lft, B_col_mid - 1) +\\\n",
" A_row_mid, A_row_btm, A_col_lft, A_col_mid,\n",
" B_row_top, B_row_mid, B_col_lft, B_col_mid) +\\\n",
" strassen_helper(A, B,\n",
" A_row_mid + 1, A_row_btm, A_col_mid + 1, A_col_rgt,\n",
" B_row_mid + 1, B_row_btm, B_col_lft, B_col_mid - 1)\n",
" A_row_mid, A_row_btm, A_col_mid, A_col_rgt,\n",
" B_row_mid, B_row_btm, B_col_lft, B_col_mid)\n",
" \n",
" C22 = \\\n",
" strassen_helper(A, B,\n",
" A_row_mid + 1, A_row_btm, A_col_lft, A_col_mid - 1,\n",
" B_row_top, B_row_mid - 1, B_col_mid + 1, B_col_rgt) +\\\n",
" A_row_mid, A_row_btm, A_col_lft, A_col_mid,\n",
" B_row_top, B_row_mid, B_col_mid, B_col_rgt) +\\\n",
" strassen_helper(A, B,\n",
" A_row_mid + 1, A_row_btm, A_col_mid + 1, A_col_rgt,\n",
" B_row_mid + 1, B_row_btm, B_col_mid + 1, B_col_rgt)\n",
" A_row_mid , A_row_btm, A_col_mid, A_col_rgt,\n",
" B_row_mid, B_row_btm, B_col_mid, B_col_rgt)\n",
" \n",
" C = [C11[i] + C12[i] for i in range(n)] + [C21[i] + C22[i] for i in range(n)]\n",
" C = [C11[i] + C12[i] for i in range(n - 1)] + [C21[i] + C22[i] for i in range(n - 1)]\n",
" #print(C)\n",
" return C"
]
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": 107,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 1, 5, 6], [3, 4, 7, 8], [0, 1, 5, 6], [3, 4, 7, 8]]\n"
"Check 3 3 3 3\n",
"Check 1 1 1 1\n",
"Check 1 2 2 1\n",
"Check 1 1 1 2\n",
"Check 1 2 2 1\n",
"Check 2 1 1 1\n",
"Check 1 0 0 0\n",
"Check 1 1 1 0\n",
"Check 1 0 0 1\n",
"Check 1 1 1 0\n",
"Check 1 0 0 0\n",
"Check 1 1 1 0\n",
"Check 1 0 0 1\n",
"Check 1 1 1 1\n",
"Check 2 2 2 1\n",
"Check 1 1 1 0\n",
"Check 1 1 1 0\n",
"Check 1 1 1 1\n",
"Check 1 1 1 0\n",
"Check 1 1 1 0\n",
"Check 1 1 1 0\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 2 1 1 2\n",
"Check 1 0 0 1\n",
"Check 1 1 1 1\n",
"Check 1 0 0 1\n",
"Check 1 1 1 0\n",
"Check 1 0 0 1\n",
"Check 1 1 1 1\n",
"Check 1 0 0 1\n",
"Check 1 1 1 1\n",
"Check 2 2 2 2\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 1 1 1 0\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"Check 1 1 1 1\n",
"[[0, 0], [4, 6], [40, 40, 44, 48], [80, 80, 88, 96]]\n"
]
}
],
"source": [
"A = [[0, 1],\n",
" [3, 4]]\n",
"B = [[5, 6],\n",
" [7, 8]]\n",
"n = 2\n",
"\n",
"C = [A[i] + B[i] for i in range(n)] + [A[i] + B[i] for i in range(n)]\n",
"print(C)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = [[0, 1, 2, 3],\n",
" [4, 5, 6, 7],\n",
Expand Down

0 comments on commit 0b8d9d8

Please sign in to comment.