finite_differences_functions¶
shortfx.fxNumeric.finite_differences_functions
¶
Finite differences and interpolation formulas.
Classical finite-difference methods from Murray R. Spiegel's Mathematical Handbook of Formulas and Tables: difference tables, Newton forward/backward interpolation, Stirling and Bessel central- difference interpolation formulas.
Functions¶
backward_difference_table(y: list) -> List[list]
¶
Builds a backward-difference table ∇ⁿy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
list
|
Equally-spaced function values [y0, y1, y2, ...]. |
required |
Returns:
| Type | Description |
|---|---|
List[list]
|
List of lists: |
List[list]
|
Differences are aligned to the last element. |
Example
backward_difference_table([1, 4, 9, 16]) [[1, 4, 9, 16], [3, 5, 7], [2, 2], [0]]
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
bessel_interpolation(x_values: list, y_values: list, x: float) -> float
¶
Bessel's central-difference interpolation formula.
Best for interpolating at or near the midpoint between two central nodes. Uses averages of function values and differences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_values
|
list
|
Equally-spaced x nodes (even count preferred). |
required |
y_values
|
list
|
Corresponding y values. |
required |
x
|
float
|
Point at which to interpolate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated value. |
Example
bessel_interpolation([0, 1, 2, 3], [0, 1, 8, 27], 1.5) 3.375
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
central_difference_table(y: list) -> List[list]
¶
Builds a central-difference table δⁿy.
Central differences are stored with half-integer indexing. For even order k, δ^k y_i lives at integer nodes; for odd k, at half-integer nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
list
|
Equally-spaced function values. |
required |
Returns:
| Type | Description |
|---|---|
List[list]
|
List of lists: |
Example
central_difference_table([1, 4, 9, 16, 25]) [[1, 4, 9, 16, 25], [3, 5, 7, 9], [2, 2, 2], [0, 0], [0]]
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
divided_difference_table(x_values: list, y_values: list) -> List[list]
¶
Builds a divided-difference table for Newton's general interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_values
|
list
|
Distinct x nodes (not necessarily equally spaced). |
required |
y_values
|
list
|
Corresponding y values. |
required |
Returns:
| Type | Description |
|---|---|
List[list]
|
Table where |
Example
divided_difference_table([1, 2, 4], [1, 8, 64]) [[1, 8, 64], [7.0, 28.0], [7.0]]
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
forward_difference_table(y: list) -> List[list]
¶
Builds a forward-difference table Δⁿy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
list
|
Equally-spaced function values [y0, y1, y2, ...]. |
required |
Returns:
| Type | Description |
|---|---|
List[list]
|
List of lists: |
List[list]
|
|
Example
forward_difference_table([1, 4, 9, 16]) [[1, 4, 9, 16], [3, 5, 7], [2, 2], [0]]
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
gauss_forward_interpolation(x_values: list, y_values: list, x: float) -> float
¶
Gauss's forward central-difference interpolation formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_values
|
list
|
Equally-spaced x nodes. |
required |
y_values
|
list
|
Corresponding y values. |
required |
x
|
float
|
Point at which to interpolate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated value. |
Example
gauss_forward_interpolation([0, 1, 2, 3, 4], [0, 1, 8, 27, 64], 2.5) 15.625
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
newton_backward_interpolation(x_values: list, y_values: list, x: float) -> float
¶
Newton's backward-difference interpolation formula.
Uses equally-spaced nodes with values y_i. Computes: p(x) = Σ C(s,k)·(-1)^k · Δ^k y_{n-k} where s = (x - x_n) / h. Equivalent to using backward differences ∇^k y_n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_values
|
list
|
Equally-spaced x nodes. |
required |
y_values
|
list
|
Corresponding y values. |
required |
x
|
float
|
Point at which to interpolate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated value at x. |
Example
newton_backward_interpolation([0, 1, 2, 3], [1, 4, 9, 16], 2.5) 12.25
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
newton_forward_interpolation(x_values: list, y_values: list, x: float) -> float
¶
Newton's forward-difference interpolation formula.
Uses equally-spaced nodes x0, x0+h, x0+2h, ... with values y_i. Computes: p(x) = Σ C(s,k) Δ^k y_0 where s = (x - x0) / h.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_values
|
list
|
Equally-spaced x nodes. |
required |
y_values
|
list
|
Corresponding y values. |
required |
x
|
float
|
Point at which to interpolate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated value at x. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If lengths don't match or fewer than 2 points. |
Example
newton_forward_interpolation([0, 1, 2, 3], [1, 4, 9, 16], 1.5) 6.25
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
stirling_interpolation(x_values: list, y_values: list, x: float) -> float
¶
Stirling's central-difference interpolation formula.
Best for interpolating near the centre of the data. Uses the average of forward and backward differences at the central node.
f(x) ≈ y_0 + s·μδy_0 + s²/2!·δ²y_0 + s(s²-1)/3!·μδ³y_0 + ...
where s = (x - x_center) / h and μδ^k y_0 = (Δ^k + Δ^k shifted)/2 for odd k.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_values
|
list
|
Equally-spaced x nodes (odd count preferred for symmetric center). |
required |
y_values
|
list
|
Corresponding y values. |
required |
x
|
float
|
Point at which to interpolate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated value. |
Example
stirling_interpolation([0, 1, 2, 3, 4], [0, 1, 8, 27, 64], 2.0) 8.0
Complexity: O(n^2)
Source code in shortfx/fxNumeric/finite_differences_functions.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 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 | |