polynomial_functions¶
shortfx.fxNumeric.polynomial_functions
¶
Polynomial algebra and manipulation.
Pure-Python polynomial operations from Murray R. Spiegel's Mathematical Handbook of Formulas and Tables: addition, multiplication, division, differentiation, integration, GCD, evaluation, and partial-fraction decomposition.
Polynomials are represented as lists of coefficients in descending order
of degree: [a_n, a_{n-1}, ..., a_1, a_0].
Functions¶
partial_fraction_simple(numerator: list, denominator_roots: List[float]) -> List[float]
¶
Partial-fraction coefficients for simple (non-repeated) linear factors.
Given N(x) and roots r1, r2, ..., rn of the denominator D(x) = (x-r1)(x-r2)...(x-rn), returns coefficients [A1, A2, ..., An] such that N(x)/D(x) = A1/(x-r1) + A2/(x-r2) + ... + An/(x-rn).
The degree of numerator must be less than the number of roots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
list
|
Coefficients of the numerator polynomial (descending degree). |
required |
denominator_roots
|
List[float]
|
List of distinct real roots of the denominator. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
List of partial-fraction coefficients [A1, A2, ...]. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If numerator degree >= number of roots or roots are not distinct. |
Example
partial_fraction_simple([1], [1, 2]) [-1.0, 1.0]
Complexity: O(n^2)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_add(p: list, q: list) -> list
¶
Adds two polynomials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients of first polynomial (descending degree). |
required |
q
|
list
|
Coefficients of second polynomial (descending degree). |
required |
Returns:
| Type | Description |
|---|---|
list
|
Coefficients of the sum polynomial. |
Example
polynomial_add([1, 2, 3], [4, 5]) [1, 6, 8]
Complexity: O(max(len(p), len(q)))
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_composition(p: list, q: list) -> list
¶
Computes the composition p(q(x)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Outer polynomial coefficients (descending degree). |
required |
q
|
list
|
Inner polynomial coefficients (descending degree). |
required |
Returns:
| Type | Description |
|---|---|
list
|
Coefficients of the composed polynomial. |
Example
polynomial_composition([1, 0, 1], [1, 1]) [1, 2, 2]
Complexity: O(n^2 * m) where n = deg(p), m = deg(q)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_degree(p: list) -> int
¶
Returns the degree of a polynomial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients (descending degree). |
required |
Returns:
| Type | Description |
|---|---|
int
|
Degree of the polynomial. |
Example
polynomial_degree([3, 0, -2, 1]) 3
Complexity: O(n)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_derivative(p: list) -> list
¶
Computes the derivative of a polynomial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients (descending degree). |
required |
Returns:
| Type | Description |
|---|---|
list
|
Coefficients of the derivative polynomial. |
Example
polynomial_derivative([3, 0, -2, 1]) [9, 0, -2]
Complexity: O(n)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_divide(p: list, q: list) -> Tuple[list, list]
¶
Divides polynomial p by q, returning (quotient, remainder).
Uses synthetic long division.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Dividend coefficients (descending degree). |
required |
q
|
list
|
Divisor coefficients (descending degree). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[list, list]
|
Tuple (quotient_coefficients, remainder_coefficients). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If divisor is zero polynomial. |
Example
polynomial_divide([1, -3, 2], [1, -1]) ([1.0, -2.0], [0.0])
Complexity: O(n * m) where n = deg(p), m = deg(q)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_evaluate(p: list, x: Union[int, float]) -> float
¶
Evaluates a polynomial at x using Horner's method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients (descending degree). |
required |
x
|
Union[int, float]
|
Point at which to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Value of p(x). |
Example
polynomial_evaluate([1, -3, 2], 2) 0
Complexity: O(n)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_from_roots(roots: List[Union[int, float]]) -> list
¶
Constructs a monic polynomial from its roots.
Given roots [r1, r2, ..., rn], returns coefficients of (x - r1)(x - r2)...(x - rn).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roots
|
List[Union[int, float]]
|
List of real roots. |
required |
Returns:
| Type | Description |
|---|---|
list
|
Polynomial coefficients (descending degree, monic). |
Example
polynomial_from_roots([1, -1]) [1, 0, -1]
Complexity: O(n^2)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_gcd(p: list, q: list) -> list
¶
Computes the GCD of two polynomials using the Euclidean algorithm.
The result is monic (leading coefficient 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients of first polynomial (descending degree). |
required |
q
|
list
|
Coefficients of second polynomial (descending degree). |
required |
Returns:
| Type | Description |
|---|---|
list
|
Coefficients of the GCD polynomial (monic). |
Example
polynomial_gcd([1, -3, 2], [1, -1]) [1.0, -1.0]
Complexity: O(n * m) per division, iterated
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_integral(p: list, constant: Union[int, float] = 0) -> list
¶
Computes the indefinite integral of a polynomial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients (descending degree). |
required |
constant
|
Union[int, float]
|
Integration constant (default 0). |
0
|
Returns:
| Type | Description |
|---|---|
list
|
Coefficients of the integral polynomial. |
Example
polynomial_integral([3, 0, -2]) [1.0, 0.0, -2.0, 0]
Complexity: O(n)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_multiply(p: list, q: list) -> list
¶
Multiplies two polynomials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients of first polynomial (descending degree). |
required |
q
|
list
|
Coefficients of second polynomial (descending degree). |
required |
Returns:
| Type | Description |
|---|---|
list
|
Coefficients of the product polynomial. |
Example
polynomial_multiply([1, 1], [1, -1]) [1, 0, -1]
Complexity: O(len(p) * len(q))
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_scale(p: list, scalar: Union[int, float]) -> list
¶
Scales a polynomial by a scalar factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients (descending degree). |
required |
scalar
|
Union[int, float]
|
Scalar multiplier. |
required |
Returns:
| Type | Description |
|---|---|
list
|
Scaled polynomial coefficients. |
Example
polynomial_scale([1, 2, 3], 2) [2, 4, 6]
Complexity: O(n)
Source code in shortfx/fxNumeric/polynomial_functions.py
polynomial_subtract(p: list, q: list) -> list
¶
Subtracts polynomial q from p.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list
|
Coefficients of first polynomial (descending degree). |
required |
q
|
list
|
Coefficients of second polynomial (descending degree). |
required |
Returns:
| Type | Description |
|---|---|
list
|
Coefficients of the difference polynomial p - q. |
Example
polynomial_subtract([1, 2, 3], [4, 5]) [1, -2, -2]
Complexity: O(max(len(p), len(q)))