numbacs.utils
Module Contents
- numbacs.utils.unravel_index(index, shape)[source]
Numba currently does not implement np.unravel_index so we create an implementation here for the specific case where order=’C’ (row-major).
- Parameters:
index (int) – raveled index.
shape (np.ndarray, shape = (ndims,)) – shape of array used for unraveling index.
- Returns:
array containing the unraveled index.
- Return type:
np.ndarray
- numbacs.utils.ravel_index(inds, shape)[source]
Finds raveled index corresponding to grid index given by inds from array with shape=shape where shape must be a np.ndarray.
- Parameters:
inds (np.ndarray, shape = (ndims,)) – array containing indices to be raveled.
shape (np.ndarray, shape = (ndims,)) – shape of array used for raveling index.
- Returns:
r_ind – raveled index.
- Return type:
int
- numbacs.utils.finite_diff_2D(f, i, j, h, axis, direction='c')[source]
Compute 2nd order partial finite difference in the array f @ [i,j] along axis=axis and using a directional difference scheme defined by direction.
- Parameters:
f (np.ndarray, shape = (nx,ny)) – array on which finite differencing is performed.
i (int) – index for axis=0 at which finite differencing is performed.
j (int) – index for axis=1 at which finite differencing is performed.
h (float) – spacing in axis direction.
axis (int) – axis over which finite differencing is performed.
direction (str, optional) – finite differencing direction, optional values are ‘b’ for backward, ‘c’ for centered, and ‘f’ for forward. The default is ‘c’.
- Returns:
df – finite difference value.
- Return type:
float
- numbacs.utils.finite_diff_ND(f, ind, h, axis, shape, direction=0)[source]
Compute 2nd order partial finite difference in the array f @ [ind] where f is an (n_1*n_2*…*n_ndims) array. Axis=axis determines the axis the finite differencing will be performed along shape is [n1,n2,…,n_ndims]. shape must be a np.array and ‘ij’ indexing is assumed.
- Parameters:
f (np.ndarray, shape = (nx_1*nx_2*...*nx_ndims,)) – flattened array of which finite differencing is performed.
ind (int) – raveled index at which finite differencing is performed.
h (float) – spacing in axis direction.
axis (int) – axis over which finite differencing is performed.
shape (np.ndarray, shape = (ndims,)) – shape of original array before raveled.
direction (str, optional) – finite differencing direction, optional values are -1 for backward, 0 for centered, and 1 for forward. The default is 0.
- Returns:
df – finite difference value.
- Return type:
float
- numbacs.utils.finite_diff_2D_2nd(f, i, j, h, axis, direction='c')[source]
Compute 2nd order partial finite difference in the array f @ [i,j] along axis=axis and using a directional difference scheme defined by direction for the second derivative. axis=2 is for the mixed partial finite difference.
- numbacs.utils.curl_vel(u, v, dx, dy)[source]
Compute curl of vector field defined by u and v.
- Parameters:
u (np.ndarray, shape = (nx,ny)) – array containing x component of vector field.
v (np.ndarray, shape = (nx,ny)) – array containing y component of vector field.
dx (float) – spacing in grid in x-direction.
dy (float) – spacing in grid in y-direction.
- Returns:
curl – array containing values of curl of vector field defined by u and v.
- Return type:
np.ndarray, shape = (nx,ny)
- numbacs.utils.curl_vel_tspan(u, v, dx, dy)[source]
Compute curl of vector field defined by u and v over some timespan.
- Parameters:
u (np.ndarray, shape = (nt,nx,ny)) – array containing x component of vector field.
v (np.ndarray, shape = (nt,nx,ny)) – array containing y component of vector field.
dx (float) – spacing in grid in x-direction.
dy (float) – spacing in grid in y-direction.
- Returns:
curl – array containing values of curl of vector field defined by u and v.
- Return type:
np.ndarray, shape = (nt,nx,ny)
- numbacs.utils.curl_func(fnc, x, y, h=0.001)[source]
Compute curl over x,y of vector field defined by fnc.
- Parameters:
fnc (jit-callable) – callable containing returing x and y components of vector field.
x (np.ndarray, shape = (nx,)) – array containing x-values.
y (np.ndarray, shape = (ny,)) – array containing y-values.
h (float, optional) – spacing used in finite differencing. The default is 1e-3.
- Returns:
curlf – array containing values of curl of f.
- Return type:
np.ndarray, shape = (nx,ny)
- numbacs.utils.curl_func_tspan(fnc, t, x, y, h=0.001)[source]
Compute curl over x,y of vector field defined by func over times t.
- Parameters:
fnc (jit-callable) – callable containing returing x and y components of vector field.
x (np.ndarray, shape = (nx,)) – array containing x-values.
y (np.ndarray, shape = (ny,)) – array containing y-values.
h (float, optional) – spacing used in finite differencing. The default is 1e-3.
- Returns:
curlf – array containing values of curl of f.
- Return type:
np.ndarray, shape = (nx,ny)
- numbacs.utils.composite_simpsons(f, h)[source]
Composite Simpson’s 1/3 rule to compute integral of f between endpoitns of pts with regular spacing given by h.
- Parameters:
f (np.ndarray, shape = (n+1,)) – values of f at regularly spaced points.
h (float) – value of spacing between points at which f was evaluated.
- Returns:
val – value of integral.
- Return type:
float
- numbacs.utils.composite_simpsons_38_irregular(f, h)[source]
Composite Simpson’s 3/8 rule to compute integral of f between endpoitns of pts with irregular spacing given by h which is an np.ndarray.
- Parameters:
f (np.ndarray, shape = (n+1,)) – values of f at irregularly spaced points.
h (np.ndarray, shape = (n,)) – values of spacing between points at which f was evaluated.
- Returns:
val – value of integral.
- Return type:
float
- numbacs.utils.dist_2d(p1, p2)[source]
Compute 2D Euclidean distance between p1 and p2.
- Parameters:
p1 (np,ndarry, shape = (2,)) – point 1.
p2 (np,ndarry, shape = (2,)) – point 2.
- Returns:
distance between p1 and p2.
- Return type:
float
- numbacs.utils.dist_tol(point, arr, tol)[source]
Check if pt is within tolerance of any point in arr.
- Parameters:
point (np.ndarray, shape = (2,)) – point.
arr (np.ndarray, shape = (n,2)) – array containing points.
tol (float) – tolerance used for checking distance.
- Returns:
near – truth value determining if point is within tol of any point in arr.
- Return type:
boolean
- numbacs.utils.shoelace(polygon)[source]
Compute area of simple polygon using shoelace algorithm.
- Parameters:
polygon (np.ndarray, shape = (n,2)) – array containing vertices of polygon, first and last vertex should be the same.
- Returns:
area – area of polygon.
- Return type:
float
- numbacs.utils.max_in_radius(arr, r, dx, dy, n=-1, min_val=0.0)[source]
Finds n local maxima values in arr such that each max is a local maximum within radius r where spacing in arr is given by dx,dy. If all local maxima are desired, set n = -1. Should pass a copy of arr to the function to avoid orginial arr being overwritten (i.e. pass in arr.copy()).
- Parameters:
arr (np.ndarray, shape = (nx,ny)) – array in which local maxima are to be found.
r (float) – radius in which points will be discared after a maximum is found at the center.
dx (float) – gird spacing in x-direction.
dy (float) – grid spacing in y-direction.
n (int, optional) – number of maxima to return, -1 returns all. The default is -1.
min_val (float, optional) – miniumum value allowed for local maxima. The default is 0.0.
- Returns:
max_vals (np.ndarray, shape = (k,) where k number of maxima found) – maxima values in radius.
max_inds (np.ndarray, shape = (k,) where k number of maxima found) – indices corresponding to max_vals.
- numbacs.utils.gen_circ(r, c, n, xlims=None, ylims=None)[source]
Generate n points on a circle with radius r and center c.
- Parameters:
r (float) – radius of circle.
c (np.ndarray, shape = (2,)) – center of circle.
n (int) – number of points on circle.
xlims (tuple, optional) – boundary in x-direction, points outside of this boundary will not be returned. The default is None.
ylims (tuple, optional) – boundary in y-direction, points outside of this boundary will not be returned. The default is None.
- Returns:
pts – points on the circle.
- Return type:
np.ndarray, shape = (n,2)
- numbacs.utils.gen_filled_circ(r, n, alpha=3.0, c=np.array([0.0, 0.0]), xlims=None, ylims=None)[source]
Generate points filling a circle with radius r and center c. Uses the sunflower seed arangement.
- Parameters:
r (float) – radius of circle.
n (int) – number of points to fill the circle.
alpha (float, optional) – parameter determining how smooth the boundary is. The default is 3.0.
c (np.ndarray, shape = (2,), optional) – center of the circle. The default is np.array([0.0,0.0]).
xlims (tuple, optional) – boundary in x-direction, points outside of this boundary will not be returned. The default is None.
ylims (tuple, optional) – boundary in y-direction, points outside of this boundary will not be returned. The default is None.
- Returns:
pts – array containing points which fill the circle.
- Return type:
np.ndarray, shape = (n,2)
- numbacs.utils.gen_filled_circ_radius(r, n, alpha=3.0, c=np.array([0.0, 0.0]), xlims=None, ylims=None)[source]
Generate points filling a circle with radius r and center c. Uses the sunflower seed arangement. Also returns radius of each point from center c.
- Parameters:
r (float) – radius of circle.
n (int) – number of points to fill the circle.
alpha (float, optional) – parameter determining how smooth the boundary is. The default is 3.0.
c (np.ndarray, shape = (2,), optional) – center of the circle. The default is np.array([0.0,0.0]).
xlims (tuple, optional) – boundary in x-direction, points outside of this boundary will not be returned. The default is None.
ylims (tuple, optional) – boundary in y-direction, points outside of this boundary will not be returned. The default is None.
- Returns:
pts (np.ndarray, shape = (n,2)) – array containing points which fill the circle.
radius (np.ndarray, shape = (n,), optional) – array containing radius of each point from center c.
- numbacs.utils.arclength(pts)[source]
Compute total arclength of curve defined by pts.
- Parameters:
pts (np.ndarray, shape=(npts,)) – points representing curve for which arclength is to be computed.
- Returns:
arclength_ – arclength of curve defined by points.
- Return type:
float
- numbacs.utils.arclength_along_arc(pts)[source]
Compute cummulative arclength at each point defining a curve.
- Parameters:
pts (np.ndarray, shape=(npts,)) – points representing curve for which arclength is to be computed.
- Returns:
arclength_ – array containing cummulative arclength of curve defined by pts.
- Return type:
np.ndarray, shape = (npts,)
- numbacs.utils.interp_curve(curve, n, s=0, k=3, per=0)[source]
Return n interpolated values of curve.
- Parameters:
curve (np.ndarray, shape = (npts,2)) – array containing x,y-coordinates of curve being interpolated.
n (int) – number of equally spaced interpolated points to return.
s (float, optional) – smoothing parameter for spline, 0 will fit all data points exactly. The default is 0.
k (int, optional) – degree of spline, using even numbers is not recommended, must be 1 <= k <= 5. The default is 3.
per (int, optional) – if nonzero, data points are considered periodic. The default is 0.
- Returns:
curvei – array containing interpolated values of curve.
- Return type:
np.ndarray, shape = (n,2)
- numbacs.utils.wn_pt_in_poly(polygon, point)[source]
Winding number algorithm to determine if point is inside polygon. Based off of java implementation - https://observablehq.com/@jrus/winding-number - by Jacob Rus.
- Parameters:
polygon (np.ndarray, shape = (n,2)) – array containing vertices of polygon, first and last points should match.
point (np.ndarray, shape = (2,)) – array containing the coordinates of point to be checked.
- Returns:
wn – winding number, returns 1 if point is inside polygon and 0 if not.
- Return type:
int
- numbacs.utils.pts_in_poly(polygon, pts)[source]
Checks if any point from pts is inside polygon. If a point is, the index of the first point found inside polygon is returned. Else, -1 is returned.
- Parameters:
polygon (np.ndarray, shape = (n,2)) – array containing vertices of polygon, first and last points should match.
pts (np.ndarray, shape = (npts,2)) – array containing the coordinates of the points to be checked.
- Returns:
if point from pts is found inside polygon, its index is returned, if not, -1 is returned.
- Return type:
int
- numbacs.utils.pts_in_poly_mask(polygon, pts)[source]
Checks which points from pts are inside polygon. Returns a boolean mask corresponding to points inside polygon.
- Parameters:
polygon (np.ndarray, shape = (n,2)) – array containing vertices of polygon, first and last points should match.
pts (np.ndarray, shape = (npts,2)) – array containing the coordinates of the points to be checked.
- Returns:
mask – bool mask with indices matching those of pts, True if in polygon, False if not.
- Return type:
np.ndarray, shape = (npts,)
- numbacs.utils.cart_prod(vecs)[source]
Computes the Cartesian product using vectors from vecs, works for any number of vectors.
- Parameters:
vecs (tuple) – tuple containing vectors representing the sets which will be used to compute Cartestian product, all vectors must be 1D np.ndarrays of the same type.
- Returns:
prod – array containing the Cartesian product.
- Return type:
np.ndarray, shape = (npts,nvecs)