From 8e841729fe8ad2c31b7514bbd99e9dfee1a55628 Mon Sep 17 00:00:00 2001
From: Bas Nijholt <basnijholt@gmail.com>
Date: Thu, 12 Sep 2019 17:22:00 +0200
Subject: [PATCH] start new figure

---
 figures.ipynb | 132 ++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 113 insertions(+), 19 deletions(-)

diff --git a/figures.ipynb b/figures.ipynb
index 49bdf4d..53ccedf 100644
--- a/figures.ipynb
+++ b/figures.ipynb
@@ -57,7 +57,7 @@
    "source": [
     "np.random.seed(1)\n",
     "xs = np.array([0.1, 0.3, 0.35, 0.45])\n",
-    "f = lambda x: x**3\n",
+    "f = lambda x: x ** 3\n",
     "ys = f(xs)\n",
     "means = lambda x: np.convolve(x, np.ones(2) / 2, mode=\"valid\")\n",
     "xs_means = means(xs)\n",
@@ -70,7 +70,7 @@
     "ax.annotate(\n",
     "    s=r\"$L_{1,2} = \\sqrt{\\Delta x^2 + \\Delta y^2}$\",\n",
     "    xy=(np.mean([xs[0], xs[1]]), np.mean([ys[0], ys[1]])),\n",
-    "    xytext=(xs[0]+0.05, ys[0] - 0.05),\n",
+    "    xytext=(xs[0] + 0.05, ys[0] - 0.05),\n",
     "    arrowprops=dict(arrowstyle=\"->\"),\n",
     "    ha=\"center\",\n",
     "    zorder=10,\n",
@@ -85,10 +85,12 @@
     "        arrowprops=dict(arrowstyle=\"->\"),\n",
     "        ha=\"center\",\n",
     "    )\n",
-    "    \n",
+    "\n",
     "ax.scatter(xs, ys, c=\"green\", s=5, zorder=5, label=\"existing data\")\n",
     "losses = np.hypot(xs[1:] - xs[:-1], ys[1:] - ys[:-1])\n",
-    "ax.scatter(xs_means, ys_means, c=\"red\", s=300*losses, zorder=8, label=\"candidate points\")\n",
+    "ax.scatter(\n",
+    "    xs_means, ys_means, c=\"red\", s=300 * losses, zorder=8, label=\"candidate points\"\n",
+    ")\n",
     "xs_dense = np.linspace(xs[0], xs[-1], 400)\n",
     "ax.plot(xs_dense, f(xs_dense), alpha=0.3, zorder=7, label=\"function\")\n",
     "\n",
@@ -113,18 +115,26 @@
    "source": [
     "import adaptive\n",
     "\n",
+    "\n",
     "def f(x, offset=0.123):\n",
     "    a = 0.02\n",
-    "    return x + a**2 / (a**2 + (x - offset)**2)\n",
+    "    return x + a ** 2 / (a ** 2 + (x - offset) ** 2)\n",
+    "\n",
     "\n",
     "def g(x):\n",
-    "    return np.tanh(x*40)\n",
+    "    return np.tanh(x * 40)\n",
+    "\n",
     "\n",
     "def h(x):\n",
-    "    return np.sin(100*x) * np.exp(-x**2 / 0.1**2)\n",
+    "    return np.sin(100 * x) * np.exp(-x ** 2 / 0.1 ** 2)\n",
+    "\n",
     "\n",
-    "funcs = [dict(function=f, bounds=(-1, 1), title=\"peak\"), dict(function=g, bounds=(-1, 1), title=\"tanh\"), dict(function=h, bounds=(-0.3, 0.3), title=\"wave packet\")]\n",
-    "fig, axs = plt.subplots(2, len(funcs), figsize=(fig_width, 1.5*fig_height))\n",
+    "funcs = [\n",
+    "    dict(function=f, bounds=(-1, 1), title=\"peak\"),\n",
+    "    dict(function=g, bounds=(-1, 1), title=\"tanh\"),\n",
+    "    dict(function=h, bounds=(-0.3, 0.3), title=\"wave packet\"),\n",
+    "]\n",
+    "fig, axs = plt.subplots(2, len(funcs), figsize=(fig_width, 1.5 * fig_height))\n",
     "n_points = 50\n",
     "for i, ax in enumerate(axs.T.flatten()):\n",
     "    ax.xaxis.set_ticks([])\n",
@@ -132,24 +142,108 @@
     "    if i % 2 == 0:\n",
     "        d = funcs[i // 2]\n",
     "        # homogeneous\n",
-    "        xs = np.linspace(*d['bounds'], n_points)\n",
-    "        ys = d['function'](xs)\n",
+    "        xs = np.linspace(*d[\"bounds\"], n_points)\n",
+    "        ys = d[\"function\"](xs)\n",
     "        ax.set_title(rf\"\\textrm{{{d['title']}}}\")\n",
     "    else:\n",
     "        d = funcs[(i - 1) // 2]\n",
     "        loss = adaptive.learner.learner1D.curvature_loss_function()\n",
-    "        learner = adaptive.Learner1D(d['function'], bounds=d['bounds'], loss_per_interval=loss)\n",
+    "        learner = adaptive.Learner1D(\n",
+    "            d[\"function\"], bounds=d[\"bounds\"], loss_per_interval=loss\n",
+    "        )\n",
     "        adaptive.runner.simple(learner, goal=lambda l: l.npoints >= n_points)\n",
     "        # adaptive\n",
     "        xs, ys = zip(*sorted(learner.data.items()))\n",
-    "    xs_dense = np.linspace(*d['bounds'], 1000)\n",
-    "    ax.plot(xs_dense, d['function'](xs_dense), c='red', alpha=0.3, lw=0.5)\n",
-    "    ax.scatter(xs, ys, s=0.5, c='k')\n",
-    "    \n",
-    "axs[0][0].set_ylabel(r'$\\textrm{homogeneous}$')\n",
-    "axs[1][0].set_ylabel(r'$\\textrm{adaptive}$')\n",
-    "plt.savefig(\"figures/adaptive_vs_grid.pdf\", bbox_inches=\"tight\", transparent=True)\n"
+    "    xs_dense = np.linspace(*d[\"bounds\"], 1000)\n",
+    "    ax.plot(xs_dense, d[\"function\"](xs_dense), c=\"red\", alpha=0.3, lw=0.5)\n",
+    "    ax.scatter(xs, ys, s=0.5, c=\"k\")\n",
+    "\n",
+    "axs[0][0].set_ylabel(r\"$\\textrm{homogeneous}$\")\n",
+    "axs[1][0].set_ylabel(r\"$\\textrm{adaptive}$\")\n",
+    "plt.savefig(\"figures/adaptive_vs_grid.pdf\", bbox_inches=\"tight\", transparent=True)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Fig 3. "
    ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import itertools\n",
+    "import adaptive\n",
+    "import holoviews.plotting.mpl\n",
+    "import matplotlib.tri as mtri\n",
+    "\n",
+    "\n",
+    "def f(xy, offset=0.123):\n",
+    "    a = 0.1\n",
+    "    x, y = xy\n",
+    "    return x * y + a ** 2 / (a ** 2 + (x - offset) ** 2 + (y - offset) ** 2)\n",
+    "\n",
+    "\n",
+    "def g(xy):\n",
+    "    x, y = xy\n",
+    "    return np.tanh(x * 40) * np.tanh(y * 40)\n",
+    "\n",
+    "\n",
+    "def h(xy):\n",
+    "    x, y = xy\n",
+    "    return np.sin(100 * x * y) * np.exp(-x ** 2 / 0.1 ** 2 - y ** 2 / 0.4 ** 2)\n",
+    "\n",
+    "\n",
+    "funcs = [\n",
+    "    dict(function=f, bounds=[(-1, 1), (-1, 1)], title=\"peak\"),\n",
+    "    dict(function=g, bounds=[(-1, 1), (-1, 1)], title=\"tanh\"),\n",
+    "    dict(function=h, bounds=[(-0.3, 0.3), (-0.3, 0.3)], title=\"wave packet\"),\n",
+    "]\n",
+    "fig, axs = plt.subplots(2, len(funcs), figsize=(fig_width, 1.5 * fig_height))\n",
+    "\n",
+    "plt.subplots_adjust(hspace=-0.1, wspace=0.1)\n",
+    "n_points = 50\n",
+    "for i, ax in enumerate(axs.T.flatten()):\n",
+    "    ax.xaxis.set_ticks([])\n",
+    "    ax.yaxis.set_ticks([])\n",
+    "    if i % 2 == 0:\n",
+    "        d = funcs[i // 2]\n",
+    "        # homogeneous\n",
+    "        ax.set_title(rf\"\\textrm{{{d['title']}}}\")\n",
+    "        x, y = d[\"bounds\"]\n",
+    "        xs = np.linspace(*x, n_points)\n",
+    "        ys = np.linspace(*y, n_points)\n",
+    "        data = {xy: d[\"function\"](xy) for xy in itertools.product(xs, ys)}\n",
+    "        learner = adaptive.Learner2D(d[\"function\"], bounds=d[\"bounds\"])\n",
+    "        learner.data = data\n",
+    "    else:\n",
+    "        # adaptive\n",
+    "        d = funcs[(i - 1) // 2]\n",
+    "        learner = adaptive.Learner2D(d[\"function\"], bounds=d[\"bounds\"])\n",
+    "        adaptive.runner.simple(learner, goal=lambda l: l.npoints >= n_points ** 2)\n",
+    "    tri = learner.ip().tri\n",
+    "    triang = mtri.Triangulation(*tri.points.T, triangles=tri.vertices)\n",
+    "    #     ax.triplot(triang, c=\"w\", lw=0.2, alpha=0.8)\n",
+    "    values = np.array(list(learner.data.values()))\n",
+    "    ax.imshow(learner.plot().Image.I.data, extent=(-0.5, 0.5, -0.5, 0.5))\n",
+    "    ax.set_xticks([])\n",
+    "    ax.set_yticks([])\n",
+    "\n",
+    "axs[0][0].set_ylabel(r\"$\\textrm{homogeneous}$\")\n",
+    "axs[1][0].set_ylabel(r\"$\\textrm{adaptive}$\")\n",
+    "plt.savefig(\"figures/adaptive_2D.pdf\", bbox_inches=\"tight\", transparent=True)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
   }
  ],
  "metadata": {
-- 
GitLab