xopto.mcbase.mcutil.interp module¶
- interp1(xi: numpy.ndarray, x: numpy.ndarray, f: numpy.ndarray, order: int = 1, **kwargs) → numpy.ndarray[source]¶
Interpolates 1D functions that are defined on a uniform grid.
- Parameters
xi (np.ndarray) – Points at which to interpolate f.
x (np.ndarray) – A vector of uniformly distributed points at which the values of f are defined.
f (np.ndarray) – Function values at x. The shape of f should be (x.size,).
order (int) – Interpolation order. 0 - nearest neighbour, 1 - linear, …
kwargs (dict) – Keyword arguments passed to map_coordinates.
- Returns
fxi – Interpolated values of f at xi.
- Return type
np.ndarray
Example
>>> import numpy as np >>> from matplotlib import pyplot as pp >>> >>> x = np.linspace(0, np.pi, 5) >>> x_ref = np.linspace(x[0], x[-1], 1000) >>> f = np.cos(x) >>> >>> xi = np.linspace(0, np.pi, 50) >>> fi_lin = interp1(xi, x, f) >>> fi_quad = interp1(xi, x, f, 2) >>> >>> pp.figure() >>> pp.plot(x_ref, np.cos(x_ref), '-k', label='cos(x)') >>> pp.plot(x, f, 'or', label='sample points', markersize=6) >>> pp.plot(xi, fi_lin, 'xg', label='linear', markersize=6) >>> pp.plot(xi, fi_quad, 'xb', label='quadratic', markersize=6) >>> pp.legend()
- interp2(xi: numpy.ndarray, yi: numpy.ndarray, x: numpy.ndarray, y: numpy.ndarray, f: numpy.ndarray, order: int = 1, **kwargs) → numpy.ndarray[source]¶
Interpolates 2D functions that are defined on a uniform grid.
- Parameters
xi (np.ndarray) – X coordinates of the points at which to interpolate f.
yi (np.ndarray) – Y coordinates of the points at which to interpolate f.
x (np.ndarray) – A vector of uniformly distributed points along the x axis at which the values of f are defined.
y (np.ndarray) – A vector of uniformly distributed points along the y axis at which the values of f are defined.
f (np.ndarray) – Function values at points (x, y). The shape of f should be (y.size, x.size).
order (int) – Interpolation order. 0 - nearest neighbour, 1 - linear, …
kwargs (dict) – Keyword arguments passed to map_coordinates.
- Returns
fxyi – Interpolated values of f at (xi, yi).
- Return type
np.ndarray
Example
>>> import numpy as np >>> from matplotlib import pyplot as pp >>> from mpl_toolkits.mplot3d import Axes3D >>> >>> x = np.linspace(-1, 1, 10) >>> y = np.linspace(-1, 1, 10) >>> Y, X = np.meshgrid(y, x, indexing='ij') >>> f = 1.0/(X**2 + Y**2 + 1) >>> >>> xi = np.linspace(0, 1, 30) >>> yi = np.linspace(0, 1, 30) >>> Yi, Xi = np.meshgrid(yi, xi, indexing='ij') >>> fi = interp2(Xi, Yi, x, y, f) >>> >>> fig = pp.figure() >>> ax = fig.add_subplot(111, projection='3d') >>> ax.plot_wireframe(X, Y, f, color='r', label='sample points') >>> ax.plot_wireframe(Xi, Yi, fi, color='g', label='interpolated values') >>> ax.legend()
- interp3(xi: numpy.ndarray, yi: numpy.ndarray, zi: numpy.ndarray, x: numpy.ndarray, y: numpy.ndarray, z: numpy.ndarray, f: numpy.ndarray, order: int = 1, **kwargs) → numpy.ndarray[source]¶
Interpolates 3D functions that are defined on a uniform grid.
- Parameters
xi (np.ndarray) – X coordinates of the points at which to interpolate f.
yi (np.ndarray) – Y coordinates of the points at which to interpolate f.
zi (np.ndarray) – Z coordinates of the points at which to interpolate f.
x (np.ndarray) – A vector of uniformly distributed points along the x axis at which the values of f are defined.
y (np.ndarray) – A vector of uniformly distributed points along the y axis at which the values of f are defined.
z (np.ndarray) – A vector of uniformly distributed points along the z axis at which the values of f are defined.
f (np.ndarray) – Function values at points (x, y, z). The shape of f should be (z.size, y.size, x.size).
order (int) – Interpolation order. 0 - nearest neighbour, 1 - linear, …
kwargs (dict) – Keyword arguments passed to map_coordinates.
- Returns
fxyzi – Interpolated values of f at (xi, yi, zi).
- Return type
np.ndarray
Example
>>> import numpy as np >>>> >>> x = np.linspace(-1, 1, 10) >>> y = np.linspace(-1, 1, 10) >>> z = np.linspace(-1, 1, 10) >>> Z, Y, X = np.meshgrid(z, y, x, indexing='ij') >>> f = 1.0/(X**2 + Y**2 + Z**2 + 1) >>> >>> xi = np.linspace(0, 1, 30) >>> yi = np.linspace(0, 1, 30) >>> zi = np.linspace(0, 1, 30) >>> Zi, Yi, Xi = np.meshgrid(zi, yi, xi, indexing='ij') >>> >>> fi = interp3(Xi, Yi, Zi, x, y, z, f)
- interpn(ti: list, t: list, f: numpy.ndarray, order: int = 1, **kwargs) → numpy.ndarray[source]¶
Interpolates ND functions that are defined on a uniform grid.
- Parameters
ti (list) – A list of coordinates at which to interpolate f ([zi, yi, xi, …]).
t (list) – List of uniformly distributed points along each axis of f. (z, y, x, ..)
f (np.ndarray) – Function values at points (x, y, z, …). The shape of f should be (z.size, y.size, x.size, …).
order (int) – Interpolation order. 0 - nearest neighbour, 1 - linear, …
kwargs (dict) – Keyword arguments passed to map_coordinates.
- Returns
fti – Interpolated values of f at f[t[0], t[1], …, t[-1]].
- Return type
np.ndarray
Example
>>> import numpy as np >>> from matplotlib import pyplot as pp >>> from mpl_toolkits.mplot3d import Axes3D >>> >>> x = np.linspace(-1, 1, 15) >>> y = np.linspace(-1, 1, 10) >>> Y, X = np.meshgrid(y, x, indexing='ij') >>> f = 1.0/(X**2 + Y**2 + 1) >>> >>> xi = np.linspace(0, 1, 30) >>> yi = np.linspace(0, 1, 30) >>> Yi, Xi = np.meshgrid(yi, xi, indexing='ij') >>> fi = interpn([Yi, Xi], [y, x], f) >>> >>> fig = pp.figure() >>> ax = fig.add_subplot(111, projection='3d') >>> ax.plot_wireframe(X, Y, f, color='r', label='sample points') >>> ax.plot_wireframe(Xi, Yi, fi, color='g', label='interpolated values') >>> ax.legend()