Created
May 12, 2026 19:50
-
-
Save ajschumacher/46042d01a47a421aaf3fdcea10a070a9 to your computer and use it in GitHub Desktop.
Linear interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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