Skip to content

Instantly share code, notes, and snippets.

@ajschumacher
Created May 12, 2026 19:50
Show Gist options
  • Select an option

  • Save ajschumacher/46042d01a47a421aaf3fdcea10a070a9 to your computer and use it in GitHub Desktop.

Select an option

Save ajschumacher/46042d01a47a421aaf3fdcea10a070a9 to your computer and use it in GitHub Desktop.
Linear interpolation
def linterp(x_1, y_1, x_2, y_2, x_new):
"""Linear interpolation"""
assert min(x_1, x_2) <= x_new <= max(x_1, x_2), 'extrapolation'
factor = (x_new - x_1) / (x_2 - x_1)
y_new = factor * (y_2 - y_1) + y_1
return y_new
assert linterp(0, 0, 2, 2, 1) == 1
assert linterp(-2, -1, 2, 0, -1) == -0.75
assert linterp(1.2, 1.2, 0.8, 0.8, 1) == 1
assert linterp(-3, 0, -2, -1, -3) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment