.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/FOR_README.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_FOR_README.py: ========================================= Tutorial for basic usage ========================================= .. GENERATED FROM PYTHON SOURCE LINES 6-125 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_FOR_README_001.png :alt: FOR README :srcset: /auto_examples/images/sphx_glr_FOR_README_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_FOR_README_002.png :alt: FOR README :srcset: /auto_examples/images/sphx_glr_FOR_README_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_FOR_README_003.png :alt: FOR README :srcset: /auto_examples/images/sphx_glr_FOR_README_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_FOR_README_004.png :alt: FOR README :srcset: /auto_examples/images/sphx_glr_FOR_README_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none [[-3.14159265 -1. 2. ] [-3.14159265 -0.5 2. ] [-3.14159265 0. 2. ] [-3.14159265 0.5 2. ] [-3.14159265 1. 2. ] [-2.74889357 -1. 1.27648914] [-2.74889357 -0.5 1.707008 ] [-2.74889357 0. 1.94618514] [-2.74889357 0.5 1.99402057] [-2.74889357 1. 1.85051428] [-2.35619449 -1. 0.66312624] [-2.35619449 -0.5 1.45862137] [-2.35619449 0. 1.90056311] [-2.35619449 0.5 1.98895146] [-2.35619449 1. 1.72378641] [-1.57079633 -1. 0.109375 ] [-1.57079633 -0.5 1.234375 ] [-1.57079633 0. 1.859375 ] [-1.57079633 0.5 1.984375 ] [-1.57079633 1. 1.609375 ] [-0.78539816 -1. 0.66312624] [-0.78539816 -0.5 1.45862137] [-0.78539816 0. 1.90056311] [-0.78539816 0.5 1.98895146] [-0.78539816 1. 1.72378641] [-0.39269908 -1. 1.27648914] [-0.39269908 -0.5 1.707008 ] [-0.39269908 0. 1.94618514] [-0.39269908 0.5 1.99402057] [-0.39269908 1. 1.85051428] [ 0. -1. 2. ] [ 0. -0.5 2. ] [ 0. 0. 2. ] [ 0. 0.5 2. ] [ 0. 1. 2. ] [ 0.39269908 -1. 2.72351086] [ 0.39269908 -0.5 2.292992 ] [ 0.39269908 0. 2.05381486] [ 0.39269908 0.5 2.00597943] [ 0.39269908 1. 2.14948572] [ 0.78539816 -1. 3.33687376] [ 0.78539816 -0.5 2.54137863] [ 0.78539816 0. 2.09943689] [ 0.78539816 0.5 2.01104854] [ 0.78539816 1. 2.27621359] [ 1.57079633 -1. 3.890625 ] [ 1.57079633 -0.5 2.765625 ] [ 1.57079633 0. 2.140625 ] [ 1.57079633 0.5 2.015625 ] [ 1.57079633 1. 2.390625 ] [ 2.35619449 -1. 3.33687376] [ 2.35619449 -0.5 2.54137863] [ 2.35619449 0. 2.09943689] [ 2.35619449 0.5 2.01104854] [ 2.35619449 1. 2.27621359] [ 2.74889357 -1. 2.72351086] [ 2.74889357 -0.5 2.292992 ] [ 2.74889357 0. 2.05381486] [ 2.74889357 0.5 2.00597943] [ 2.74889357 1. 2.14948572] [ 3.14159265 -1. 2. ] [ 3.14159265 -0.5 2. ] [ 3.14159265 0. 2. ] [ 3.14159265 0.5 2. ] [ 3.14159265 1. 2. ]] Coefficients all same? True Knots all same? True Antiderivative of derivative: Coefficients differ by constant? True Knots all same? True Derivative of antiderivative: Coefficients the same? True Knots all same? True | .. code-block:: Python :lineno-start: 8 import ndsplines import numpy as np import matplotlib.pyplot as plt # generate grid of independent variables x = np.array([-1, -7/8, -3/4, -1/2, -1/4, -1/8, 0, 1/8, 1/4, 1/2, 3/4, 7/8, 1])*np.pi y = np.array([-1, -1/2, 0, 1/2, 1]) meshx, meshy = np.meshgrid(x, y, indexing='ij') gridxy = np.stack((meshx, meshy), axis=-1) # generate denser grid of independent variables to interpolate sparse_dense = 2**7 xx = np.concatenate([np.linspace(x[i], x[i+1], sparse_dense) for i in range(x.size-1)]) # np.linspace(x[0], x[-1], x.size*sparse_dense) yy = np.concatenate([np.linspace(y[i], y[i+1], sparse_dense) for i in range(y.size-1)]) # np.linspace(y[0], y[-1], y.size*sparse_dense) gridxxyy = np.stack(np.meshgrid(xx, yy, indexing='ij'), axis=-1) def plots(sparse_data, dense_data, ylabel='f(x,y)'): fig, axes = plt.subplots(1, 2, constrained_layout=True) for yidx in range(sparse_data.shape[1]): axes[0].plot(x, sparse_data[:, yidx], 'o', color='C%d'%yidx, label='y=%.2f'%y[yidx]) axes[0].plot(xx, dense_data[:, np.clip(yidx*sparse_dense, 0, yy.size-1)], color='C%d'%yidx)# label='y=%.1f'%y[yidx]) axes[0].legend() axes[0].set_xlabel('x') axes[0].set_ylabel(ylabel) for xidx in range(sparse_data.shape[0]//2): axes[1].plot(yy, dense_data[(xidx+3)*sparse_dense, :], '--', color='C%d'%xidx,)# label='x=%.1f'%x[xidx+3]) axes[1].plot(y, sparse_data[xidx+3, :], 'o', color='C%d'%xidx, label='x=%.1f'%x[xidx+3],) axes[1].legend() axes[1].set_xlabel('y') plt.show() # evaluate a function to interpolate over input grid meshf = np.sin(meshx) * (meshy-3/8)**2 + 2 # create the interpolating splane interp = ndsplines.make_interp_spline(gridxy, meshf) # evaluate spline over denser grid meshff = interp(gridxxyy) plots(meshf, meshff) ## # as subplots fig, axes = plt.subplots(1,2, constrained_layout=True) gridxxy = np.stack(np.meshgrid(xx, y, indexing='ij'), axis=-1) meshff = interp(gridxxy) for yidx in range(meshf.shape[1]): axes[0].plot(x, meshf[:, yidx], 'o', color='C%d'%yidx, label='y=%.1f'%y[yidx]) axes[0].plot(xx, meshff[:, yidx], color='C%d'%yidx) axes[0].legend() axes[0].set_xlabel('$x$') axes[0].set_ylabel('$f(x,y)$') # y-dir plot gridxyy = np.stack(np.meshgrid(x, yy, indexing='ij'), axis=-1) meshff = interp(gridxyy) for xidx in range(meshf.shape[0]//2): axes[1].plot(yy, meshff[xidx*1+3, :], '--', color='C%d'%xidx, label='x=%.1f'%x[xidx*1+3]) axes[1].plot(y, meshf[xidx*1+3, :], 'o', color='C%d'%xidx) axes[1].legend() axes[1].set_xlabel('$y$') # plt.ylabel(r'$\frac{\partial f(x,y)}{\partial y}$') plt.show() ## # we could also use tidy data format to make the grid tidy_data = np.dstack((gridxy, meshf)).reshape((-1,3)) print(tidy_data) tidy_interp = ndsplines.make_interp_spline_from_tidy(tidy_data, [0,1], [2]) print("\nCoefficients all same?", np.all(tidy_interp.coefficients == interp.coefficients)) print("Knots all same?", np.all([np.all(knot0 == knot1) for knot0, knot1 in zip(tidy_interp.knots, interp.knots)])) # send to example of least squares ## # two ways to evaluate derivative - y direction deriv_interp = interp.derivative(1) deriv1 = deriv_interp(gridxy) deriv2 = interp(gridxxyy, nus=np.array([0,1])) plots(deriv1, deriv2, r'$\frac{\partial f(x,y)}{\partial y}$') ## # two ways to evaluate derivatives x-direction: create a derivative spline or call with nus: deriv_interp = interp.derivative(0) deriv1 = deriv_interp(gridxy) deriv2 = interp(gridxxyy, nus=np.array([1,0])) plots(deriv1, deriv2, r'$\frac{\partial f(x,y)}{\partial x}$') ## # Calculus demonstration interp1 = deriv_interp.antiderivative(0) coeff_diff = interp1.coefficients - interp.coefficients print("\nAntiderivative of derivative:\n","Coefficients differ by constant?", np.allclose(interp1.coefficients+2.0, interp.coefficients)) print("Knots all same?", np.all([np.all(knot0 == knot1) for knot0, knot1 in zip(interp1.knots, interp.knots)])) antideriv_interp = interp.antiderivative(0) interp2 = antideriv_interp.derivative(0) print("\nDerivative of antiderivative:\n","Coefficients the same?", np.allclose(interp2.coefficients, interp.coefficients)) print("Knots all same?", np.all([np.all(knot0 == knot1) for knot0, knot1 in zip(interp2.knots, interp.knots)])) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.784 seconds) .. _sphx_glr_download_auto_examples_FOR_README.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: FOR_README.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: FOR_README.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_