Skip to content

Instantly share code, notes, and snippets.

@mkolod
Created January 18, 2021 20:21
Show Gist options
  • Save mkolod/b53512ed70f6d2be61371544327eaf75 to your computer and use it in GitHub Desktop.
Save mkolod/b53512ed70f6d2be61371544327eaf75 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy.random import randn\n",
"np.random.seed(123)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"x = randn(4, 4)\n",
"y = randn(4, 4)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-1.80325051, 0.23031978, -1.38165191, 4.56466603],\n",
" [ 3.04420914, 3.11381025, -4.21803106, 6.38565951],\n",
" [ 3.01796772, 1.9908307 , 1.49058155, 0.70471186],\n",
" [ 3.43633441, 2.96637292, 1.80363195, 1.67459407]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res0 = x.dot(y)\n",
"res0"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-1.80325051, 0.23031978, -1.38165191, 4.56466603],\n",
" [ 3.04420914, 3.11381025, -4.21803106, 6.38565951],\n",
" [ 3.01796772, 1.9908307 , 1.49058155, 0.70471186],\n",
" [ 3.43633441, 2.96637292, 1.80363195, 1.67459407]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res1 = np.outer(x[:, 0], y[0, :])\n",
"res1 += np.outer(x[:, 1], y[1, :])\n",
"res1 += np.outer(x[:, 2], y[2, :])\n",
"res1 += np.outer(x[:, 3], y[3, :])\n",
"res1"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-1.80325051, 0.23031978, -1.38165191, 4.56466603],\n",
" [ 3.04420914, 3.11381025, -4.21803106, 6.38565951],\n",
" [ 3.01796772, 1.9908307 , 1.49058155, 0.70471186],\n",
" [ 3.43633441, 2.96637292, 1.80363195, 1.67459407]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res2 = np.zeros((4, 4))\n",
"for i in range(4):\n",
" res2 += np.expand_dims(x[:, i], 1).dot(np.expand_dims(y[i, :], 0))\n",
"res2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.allclose(res0, res1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.allclose(res0, res2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment