{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# 1-Dimensional Derivatives\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import interpolate\nfrom scipy.stats import norm\n\nimport itertools\n\nimport ndsplines\n\n\ndef sin(x_in):\n    x = np.pi*(x_in-0.5)\n    return np.sin(x)\n\ndef cos(x_in):\n    x = np.pi*(x_in-0.5)\n    return np.cos(x)\n\nfuncs = [sin, cos]\n\nx = np.linspace(0, 1, 9)\nxx = np.linspace(-.0625, 1.0625, 1024)\nk = 3\n\nfor degree in range(1,4):\n    for func in funcs:\n        fvals = func(x)\n        truef = func(xx)\n        if degree > 0:\n            fig, axes = plt.subplots(3,1, constrained_layout=True)\n        else:\n            fig, axes = plt.subplots(2,1, constrained_layout=True)\n    \n        plot_sel = slice(None)\n    \n        plt.gca().set_prop_cycle(None)\n        test_Bspline = interpolate.make_interp_spline(x, fvals, k=degree)\n        splinef = test_Bspline(xx.copy(), extrapolate=True)\n        axes[0].plot(xx, splinef, '--', lw=3.0, label='BSpline')\n        if degree > 0:\n            der_Bspline = test_Bspline.derivative()\n            axes[1].plot(xx, der_Bspline(xx.copy()), '--', lw=3.0, label='BSpline')\n        antider_Bspline = test_Bspline.antiderivative()\n        axes[-1].plot(xx, antider_Bspline(xx.copy()), '--', lw=3.0, label='BSpline')\n\n        for ax in axes:\n            ax.set_prop_cycle(None)\n        test_NDBspline = ndsplines.make_interp_spline(x, fvals, degrees=degree)\n\n        NDsplinef = test_NDBspline(xx.copy())\n        axes[0].plot(xx, NDsplinef, label='ndspline' )\n        if degree>0:\n            der_NDspline = test_NDBspline.derivative(0)\n            axes[1].plot(xx, der_NDspline(xx.copy()), label='ndspline' )\n        antider_NDspline = test_NDBspline.antiderivative(0)\n        axes[-1].plot(xx, antider_NDspline(xx.copy()), label='ndspline')\n\n        \n        axes[0].plot(xx, truef, 'k--', label=\"True \" + func.__name__)\n        axes[0].plot(x, fvals, 'ko')\n        plt.suptitle('k=%d'%degree)\n        \n        axes[0].legend(loc='best')\n        plt.show()"
      ]
    }
  ],
  "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.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}