Separate functionality of ordinary linear interpolation from that using np.outer on arrays

This commit is contained in:
Grant Sanderson 2022-04-11 10:47:11 -07:00
parent 773520bcd9
commit 705f1a528b

View file

@ -80,15 +80,10 @@ def partial_quadratic_bezier_points(
# Linear interpolation variants
def interpolate(start: T, end: T, alpha: float) -> T:
def interpolate(start: T, end: T, alpha: np.ndarray | float) -> T:
try:
if isinstance(alpha, float):
return (1 - alpha) * start + alpha * end
# Otherwise, assume alpha is a list or array, and return
# an appropriated shaped array of all corresponding
# interpolations
result = np.outer(1 - alpha, start) + np.outer(alpha, end)
return result.reshape((*np.shape(alpha), *np.shape(start)))
return (1 - alpha) * start + alpha * end
except TypeError:
log.debug(f"`start` parameter with type `{type(start)}` and dtype `{start.dtype}`")
log.debug(f"`end` parameter with type `{type(end)}` and dtype `{end.dtype}`")
@ -97,6 +92,15 @@ def interpolate(start: T, end: T, alpha: float) -> T:
sys.exit(2)
def outer_interpolate(
start: np.ndarray | float,
end: np.ndarray | float,
alpha: np.ndarray | float,
) -> T:
result = np.outer(1 - alpha, start) + np.outer(alpha, end)
return result.reshape((*np.shape(alpha), *np.shape(start)))
def set_array_by_interpolation(
arr: np.ndarray,
arr1: np.ndarray,