CTB_HORNER
This function evaluates a polynomial at a specified scalar input using Horner’s method, which is a numerically efficient nested form for polynomial evaluation.
For coefficients a_0, a_1, \dots, a_n, Horner’s form evaluates a_0x^n + a_1x^{n-1} + \dots + a_n with reduced arithmetic operations and good computational efficiency.
Excel Usage
=CTB_HORNER(coeffs, x)
coeffs(list[list], required): Polynomial coefficients ordered from highest power to constant (-).x(float, required): Point at which to evaluate the polynomial (-).
Returns (float): Polynomial value at the specified point, or an error message if invalid.
Example 1: Linear polynomial
Inputs:
| coeffs | x | |
|---|---|---|
| 1 | 3 | 2 |
Excel formula:
=CTB_HORNER({1,3}, 2)
Expected output:
5
Example 2: Quadratic polynomial
Inputs:
| coeffs | x | ||
|---|---|---|---|
| 1 | 0 | -1 | 3 |
Excel formula:
=CTB_HORNER({1,0,-1}, 3)
Expected output:
8
Example 3: Constant polynomial
Inputs:
| coeffs | x |
|---|---|
| 2.5 | 10 |
Excel formula:
=CTB_HORNER({2.5}, 10)
Expected output:
2.5
Example 4: Coefficients provided as a column
Inputs:
| coeffs | x |
|---|---|
| 1 | 1.5 |
| 2 | |
| 3 |
Excel formula:
=CTB_HORNER({1;2;3}, 1.5)
Expected output:
8.25
Python Code
Show Code
from ht.conv_tube_bank import horner as ht_horner
def ctb_horner(coeffs, x):
"""
Evaluate a polynomial using Horner's method.
See: https://ht.readthedocs.io/en/latest/ht.conv_tube_bank.html
This example function is provided as-is without any representation of accuracy.
Args:
coeffs (list[list]): Polynomial coefficients ordered from highest power to constant (-).
x (float): Point at which to evaluate the polynomial (-).
Returns:
float: Polynomial value at the specified point, or an error message if invalid.
"""
try:
def to2d(x_value):
return [[x_value]] if not isinstance(x_value, list) else x_value
coeffs = to2d(coeffs)
if not all(isinstance(row, list) for row in coeffs):
return "Error: coeffs must be a 2D list"
flat = []
for row in coeffs:
for val in row:
try:
flat.append(float(val))
except (TypeError, ValueError):
return "Error: coeffs must be numeric"
if not flat:
return "Error: coeffs must contain at least one value"
return ht_horner(flat, x)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Polynomial coefficients ordered from highest power to constant (-).
Point at which to evaluate the polynomial (-).