Created
November 19, 2017 03:08
-
-
Save myurasov/6bc927a538f8247b25cbc4aec2e90aac to your computer and use it in GitHub Desktop.
matmul - manual vs numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import numpy as np", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "METHOD_MANUAL = 0\nMETHOD_NUMPY = 1\n\n\ndef matmul(x, y, method=METHOD_NUMPY):\n\n if METHOD_MANUAL == method:\n\n # CX == RY\n # RXxCX * RYxCY -> RXxCY\n\n RX = len(x)\n CX = len(x[0]) if RX else 0\n RY = len(y)\n CY = len(y[0]) if RY else 0\n\n if CX != RY:\n raise 'Incompatible dimensions'\n\n result = np.zeros(\n (RX, CY), dtype=x.dtype if isinstance(x, np.ndarray) else np.float\\\n )\n\n if RX and CY:\n for rx in range(RX):\n for cy in range(CY):\n for ry in range(RY):\n result[rx][cy] += x[rx][ry] * y[ry][cy]\n\n return result\n\n elif METHOD_NUMPY == method:\n\n return np.matmul(x, y)", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"nbconvert_exporter": "python", | |
"name": "python", | |
"codemirror_mode": { | |
"version": 3, | |
"name": "ipython" | |
}, | |
"version": "3.5.2", | |
"pygments_lexer": "ipython3", | |
"mimetype": "text/x-python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "7f6481cc4ab599952252430c39c103d4", | |
"data": { | |
"description": "matmul - manual vs numpy", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/7f6481cc4ab599952252430c39c103d4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment