Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Created December 1, 2020 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuxXx/52ea330ef38f08d43479f2dcc3e41cc3 to your computer and use it in GitHub Desktop.
Save LuxXx/52ea330ef38f08d43479f2dcc3e41cc3 to your computer and use it in GitHub Desktop.
Linear Mapping Animation
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import interact\n",
"import ipywidgets as widgets"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([[2, 0],\n",
" [0, 0]]), 0.0)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = 1/5*np.array([\n",
" [3, 4],\n",
" [-4, 3]\n",
"])\n",
"A, np.linalg.det(A)\n",
"B = lambda a: np.array([\n",
" [np.cos(a), -np.sin(a)],\n",
" [np.sin(a), np.cos(a)]\n",
"])\n",
"B(np.pi), np.linalg.det(B(np.pi))\n",
"C = np.array([\n",
" [0, 1],\n",
" [1, 0]\n",
"])\n",
"C, np.linalg.det(C)\n",
"D = np.array([\n",
" [2, 0],\n",
" [0, 0]\n",
"])\n",
"D, np.linalg.det(D)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"N = 10+1\n",
"a = -100\n",
"b = 100\n",
"space = np.linspace(a, b, N)\n",
"x, y = np.meshgrid(space, space)\n",
"color = np.linspace(0, 1, N*N)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "42558b776e734919a7e70b5808edbb03",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='w', max=1.0, step=0.01), Output()), _dom_classes=('w…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"@interact(w=(0, 1, 0.01))\n",
"def draw(w):\n",
" M = (1-w)*np.eye(2) + w*C\n",
" \n",
" x2, y2 = M.dot([x.flatten(), y.flatten()])\n",
" x2, y2 = x2.reshape((N,N)), y2.reshape((N,N))\n",
"\n",
" plt.figure(figsize=(20, 10))\n",
" plt.ylim((3*a, 3*b))\n",
" plt.xlim((3*a, 3*b))\n",
" plt.gca().set_aspect('equal', adjustable='box')\n",
" plt.scatter(x, y, c=color, alpha=0.8)\n",
" plt.scatter(x2, y2, c=color, alpha=0.8)"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"i = 0\n",
"for w in np.linspace(0, 1, 100+1):\n",
" draw(w)\n",
" plt.savefig('data/anim_' + str(i) + '.png')\n",
" plt.close('all')\n",
" i+=1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment