interpolation_functions¶
shortfx.fxNumeric.interpolation_functions
¶
Interpolation utilities module.
Provides linear interpolation functions for mapping values between ranges, performing piecewise interpolation over datasets, and computing inverse interpolation factors.
Functions¶
bilinear_interpolation(x: Union[int, float], y: Union[int, float], x1: Union[int, float], x2: Union[int, float], y1: Union[int, float], y2: Union[int, float], q11: Union[int, float], q21: Union[int, float], q12: Union[int, float], q22: Union[int, float]) -> float
¶
Performs bilinear interpolation on a 2D grid.
Given four corner values Q(x1,y1), Q(x2,y1), Q(x1,y2), Q(x2,y2), estimates the value at (x, y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The x coordinate to evaluate. |
required |
y
|
Union[int, float]
|
The y coordinate to evaluate. |
required |
x1
|
Union[int, float]
|
Lower x boundary. |
required |
x2
|
Union[int, float]
|
Upper x boundary. |
required |
y1
|
Union[int, float]
|
Lower y boundary. |
required |
y2
|
Union[int, float]
|
Upper y boundary. |
required |
q11
|
Union[int, float]
|
Value at (x1, y1). |
required |
q21
|
Union[int, float]
|
Value at (x2, y1). |
required |
q12
|
Union[int, float]
|
Value at (x1, y2). |
required |
q22
|
Union[int, float]
|
Value at (x2, y2). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interpolated value at (x, y). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any input is not numeric. |
ValueError
|
If x1 == x2 or y1 == y2. |
Example
bilinear_interpolation(0.5, 0.5, 0, 1, 0, 1, 0, 1, 1, 2) 1.0
Complexity: O(1)
Source code in shortfx/fxNumeric/interpolation_functions.py
clamp_interpolate(x: float, x0: float, y0: float, x1: float, y1: float) -> float
¶
Linearly interpolate between two points, clamped to the output range.
Same as :func:linear_interpolate but the result is clamped to
[min(y0, y1), max(y0, y1)].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The x position to evaluate. |
required |
x0
|
float
|
x coordinate of the first point. |
required |
y0
|
float
|
y coordinate of the first point. |
required |
x1
|
float
|
x coordinate of the second point. |
required |
y1
|
float
|
y coordinate of the second point. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Clamped interpolated y value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If x0 == x1. |
Example
clamp_interpolate(15, 0, 0, 10, 100) 100.0
Complexity: O(1)
Source code in shortfx/fxNumeric/interpolation_functions.py
clip_number(x: Union[int, float], min_val: Union[int, float], max_val: Union[int, float]) -> Union[int, float]
¶
Forces a number to be within a specific range [min_val, max_val].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The number to clip. |
required |
min_val
|
Union[int, float]
|
The minimum allowed value. |
required |
max_val
|
Union[int, float]
|
The maximum allowed value. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
Union[int, float]: The number adjusted to the range. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If 'min_val' is greater than 'max_val'. |
Example
clip_number(10, 0, 100) 10 clip_number(-5, 0, 100) 0 clip_number(150, 0, 100) 100
Cost: O(1), comparisons and value adjustment.
Source code in shortfx/fxNumeric/interpolation_functions.py
cubic_spline_natural(x_points: List[Union[int, float]], y_points: List[Union[int, float]], x: Union[int, float]) -> float
¶
Evaluates a natural cubic spline at x.
Uses natural boundary conditions (second derivative = 0 at endpoints). Solves the tridiagonal system for spline coefficients using the Thomas algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_points
|
List[Union[int, float]]
|
Sorted, distinct known x-values (>= 2 points). |
required |
y_points
|
List[Union[int, float]]
|
Known y-values (same length as x_points). |
required |
x
|
Union[int, float]
|
The x-value to interpolate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interpolated y-value at x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers / a number. |
ValueError
|
If fewer than 2 points, different lengths, duplicates, or x_points not sorted ascending. |
Example
cubic_spline_natural([0, 1, 2, 3], [0, 1, 4, 9], 1.5) 2.25
Complexity: O(n)
Source code in shortfx/fxNumeric/interpolation_functions.py
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | |
inverse_interpolate(y: float, y0: float, y1: float) -> float
¶
Compute the interpolation factor t such that lerp(y0, y1, t) == y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
float
|
The target value. |
required |
y0
|
float
|
Start of the range. |
required |
y1
|
float
|
End of the range. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Factor t in [0, 1] when y is within [y0, y1], or outside |
float
|
when y is extrapolated. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If y0 == y1 (zero-length range). |
Example
inverse_interpolate(50, 0, 100) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/interpolation_functions.py
lagrange_interpolation(x_points: List[Union[int, float]], y_points: List[Union[int, float]], x: Union[int, float]) -> float
¶
Evaluates the Lagrange interpolating polynomial at a given x.
Constructs the unique polynomial of degree ≤ n-1 that passes through all n data points and returns its value at x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_points
|
List[Union[int, float]]
|
The x coordinates of the data points (must be distinct). |
required |
y_points
|
List[Union[int, float]]
|
The y coordinates of the data points (same length as x_points). |
required |
x
|
Union[int, float]
|
The x position to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interpolated y value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists/numbers. |
ValueError
|
If lists are empty, different lengths, or x_points not distinct. |
Example
lagrange_interpolation([1, 2, 3], [1, 4, 9], 2.5) 6.25
Complexity: O(n²)
Source code in shortfx/fxNumeric/interpolation_functions.py
linear_interpolate(x: float, x0: float, y0: float, x1: float, y1: float) -> float
¶
Linearly interpolate between two points.
Computes the y value at position x on the line passing through (x0, y0) and (x1, y1). Extrapolates if x is outside [x0, x1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The x position to evaluate. |
required |
x0
|
float
|
x coordinate of the first point. |
required |
y0
|
float
|
y coordinate of the first point. |
required |
x1
|
float
|
x coordinate of the second point. |
required |
y1
|
float
|
y coordinate of the second point. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated y value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If x0 == x1 (undefined slope). |
Example
linear_interpolate(5, 0, 0, 10, 100) 50.0
Complexity: O(1)
Source code in shortfx/fxNumeric/interpolation_functions.py
linear_interpolation(x: Union[int, float], x_points: List[Union[int, float]], y_points: List[Union[int, float]]) -> float
¶
Estimates a Y value for x via piecewise linear interpolation.
x_points must be sorted in ascending order. If x is outside the range of x_points it is linearly extrapolated from the nearest segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The X value to interpolate. |
required |
x_points
|
List[Union[int, float]]
|
Known X coordinates (sorted ascending). |
required |
y_points
|
List[Union[int, float]]
|
Known Y coordinates, same length as x_points. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interpolated (or extrapolated) Y value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric / lists of numbers. |
ValueError
|
If lists are empty or have different lengths, or if fewer than 2 data points are provided. |
Example
linear_interpolation(2.5, [1, 2, 3], [10, 20, 30]) 25.0 linear_interpolation(0, [1, 3], [10, 30]) 0.0
Complexity: O(n) for a linear scan; O(log n) with bisect optimization.
Source code in shortfx/fxNumeric/interpolation_functions.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
map_range(value: Union[int, float], in_min: Union[int, float], in_max: Union[int, float], out_min: Union[int, float], out_max: Union[int, float]) -> float
¶
Re-map a value from one range to another (Arduino-style).
Unlike :func:~shortfx.fxNumeric.interpolation_functions.scale_to_new_range
this function does not clamp and is intentionally minimal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Union[int, float]
|
Input value. |
required |
in_min
|
Union[int, float]
|
Lower bound of the input range. |
required |
in_max
|
Union[int, float]
|
Upper bound of the input range. |
required |
out_min
|
Union[int, float]
|
Lower bound of the output range. |
required |
out_max
|
Union[int, float]
|
Upper bound of the output range. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Value mapped to the output range. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If in_min == in_max. |
Example
map_range(5, 0, 10, 0, 100) 50.0
Complexity: O(1)
Source code in shortfx/fxNumeric/interpolation_functions.py
newton_divided_difference(x_points: List[Union[int, float]], y_points: List[Union[int, float]], x: Union[int, float]) -> float
¶
Evaluates a polynomial at x using Newton's divided-difference form.
Builds the divided-difference table and evaluates the resulting polynomial at the given x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_points
|
List[Union[int, float]]
|
Known x-values (must be distinct). |
required |
y_points
|
List[Union[int, float]]
|
Known y-values (same length as x_points). |
required |
x
|
Union[int, float]
|
The x-value to interpolate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interpolated y-value at x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers / a number. |
ValueError
|
If lists are empty, have different lengths, or x_points contains duplicates. |
Example
newton_divided_difference([1, 2, 3], [1, 4, 9], 2.5) 6.25
Complexity: O(n²)
Source code in shortfx/fxNumeric/interpolation_functions.py
normalize_list(data: List[Union[int, float]]) -> List[float]
¶
Normalizes a list to the [0, 1] range using min-max scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
A new list with values scaled to [0, 1]. If all values are equal, |
List[float]
|
returns a list of 0.0. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data is empty. |
Example
normalize_list([1, 2, 3, 4, 5]) [0.0, 0.25, 0.5, 0.75, 1.0] normalize_list([10, 10, 10]) [0.0, 0.0, 0.0]
Complexity: O(n)
Source code in shortfx/fxNumeric/interpolation_functions.py
normalize_to_0_1_range(x: Union[int, float], min_val: Union[int, float], max_val: Union[int, float]) -> float
¶
Normalizes a number to scale it to a range between 0 and 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The numeric value to normalize. |
required |
min_val
|
Union[int, float]
|
The expected minimum value in the original range. |
required |
max_val
|
Union[int, float]
|
The expected maximum value in the original range. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The normalized value between 0 and 1. If min_val == max_val, returns 0.0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If 'min_val' is greater than 'max_val'. |
Example
normalize_to_0_1_range(50, 0, 100) 0.5 normalize_to_0_1_range(75, 50, 100) 0.5
Cost: O(1), normalization calculation.
Source code in shortfx/fxNumeric/interpolation_functions.py
piecewise_interpolate(x: float, xs: List[Union[int, float]], ys: List[Union[int, float]]) -> float
¶
Linearly interpolate over a piecewise-defined dataset.
Given sorted breakpoints xs and corresponding values ys, find the segment containing x and interpolate. Clamps to the first/last segment if x is outside the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The x position to evaluate. |
required |
xs
|
List[Union[int, float]]
|
Sorted list of x breakpoints (at least 2). |
required |
ys
|
List[Union[int, float]]
|
Corresponding y values (same length as xs). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated y value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If xs and ys have different lengths or fewer than 2 points. |
Example
piecewise_interpolate(15, [0, 10, 20, 30], [0, 100, 100, 200]) 100.0
Complexity: O(n) where n = len(xs)
Source code in shortfx/fxNumeric/interpolation_functions.py
scale_to_new_range(x: Union[int, float], min_x: Union[int, float], max_x: Union[int, float], new_min: Union[int, float], new_max: Union[int, float]) -> float
¶
Scales a number from an original range to a new range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The numeric value to scale. |
required |
min_x
|
Union[int, float]
|
The minimum value of the original range. |
required |
max_x
|
Union[int, float]
|
The maximum value of the original range. |
required |
new_min
|
Union[int, float]
|
The minimum value of the new range. |
required |
new_max
|
Union[int, float]
|
The maximum value of the new range. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The scaled value in the new range. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If 'min_x' is greater than 'max_x'. |
Example
scale_to_new_range(50, 0, 100, 0, 10) 5.0 scale_to_new_range(50, 0, 100, 100, 200) 150.0
Cost: O(1), linear scaling.