Skip to content

Instantly share code, notes, and snippets.

@myurasov
Created November 19, 2017 03:08
Show Gist options
  • Save myurasov/6bc927a538f8247b25cbc4aec2e90aac to your computer and use it in GitHub Desktop.
Save myurasov/6bc927a538f8247b25cbc4aec2e90aac to your computer and use it in GitHub Desktop.
matmul - manual vs numpy
Display the source blob
Display the rendered blob
Raw
{
"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